Debian Codegraph @

Bonmin/src/Algorithms/Branching/BonChooseVariable.hpp

raw
1 // Copyright (C) 2006, 2008 International Business Machines
2 // Corporation and others. All Rights Reserved.
3 #ifndef BonChooseVariable_H
4 #define BonChooseVariable_H
5
6 #include "OsiChooseVariable.hpp"
7 #ifdef BONMIN_CURVATURE_BRANCHING
8 #include "BonCurvatureEstimator.hpp"
9 #endif
10 #include "BonOsiTMINLPInterface.hpp"
11 #include "CoinMessageHandler.hpp"
12 #include "BonBabSetupBase.hpp"
13 // Forward declaration
14 class CbcModel;
15
16 #define OLD_USEFULLNESS
17
18 namespace Bonmin
19 {
20
21 class HotInfo : public OsiHotInfo {
22 public:
23 /// Default constructor
24 HotInfo();
25
26 /// Constructor from usefull information
27 HotInfo( OsiSolverInterface * solver,
28 const OsiBranchingInformation *info,
29 const OsiObject * const * objects, int whichObject);
30
31 /// Copy constructor
32 HotInfo(const HotInfo & other);
33
34 /// Assignment operator
35 HotInfo & operator=(const HotInfo & rhs);
36
37 /// Clone
38 virtual OsiHotInfo * clone() const;
39
40 /// Destructor
41 virtual ~HotInfo();
42
43 /// Fill in some usefull information after a strong branching is done:
44 int updateInformation( const OsiSolverInterface * solver, const OsiBranchingInformation * info,
45 OsiChooseVariable * choose);
46
47 /// up infeasibility
48 double upInfeasibility() const{
49 return infeasibilities_[1];
50 }
51
52 /// down infeasibility
53 double downInfeasibility() const{
54 return infeasibilities_[0];
55 }
56
57
58 /// Set the down infeasibility
59 void setUpInfeasibility(double x){
60 assert(branchingObject_->numberBranches()==2);
61 infeasibilities_[1] = x;
62 }
63
64 /// Set the down infeasibility
65 void setDownInfeasibility(double x){
66 assert(branchingObject_->numberBranches()==2);
67 infeasibilities_[0] = x;
68 }
69 private:
70 /// infeasibilities of children
71 vector<double> infeasibilities_;
72 };
73
74 /** This class chooses a variable to branch on
75
76 This is the base class for the branching rules in Bonmin (inherits
77 from OsiChooseVariable). This class implements a simple strong branching algorithm where the changes in the objective
78 value induced by branching on a specific object are estimated with the pure virtual function fill_changes.
79 */
80
81 class BonChooseVariable : public OsiChooseVariable
82 {
83 protected:
84 /** This is a utility function which does strong branching on
85 a list of objects and stores the results in OsiHotInfo.objects.
86 On entry the object sequence is stored in the OsiHotInfo object
87 and maybe more.
88 It returns -
89 -1 - one branch was infeasible both ways
90 0 - all inspected - nothing can be fixed
91 1 - all inspected - some can be fixed (returnCriterion==0)
92 2 - may be returning early - one can be fixed (last one done) (returnCriterion==1)
93 3 - returning because max time
94
95 */
96 virtual int doStrongBranching( OsiSolverInterface * solver,
97 OsiBranchingInformation *info,
98 int numberToDo, int returnCriterion);
99 #ifndef OLD_USEFULLNESS
100 /** Criterion applied to sort candidates.*/
101 enum CandidateSortCriterion {
102 DecrPs = 0,
103 IncrPs,
104 DecrInfeas,
105 IncrInfeas};
106 #endif
107
108 /** Statuses for strong branching candidates.*/
109 enum StrongStatus{
110 NotDone=-1,
111 Feasible/** Child is proven feasible.*/,
112 Infeasible /** Child is proven infeasible.*/,
113 NotFinished /** Child is not finished.*/};
114 public:
115 /** \name Message handling.*/
116 /** @{ */
117 enum Messages_Types {
118 PS_COST_HISTORY = 0,
119 PS_COST_MULT,
120 PS_COST_ESTIMATES,
121 CANDIDATE_LIST,
122 CANDIDATE_LIST2,
123 CANDIDATE_LIST3,
124 SB_START,
125 SB_HEADER,
126 SB_RES,
127 BRANCH_VAR,
128 CHOSEN_VAR,
129 UPDATE_PS_COST,
130 BON_CHOOSE_MESSAGES_DUMMY_END
131 };
132
133 class Messages : public CoinMessages
134 {
135 public:
136 Messages();
137 };
138
139 void passInMessageHandler(CoinMessageHandler * handler) {
140 int logLevel = handler_->logLevel();
141 delete handler_;
142 handler_ = handler->clone();
143 handler_->setLogLevel(logLevel);
144 }
145
146 CoinMessageHandler& message(Messages_Types type) const {
147 return handler_->message(type, messages_);
148 }
149 /** @} */
150
151
152
153 enum DoStrongReturnStatuses{
154 provenInfeasible = -1 /** One branch has two infeasible children.*/,
155 doneNoFixing /** All done no variable can be fixed.*/,
156 doneCanFix /** Several variable can be fixed.*/,
157 interuptedCanFix /** Interrupted and found a variable to fix.*/,
158 maxTime /** Interrupted because of time limit.*/};
159
160 /** Return statuses for chooseVariable.*/
161 enum chooseVariableReturnStatuses{
162 infeasibleNode = -1/** Node has been proven infeasible.*/,
163 hasCandidate /** Normal termination, found a variable to branch on.*/,
164 feasibleNode /** All variable are feasible, the node is feasible.*/,
165 canFixAndStrongBranch /** Found variable to fix and also has remaining candidate for strong branching.*/,
166 canFixAndBranch/** Found variable to fix and also has a (non-strong) branching candidate.*/,
167 canFixNoCandidate /** Can fix variables but does not have strong branching candidates.*/
168 };
169 /// Constructor from solver (so we can set up arrays etc)
170 BonChooseVariable (BabSetupBase& b, const OsiSolverInterface* solver);
171
172 /// Copy constructor
173 BonChooseVariable (const BonChooseVariable &);
174
175 /// Assignment operator
176 BonChooseVariable & operator= (const BonChooseVariable& rhs);
177
178 /// Clone
179 virtual OsiChooseVariable * clone() const;
180
181 /// Destructor
182 virtual ~BonChooseVariable ();
183
184 static void registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions);
185
186 /** Helper functions for setupList and chooseVariable */
187 double maxminCrit(const OsiBranchingInformation* info) const;
188 void computeMultipliers(double& upMult, double& downMult) const;
189 double computeUsefulness(const double MAXMIN_CRITERION,
190 const double upMult, const double dowMult,
191 const double value,
192 const OsiObject* object, int i,
193 double& value2) const;
194
195 /** Sets up strong list and clears all if initialize is true.
196 Returns number of infeasibilities. */
197 virtual int setupList ( OsiBranchingInformation *info, bool initialize);
198
199 /** Choose a variable
200 Returns -
201 -1 Node is infeasible
202 0 Normal termination - we have a candidate
203 1 All looks satisfied - no candidate
204 2 We can change the bound on a variable - but we also have a strong branching candidate
205 3 We can change the bound on a variable - but we have a non-strong branching candidate
206 4 We can change the bound on a variable - no other candidates
207 We can pick up branch from bestObjectIndex() and bestWhichWay()
208 We can pick up a forced branch (can change bound) from firstForcedObjectIndex() and firstForcedWhichWay()
209 If we have a solution then we can pick up from goodObjectiveValue() and goodSolution()
210 If fixVariables is true then 2,3,4 are all really same as problem changed
211 */
212 virtual int chooseVariable( OsiSolverInterface * solver, OsiBranchingInformation *info, bool fixVariables);
213 /** This is a utility function which does strong branching on
214 a list of objects and stores the results in OsiHotInfo.objects.
215 On entry the object sequence is stored in the OsiHotInfo object
216 and maybe more.
217 It returns -
218 -1 - one branch was infeasible both ways
219 0 - all inspected - nothing can be fixed
220 1 - all inspected - some can be fixed (returnCriterion==0)
221 2 - may be returning early - one can be fixed (last one done) (returnCriterion==1)
222 3 - returning because max time
223
224 */
225
226 /// Given a candidate fill in useful information e.g. estimates
227 virtual void updateInformation(const OsiBranchingInformation *info,
228 int branch, OsiHotInfo * hotInfo);
229 #if 1
230 /// Given a branch fill in useful information e.g. estimates
231 virtual void updateInformation( int whichObject, int branch,
232 double changeInObjective, double changeInValue,
233 int status);
234 #endif
235
236 /** Method for setting CbcModel, which is used to get statusOfSearch */
237 void setCbcModel(CbcModel* cbc_model)
238 {
239 cbc_model_ = cbc_model;
240 }
241
242 void setOnlyPseudoWhenTrusted(bool only_pseudo_when_trusted)
243 {
244 only_pseudo_when_trusted_ = only_pseudo_when_trusted;
245 }
246
247
248 /** Access to pseudo costs storage.*/
249 const OsiPseudoCosts & pseudoCosts() const{
250 return pseudoCosts_;}
251
252 /** Access to pseudo costs storage.*/
253 OsiPseudoCosts & pseudoCosts() {
254 return pseudoCosts_;}
255 protected:
256
257 /// Holding on the a pointer to the journalist
258 Ipopt::SmartPtr<Ipopt::Journalist> jnlst_;
259
260 /// verbosity level
261 int bb_log_level_;
262
263 /** Stores strong branching results.*/
264 vector<HotInfo> results_;
265
266 /** Determine status of strong branching solution.*/
267 int determineStatus(OsiSolverInterface * solver) const {
268 if (solver->isProvenOptimal())
269 return 0; // optimal
270 else if (solver->isIterationLimitReached()
271 &&!solver->isDualObjectiveLimitReached())
272 return 2; // unknown
273 else
274 return 1; // infeasible
275 }
276
277 private:
278 /** Default Constructor, forbiden for some reason.*/
279 BonChooseVariable ();
280
281 /** Global time limit for algorithm. */
282 double time_limit_;
283
284 /** Starting time of algorithm.*/
285 double start_time_;
286 protected:
287 /// CbcModel, used to get status of search
288 CbcModel* cbc_model_;
289
290 /** Flag indicating whether we don't want to mix strong branching
291 * and pseudo costs during the decision which variable to branch
292 * on */
293 bool only_pseudo_when_trusted_;
294
295 /** Number of variables put into the list because there were not
296 * trusted */
297 int number_not_trusted_;
298
299 /** Message handler.*/
300 CoinMessageHandler * handler_;
301
302 /** Messages.*/
303 Messages messages_;
304 // ToDo: Make this options
305 /** @name Algoirithmic options */
306 //@{
307 /** maxmin weight in branching decision when no solution has been
308 * found yet */
309 double maxmin_crit_no_sol_;
310 /** maxmin weight in branching decision when no solution has been
311 * found yet */
312 double maxmin_crit_have_sol_;
313 /** fraction of branching candidates that are not trusted yet */
314 double setup_pseudo_frac_;
315 /** number of times a branch has to happen so that it is trusted in
316 * setupList */
317 int numberBeforeTrustedList_;
318 /** number of strong branching points at root node */
319 int numberStrongRoot_;
320 /** backup of numberStrong_ before Root node solve */
321 int numberStrongBackup_;
322 /** number of look-ahead strong-branching steps */
323 int numberLookAhead_;
324 #ifndef OLD_USEFULLNESS
325 /** Criterion to use in setup list.*/
326 CandidateSortCriterion sortCrit_;
327 #endif
328 /** Always strong branch that many first candidate in the list regardless of numberTrusted.*/
329 int minNumberStrongBranch_;
330 /** Stores the pseudo costs. */
331 OsiPseudoCosts pseudoCosts_;
332 /** Wether or not to trust strong branching results for updating pseudo costs.*/
333 int trustStrongForPseudoCosts_;
334
335 //@}
336
337 /** detecting if this is root node */
338 bool isRootNode(const OsiBranchingInformation *info) const;
339
340 /** Stores the class name for throwing errors.*/
341 static const std::string CNAME;
342 };
343
344 }
345 #endif