1 | // $Id$ |
---|
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/9/2009-- carved out of CbcBranchActual |
---|
7 | |
---|
8 | #ifndef CbcSimpleInteger_H |
---|
9 | #define CbcSimpleInteger_H |
---|
10 | |
---|
11 | /** Simple branching object for an integer variable |
---|
12 | |
---|
13 | This object can specify a two-way branch on an integer variable. For each |
---|
14 | arm of the branch, the upper and lower bounds on the variable can be |
---|
15 | independently specified. |
---|
16 | |
---|
17 | Variable_ holds the index of the integer variable in the integerVariable_ |
---|
18 | array of the model. |
---|
19 | */ |
---|
20 | |
---|
21 | class CbcIntegerBranchingObject : public CbcBranchingObject { |
---|
22 | |
---|
23 | public: |
---|
24 | |
---|
25 | /// Default constructor |
---|
26 | CbcIntegerBranchingObject (); |
---|
27 | |
---|
28 | /** Create a standard floor/ceiling branch object |
---|
29 | |
---|
30 | Specifies a simple two-way branch. Let \p value = x*. One arm of the |
---|
31 | branch will be lb <= x <= floor(x*), the other ceil(x*) <= x <= ub. |
---|
32 | Specify way = -1 to set the object state to perform the down arm first, |
---|
33 | way = 1 for the up arm. |
---|
34 | */ |
---|
35 | CbcIntegerBranchingObject (CbcModel *model, int variable, |
---|
36 | int way , double value) ; |
---|
37 | |
---|
38 | /** Create a degenerate branch object |
---|
39 | |
---|
40 | Specifies a `one-way branch'. Calling branch() for this object will |
---|
41 | always result in lowerValue <= x <= upperValue. Used to fix a variable |
---|
42 | when lowerValue = upperValue. |
---|
43 | */ |
---|
44 | |
---|
45 | CbcIntegerBranchingObject (CbcModel *model, int variable, int way, |
---|
46 | double lowerValue, double upperValue) ; |
---|
47 | |
---|
48 | /// Copy constructor |
---|
49 | CbcIntegerBranchingObject ( const CbcIntegerBranchingObject &); |
---|
50 | |
---|
51 | /// Assignment operator |
---|
52 | CbcIntegerBranchingObject & operator= (const CbcIntegerBranchingObject& rhs); |
---|
53 | |
---|
54 | /// Clone |
---|
55 | virtual CbcBranchingObject * clone() const; |
---|
56 | |
---|
57 | /// Destructor |
---|
58 | virtual ~CbcIntegerBranchingObject (); |
---|
59 | |
---|
60 | /// Does part of constructor |
---|
61 | void fillPart ( int variable, int way , double value) ; |
---|
62 | using CbcBranchingObject::branch ; |
---|
63 | /** \brief Sets the bounds for the variable according to the current arm |
---|
64 | of the branch and advances the object state to the next arm. |
---|
65 | Returns change in guessed objective on next branch |
---|
66 | */ |
---|
67 | virtual double branch(); |
---|
68 | /** Update bounds in solver as in 'branch' and update given bounds. |
---|
69 | branchState is -1 for 'down' +1 for 'up' */ |
---|
70 | virtual void fix(OsiSolverInterface * solver, |
---|
71 | double * lower, double * upper, |
---|
72 | int branchState) const ; |
---|
73 | /** Change (tighten) bounds in object to reflect bounds in solver. |
---|
74 | Return true if now fixed */ |
---|
75 | virtual bool tighten(OsiSolverInterface * ) ; |
---|
76 | |
---|
77 | #ifdef JJF_ZERO |
---|
78 | // No need to override. Default works fine. |
---|
79 | /** Reset every information so that the branching object appears to point to |
---|
80 | the previous child. This method does not need to modify anything in any |
---|
81 | solver. */ |
---|
82 | virtual void previousBranch(); |
---|
83 | #endif |
---|
84 | |
---|
85 | using CbcBranchingObject::print ; |
---|
86 | /** \brief Print something about branch - only if log level high |
---|
87 | */ |
---|
88 | virtual void print(); |
---|
89 | |
---|
90 | /// Lower and upper bounds for down branch |
---|
91 | inline const double * downBounds() const { |
---|
92 | return down_; |
---|
93 | } |
---|
94 | /// Lower and upper bounds for up branch |
---|
95 | inline const double * upBounds() const { |
---|
96 | return up_; |
---|
97 | } |
---|
98 | /// Set lower and upper bounds for down branch |
---|
99 | inline void setDownBounds(const double bounds[2]) { |
---|
100 | memcpy(down_, bounds, 2*sizeof(double)); |
---|
101 | } |
---|
102 | /// Set lower and upper bounds for up branch |
---|
103 | inline void setUpBounds(const double bounds[2]) { |
---|
104 | memcpy(up_, bounds, 2*sizeof(double)); |
---|
105 | } |
---|
106 | #ifdef FUNNY_BRANCHING |
---|
107 | /** Which variable (top bit if upper bound changing, |
---|
108 | next bit if on down branch */ |
---|
109 | inline const int * variables() const { |
---|
110 | return variables_; |
---|
111 | } |
---|
112 | // New bound |
---|
113 | inline const double * newBounds() const { |
---|
114 | return newBounds_; |
---|
115 | } |
---|
116 | /// Number of bound changes |
---|
117 | inline int numberExtraChangedBounds() const { |
---|
118 | return numberExtraChangedBounds_; |
---|
119 | } |
---|
120 | /// Just apply extra bounds to one variable - COIN_DBL_MAX ignore |
---|
121 | int applyExtraBounds(int iColumn, double lower, double upper, int way) ; |
---|
122 | /// Deactivate bounds for branching |
---|
123 | void deactivate(); |
---|
124 | /// Are active bounds for branching |
---|
125 | inline bool active() const { |
---|
126 | return (down_[1] != -COIN_DBL_MAX); |
---|
127 | } |
---|
128 | #endif |
---|
129 | |
---|
130 | /** Return the type (an integer identifier) of \c this */ |
---|
131 | virtual CbcBranchObjType type() const { |
---|
132 | return SimpleIntegerBranchObj; |
---|
133 | } |
---|
134 | |
---|
135 | /** Compare the \c this with \c brObj. \c this and \c brObj must be os the |
---|
136 | same type and must have the same original object, but they may have |
---|
137 | different feasible regions. |
---|
138 | Return the appropriate CbcRangeCompare value (first argument being the |
---|
139 | sub/superset if that's the case). In case of overlap (and if \c |
---|
140 | replaceIfOverlap is true) replace the current branching object with one |
---|
141 | whose feasible region is the overlap. |
---|
142 | */ |
---|
143 | virtual CbcRangeCompare compareBranchingObject |
---|
144 | (const CbcBranchingObject* brObj, const bool replaceIfOverlap = false); |
---|
145 | |
---|
146 | protected: |
---|
147 | /// Lower [0] and upper [1] bounds for the down arm (way_ = -1) |
---|
148 | double down_[2]; |
---|
149 | /// Lower [0] and upper [1] bounds for the up arm (way_ = 1) |
---|
150 | double up_[2]; |
---|
151 | #ifdef FUNNY_BRANCHING |
---|
152 | /** Which variable (top bit if upper bound changing) |
---|
153 | next bit if changing on down branch only */ |
---|
154 | int * variables_; |
---|
155 | // New bound |
---|
156 | double * newBounds_; |
---|
157 | /// Number of Extra bound changes |
---|
158 | int numberExtraChangedBounds_; |
---|
159 | #endif |
---|
160 | }; |
---|
161 | |
---|
162 | /// Define a single integer class |
---|
163 | |
---|
164 | |
---|
165 | class CbcSimpleInteger : public CbcObject { |
---|
166 | |
---|
167 | public: |
---|
168 | |
---|
169 | // Default Constructor |
---|
170 | CbcSimpleInteger (); |
---|
171 | |
---|
172 | // Useful constructor - passed model and index |
---|
173 | CbcSimpleInteger (CbcModel * model, int iColumn, double breakEven = 0.5); |
---|
174 | |
---|
175 | // Useful constructor - passed model and Osi object |
---|
176 | CbcSimpleInteger (CbcModel * model, const OsiSimpleInteger * object); |
---|
177 | |
---|
178 | // Copy constructor |
---|
179 | CbcSimpleInteger ( const CbcSimpleInteger &); |
---|
180 | |
---|
181 | /// Clone |
---|
182 | virtual CbcObject * clone() const; |
---|
183 | |
---|
184 | // Assignment operator |
---|
185 | CbcSimpleInteger & operator=( const CbcSimpleInteger& rhs); |
---|
186 | |
---|
187 | // Destructor |
---|
188 | virtual ~CbcSimpleInteger (); |
---|
189 | /// Construct an OsiSimpleInteger object |
---|
190 | OsiSimpleInteger * osiObject() const; |
---|
191 | /// Infeasibility - large is 0.5 |
---|
192 | virtual double infeasibility(const OsiBranchingInformation * info, |
---|
193 | int &preferredWay) const; |
---|
194 | |
---|
195 | using CbcObject::feasibleRegion ; |
---|
196 | /** Set bounds to fix the variable at the current (integer) value. |
---|
197 | |
---|
198 | Given an integer value, set the lower and upper bounds to fix the |
---|
199 | variable. Returns amount it had to move variable. |
---|
200 | */ |
---|
201 | virtual double feasibleRegion(OsiSolverInterface * solver, const OsiBranchingInformation * info) const; |
---|
202 | |
---|
203 | /** Create a branching object and indicate which way to branch first. |
---|
204 | |
---|
205 | The branching object has to know how to create branches (fix |
---|
206 | variables, etc.) |
---|
207 | */ |
---|
208 | virtual CbcBranchingObject * createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) ; |
---|
209 | /// Fills in a created branching object |
---|
210 | void fillCreateBranch(CbcIntegerBranchingObject * branching, const OsiBranchingInformation * info, int way) ; |
---|
211 | |
---|
212 | using CbcObject::solverBranch ; |
---|
213 | /** Create an OsiSolverBranch object |
---|
214 | |
---|
215 | This returns NULL if branch not represented by bound changes |
---|
216 | */ |
---|
217 | virtual OsiSolverBranch * solverBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info) const; |
---|
218 | |
---|
219 | /** Set bounds to fix the variable at the current (integer) value. |
---|
220 | |
---|
221 | Given an integer value, set the lower and upper bounds to fix the |
---|
222 | variable. The algorithm takes a bit of care in order to compensate for |
---|
223 | minor numerical inaccuracy. |
---|
224 | */ |
---|
225 | virtual void feasibleRegion(); |
---|
226 | |
---|
227 | /** Column number if single column object -1 otherwise, |
---|
228 | so returns >= 0 |
---|
229 | Used by heuristics |
---|
230 | */ |
---|
231 | virtual int columnNumber() const; |
---|
232 | /// Set column number |
---|
233 | inline void setColumnNumber(int value) { |
---|
234 | columnNumber_ = value; |
---|
235 | } |
---|
236 | |
---|
237 | /** Reset variable bounds to their original values. |
---|
238 | |
---|
239 | Bounds may be tightened, so it may be good to be able to set this info in object. |
---|
240 | */ |
---|
241 | virtual void resetBounds(const OsiSolverInterface * solver) ; |
---|
242 | |
---|
243 | /** Change column numbers after preprocessing |
---|
244 | */ |
---|
245 | virtual void resetSequenceEtc(int numberColumns, const int * originalColumns) ; |
---|
246 | /// Original bounds |
---|
247 | inline double originalLowerBound() const { |
---|
248 | return originalLower_; |
---|
249 | } |
---|
250 | inline void setOriginalLowerBound(double value) { |
---|
251 | originalLower_ = value; |
---|
252 | } |
---|
253 | inline double originalUpperBound() const { |
---|
254 | return originalUpper_; |
---|
255 | } |
---|
256 | inline void setOriginalUpperBound(double value) { |
---|
257 | originalUpper_ = value; |
---|
258 | } |
---|
259 | /// Breakeven e.g 0.7 -> >= 0.7 go up first |
---|
260 | inline double breakEven() const { |
---|
261 | return breakEven_; |
---|
262 | } |
---|
263 | /// Set breakeven e.g 0.7 -> >= 0.7 go up first |
---|
264 | inline void setBreakEven(double value) { |
---|
265 | breakEven_ = value; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | protected: |
---|
270 | /// data |
---|
271 | |
---|
272 | /// Original lower bound |
---|
273 | double originalLower_; |
---|
274 | /// Original upper bound |
---|
275 | double originalUpper_; |
---|
276 | /// Breakeven i.e. >= this preferred is up |
---|
277 | double breakEven_; |
---|
278 | /// Column number in model |
---|
279 | int columnNumber_; |
---|
280 | /// If -1 down always chosen first, +1 up always, 0 normal |
---|
281 | int preferredWay_; |
---|
282 | }; |
---|
283 | #endif |
---|
284 | |
---|