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