Debian Codegraph @

Bonmin/src/CbcBonmin/BonDiver.hpp

raw
1 // (C) Copyright International Business Machines Corporation 2007
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, International Business Machines Corporation
7 //
8 // Date : 09/01/2007
9
10 #ifndef BonDiver_H
11 #define BonDiver_H
12
13 #include "BonminConfig.h"
14 #include "CbcCompareBase.hpp"
15 #include "CbcTree.hpp"
16 #include "IpRegOptions.hpp"
17 #include "IpOptionsList.hpp"
18 #include "CbcCompareActual.hpp"
19 #include "BonRegisteredOptions.hpp"
20 #include <list>
21 namespace Bonmin
22 {
23 class BabSetupBase;
24 /** Class to do diving in the tree. Principle is that branch-and-bound follows current branch of the tree untill it
25 hits the bottom at which point it goes to the best candidate (according to CbcCompare) on the heap.*/
26 class CbcDiver : public CbcTree
27 {
28 public:
29 /// Default constructor.
30 CbcDiver();
31
32 ///Copy constructor.
33 CbcDiver(const CbcDiver &rhs);
34
35 /// Assignment operator.
36 CbcDiver & operator=(const CbcDiver &rhs);
37
38 /// Destructor.
39 virtual ~CbcDiver();
40
41 ///Virtual copy constructor.
42 virtual CbcTree * clone() const;
43
44 /** \name Heap access and maintenance methods.*/
45 /**@{*/
46 ///Return top node (next node to process.*/
47 virtual CbcNode * top() const;
48
49 /// Add node to the heap.
50 virtual void push(CbcNode * x);
51 /// Remove the top node of the heap.
52 virtual void pop();
53 /// Remove the best node from the heap and return it
54 virtual CbcNode * bestNode(double cutoff);
55 /** @} */
56
57 /// \name vector methods
58 /** @{ */
59 /** Test if empty. */
60 virtual bool empty();
61 /** Give size of the tree.*/
62 virtual int size()
63 {
64 return (static_cast<int>(nodes_.size()) + (nextOnBranch_ != NULL) );
65 }
66 /** @} */
67
68 /*! \brief Prune the tree using an objective function cutoff
69
70 This routine removes all nodes with objective worst than the
71 specified cutoff value.
72 It also sets bestPossibleObjective to best
73 of all on tree before deleting.
74 */
75 virtual void cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective);
76
77 /// Get best possible objective function in the tree
78 virtual double getBestPossibleObjective();
79
80
81 ///Don't know what this is yet?
82 virtual void endSearch()
83 {
84 nextOnBranch_ = NULL;
85 }
86
87 ///Register the options of the method.
88 static void registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions);
89
90 /// Initialize the method (get options)
91 void initialize(BabSetupBase &b);
92
93 private:
94 /** Say if we are cleaning the tree (then only call CbcTree functions).*/
95 bool treeCleaning_;
96 /** Noext node on the branch.*/
97 CbcNode * nextOnBranch_;
98 /** Flag indicating if we want to stop diving based on the guessed
99 objective value and the cutoff value */
100 bool stop_diving_on_cutoff_;
101 };
102
103
104 /** Class to do probed diving in the tree.
105 * Principle is that branch-and-bound follows current branch of the tree by exploring the two children at each level
106 * and continuing the dive on the best one of the two. Untill it
107 * hits the bottom at which point it goes to the best candidate (according to CbcCompare) on the heap.*/
108 class CbcProbedDiver : public CbcTree
109 {
110 public:
111 /// Default constructor.
112 CbcProbedDiver();
113
114 ///Copy constructor.
115 CbcProbedDiver(const CbcProbedDiver &rhs);
116
117 /// Assignment operator.
118 CbcProbedDiver & operator=(const CbcProbedDiver &rhs);
119
120 /// Destructor.
121 virtual ~CbcProbedDiver();
122
123 ///Virtual copy constructor.
124 virtual CbcTree * clone() const;
125
126 /** \name Heap access and maintenance methods.*/
127 /**@{*/
128 ///Return top node (next node to process.*/
129 virtual CbcNode * top() const;
130
131 /// Add node to the heap.
132 virtual void push(CbcNode * x);
133 /// Remove the top node of the heap.
134 virtual void pop();
135 /// Remove the best node from the heap and return it
136 virtual CbcNode * bestNode(double cutoff);
137 /** @} */
138
139 /// \name vector methods
140 /** @{ */
141 /** Test if empty. */
142 virtual bool empty();
143 /** Give size of the tree.*/
144 virtual int size()
145 {
146 return (static_cast<int>(nodes_.size()) + (nextOnBranch_ != NULL) + (candidateChild_ != NULL) );
147 }
148 /** @} */
149
150 /*! \brief Prune the tree using an objective function cutoff
151
152 This routine removes all nodes with objective worst than the
153 specified cutoff value.
154 It also sets bestPossibleObjective to best
155 of all on tree before deleting.
156 */
157 virtual void cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective);
158
159 /// Get best possible objective function in the tree
160 virtual double getBestPossibleObjective();
161
162
163 ///Don't know what this is yet?
164 virtual void endSearch()
165 {
166 nextOnBranch_ = NULL;
167 }
168
169 /// Initialize the method (get options)
170 void initialize(BabSetupBase &b);
171
172 private:
173 /** Say if we are cleaning the tree (then only call CbcTree functions).*/
174 bool treeCleaning_;
175 /** Next node on the branch.*/
176 CbcNode * nextOnBranch_;
177 /** Candidate child explored.*/
178 CbcNode * candidateChild_;
179 /** Flag indicating if we want to stop diving based on the guessed
180 objective value and the cutoff value */
181 bool stop_diving_on_cutoff_;
182 };
183
184
185 /** A more elaborate diving class. First there are several modes which can be commanded by the Comparison class below.
186 In particular can command to dive to find solutions, to try to close the bound as possible or to limit the size of
187 the tree.
188
189 The diving goes into the tree doing depth-first search until one of the following happens:
190 \li A prescibed \c maxDiveBacktrack_ number of backtracking are performed.
191 \li The guessed objective value of the current node is worst than the best incumbent.
192 \li The depth of the dive is bigger than \c maxDiveDepth_
193
194 In the first case all the nodes are put on the tree and the next node on top will be the top of the heap, in the
195 two latter case we just put the node on the tree and backtrack in the list of depth-first search nodes.
196
197 \bug This won't work in a non-convex problem where objective does not decrease down branches.
198 */
199 class CbcDfsDiver :public CbcTree
200 {
201 public:
202 enum ComparisonModes{
203 Enlarge/** At the very beginning we might want to enlarge the tree just a bit*/,
204 FindSolutions,
205 CloseBound,
206 LimitTreeSize};
207 /// Default constructor.
208 CbcDfsDiver();
209
210 ///Copy constructor.
211 CbcDfsDiver(const CbcDfsDiver &rhs);
212
213 /// Assignment operator.
214 CbcDfsDiver & operator=(const CbcDfsDiver &rhs);
215
216 /// Destructor.
217 virtual ~CbcDfsDiver();
218
219 ///Virtual copy constructor.
220 virtual CbcTree * clone() const;
221
222 /** \name Heap access and maintenance methods.*/
223 /**@{*/
224 ///Return top node (next node to process.*/
225 virtual CbcNode * top() const;
226
227 /// Add node to the heap.
228 virtual void push(CbcNode * x);
229 /// Remove the top node of the heap.
230 virtual void pop();
231 /// Remove the best node from the heap and return it
232 virtual CbcNode * bestNode(double cutoff);
233 /** @} */
234
235 /// \name vector methods
236 /** @{ */
237 /** Test if empty. */
238 virtual bool empty();
239 /** Give size of the tree.*/
240 virtual int size()
241 {
242 return static_cast<int>(nodes_.size()) + diveListSize_;
243 }
244 /** @} */
245
246 /*! \brief Prune the tree using an objective function cutoff
247
248 This routine removes all nodes with objective worst than the
249 specified cutoff value.
250 It also sets bestPossibleObjective to best
251 of all on tree before deleting.
252 \bug This won't work in a non-convex problem where objective does not decrease down branches.
253 */
254 virtual void cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective);
255
256 /// Get best possible objective function in the tree
257 virtual double getBestPossibleObjective();
258
259 //#ifdef COIN_HAS_BONMIN
260 ///Register the options of the method.
261 static void registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions);
262
263 /// Initialize the method (get options)
264 void initialize(BabSetupBase &b);
265 //#endif
266 ///Don't know what this is yet?
267 virtual void endSearch()
268 {}
269
270 /** Changes the mode of comparison of the tree for "safety reasons" if the mode really changes we always
271 finish the current dive and put all the node back onto the heap.*/
272 void setComparisonMode(ComparisonModes newMode);
273 /** get the mode of comparison of the tree.*/
274 ComparisonModes getComparisonMode()
275 {
276 return mode_;
277 }
278 protected:
279 /**Flag to say that we are currently cleaning the tree and should work only
280 on the heap.*/
281 int treeCleaning_;
282 /** List of the nodes in the current dive.*/
283 std::list<CbcNode *> dive_;
284 /** Record dive list size for constant time access.*/
285 int diveListSize_;
286 /** Depth of the node from which diving was started (we call this node the diving board).*/
287 int divingBoardDepth_;
288 /** Last reported cutoff.*/
289 double cutoff_;
290 /** number of backtracks done in current dive.*/
291 int nBacktracks_;
292 /** \name Parameters of the method.*/
293 /** @{ */
294 /** Maximum depth until which we'll do a bredth-first-search.*/
295 int maxDepthBFS_;
296 /** Maximum number of backtrack in one dive.*/
297 int maxDiveBacktracks_;
298 /** Maximum depth to go from divingBoard.*/
299 int maxDiveDepth_;
300 /** Current mode of the diving strategy.*/
301 ComparisonModes mode_;
302 /** @} */
303 private:
304 /** Pushes onto heap all the nodes with objective value > cutoff. */
305 void pushDiveOntoHeap(double cutoff);
306
307 };
308
309 class DiverCompare : public CbcCompareBase
310 {
311 public:
312 // Default Constructor
313 DiverCompare ():
314 CbcCompareBase(),
315 diver_(NULL),
316 numberSolToStopDive_(5),
317 numberNodesToLimitTreeSize_(1000000),
318 comparisonDive_(NULL),
319 comparisonBound_(NULL)
320 {}
321
322
323 virtual ~DiverCompare()
324 {
325 delete comparisonDive_;
326 delete comparisonBound_;
327 }
328
329 // Copy constructor
330 DiverCompare ( const DiverCompare & rhs):
331 CbcCompareBase(rhs),
332 diver_(rhs.diver_),
333 numberSolToStopDive_(rhs.numberSolToStopDive_),
334 numberNodesToLimitTreeSize_(rhs.numberNodesToLimitTreeSize_),
335 comparisonDive_(rhs.comparisonDive_->clone()),
336 comparisonBound_(rhs.comparisonBound_->clone())
337 {}
338
339 // Assignment operator
340 DiverCompare & operator=( const DiverCompare& rhs)
341 {
342 if (this != &rhs) {
343 CbcCompareBase::operator=(rhs);
344 diver_ = rhs.diver_;
345 numberSolToStopDive_ = rhs.numberSolToStopDive_;
346 numberNodesToLimitTreeSize_ = rhs.numberNodesToLimitTreeSize_;
347 delete comparisonDive_;
348 delete comparisonBound_;
349 comparisonDive_ = NULL;
350 comparisonBound_ = NULL;
351 if (rhs.comparisonDive_) comparisonDive_ = rhs.comparisonDive_->clone();
352 if (rhs.comparisonBound_) comparisonBound_ = rhs.comparisonBound_->clone();
353 }
354 return *this;
355 }
356
357 /// Clone
358 virtual CbcCompareBase * clone() const
359 {
360 return new DiverCompare(*this);
361 }
362
363 /// This is test function
364 virtual bool test (CbcNode * x, CbcNode * y);
365
366 /// Called after each new solution
367 virtual bool newSolution(CbcModel * model);
368
369 /// Called after each new solution
370 virtual bool newSolution(CbcModel * model,
371 double objectiveAtContinuous,
372 int numberInfeasibilitiesAtContinuous);
373
374 /** Called 1000 nodes.
375 * Return true if want tree re-sorted.*/
376 virtual bool every1000Nodes(CbcModel * model,int numberNodes);
377
378 /** Set the dfs diver to use.*/
379 void setDiver(CbcDfsDiver * diver)
380 {
381 diver_ = diver;
382 }
383
384 /** Set numberSolToStopDive_ */
385 void setNumberSolToStopDive(int val)
386 {
387 numberSolToStopDive_ = val;
388 }
389
390 /** Set numberNodesToLimitTreeSize_.*/
391 void setNumberNodesToLimitTreeSize(int val)
392 {
393 numberNodesToLimitTreeSize_ = val;
394 }
395
396 /** Set comparison method when diving.*/
397 void setComparisonDive(const CbcCompareBase & val)
398 {
399 comparisonDive_ = val.clone();
400 }
401 /** Set comparison method when closing bound.*/
402 void setComparisonBound(const CbcCompareBase & val)
403 {
404 comparisonBound_ = val.clone();
405 }
406 private:
407 /** Pointer to the CbcDfsDiver handling the tree.*/
408 CbcDfsDiver * diver_;
409 /** Number of solution before we command diver_ to stop diving.*/
410 int numberSolToStopDive_;
411 /** Number of nodes before we command diver_ to limit the tree size.*/
412 int numberNodesToLimitTreeSize_;
413 /** Comparison method used in diving mode*/
414 CbcCompareBase * comparisonDive_;
415 /** Comparison method used bound mode*/
416 CbcCompareBase * comparisonBound_;
417 /** Comparison method used when limit tree size.*/
418 CbcCompareDepth comparisonDepth_;
419 };
420
421 }/* Ends bonmin namespace.*/
422
423 #endif
424