1 | // $Id: CbcBranchingObject.hpp 1899 2013-04-09 18:12:08Z unxusr $ |
---|
2 | // Copyright (C) 2002, International Business Machines |
---|
3 | // Corporation and others. All Rights Reserved. |
---|
4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
---|
5 | |
---|
6 | // Edwin 11/12/2009 carved from CbcBranchBase |
---|
7 | |
---|
8 | #ifndef CbcBranchingObject_H |
---|
9 | #define CbcBranchingObject_H |
---|
10 | |
---|
11 | #include <string> |
---|
12 | #include <vector> |
---|
13 | #include "CbcBranchBase.hpp" |
---|
14 | #include "OsiBranchingObject.hpp" |
---|
15 | |
---|
16 | |
---|
17 | // The types of objects that will be derived from this class. |
---|
18 | enum CbcBranchObjType |
---|
19 | { |
---|
20 | SimpleIntegerBranchObj = 100, |
---|
21 | SimpleIntegerDynamicPseudoCostBranchObj = 101, |
---|
22 | CliqueBranchObj = 102, |
---|
23 | LongCliqueBranchObj = 103, |
---|
24 | SoSBranchObj = 104, |
---|
25 | NWayBranchObj = 105, |
---|
26 | FollowOnBranchObj = 106, |
---|
27 | DummyBranchObj = 107, |
---|
28 | GeneralDepthBranchObj = 108, |
---|
29 | OneGeneralBranchingObj = 110, |
---|
30 | CutBranchingObj = 200, |
---|
31 | LotsizeBranchObj = 300, |
---|
32 | DynamicPseudoCostBranchObj = 400 |
---|
33 | }; |
---|
34 | |
---|
35 | /** \brief Abstract branching object base class |
---|
36 | Now just difference with OsiBranchingObject |
---|
37 | |
---|
38 | In the abstract, an CbcBranchingObject contains instructions for how to |
---|
39 | branch. We want an abstract class so that we can describe how to branch on |
---|
40 | simple objects (<i>e.g.</i>, integers) and more exotic objects |
---|
41 | (<i>e.g.</i>, cliques or hyperplanes). |
---|
42 | |
---|
43 | The #branch() method is the crucial routine: it is expected to be able to |
---|
44 | step through a set of branch arms, executing the actions required to create |
---|
45 | each subproblem in turn. The base class is primarily virtual to allow for |
---|
46 | a wide range of problem modifications. |
---|
47 | |
---|
48 | See CbcObject for an overview of the three classes (CbcObject, |
---|
49 | CbcBranchingObject, and CbcBranchDecision) which make up cbc's branching |
---|
50 | model. |
---|
51 | */ |
---|
52 | |
---|
53 | class CbcBranchingObject : public OsiBranchingObject { |
---|
54 | |
---|
55 | public: |
---|
56 | |
---|
57 | /// Default Constructor |
---|
58 | CbcBranchingObject (); |
---|
59 | |
---|
60 | /// Constructor |
---|
61 | CbcBranchingObject (CbcModel * model, int variable, int way , double value); |
---|
62 | |
---|
63 | /// Copy constructor |
---|
64 | CbcBranchingObject ( const CbcBranchingObject &); |
---|
65 | |
---|
66 | /// Assignment operator |
---|
67 | CbcBranchingObject & operator=( const CbcBranchingObject& rhs); |
---|
68 | |
---|
69 | /// Clone |
---|
70 | virtual CbcBranchingObject * clone() const = 0; |
---|
71 | |
---|
72 | /// Destructor |
---|
73 | virtual ~CbcBranchingObject (); |
---|
74 | |
---|
75 | /** Some branchingObjects may claim to be able to skip |
---|
76 | strong branching. If so they have to fill in CbcStrongInfo. |
---|
77 | The object mention in incoming CbcStrongInfo must match. |
---|
78 | Returns nonzero if skip is wanted */ |
---|
79 | virtual int fillStrongInfo( CbcStrongInfo & ) { |
---|
80 | return 0; |
---|
81 | } |
---|
82 | /// Reset number of branches left to original |
---|
83 | inline void resetNumberBranchesLeft() { |
---|
84 | branchIndex_ = 0; |
---|
85 | } |
---|
86 | /// Set number of branches to do |
---|
87 | inline void setNumberBranches(int value) { |
---|
88 | branchIndex_ = 0; |
---|
89 | numberBranches_ = value; |
---|
90 | } |
---|
91 | |
---|
92 | /** \brief Execute the actions required to branch, as specified by the |
---|
93 | current state of the branching object, and advance the object's |
---|
94 | state. Mainly for diagnostics, whether it is true branch or |
---|
95 | strong branching is also passed. |
---|
96 | Returns change in guessed objective on next branch |
---|
97 | */ |
---|
98 | virtual double branch() = 0; |
---|
99 | /** \brief Execute the actions required to branch, as specified by the |
---|
100 | current state of the branching object, and advance the object's |
---|
101 | state. Mainly for diagnostics, whether it is true branch or |
---|
102 | strong branching is also passed. |
---|
103 | Returns change in guessed objective on next branch |
---|
104 | */ |
---|
105 | virtual double branch(OsiSolverInterface * ) { |
---|
106 | return branch(); |
---|
107 | } |
---|
108 | /** Update bounds in solver as in 'branch' and update given bounds. |
---|
109 | branchState is -1 for 'down' +1 for 'up' */ |
---|
110 | virtual void fix(OsiSolverInterface * , |
---|
111 | double * , double * , |
---|
112 | int ) const {} |
---|
113 | |
---|
114 | /** Change (tighten) bounds in object to reflect bounds in solver. |
---|
115 | Return true if now fixed */ |
---|
116 | virtual bool tighten(OsiSolverInterface * ) {return false;} |
---|
117 | |
---|
118 | /** Reset every information so that the branching object appears to point to |
---|
119 | the previous child. This method does not need to modify anything in any |
---|
120 | solver. */ |
---|
121 | virtual void previousBranch() { |
---|
122 | assert(branchIndex_ > 0); |
---|
123 | branchIndex_--; |
---|
124 | way_ = -way_; |
---|
125 | } |
---|
126 | |
---|
127 | using OsiBranchingObject::print ; |
---|
128 | /** \brief Print something about branch - only if log level high |
---|
129 | */ |
---|
130 | virtual void print() const {} |
---|
131 | |
---|
132 | /** \brief Index identifying the associated CbcObject within its class. |
---|
133 | |
---|
134 | The name is misleading, and typically the index will <i>not</i> refer |
---|
135 | directly to a variable. |
---|
136 | Rather, it identifies an CbcObject within the class of similar |
---|
137 | CbcObjects |
---|
138 | |
---|
139 | <i>E.g.</i>, for an CbcSimpleInteger, variable() is the index of the |
---|
140 | integer variable in the set of integer variables (<i>not</i> the index of |
---|
141 | the variable in the set of all variables). |
---|
142 | */ |
---|
143 | inline int variable() const { |
---|
144 | return variable_; |
---|
145 | } |
---|
146 | |
---|
147 | /** Get the state of the branching object |
---|
148 | |
---|
149 | Returns a code indicating the active arm of the branching object. |
---|
150 | The precise meaning is defined in the derived class. |
---|
151 | |
---|
152 | \sa #way_ |
---|
153 | */ |
---|
154 | inline int way() const { |
---|
155 | return way_; |
---|
156 | } |
---|
157 | |
---|
158 | /** Set the state of the branching object. |
---|
159 | |
---|
160 | See #way() |
---|
161 | */ |
---|
162 | inline void way(int way) { |
---|
163 | way_ = way; |
---|
164 | } |
---|
165 | |
---|
166 | /// update model |
---|
167 | inline void setModel(CbcModel * model) { |
---|
168 | model_ = model; |
---|
169 | } |
---|
170 | /// Return model |
---|
171 | inline CbcModel * model() const { |
---|
172 | return model_; |
---|
173 | } |
---|
174 | |
---|
175 | /// Return pointer back to object which created |
---|
176 | inline CbcObject * object() const { |
---|
177 | return originalCbcObject_; |
---|
178 | } |
---|
179 | /// Set pointer back to object which created |
---|
180 | inline void setOriginalObject(CbcObject * object) { |
---|
181 | originalCbcObject_ = object; |
---|
182 | } |
---|
183 | |
---|
184 | // Methods used in heuristics |
---|
185 | |
---|
186 | /** Return the type (an integer identifier) of \c this. |
---|
187 | See definition of CbcBranchObjType above for possibilities |
---|
188 | */ |
---|
189 | |
---|
190 | virtual CbcBranchObjType type() const = 0; |
---|
191 | |
---|
192 | /** Compare the original object of \c this with the original object of \c |
---|
193 | brObj. Assumes that there is an ordering of the original objects. |
---|
194 | This method should be invoked only if \c this and brObj are of the same |
---|
195 | type. |
---|
196 | Return negative/0/positive depending on whether \c this is |
---|
197 | smaller/same/larger than the argument. |
---|
198 | */ |
---|
199 | virtual int compareOriginalObject(const CbcBranchingObject* brObj) const { |
---|
200 | const CbcBranchingObject* br = dynamic_cast<const CbcBranchingObject*>(brObj); |
---|
201 | return variable() - br->variable(); |
---|
202 | } |
---|
203 | |
---|
204 | /** Compare the \c this with \c brObj. \c this and \c brObj must be of the |
---|
205 | same type and must have the same original object, but they may have |
---|
206 | different feasible regions. |
---|
207 | Return the appropriate CbcRangeCompare value (first argument being the |
---|
208 | sub/superset if that's the case). In case of overlap (and if \c |
---|
209 | replaceIfOverlap is true) replace the current branching object with one |
---|
210 | whose feasible region is the overlap. |
---|
211 | */ |
---|
212 | virtual CbcRangeCompare compareBranchingObject |
---|
213 | (const CbcBranchingObject* brObj, const bool replaceIfOverlap = false) = 0; |
---|
214 | |
---|
215 | protected: |
---|
216 | |
---|
217 | /// The model that owns this branching object |
---|
218 | CbcModel * model_; |
---|
219 | /// Pointer back to object which created |
---|
220 | CbcObject * originalCbcObject_; |
---|
221 | |
---|
222 | /// Branching variable (0 is first integer) |
---|
223 | int variable_; |
---|
224 | // was - Way to branch - -1 down (first), 1 up, -2 down (second), 2 up (second) |
---|
225 | /** The state of the branching object. |
---|
226 | |
---|
227 | Specifies the active arm of the branching object. Coded as -1 to take |
---|
228 | the `down' arm, +1 for the `up' arm. `Down' and `up' are defined based on |
---|
229 | the natural meaning (floor and ceiling, respectively) for a simple integer. |
---|
230 | The precise meaning is defined in the derived class. |
---|
231 | */ |
---|
232 | int way_; |
---|
233 | |
---|
234 | }; |
---|
235 | #endif |
---|
236 | |
---|