summaryrefslogtreecommitdiff
path: root/newstructure/thirdparty/linux/include/coin/BonDiver.hpp
blob: 20a9fa64378237f93f5530daf9fa8bbb1f8ef7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// (C) Copyright International Business Machines Corporation 2007
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors :
// Pierre Bonami, International Business Machines Corporation
//
// Date : 09/01/2007

#ifndef BonDiver_H
#define BonDiver_H

#include "BonminConfig.h"
#include "CbcCompareBase.hpp"
#include "CbcTree.hpp"
#include "IpRegOptions.hpp"
#include "IpOptionsList.hpp"
#include "CbcCompareActual.hpp"
#include "BonRegisteredOptions.hpp"
#include <list>
namespace Bonmin
{
  class BabSetupBase;
  /** Class to do diving in the tree. Principle is that branch-and-bound follows current branch of the tree untill it
      hits the bottom at which point it goes to the best candidate (according to CbcCompare) on the heap.*/
  class CbcDiver : public CbcTree
  {
  public:
    /// Default constructor.
    CbcDiver();

    ///Copy constructor.
    CbcDiver(const CbcDiver &rhs);

    /// Assignment operator.
    CbcDiver & operator=(const CbcDiver &rhs);

    /// Destructor.
    virtual ~CbcDiver();

    ///Virtual copy constructor.
    virtual CbcTree * clone() const;

    /** \name Heap access and maintenance methods.*/
    /**@{*/
    ///Return top node (next node to process.*/
    virtual CbcNode * top() const;

    /// Add node to the heap.
    virtual void push(CbcNode * x);
    /// Remove the top node of the heap.
    virtual void pop();
    /// Remove the best node from the heap and return it
    virtual CbcNode * bestNode(double cutoff);
    /** @} */

    /// \name vector methods
    /** @{ */
    /** Test if empty. */
    virtual bool empty();
    /** Give size of the tree.*/
    virtual int size()
    {
      return (static_cast<int>(nodes_.size()) + (nextOnBranch_ != NULL) );
    }
    /** @} */

    /*! \brief Prune the tree using an objective function cutoff
      
    This routine removes all nodes with objective worst than the
    specified cutoff value.
    It also sets bestPossibleObjective to best
    of all on tree before deleting.
    */
    virtual void cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective);

    /// Get best possible objective function in the tree
    virtual double getBestPossibleObjective();


    ///Don't know what this is yet?
    virtual void endSearch()
    {
      nextOnBranch_ = NULL;
    }

    ///Register the options of the method.
    static void registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions);

    /// Initialize the method (get options)
    void initialize(BabSetupBase &b);

  private:
    /** Say if we are cleaning the tree (then only call CbcTree functions).*/
    bool treeCleaning_;
    /** Noext node on the branch.*/
    CbcNode * nextOnBranch_;
    /** Flag indicating if we want to stop diving based on the guessed
    objective value and the cutoff value */
    bool stop_diving_on_cutoff_;
  };


  /** Class to do probed diving in the tree.
    * Principle is that branch-and-bound follows current branch of the tree by exploring the two children at each level
    * and continuing the dive on the best one of the two. Untill it 
    *  hits the bottom at which point it goes to the best candidate (according to CbcCompare) on the heap.*/
  class CbcProbedDiver : public CbcTree
  {
  public:
    /// Default constructor.
    CbcProbedDiver();

    ///Copy constructor.
    CbcProbedDiver(const CbcProbedDiver &rhs);

    /// Assignment operator.
    CbcProbedDiver & operator=(const CbcProbedDiver &rhs);

    /// Destructor.
    virtual ~CbcProbedDiver();

    ///Virtual copy constructor.
    virtual CbcTree * clone() const;

    /** \name Heap access and maintenance methods.*/
    /**@{*/
    ///Return top node (next node to process.*/
    virtual CbcNode * top() const;

    /// Add node to the heap.
    virtual void push(CbcNode * x);
    /// Remove the top node of the heap.
    virtual void pop();
    /// Remove the best node from the heap and return it
    virtual CbcNode * bestNode(double cutoff);
    /** @} */

    /// \name vector methods
    /** @{ */
    /** Test if empty. */
    virtual bool empty();
    /** Give size of the tree.*/
    virtual int size()
    {
      return (static_cast<int>(nodes_.size()) + (nextOnBranch_ != NULL) + (candidateChild_ != NULL) );
    }
    /** @} */

    /*! \brief Prune the tree using an objective function cutoff
      
    This routine removes all nodes with objective worst than the
    specified cutoff value.
    It also sets bestPossibleObjective to best
    of all on tree before deleting.
    */
    virtual void cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective);

    /// Get best possible objective function in the tree
    virtual double getBestPossibleObjective();


    ///Don't know what this is yet?
    virtual void endSearch()
    {
      nextOnBranch_ = NULL;
    }

    /// Initialize the method (get options)
    void initialize(BabSetupBase &b);

  private:
    /** Say if we are cleaning the tree (then only call CbcTree functions).*/
    bool treeCleaning_;
    /** Next node on the branch.*/
    CbcNode * nextOnBranch_;
    /** Candidate child explored.*/
    CbcNode * candidateChild_;
    /** Flag indicating if we want to stop diving based on the guessed
    objective value and the cutoff value */
    bool stop_diving_on_cutoff_;
  };


  /** A more elaborate diving class. First there are several modes which can be commanded by the Comparison class below.
     In particular can command to dive to find solutions, to try to close the bound as possible or to limit the size of
     the tree.

     The diving goes into the tree doing depth-first search until one of the following happens:
     \li A prescibed \c maxDiveBacktrack_ number of backtracking are performed.
     \li The guessed objective value of the current node is worst than the best incumbent.
     \li The depth of the dive is bigger than \c maxDiveDepth_

     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
     two latter case we just put the node on the tree and backtrack in the list of depth-first search nodes.

     \bug This won't work in a non-convex problem where objective does not decrease down branches.
   */
  class CbcDfsDiver :public CbcTree
  {
  public:
    enum ComparisonModes{
      Enlarge/** At the very beginning we might want to enlarge the tree just a bit*/,
      FindSolutions,
      CloseBound,
      LimitTreeSize};
    /// Default constructor.
    CbcDfsDiver();

    ///Copy constructor.
    CbcDfsDiver(const CbcDfsDiver &rhs);

    /// Assignment operator.
    CbcDfsDiver & operator=(const CbcDfsDiver &rhs);

    /// Destructor.
    virtual ~CbcDfsDiver();

    ///Virtual copy constructor.
    virtual CbcTree * clone() const;

    /** \name Heap access and maintenance methods.*/
    /**@{*/
    ///Return top node (next node to process.*/
    virtual CbcNode * top() const;

    /// Add node to the heap.
    virtual void push(CbcNode * x);
    /// Remove the top node of the heap.
    virtual void pop();
    /// Remove the best node from the heap and return it
    virtual CbcNode * bestNode(double cutoff);
    /** @} */

    /// \name vector methods
    /** @{ */
    /** Test if empty. */
    virtual bool empty();
    /** Give size of the tree.*/
    virtual int size()
    {
      return static_cast<int>(nodes_.size()) + diveListSize_;
    }
    /** @} */

    /*! \brief Prune the tree using an objective function cutoff
      
    This routine removes all nodes with objective worst than the
    specified cutoff value.
    It also sets bestPossibleObjective to best
    of all on tree before deleting.
     \bug This won't work in a non-convex problem where objective does not decrease down branches.
    */
    virtual void cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective);

    /// Get best possible objective function in the tree
    virtual double getBestPossibleObjective();

//#ifdef COIN_HAS_BONMIN
    ///Register the options of the method.
    static void registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions);

    /// Initialize the method (get options)
    void initialize(BabSetupBase &b);
//#endif
    ///Don't know what this is yet?
    virtual void endSearch()
    {}

    /** Changes the mode of comparison of the tree for "safety reasons" if the mode really changes we always
        finish the current dive and put all the node back onto the heap.*/
    void setComparisonMode(ComparisonModes newMode);
    /** get the mode of comparison of the tree.*/
    ComparisonModes getComparisonMode()
    {
      return mode_;
    }
  protected:
    /**Flag to say that we are currently cleaning the tree and should work only
       on the heap.*/
    int treeCleaning_;
    /** List of the nodes in the current dive.*/
    std::list<CbcNode *> dive_;
    /** Record dive list size for constant time access.*/
    int diveListSize_;
    /** Depth of the node from which diving was started (we call this node the diving board).*/
    int divingBoardDepth_;
    /** Last reported cutoff.*/
    double cutoff_;
    /** number of backtracks done in current dive.*/
    int nBacktracks_;
    /** \name Parameters of the method.*/
    /** @{ */
    /** Maximum depth until which we'll do a bredth-first-search.*/
    int maxDepthBFS_;
    /** Maximum number of backtrack in one dive.*/
    int maxDiveBacktracks_;
    /** Maximum depth to go from divingBoard.*/
    int maxDiveDepth_;
    /** Current mode of the diving strategy.*/
    ComparisonModes mode_;
    /** @} */
  private:
    /** Pushes onto heap all the nodes with objective value > cutoff. */
    void pushDiveOntoHeap(double cutoff);

  };

  class DiverCompare : public CbcCompareBase
  {
  public:
    // Default Constructor
    DiverCompare ():
        CbcCompareBase(),
        diver_(NULL),
        numberSolToStopDive_(5),
        numberNodesToLimitTreeSize_(1000000),
        comparisonDive_(NULL),
        comparisonBound_(NULL)
    {}


    virtual ~DiverCompare()
    {
      delete comparisonDive_;
      delete comparisonBound_;
    }

    // Copy constructor
    DiverCompare ( const DiverCompare & rhs):
        CbcCompareBase(rhs),
        diver_(rhs.diver_),
        numberSolToStopDive_(rhs.numberSolToStopDive_),
        numberNodesToLimitTreeSize_(rhs.numberNodesToLimitTreeSize_),
        comparisonDive_(rhs.comparisonDive_->clone()),
        comparisonBound_(rhs.comparisonBound_->clone())
    {}

    // Assignment operator
    DiverCompare & operator=( const DiverCompare& rhs)
    {
      if (this != &rhs) {
        CbcCompareBase::operator=(rhs);
        diver_ = rhs.diver_;
        numberSolToStopDive_ = rhs.numberSolToStopDive_;
        numberNodesToLimitTreeSize_ = rhs.numberNodesToLimitTreeSize_;
        delete comparisonDive_;
        delete comparisonBound_;
        comparisonDive_ = NULL;
        comparisonBound_ = NULL;
        if (rhs.comparisonDive_) comparisonDive_ = rhs.comparisonDive_->clone();
        if (rhs.comparisonBound_) comparisonBound_ = rhs.comparisonBound_->clone();
      }
      return *this;
    }

    /// Clone
    virtual CbcCompareBase * clone() const
    {
      return new DiverCompare(*this);
    }

    /// This is test function
    virtual bool test (CbcNode * x, CbcNode * y);

    ///  Called after each new solution
    virtual bool newSolution(CbcModel * model);

    ///  Called after each new solution
    virtual bool newSolution(CbcModel * model,
        double objectiveAtContinuous,
        int numberInfeasibilitiesAtContinuous);

    /** Called 1000 nodes.
      * Return true if want tree re-sorted.*/
    virtual bool every1000Nodes(CbcModel * model,int numberNodes);

    /** Set the dfs diver to use.*/
    void setDiver(CbcDfsDiver * diver)
    {
      diver_ = diver;
    }

    /** Set numberSolToStopDive_ */
    void setNumberSolToStopDive(int val)
    {
      numberSolToStopDive_ = val;
    }

    /** Set numberNodesToLimitTreeSize_.*/
    void setNumberNodesToLimitTreeSize(int val)
    {
      numberNodesToLimitTreeSize_ = val;
    }

    /** Set comparison method when diving.*/
    void setComparisonDive(const CbcCompareBase & val)
    {
      comparisonDive_ = val.clone();
    }
    /** Set comparison method when closing bound.*/
    void setComparisonBound(const CbcCompareBase & val)
    {
      comparisonBound_ = val.clone();
    }
  private:
    /** Pointer to the CbcDfsDiver handling the tree.*/
    CbcDfsDiver * diver_;
    /** Number of solution before we command diver_ to stop diving.*/
    int numberSolToStopDive_;
    /** Number of nodes before we command diver_ to limit the tree size.*/
    int numberNodesToLimitTreeSize_;
    /** Comparison method used in diving mode*/
    CbcCompareBase * comparisonDive_;
    /** Comparison method used bound mode*/
    CbcCompareBase * comparisonBound_;
    /** Comparison method used when limit tree size.*/
    CbcCompareDepth comparisonDepth_;
  };

}/* Ends bonmin namespace.*/

#endif