CbcTreeLocal.hpp 10.4 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
/* $Id$ */
// Copyright (C) 2004, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CbcTreeLocal_H
#define CbcTreeLocal_H

//#############################################################################
/*  This implements (approximately) local branching as in the 2002 paper by
    Matteo Fischetti and Andrea Lodi.

    The very simple version of the algorithm for problems with
    0-1 variables and continuous is as follows:

    Obtain a feasible solution (one can be passed in).

    Add a cut which limits search to a k neighborhood of this solution.
    (At most k 0-1 variables may change value)
    Do branch and bound on this problem.

    If finished search and proven optimal then we can reverse cut so
    any solutions must be at least k+1 away from solution and we can
    add a new cut limiting search to a k neighborhood of new solution
    repeat.

    If finished search and no new solution then the simplest version
    would reverse last cut and complete search.  The version implemented
    here can use time and node limits and can widen search (increase effective k)
    .... and more

*/

#include "CbcTree.hpp"
#include "CbcNode.hpp"
#include "OsiRowCut.hpp"
class CbcModel;

class CbcTreeLocal : public CbcTree {

public:
  // Default Constructor
  CbcTreeLocal();

  /* Constructor with solution.
       If solution NULL no solution, otherwise must be integer
       range is initial upper bound (k) on difference from given solution.
       typeCuts -
                0 means just 0-1 cuts and will need to refine 0-1 solution
            1 uses weaker cuts on all integer variables
       maxDiversification is maximum number of range widenings to try
       timeLimit is seconds in subTree
       nodeLimit is nodes in subTree
       refine is whether to see if we can prove current solution is optimal
       when we fix all 0-1 (in case typeCuts==0 and there are general integer variables)
       if false then no refinement but reverse cuts weaker
    */
  CbcTreeLocal(CbcModel *model, const double *solution, int range = 10,
    int typeCuts = 0, int maxDiversification = 0,
    int timeLimit = 1000000, int nodeLimit = 1000000, bool refine = true);
  // Copy constructor
  CbcTreeLocal(const CbcTreeLocal &rhs);

  // = operator
  CbcTreeLocal &operator=(const CbcTreeLocal &rhs);

  virtual ~CbcTreeLocal();

  /// Clone
  virtual CbcTree *clone() const;
  /// Create C++ lines to get to current state
  virtual void generateCpp(FILE *fp);

  /*! \name Heap access and maintenance methods */
  //@{

  /// Return the top node of the heap
  virtual CbcNode *top() const;

  /// Add a node to the heap
  virtual void push(CbcNode *x);

  /// Remove the top node from the heap
  virtual void pop();

  //@}
  /*! \name Other stuff */
  //@{

  /// Create cut - return -1 if bad, 0 if okay and 1 if cut is everything
  int createCut(const double *solution, OsiRowCut &cut);

  /// Test if empty *** note may be overridden
  virtual bool empty();

  /// We may have got an intelligent tree so give it one more chance
  virtual void endSearch();
  /// Other side of last cut branch (if bias==rhs_ will be weakest possible)
  void reverseCut(int state, double bias = 0.0);
  /// Delete last cut branch
  void deleteCut(OsiRowCut &cut);
  /// Pass in solution (so can be used after heuristic)
  void passInSolution(const double *solution, double solutionValue);
  // range i.e. k
  inline int range() const
  {
    return range_;
  }
  // setrange i.e. k
  inline void setRange(int value)
  {
    range_ = value;
  }
  // Type of cuts - 0=just 0-1, 1=all
  inline int typeCuts() const
  {
    return typeCuts_;
  }
  // Type of cuts - 0=just 0-1, 1=all
  inline void setTypeCuts(int value)
  {
    typeCuts_ = value;
  }
  // maximum number of diversifications
  inline int maxDiversification() const
  {
    return maxDiversification_;
  }
  // maximum number of diversifications
  inline void setMaxDiversification(int value)
  {
    maxDiversification_ = value;
  }
  // time limit per subtree
  inline int timeLimit() const
  {
    return timeLimit_;
  }
  // time limit per subtree
  inline void setTimeLimit(int value)
  {
    timeLimit_ = value;
  }
  // node limit for subtree
  inline int nodeLimit() const
  {
    return nodeLimit_;
  }
  // node limit for subtree
  inline void setNodeLimit(int value)
  {
    nodeLimit_ = value;
  }
  // Whether to do refinement step
  inline bool refine() const
  {
    return refine_;
  }
  // Whether to do refinement step
  inline void setRefine(bool yesNo)
  {
    refine_ = yesNo;
  }

  //@}
private:
  // Node for local cuts
  CbcNode *localNode_;
  // best solution
  double *bestSolution_;
  // saved solution
  double *savedSolution_;
  // solution number at start of pass
  int saveNumberSolutions_;
  /* Cut.  If zero size then no solution yet.  Otherwise is left hand branch */
  OsiRowCut cut_;
  // This cut fixes all 0-1 variables
  OsiRowCut fixedCut_;
  // Model
  CbcModel *model_;
  // Original lower bounds
  double *originalLower_;
  // Original upper bounds
  double *originalUpper_;
  // range i.e. k
  int range_;
  // Type of cuts - 0=just 0-1, 1=all
  int typeCuts_;
  // maximum number of diversifications
  int maxDiversification_;
  // current diversification
  int diversification_;
  // Whether next will be strong diversification
  bool nextStrong_;
  // Current rhs
  double rhs_;
  // Save allowable gap
  double savedGap_;
  // Best solution
  double bestCutoff_;
  // time limit per subtree
  int timeLimit_;
  // time when subtree started
  int startTime_;
  // node limit for subtree
  int nodeLimit_;
  // node count when subtree started
  int startNode_;
  // -1 not started, 0 == stop on first solution, 1 don't stop on first, 2 refinement step
  int searchType_;
  // Whether to do refinement step
  bool refine_;
};

class CbcTreeVariable : public CbcTree {

public:
  // Default Constructor
  CbcTreeVariable();

  /* Constructor with solution.
       If solution NULL no solution, otherwise must be integer
       range is initial upper bound (k) on difference from given solution.
       typeCuts -
                0 means just 0-1 cuts and will need to refine 0-1 solution
            1 uses weaker cuts on all integer variables
       maxDiversification is maximum number of range widenings to try
       timeLimit is seconds in subTree
       nodeLimit is nodes in subTree
       refine is whether to see if we can prove current solution is optimal
       when we fix all 0-1 (in case typeCuts==0 and there are general integer variables)
       if false then no refinement but reverse cuts weaker
    */
  CbcTreeVariable(CbcModel *model, const double *solution, int range = 10,
    int typeCuts = 0, int maxDiversification = 0,
    int timeLimit = 1000000, int nodeLimit = 1000000, bool refine = true);
  // Copy constructor
  CbcTreeVariable(const CbcTreeVariable &rhs);

  // = operator
  CbcTreeVariable &operator=(const CbcTreeVariable &rhs);

  virtual ~CbcTreeVariable();

  /// Clone
  virtual CbcTree *clone() const;
  /// Create C++ lines to get to current state
  virtual void generateCpp(FILE *fp);

  /*! \name Heap access and maintenance methods */
  //@{

  /// Return the top node of the heap
  virtual CbcNode *top() const;

  /// Add a node to the heap
  virtual void push(CbcNode *x);

  /// Remove the top node from the heap
  virtual void pop();

  //@}
  /*! \name Other stuff */
  //@{

  /// Create cut - return -1 if bad, 0 if okay and 1 if cut is everything
  int createCut(const double *solution, OsiRowCut &cut);

  /// Test if empty *** note may be overridden
  virtual bool empty();

  /// We may have got an intelligent tree so give it one more chance
  virtual void endSearch();
  /// Other side of last cut branch (if bias==rhs_ will be weakest possible)
  void reverseCut(int state, double bias = 0.0);
  /// Delete last cut branch
  void deleteCut(OsiRowCut &cut);
  /// Pass in solution (so can be used after heuristic)
  void passInSolution(const double *solution, double solutionValue);
  // range i.e. k
  inline int range() const
  {
    return range_;
  }
  // setrange i.e. k
  inline void setRange(int value)
  {
    range_ = value;
  }
  // Type of cuts - 0=just 0-1, 1=all
  inline int typeCuts() const
  {
    return typeCuts_;
  }
  // Type of cuts - 0=just 0-1, 1=all
  inline void setTypeCuts(int value)
  {
    typeCuts_ = value;
  }
  // maximum number of diversifications
  inline int maxDiversification() const
  {
    return maxDiversification_;
  }
  // maximum number of diversifications
  inline void setMaxDiversification(int value)
  {
    maxDiversification_ = value;
  }
  // time limit per subtree
  inline int timeLimit() const
  {
    return timeLimit_;
  }
  // time limit per subtree
  inline void setTimeLimit(int value)
  {
    timeLimit_ = value;
  }
  // node limit for subtree
  inline int nodeLimit() const
  {
    return nodeLimit_;
  }
  // node limit for subtree
  inline void setNodeLimit(int value)
  {
    nodeLimit_ = value;
  }
  // Whether to do refinement step
  inline bool refine() const
  {
    return refine_;
  }
  // Whether to do refinement step
  inline void setRefine(bool yesNo)
  {
    refine_ = yesNo;
  }

  //@}
private:
  // Node for local cuts
  CbcNode *localNode_;
  // best solution
  double *bestSolution_;
  // saved solution
  double *savedSolution_;
  // solution number at start of pass
  int saveNumberSolutions_;
  /* Cut.  If zero size then no solution yet.  Otherwise is left hand branch */
  OsiRowCut cut_;
  // This cut fixes all 0-1 variables
  OsiRowCut fixedCut_;
  // Model
  CbcModel *model_;
  // Original lower bounds
  double *originalLower_;
  // Original upper bounds
  double *originalUpper_;
  // range i.e. k
  int range_;
  // Type of cuts - 0=just 0-1, 1=all
  int typeCuts_;
  // maximum number of diversifications
  int maxDiversification_;
  // current diversification
  int diversification_;
  // Whether next will be strong diversification
  bool nextStrong_;
  // Current rhs
  double rhs_;
  // Save allowable gap
  double savedGap_;
  // Best solution
  double bestCutoff_;
  // time limit per subtree
  int timeLimit_;
  // time when subtree started
  int startTime_;
  // node limit for subtree
  int nodeLimit_;
  // node count when subtree started
  int startNode_;
  // -1 not started, 0 == stop on first solution, 1 don't stop on first, 2 refinement step
  int searchType_;
  // Whether to do refinement step
  bool refine_;
};
#endif

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