CbcSolver.hpp 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/* $Id$ */
// Copyright (C) 2007, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

/*! \file CbcSolver.hpp
    \brief Defines CbcSolver, the proposed top-level class for the new-style
    cbc solver.

    This class is currently an orphan. With the removal of all code flagged
    with the NEW_STYLE_SOLVER, this class is never instantiated (and cannot
    be instantiated). It is available to be coopted as a top-level object
    wrapping the current CbcMain0 and CbcMain1, should that appear to be a
    desireable path forward.  -- lh, 091211 --
*/

#ifndef CbcSolver_H
#define CbcSolver_H

#include <string>
#include <vector>
#include "CoinMessageHandler.hpp"
#include "OsiClpSolverInterface.hpp"

#if CBC_OTHER_SOLVER == 1
#include "OsiCpxSolverInterface.hpp"
#endif

#include "CbcModel.hpp"
#include "CbcOrClpParam.hpp"

class CbcUser;
class CbcStopNow;
class CglCutGenerator;

//#############################################################################

/*! \brief This allows the use of the standalone solver in a flexible manner.

    It has an original OsiClpSolverInterface and CbcModel which it can use
    repeatedly, e.g., to get a heuristic solution and then start again.

    So I [jjf] will need a primitive scripting language which can then call
    solve and manipulate solution value and solution arrays.

    Also provides for user callback functions. Currently two ideas in
    gestation, CbcUser and CbcStopNow. The latter seems limited to deciding
    whether or not to stop. The former seems completely general, with a notion
    of importing and exporting, and a `solve', which should be interpreted as
    `do whatever this user function does'.

    Parameter initialisation is at last centralised in fillParameters().
*/

class CbcSolver {

public:
  ///@name Solve method
  //@{
  /** This takes a list of commands, does "stuff" and returns
        returnMode -
        0 model and solver untouched - babModel updated
        1 model updated - just with solution basis etc
        2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
    */
  int solve(int argc, const char *argv[], int returnMode);
  /** This takes a list of commands, does "stuff" and returns
        returnMode -
        0 model and solver untouched - babModel updated
        1 model updated - just with solution basis etc
        2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
    */
  int solve(const char *input, int returnMode);
  //@}
  ///@name Constructors and destructors etc
  //@{
  /// Default Constructor
  CbcSolver();

  /// Constructor from solver
  CbcSolver(const OsiClpSolverInterface &);

  /// Constructor from model
  CbcSolver(const CbcModel &);

  /** Copy constructor .
     */
  CbcSolver(const CbcSolver &rhs);

  /// Assignment operator
  CbcSolver &operator=(const CbcSolver &rhs);

  /// Destructor
  ~CbcSolver();
  /// Fill with standard parameters
  void fillParameters();
  /*! \brief Set default values in solvers from parameters

      Misleading. The current code actually reads default values from
      the underlying solvers and installs them as default values for a subset of
      parameters in #parameters_.
    */
  void fillValuesInSolver();
  /// Add user function
  void addUserFunction(CbcUser *function);
  /// Set user call back
  void setUserCallBack(CbcStopNow *function);
  /// Add cut generator
  void addCutGenerator(CglCutGenerator *generator);
  //@}
  ///@name miscellaneous methods to line up with old
  //@{
  // analyze model
  int *analyze(OsiClpSolverInterface *solverMod, int &numberChanged, double &increment,
    bool changeInt, CoinMessageHandler *generalMessageHandler);
  /** 1 - add heuristics to model
        2 - do heuristics (and set cutoff and best solution)
        3 - for miplib test so skip some
        (out model later)
    */
  //int doHeuristics(CbcModel * model, int type);
  /** Updates model_ from babModel_ according to returnMode
        returnMode -
        0 model and solver untouched - babModel updated
        1 model updated - just with solution basis etc
        2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
    */
  void updateModel(ClpSimplex *model2, int returnMode);
  //@}
  ///@name useful stuff
  //@{
  /// Get int value
  int intValue(CbcOrClpParameterType type) const;
  /// Set int value
  void setIntValue(CbcOrClpParameterType type, int value);
  /// Get double value
  double doubleValue(CbcOrClpParameterType type) const;
  /// Set double value
  void setDoubleValue(CbcOrClpParameterType type, double value);
  /// User function (NULL if no match)
  CbcUser *userFunction(const char *name) const;
  /// Return original Cbc model
  inline CbcModel *model()
  {
    return &model_;
  }
  /// Return updated Cbc model
  inline CbcModel *babModel()
  {
    return babModel_;
  }
  /// Number of userFunctions
  inline int numberUserFunctions() const
  {
    return numberUserFunctions_;
  }
  /// User function array
  inline CbcUser **userFunctionArray() const
  {
    return userFunction_;
  }
  /// Copy of model on initial load (will contain output solutions)
  inline OsiClpSolverInterface *originalSolver() const
  {
    return originalSolver_;
  }
  /// Copy of model on initial load
  inline CoinModel *originalCoinModel() const
  {
    return originalCoinModel_;
  }
  /// Copy of model on initial load (will contain output solutions)
  void setOriginalSolver(OsiClpSolverInterface *originalSolver);
  /// Copy of model on initial load
  void setOriginalCoinModel(CoinModel *originalCoinModel);
  /// Number of cutgenerators
  inline int numberCutGenerators() const
  {
    return numberCutGenerators_;
  }
  /// Cut generator array
  inline CglCutGenerator **cutGeneratorArray() const
  {
    return cutGenerator_;
  }
  /// Start time
  inline double startTime() const
  {
    return startTime_;
  }
  /// Whether to print to std::cout
  inline void setPrinting(bool onOff)
  {
    noPrinting_ = !onOff;
  }
  /// Where to start reading commands
  inline void setReadMode(int value)
  {
    readMode_ = value;
  }
  //@}
private:
  ///@name Private member data
  //@{

  /// Reference model
  CbcModel model_;

  /// Updated model
  CbcModel *babModel_;

  /// User functions
  CbcUser **userFunction_;
  /** Status of user functions
        0 - not used
        1 - needs cbc_load
        2 - available - data in coinModel
        3 - data loaded - can do cbc_save
    */
  int *statusUserFunction_;
  /// Copy of model on initial load (will contain output solutions)
  OsiClpSolverInterface *originalSolver_;
  /// Copy of model on initial load
  CoinModel *originalCoinModel_;
  /// Cut generators
  CglCutGenerator **cutGenerator_;
  /// Number of user functions
  int numberUserFunctions_;
  /// Number of cut generators
  int numberCutGenerators_;
  /// Stop now stuff
  CbcStopNow *callBack_;
  /// Cpu time at instantiation
  double startTime_;
  /// Parameters and values
  std::vector< CbcOrClpParam > parameters_;
  /// Whether to do miplib test
  bool doMiplib_;
  /// Whether to print to std::cout
  bool noPrinting_;
  /// Where to start reading commands
  int readMode_;
  //@}
};
//#############################################################################

/// Structure to hold useful arrays
typedef struct {
  // Priorities
  int *priorities_;
  // SOS priorities
  int *sosPriority_;
  // Direction to branch first
  int *branchDirection_;
  // Input solution
  double *primalSolution_;
  // Down pseudo costs
  double *pseudoDown_;
  // Up pseudo costs
  double *pseudoUp_;
} CbcSolverUsefulData2;

//#############################################################################

/**
   The CbcSolver class was taken out at a 9/12/09 meeting
   This is a feeble replacement.
   At present everything is public
*/
class CbcSolverUsefulData {

public:
  ///@name Constructors and destructors etc
  //@{
  /// Default Constructor
  CbcSolverUsefulData();

  /** Copy constructor .
     */
  CbcSolverUsefulData(const CbcSolverUsefulData &rhs);

  /// Assignment operator
  CbcSolverUsefulData &operator=(const CbcSolverUsefulData &rhs);

  /// Destructor
  ~CbcSolverUsefulData();
  //@}

  ///@name Member data
  //@{
  // For time
  double totalTime_;
  // Parameters
  std::vector<CbcOrClpParam> parameters_;
  // Printing
  bool noPrinting_;
  // Whether to use signal handler
  bool useSignalHandler_;
  // Default pump tuning
  int initialPumpTune_;
  //@}
};
/// And this uses it
// When we want to load up CbcModel with options first
void CbcMain0(CbcModel &babSolver, CbcSolverUsefulData &solverData);
int CbcMain1(int argc, const char *argv[], CbcModel &babSolver, int(CbcModel *currentSolver, int whereFrom), CbcSolverUsefulData &solverData);

//#############################################################################

/*! \brief A class to allow the use of unknown user functionality

    For example, access to a modelling language (CbcAmpl).
*/
class CbcUser {

public:
  ///@name import/export methods
  //@{
  /*! \brief Import - gets full command arguments

      \return
      - -1 - no action
      -  0 - data read in without error
      -  1 - errors
    */
  virtual int importData(CbcSolver * /*model*/, int & /*argc*/, char ** /*argv[]*/)
  {
    return -1;
  }

  /*! \brief Export

      Values for mode:
      - 1 OsiClpSolver
      - 2 CbcModel
      - add 10 if infeasible from odd situation
    */
  virtual void exportSolution(CbcSolver * /*model*/,
    int /*mode*/, const char * /*message*/ = NULL) {}

  /// Export Data (i.e. at very end)
  virtual void exportData(CbcSolver * /*model*/) {}

  /// Get useful stuff
  virtual void fillInformation(CbcSolver * /*model*/,
    CbcSolverUsefulData & /*info*/) {}
  //@}

  ///@name usage methods
  //@{
  /// CoinModel if valid
  inline CoinModel *coinModel() const
  {
    return coinModel_;
  }
  /// Other info - needs expanding
  virtual void *stuff()
  {
    return NULL;
  }
  /// Name
  inline std::string name() const
  {
    return userName_;
  }
  /// Solve (whatever that means)
  virtual void solve(CbcSolver *model, const char *options) = 0;
  /// Returns true if function knows about option
  virtual bool canDo(const char *options) = 0;
  //@}

  ///@name Constructors and destructors etc
  //@{
  /// Default Constructor
  CbcUser();

  /// Copy constructor
  CbcUser(const CbcUser &rhs);

  /// Assignment operator
  CbcUser &operator=(const CbcUser &rhs);

  /// Clone
  virtual CbcUser *clone() const = 0;

  /// Destructor
  virtual ~CbcUser();
  //@}

protected:
  ///@name Private member data
  //@{

  /// CoinModel
  CoinModel *coinModel_;

  /// Name of user function
  std::string userName_;

  //@}
};
//#############################################################################

/*! \brief Support the use of a call back class to decide whether to stop

  Definitely under construction.
*/

class CbcStopNow {

public:
  ///@name Decision methods
  //@{
  /*! \brief Import

      Values for whereFrom:
       - 1 after initial solve by dualsimplex etc
       - 2 after preprocessing
       - 3 just before branchAndBound (so user can override)
       - 4 just after branchAndBound (before postprocessing)
       - 5 after postprocessing
       - 6 after a user called heuristic phase

      \return 0 if good
       nonzero return code to stop
    */
  virtual int callBack(CbcModel * /*currentSolver*/, int /*whereFrom*/)
  {
    return 0;
  }
  //@}

  ///@name Constructors and destructors etc
  //@{
  /// Default Constructor
  CbcStopNow();

  /** Copy constructor .
     */
  CbcStopNow(const CbcStopNow &rhs);

  /// Assignment operator
  CbcStopNow &operator=(const CbcStopNow &rhs);

  /// Clone
  virtual CbcStopNow *clone() const;

  /// Destructor
  virtual ~CbcStopNow();
  //@}

private:
  ///@name Private member data
  //@{
  //@}
};
#endif

/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/