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

// Edwin 11/24/09 carved from CbcNode

#ifndef CbcNodeInfo_H
#define CbcNodeInfo_H

#include <string>
#include <vector>

#include "CoinWarmStartBasis.hpp"
#include "CoinSearchTree.hpp"
#include "CbcBranchBase.hpp"

class OsiSolverInterface;
class OsiSolverBranch;

class OsiCuts;
class OsiRowCut;
class OsiRowCutDebugger;
class CoinWarmStartBasis;
class CbcCountRowCut;
class CbcModel;
class CbcNode;
class CbcSubProblem;
class CbcGeneralBranchingObject;

//#############################################################################
/** Information required to recreate the subproblem at this node

  When a subproblem is initially created, it is represented by a CbcNode
  object and an attached CbcNodeInfo object.

  The CbcNode contains information needed while the subproblem remains live.
  The CbcNode is deleted when the last branch arm has been evaluated.

  The CbcNodeInfo contains information required to maintain the branch-and-cut
  search tree structure (links and reference counts) and to recreate the
  subproblem for this node (basis, variable bounds, cutting planes). A
  CbcNodeInfo object remains in existence until all nodes have been pruned from
  the subtree rooted at this node.

  The principle used to maintain the reference count is that the reference
  count is always the sum of all potential and actual children of the node.
  Specifically,
  <ul>
    <li> Once it's determined how the node will branch, the reference count
	 is set to the number of potential children (<i>i.e.</i>, the number
	 of arms of the branch).
    <li> As each child is created by CbcNode::branch() (converting a potential
	 child to the active subproblem), the reference count is decremented.
    <li> If the child survives and will become a node in the search tree
	 (converting the active subproblem into an actual child), increment the
	 reference count.
  </ul>
  Notice that the active subproblem lives in a sort of limbo, neither a
  potential or an actual node in the branch-and-cut tree.

  CbcNodeInfo objects come in two flavours. A CbcFullNodeInfo object contains
  a full record of the information required to recreate a subproblem.
  A CbcPartialNodeInfo object expresses this information in terms of
  differences from the parent.
*/

class CbcNodeInfo {

public:
  /** \name Constructors & destructors */
  //@{
  /** Default Constructor

      Creates an empty NodeInfo object.
    */
  CbcNodeInfo();

  /// Copy constructor
  CbcNodeInfo(const CbcNodeInfo &);

#ifdef JJF_ZERO
  /** Construct with parent

      Creates a NodeInfo object which knows its parent and assumes it will
      in turn have two children.
    */
  CbcNodeInfo(CbcNodeInfo *parent);
#endif

  /** Construct with parent and owner

      As for `construct with parent', and attached to \p owner.
    */
  CbcNodeInfo(CbcNodeInfo *parent, CbcNode *owner);

  /** Destructor

      Note that the destructor will recursively delete the parent if this
      nodeInfo is the last child.
    */
  virtual ~CbcNodeInfo();
  //@}

  /** \brief Modify model according to information at node

        The routine modifies the model according to bound and basis
        information at node and adds any cuts to the addCuts array.
    */
  virtual void applyToModel(CbcModel *model, CoinWarmStartBasis *&basis,
    CbcCountRowCut **addCuts,
    int &currentNumberCuts) const = 0;
  /// Just apply bounds to one variable - force means overwrite by lower,upper (1=>infeasible)
  virtual int applyBounds(int iColumn, double &lower, double &upper, int force) = 0;

  /** Builds up row basis backwards (until original model).
        Returns NULL or previous one to apply .
        Depends on Free being 0 and impossible for cuts
    */
  virtual CbcNodeInfo *buildRowBasis(CoinWarmStartBasis &basis) const = 0;
  /// Clone
  virtual CbcNodeInfo *clone() const = 0;
  /// Called when number branches left down to zero
  virtual void allBranchesGone() {}
#ifndef JJF_ONE
  /// Increment number of references
  inline void increment(int amount = 1)
  {
    numberPointingToThis_ += amount; /*printf("CbcNodeInfo %x incremented by %d to %d\n",this,amount,numberPointingToThis_);*/
  }

  /// Decrement number of references and return number left
  inline int decrement(int amount = 1)
  {
    numberPointingToThis_ -= amount; /*printf("CbcNodeInfo %x decremented by %d to %d\n",this,amount,numberPointingToThis_);*/
    return numberPointingToThis_;
  }
#else
  /// Increment number of references
  void increment(int amount = 1);
  /// Decrement number of references and return number left
  int decrement(int amount = 1);
#endif
  /** Initialize reference counts

      Initialize the reference counts used for tree maintenance.
    */

  inline void initializeInfo(int number)
  {
    numberPointingToThis_ = number;
    numberBranchesLeft_ = number;
  }

  /// Return number of branches left in object
  inline int numberBranchesLeft() const
  {
    return numberBranchesLeft_;
  }

  /// Set number of branches left in object
  inline void setNumberBranchesLeft(int value)
  {
    numberBranchesLeft_ = value;
  }

  /// Return number of objects pointing to this
  inline int numberPointingToThis() const
  {
    return numberPointingToThis_;
  }

  /// Set number of objects pointing to this
  inline void setNumberPointingToThis(int number)
  {
    numberPointingToThis_ = number;
  }

  /// Increment number of objects pointing to this
  inline void incrementNumberPointingToThis()
  {
    numberPointingToThis_++;
  }

  /// Say one branch taken
  inline int branchedOn()
  {
    numberPointingToThis_--;
    numberBranchesLeft_--;
    return numberBranchesLeft_;
  }

  /// Say thrown away
  inline void throwAway()
  {
    numberPointingToThis_ -= numberBranchesLeft_;
    numberBranchesLeft_ = 0;
  }

  /// Parent of this
  CbcNodeInfo *parent() const
  {
    return parent_;
  }
  /// Set parent null
  inline void nullParent()
  {
    parent_ = NULL;
  }

  void addCuts(OsiCuts &cuts, int numberToBranch, //int * whichGenerator,
    int numberPointingToThis);
  void addCuts(int numberCuts, CbcCountRowCut **cuts, int numberToBranch);
  /** Delete cuts (decrements counts)
        Slow unless cuts in same order as saved
    */
  void deleteCuts(int numberToDelete, CbcCountRowCut **cuts);
  void deleteCuts(int numberToDelete, int *which);

  /// Really delete a cut
  void deleteCut(int whichOne);

  /// Decrement active cut counts
  void decrementCuts(int change = 1);

  /// Increment active cut counts
  void incrementCuts(int change = 1);

  /// Decrement all active cut counts in chain starting at parent
  void decrementParentCuts(CbcModel *model, int change = 1);

  /// Increment all active cut counts in parent chain
  void incrementParentCuts(CbcModel *model, int change = 1);

  /// Array of pointers to cuts
  inline CbcCountRowCut **cuts() const
  {
    return cuts_;
  }

  /// Number of row cuts (this node)
  inline int numberCuts() const
  {
    return numberCuts_;
  }
  inline void setNumberCuts(int value)
  {
    numberCuts_ = value;
  }

  /// Set owner null
  inline void nullOwner()
  {
    owner_ = NULL;
  }
  const inline CbcNode *owner() const
  {
    return owner_;
  }
  inline CbcNode *mutableOwner() const
  {
    return owner_;
  }
  /// The node number
  inline int nodeNumber() const
  {
    return nodeNumber_;
  }
  inline void setNodeNumber(int node)
  {
    nodeNumber_ = node;
  }
  /** Deactivate node information.
        1 - bounds
        2 - cuts
        4 - basis!
	8 - just marked
	16 - symmetry branching worked
    */
  void deactivate(int mode = 3);
  /// Say if normal
  inline bool allActivated() const
  {
    return ((active_ & 7) == 7);
  }
  /// Say if marked
  inline bool marked() const
  {
    return ((active_ & 8) != 0);
  }
  /// Mark
  inline void mark()
  {
    active_ |= 8;
  }
  /// Unmark
  inline void unmark()
  {
    active_ &= ~8;
  }
  /// Get symmetry value (true worked at this node)
  inline bool symmetryWorked() const
  {
    return (active_ & 16) != 0;
  }
  /// Say symmetry worked at this node)
  inline void setSymmetryWorked()
  {
    active_ |= 16;
  }

  /// Branching object for the parent
  inline const OsiBranchingObject *parentBranch() const
  {
    return parentBranch_;
  }
  /// If we need to take off parent based data
  void unsetParentBasedData();

protected:
  /** Number of other nodes pointing to this node.

      Number of existing and potential search tree nodes pointing to this node.
      `Existing' means referenced by #parent_ of some other CbcNodeInfo.
      `Potential' means children still to be created (#numberBranchesLeft_ of
      this CbcNodeInfo).
    */
  int numberPointingToThis_;

  /// parent
  CbcNodeInfo *parent_;

  /// Copy of the branching object of the parent when the node is created
  OsiBranchingObject *parentBranch_;

  /// Owner
  CbcNode *owner_;

  /// Number of row cuts (this node)
  int numberCuts_;

  /// The node number
  int nodeNumber_;

  /// Array of pointers to cuts
  CbcCountRowCut **cuts_;

  /** Number of rows in problem (before these cuts).  This
        means that for top of chain it must be rows at continuous */
  int numberRows_;

  /** Number of branch arms left to explore at this node

      \todo There seems to be redundancy between this field and
        CbcBranchingObject::numberBranchesLeft_. It'd be good to sort out if
        both are necessary.
    */
  int numberBranchesLeft_;
  /** Active node information.
        1 - bounds
        2 - cuts
        4 - basis!
    */
  int active_;

private:
  /// Illegal Assignment operator
  CbcNodeInfo &operator=(const CbcNodeInfo &rhs);

  /// routine common to constructors
  void setParentBasedData();
};

#endif // CbcNodeInfo_H

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