1 | // $Id: CbcObject.hpp 1883 2013-04-06 13:33:15Z stefan $ |
---|
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 CbcObject_H |
---|
9 | #define CbcObject_H |
---|
10 | |
---|
11 | #include <string> |
---|
12 | #include <vector> |
---|
13 | #include "OsiBranchingObject.hpp" |
---|
14 | class OsiSolverInterface; |
---|
15 | class OsiSolverBranch; |
---|
16 | |
---|
17 | class CbcModel; |
---|
18 | class CbcNode; |
---|
19 | class CbcNodeInfo; |
---|
20 | class CbcBranchingObject; |
---|
21 | class OsiChooseVariable; |
---|
22 | class CbcObjectUpdateData; |
---|
23 | //############################################################################# |
---|
24 | |
---|
25 | /** Abstract base class for `objects'. |
---|
26 | It now just has stuff that OsiObject does not have |
---|
27 | |
---|
28 | The branching model used in Cbc is based on the idea of an <i>object</i>. |
---|
29 | In the abstract, an object is something that has a feasible region, can be |
---|
30 | evaluated for infeasibility, can be branched on (<i>i.e.</i>, there's some |
---|
31 | constructive action to be taken to move toward feasibility), and allows |
---|
32 | comparison of the effect of branching. |
---|
33 | |
---|
34 | This class (CbcObject) is the base class for an object. To round out the |
---|
35 | branching model, the class CbcBranchingObject describes how to perform a |
---|
36 | branch, and the class CbcBranchDecision describes how to compare two |
---|
37 | CbcBranchingObjects. |
---|
38 | |
---|
39 | To create a new type of object you need to provide three methods: |
---|
40 | #infeasibility(), #feasibleRegion(), and #createCbcBranch(), described below. |
---|
41 | |
---|
42 | This base class is primarily virtual to allow for any form of structure. |
---|
43 | Any form of discontinuity is allowed. |
---|
44 | |
---|
45 | \todo The notion that all branches are binary (two arms) is wired into the |
---|
46 | implementation of CbcObject, CbcBranchingObject, and |
---|
47 | CbcBranchDecision. Changing this will require a moderate amount of |
---|
48 | recoding. |
---|
49 | */ |
---|
50 | // This can be used if object wants to skip strong branching |
---|
51 | typedef struct { |
---|
52 | CbcBranchingObject * possibleBranch; // what a branch would do |
---|
53 | double upMovement; // cost going up (and initial away from feasible) |
---|
54 | double downMovement; // cost going down |
---|
55 | int numIntInfeasUp ; // without odd ones |
---|
56 | int numObjInfeasUp ; // just odd ones |
---|
57 | bool finishedUp; // true if solver finished |
---|
58 | int numItersUp ; // number of iterations in solver |
---|
59 | int numIntInfeasDown ; // without odd ones |
---|
60 | int numObjInfeasDown ; // just odd ones |
---|
61 | bool finishedDown; // true if solver finished |
---|
62 | int numItersDown; // number of iterations in solver |
---|
63 | int objectNumber; // Which object it is |
---|
64 | int fix; // 0 if no fix, 1 if we can fix up, -1 if we can fix down |
---|
65 | } CbcStrongInfo; |
---|
66 | |
---|
67 | class CbcObject : public OsiObject { |
---|
68 | |
---|
69 | public: |
---|
70 | |
---|
71 | // Default Constructor |
---|
72 | CbcObject (); |
---|
73 | |
---|
74 | // Useful constructor |
---|
75 | CbcObject (CbcModel * model); |
---|
76 | |
---|
77 | // Copy constructor |
---|
78 | CbcObject ( const CbcObject &); |
---|
79 | |
---|
80 | // Assignment operator |
---|
81 | CbcObject & operator=( const CbcObject& rhs); |
---|
82 | |
---|
83 | /// Clone |
---|
84 | virtual CbcObject * clone() const = 0; |
---|
85 | |
---|
86 | /// Destructor |
---|
87 | virtual ~CbcObject (); |
---|
88 | |
---|
89 | /** Infeasibility of the object |
---|
90 | |
---|
91 | This is some measure of the infeasibility of the object. It should be |
---|
92 | scaled to be in the range [0.0, 0.5], with 0.0 indicating the object |
---|
93 | is satisfied. |
---|
94 | |
---|
95 | The preferred branching direction is returned in preferredWay, |
---|
96 | |
---|
97 | This is used to prepare for strong branching but should also think of |
---|
98 | case when no strong branching |
---|
99 | |
---|
100 | The object may also compute an estimate of cost of going "up" or "down". |
---|
101 | This will probably be based on pseudo-cost ideas |
---|
102 | */ |
---|
103 | #ifdef CBC_NEW_STYLE_BRANCH |
---|
104 | virtual double infeasibility(const OsiBranchingInformation * info, |
---|
105 | int &preferredWay) const = 0; |
---|
106 | #else |
---|
107 | virtual double infeasibility(const OsiBranchingInformation * /*info*/, |
---|
108 | int &preferredWay) const { |
---|
109 | return infeasibility(preferredWay); |
---|
110 | } |
---|
111 | virtual double infeasibility(int &/*preferredWay*/) const { |
---|
112 | throw CoinError("Need code", "infeasibility", "CbcBranchBase"); |
---|
113 | } |
---|
114 | #endif |
---|
115 | |
---|
116 | /** For the variable(s) referenced by the object, |
---|
117 | look at the current solution and set bounds to match the solution. |
---|
118 | */ |
---|
119 | virtual void feasibleRegion() = 0; |
---|
120 | /// Dummy one for compatibility |
---|
121 | virtual double feasibleRegion(OsiSolverInterface * solver, const OsiBranchingInformation * info) const; |
---|
122 | |
---|
123 | /** For the variable(s) referenced by the object, |
---|
124 | look at the current solution and set bounds to match the solution. |
---|
125 | Returns measure of how much it had to move solution to make feasible |
---|
126 | */ |
---|
127 | virtual double feasibleRegion(OsiSolverInterface * solver) const ; |
---|
128 | |
---|
129 | /** Create a branching object and indicate which way to branch first. |
---|
130 | |
---|
131 | The branching object has to know how to create branches (fix |
---|
132 | variables, etc.) |
---|
133 | */ |
---|
134 | #ifdef CBC_NEW_STYLE_BRANCH |
---|
135 | virtual CbcBranchingObject * createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) = 0; |
---|
136 | #else |
---|
137 | virtual CbcBranchingObject * createCbcBranch(OsiSolverInterface * |
---|
138 | /* solver */, |
---|
139 | const OsiBranchingInformation * |
---|
140 | /* info */, int /* way */) { |
---|
141 | // return createBranch(solver, info, way); |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | virtual OsiBranchingObject * createBranch(OsiSolverInterface * /*solver*/, |
---|
145 | const OsiBranchingInformation * /*info*/, int /*way*/) const { |
---|
146 | throw CoinError("Need code", "createBranch", "CbcBranchBase"); |
---|
147 | } |
---|
148 | #endif |
---|
149 | /** Create an Osibranching object and indicate which way to branch first. |
---|
150 | |
---|
151 | The branching object has to know how to create branches (fix |
---|
152 | variables, etc.) |
---|
153 | */ |
---|
154 | virtual OsiBranchingObject * createOsiBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) const; |
---|
155 | /** Create an OsiSolverBranch object |
---|
156 | |
---|
157 | This returns NULL if branch not represented by bound changes |
---|
158 | */ |
---|
159 | virtual OsiSolverBranch * solverBranch() const; |
---|
160 | |
---|
161 | /** \brief Given a valid solution (with reduced costs, etc.), |
---|
162 | return a branching object which would give a new feasible |
---|
163 | point in a good direction. |
---|
164 | |
---|
165 | If the method cannot generate a feasible point (because there aren't |
---|
166 | any, or because it isn't bright enough to find one), it should |
---|
167 | return null. |
---|
168 | */ |
---|
169 | virtual CbcBranchingObject * preferredNewFeasible() const { |
---|
170 | return NULL; |
---|
171 | } |
---|
172 | |
---|
173 | /** \brief Given a valid solution (with reduced costs, etc.), |
---|
174 | return a branching object which would give a new feasible |
---|
175 | point in a bad direction. |
---|
176 | |
---|
177 | If the method cannot generate a feasible point (because there aren't |
---|
178 | any, or because it isn't bright enough to find one), it should |
---|
179 | return null. |
---|
180 | */ |
---|
181 | virtual CbcBranchingObject * notPreferredNewFeasible() const { |
---|
182 | return NULL; |
---|
183 | } |
---|
184 | |
---|
185 | /** Reset variable bounds to their original values. |
---|
186 | |
---|
187 | Bounds may be tightened, so it may be good to be able to set this info in object. |
---|
188 | */ |
---|
189 | virtual void resetBounds(const OsiSolverInterface * ) {} |
---|
190 | |
---|
191 | /** Returns floor and ceiling i.e. closest valid points |
---|
192 | */ |
---|
193 | virtual void floorCeiling(double & floorValue, double & ceilingValue, double value, |
---|
194 | double tolerance) const; |
---|
195 | |
---|
196 | /** Pass in information on branch just done and create CbcObjectUpdateData instance. |
---|
197 | If object does not need data then backward pointer will be NULL. |
---|
198 | Assumes can get information from solver */ |
---|
199 | virtual CbcObjectUpdateData createUpdateInformation(const OsiSolverInterface * solver, |
---|
200 | const CbcNode * node, |
---|
201 | const CbcBranchingObject * branchingObject); |
---|
202 | |
---|
203 | /// Update object by CbcObjectUpdateData |
---|
204 | virtual void updateInformation(const CbcObjectUpdateData & ) {} |
---|
205 | |
---|
206 | /// Identifier (normally column number in matrix) |
---|
207 | inline int id() const { |
---|
208 | return id_; |
---|
209 | } |
---|
210 | |
---|
211 | /** Set identifier (normally column number in matrix) |
---|
212 | but 1000000000 to 1100000000 means optional branching object |
---|
213 | i.e. code would work without it */ |
---|
214 | inline void setId(int value) { |
---|
215 | id_ = value; |
---|
216 | } |
---|
217 | |
---|
218 | /** Return true if optional branching object |
---|
219 | i.e. code would work without it */ |
---|
220 | inline bool optionalObject() const { |
---|
221 | return (id_ >= 1000000000 && id_ < 1100000000); |
---|
222 | } |
---|
223 | |
---|
224 | /// Get position in object_ list |
---|
225 | inline int position() const { |
---|
226 | return position_; |
---|
227 | } |
---|
228 | |
---|
229 | /// Set position in object_ list |
---|
230 | inline void setPosition(int position) { |
---|
231 | position_ = position; |
---|
232 | } |
---|
233 | |
---|
234 | /// update model |
---|
235 | inline void setModel(CbcModel * model) { |
---|
236 | model_ = model; |
---|
237 | } |
---|
238 | |
---|
239 | /// Return model |
---|
240 | inline CbcModel * model() const { |
---|
241 | return model_; |
---|
242 | } |
---|
243 | |
---|
244 | /// If -1 down always chosen first, +1 up always, 0 normal |
---|
245 | inline int preferredWay() const { |
---|
246 | return preferredWay_; |
---|
247 | } |
---|
248 | /// Set -1 down always chosen first, +1 up always, 0 normal |
---|
249 | inline void setPreferredWay(int value) { |
---|
250 | preferredWay_ = value; |
---|
251 | } |
---|
252 | /// Redoes data when sequence numbers change |
---|
253 | virtual void redoSequenceEtc(CbcModel * , int , const int * ) {} |
---|
254 | /// Initialize for branching |
---|
255 | virtual void initializeForBranching(CbcModel * ) {} |
---|
256 | |
---|
257 | protected: |
---|
258 | /// data |
---|
259 | |
---|
260 | /// Model |
---|
261 | CbcModel * model_; |
---|
262 | /// Identifier (normally column number in matrix) |
---|
263 | int id_; |
---|
264 | /// Position in object list |
---|
265 | int position_; |
---|
266 | /// If -1 down always chosen first, +1 up always, 0 normal |
---|
267 | int preferredWay_; |
---|
268 | |
---|
269 | }; |
---|
270 | |
---|
271 | #endif |
---|
272 | |
---|