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 | |
---|
74 | #ifdef JJF_ZERO |
---|
75 | // No need to override. Default works fine. |
---|
76 | /** Reset every information so that the branching object appears to point to |
---|
77 | the previous child. This method does not need to modify anything in any |
---|
78 | solver. */ |
---|
79 | virtual void previousBranch(); |
---|
80 | #endif |
---|
81 | |
---|
82 | using CbcBranchingObject::print ; |
---|
83 | /** \brief Print something about branch - only if log level high |
---|
84 | */ |
---|
85 | virtual void print(); |
---|
86 | |
---|
87 | /// Lower and upper bounds for down branch |
---|
88 | inline const double * downBounds() const { |
---|
89 | return down_; |
---|
90 | } |
---|
91 | /// Lower and upper bounds for up branch |
---|
92 | inline const double * upBounds() const { |
---|
93 | return up_; |
---|
94 | } |
---|
95 | /// Set lower and upper bounds for down branch |
---|
96 | inline void setDownBounds(const double bounds[2]) { |
---|
97 | memcpy(down_, bounds, 2*sizeof(double)); |
---|
98 | } |
---|
99 | /// Set lower and upper bounds for up branch |
---|
100 | inline void setUpBounds(const double bounds[2]) { |
---|
101 | memcpy(up_, bounds, 2*sizeof(double)); |
---|
102 | } |
---|
103 | #ifdef FUNNY_BRANCHING |
---|
104 | /** Which variable (top bit if upper bound changing, |
---|
105 | next bit if on down branch */ |
---|
106 | inline const int * variables() const { |
---|
107 | return variables_; |
---|
108 | } |
---|
109 | // New bound |
---|
110 | inline const double * newBounds() const { |
---|
111 | return newBounds_; |
---|
112 | } |
---|
113 | /// Number of bound changes |
---|
114 | inline int numberExtraChangedBounds() const { |
---|
115 | return numberExtraChangedBounds_; |
---|
116 | } |
---|
117 | /// Just apply extra bounds to one variable - COIN_DBL_MAX ignore |
---|
118 | int applyExtraBounds(int iColumn, double lower, double upper, int way) ; |
---|
119 | /// Deactivate bounds for branching |
---|
120 | void deactivate(); |
---|
121 | /// Are active bounds for branching |
---|
122 | inline bool active() const { |
---|
123 | return (down_[1] != -COIN_DBL_MAX); |
---|
124 | } |
---|
125 | #endif |
---|
126 | |
---|
127 | /** Return the type (an integer identifier) of \c this */ |
---|
128 | virtual CbcBranchObjType type() const { |
---|
129 | return SimpleIntegerBranchObj; |
---|
130 | } |
---|
131 | |
---|
132 | /** Compare the \c this with \c brObj. \c this and \c brObj must be os the |
---|
133 | same type and must have the same original object, but they may have |
---|
134 | different feasible regions. |
---|
135 | Return the appropriate CbcRangeCompare value (first argument being the |
---|
136 | sub/superset if that's the case). In case of overlap (and if \c |
---|
137 | replaceIfOverlap is true) replace the current branching object with one |
---|
138 | whose feasible region is the overlap. |
---|
139 | */ |
---|
140 | virtual CbcRangeCompare compareBranchingObject |
---|
141 | (const CbcBranchingObject* brObj, const bool replaceIfOverlap = false); |
---|
142 | |
---|
143 | protected: |
---|
144 | /// Lower [0] and upper [1] bounds for the down arm (way_ = -1) |
---|
145 | double down_[2]; |
---|
146 | /// Lower [0] and upper [1] bounds for the up arm (way_ = 1) |
---|
147 | double up_[2]; |
---|
148 | #ifdef FUNNY_BRANCHING |
---|
149 | /** Which variable (top bit if upper bound changing) |
---|
150 | next bit if changing on down branch only */ |
---|
151 | int * variables_; |
---|
152 | // New bound |
---|
153 | double * newBounds_; |
---|
154 | /// Number of Extra bound changes |
---|
155 | int numberExtraChangedBounds_; |
---|
156 | #endif |
---|
157 | }; |
---|
158 | |
---|
159 | /// Define a single integer class |
---|
160 | |
---|
161 | |
---|
162 | class CbcSimpleInteger : public CbcObject { |
---|
163 | |
---|
164 | public: |
---|
165 | |
---|
166 | // Default Constructor |
---|
167 | CbcSimpleInteger (); |
---|
168 | |
---|
169 | // Useful constructor - passed model and index |
---|
170 | CbcSimpleInteger (CbcModel * model, int iColumn, double breakEven = 0.5); |
---|
171 | |
---|
172 | // Useful constructor - passed model and Osi object |
---|
173 | CbcSimpleInteger (CbcModel * model, const OsiSimpleInteger * object); |
---|
174 | |
---|
175 | // Copy constructor |
---|
176 | CbcSimpleInteger ( const CbcSimpleInteger &); |
---|
177 | |
---|
178 | /// Clone |
---|
179 | virtual CbcObject * clone() const; |
---|
180 | |
---|
181 | // Assignment operator |
---|
182 | CbcSimpleInteger & operator=( const CbcSimpleInteger& rhs); |
---|
183 | |
---|
184 | // Destructor |
---|
185 | virtual ~CbcSimpleInteger (); |
---|
186 | /// Construct an OsiSimpleInteger object |
---|
187 | OsiSimpleInteger * osiObject() const; |
---|
188 | /// Infeasibility - large is 0.5 |
---|
189 | virtual double infeasibility(const OsiBranchingInformation * info, |
---|
190 | int &preferredWay) const; |
---|
191 | |
---|
192 | using CbcObject::feasibleRegion ; |
---|
193 | /** Set bounds to fix the variable at the current (integer) value. |
---|
194 | |
---|
195 | Given an integer value, set the lower and upper bounds to fix the |
---|
196 | variable. Returns amount it had to move variable. |
---|
197 | */ |
---|
198 | virtual double feasibleRegion(OsiSolverInterface * solver, const OsiBranchingInformation * info) const; |
---|
199 | |
---|
200 | /** Create a branching object and indicate which way to branch first. |
---|
201 | |
---|
202 | The branching object has to know how to create branches (fix |
---|
203 | variables, etc.) |
---|
204 | */ |
---|
205 | virtual CbcBranchingObject * createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) ; |
---|
206 | /// Fills in a created branching object |
---|
207 | void fillCreateBranch(CbcIntegerBranchingObject * branching, const OsiBranchingInformation * info, int way) ; |
---|
208 | |
---|
209 | using CbcObject::solverBranch ; |
---|
210 | /** Create an OsiSolverBranch object |
---|
211 | |
---|
212 | This returns NULL if branch not represented by bound changes |
---|
213 | */ |
---|
214 | virtual OsiSolverBranch * solverBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info) const; |
---|
215 | |
---|
216 | /** Set bounds to fix the variable at the current (integer) value. |
---|
217 | |
---|
218 | Given an integer value, set the lower and upper bounds to fix the |
---|
219 | variable. The algorithm takes a bit of care in order to compensate for |
---|
220 | minor numerical inaccuracy. |
---|
221 | */ |
---|
222 | virtual void feasibleRegion(); |
---|
223 | |
---|
224 | /** Column number if single column object -1 otherwise, |
---|
225 | so returns >= 0 |
---|
226 | Used by heuristics |
---|
227 | */ |
---|
228 | virtual int columnNumber() const; |
---|
229 | /// Set column number |
---|
230 | inline void setColumnNumber(int value) { |
---|
231 | columnNumber_ = value; |
---|
232 | } |
---|
233 | |
---|
234 | /** Reset variable bounds to their original values. |
---|
235 | |
---|
236 | Bounds may be tightened, so it may be good to be able to set this info in object. |
---|
237 | */ |
---|
238 | virtual void resetBounds(const OsiSolverInterface * solver) ; |
---|
239 | /** Change column numbers after preprocessing |
---|
240 | */ |
---|
241 | virtual void resetSequenceEtc(int numberColumns, const int * originalColumns) ; |
---|
242 | /// Original bounds |
---|
243 | inline double originalLowerBound() const { |
---|
244 | return originalLower_; |
---|
245 | } |
---|
246 | inline void setOriginalLowerBound(double value) { |
---|
247 | originalLower_ = value; |
---|
248 | } |
---|
249 | inline double originalUpperBound() const { |
---|
250 | return originalUpper_; |
---|
251 | } |
---|
252 | inline void setOriginalUpperBound(double value) { |
---|
253 | originalUpper_ = value; |
---|
254 | } |
---|
255 | /// Breakeven e.g 0.7 -> >= 0.7 go up first |
---|
256 | inline double breakEven() const { |
---|
257 | return breakEven_; |
---|
258 | } |
---|
259 | /// Set breakeven e.g 0.7 -> >= 0.7 go up first |
---|
260 | inline void setBreakEven(double value) { |
---|
261 | breakEven_ = value; |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | protected: |
---|
266 | /// data |
---|
267 | |
---|
268 | /// Original lower bound |
---|
269 | double originalLower_; |
---|
270 | /// Original upper bound |
---|
271 | double originalUpper_; |
---|
272 | /// Breakeven i.e. >= this preferred is up |
---|
273 | double breakEven_; |
---|
274 | /// Column number in model |
---|
275 | int columnNumber_; |
---|
276 | /// If -1 down always chosen first, +1 up always, 0 normal |
---|
277 | int preferredWay_; |
---|
278 | }; |
---|
279 | #endif |
---|
280 | |
---|