File: CbcHeuristic.hpp

package info (click to toggle)
coinor-cbc 2.9.9+repack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • ctags: 5,787
  • sloc: cpp: 104,337; sh: 8,921; xml: 2,950; makefile: 520; ansic: 491; awk: 197
file content (682 lines) | stat: -rw-r--r-- 23,190 bytes parent folder | download | duplicates (3)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/* $Id: CbcHeuristic.hpp 2094 2014-11-18 11:15:36Z forrest $ */
// Copyright (C) 2002, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CbcHeuristic_H
#define CbcHeuristic_H

#include <string>
#include <vector>
#include "CoinPackedMatrix.hpp"
#include "OsiCuts.hpp"
#include "CoinHelperFunctions.hpp"
#include "OsiBranchingObject.hpp"

class OsiSolverInterface;

class CbcModel;

//#############################################################################

class CbcHeuristicNodeList;
class CbcBranchingObject;

/** A class describing the branching decisions that were made to get
    to the node where a heuristic was invoked from */

class CbcHeuristicNode {
private:
    void gutsOfConstructor(CbcModel& model);
    CbcHeuristicNode();
    CbcHeuristicNode& operator=(const CbcHeuristicNode&);
private:
    /// The number of branching decisions made
    int numObjects_;
    /** The indices of the branching objects. Note: an index may be
        listed multiple times. E.g., a general integer variable that has
        been branched on multiple times. */
    CbcBranchingObject** brObj_;
public:
    CbcHeuristicNode(CbcModel& model);

    CbcHeuristicNode(const CbcHeuristicNode& rhs);
    ~CbcHeuristicNode();
    double distance(const CbcHeuristicNode* node) const;
    double minDistance(const CbcHeuristicNodeList& nodeList) const;
    bool minDistanceIsSmall(const CbcHeuristicNodeList& nodeList,
                            const double threshold) const;
    double avgDistance(const CbcHeuristicNodeList& nodeList) const;
};

class CbcHeuristicNodeList {
private:
    void gutsOfDelete();
    void gutsOfCopy(const CbcHeuristicNodeList& rhs);
private:
    std::vector<CbcHeuristicNode*> nodes_;
public:
    CbcHeuristicNodeList() {}
    CbcHeuristicNodeList(const CbcHeuristicNodeList& rhs);
    CbcHeuristicNodeList& operator=(const CbcHeuristicNodeList& rhs);
    ~CbcHeuristicNodeList();

    void append(CbcHeuristicNode*& node);
    void append(const CbcHeuristicNodeList& nodes);
    inline const CbcHeuristicNode* node(int i) const {
        return nodes_[i];
    }
    inline int size() const {
        return static_cast<int>(nodes_.size());
    }
};

//#############################################################################
/** Heuristic base class */

class CbcHeuristic {
private:
    void gutsOfDelete() {}
    void gutsOfCopy(const CbcHeuristic & rhs);

public:
    // Default Constructor
    CbcHeuristic ();

    // Constructor with model - assumed before cuts
    CbcHeuristic (CbcModel & model);

    // Copy constructor
    CbcHeuristic ( const CbcHeuristic &);

    virtual ~CbcHeuristic();

    /// Clone
    virtual CbcHeuristic * clone() const = 0;

    /// Assignment operator
    CbcHeuristic & operator=(const CbcHeuristic& rhs);

    /// update model (This is needed if cliques update matrix etc)
    virtual void setModel(CbcModel * model);

    /// Resets stuff if model changes
    virtual void resetModel(CbcModel * model) = 0;

    /** returns 0 if no solution, 1 if valid solution
        with better objective value than one passed in
        Sets solution values if good, sets objective value
        This is called after cuts have been added - so can not add cuts
    */
    virtual int solution(double & objectiveValue,
                         double * newSolution) = 0;

    /** returns 0 if no solution, 1 if valid solution, -1 if just
        returning an estimate of best possible solution
        with better objective value than one passed in
        Sets solution values if good, sets objective value (only if nonzero code)
        This is called at same time as cut generators - so can add cuts
        Default is do nothing
    */
    virtual int solution2(double & /*objectiveValue*/,
                          double * /*newSolution*/,
                          OsiCuts & /*cs*/) {
        return 0;
    }

    /// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
    virtual void validate() {}

    /** Sets "when" flag - 0 off, 1 at root, 2 other than root, 3 always.
        If 10 added then don't worry if validate says there are funny objects
        as user knows it will be fine
    */
    inline void setWhen(int value) {
        when_ = value;
    }
    /// Gets "when" flag - 0 off, 1 at root, 2 other than root, 3 always
    inline int when() const {
        return when_;
    }

    /// Sets number of nodes in subtree (default 200)
    inline void setNumberNodes(int value) {
        numberNodes_ = value;
    }
    /// Gets number of nodes in a subtree (default 200)
    inline int numberNodes() const {
        return numberNodes_;
    }
    /** Switches (does not apply equally to all heuristics)
        1 bit - stop once allowable gap on objective reached
        2 bit - always do given number of passes
        4 bit - weaken cutoff by 5% every 50 passes?
        8 bit - if has cutoff and suminf bobbling for 20 passes then
                first try halving distance to best possible then
                try keep halving distance to known cutoff
        16 bit - needs new solution to run
        1024 bit - stop all heuristics on max time
    */
    inline void setSwitches(int value) {
        switches_ = value;
    }
    /** Switches (does not apply equally to all heuristics)
        1 bit - stop once allowable gap on objective reached
        2 bit - always do given number of passes
        4 bit - weaken cutoff by 5% every 50 passes?
        8 bit - if has cutoff and suminf bobbling for 20 passes then
                first try halving distance to best possible then
                try keep halving distance to known cutoff
        16 bit - needs new solution to run
        1024 bit - stop all heuristics on max time
	65536 bit and above used for temporary communication
    */
    inline int switches() const {
        return switches_;
    }
    /// Whether to exit at once on gap
    bool exitNow(double bestObjective) const;
    /// Sets feasibility pump options (-1 is off)
    inline void setFeasibilityPumpOptions(int value) {
        feasibilityPumpOptions_ = value;
    }
    /// Gets feasibility pump options (-1 is off)
    inline int feasibilityPumpOptions() const {
        return feasibilityPumpOptions_;
    }
    /// Just set model - do not do anything else
    inline void setModelOnly(CbcModel * model) {
        model_ = model;
    }


    /// Sets fraction of new(rows+columns)/old(rows+columns) before doing small branch and bound (default 1.0)
    inline void setFractionSmall(double value) {
        fractionSmall_ = value;
    }
    /// Gets fraction of new(rows+columns)/old(rows+columns) before doing small branch and bound (default 1.0)
    inline double fractionSmall() const {
        return fractionSmall_;
    }
    /// Get how many solutions the heuristic thought it got
    inline int numberSolutionsFound() const {
        return numberSolutionsFound_;
    }
    /// Increment how many solutions the heuristic thought it got
    inline void incrementNumberSolutionsFound() {
        numberSolutionsFound_++;
    }

    /** Do mini branch and bound - return
        0 not finished - no solution
        1 not finished - solution
        2 finished - no solution
        3 finished - solution
        (could add global cut if finished)
        -1 returned on size
        -2 time or user event
    */
    int smallBranchAndBound(OsiSolverInterface * solver, int numberNodes,
                            double * newSolution, double & newSolutionValue,
                            double cutoff , std::string name) const;
    /// Create C++ lines to get to current state
    virtual void generateCpp( FILE * ) {}
    /// Create C++ lines to get to current state - does work for base class
    void generateCpp( FILE * fp, const char * heuristic) ;
    /// Returns true if can deal with "odd" problems e.g. sos type 2
    virtual bool canDealWithOdd() const {
        return false;
    }
    /// return name of heuristic
    inline const char *heuristicName() const {
        return heuristicName_.c_str();
    }
    /// set name of heuristic
    inline void setHeuristicName(const char *name) {
        heuristicName_ = name;
    }
    /// Set random number generator seed
    void setSeed(int value);
    /// Get random number generator seed
    int getSeed() const;
    /// Sets decay factor (for howOften) on failure
    inline void setDecayFactor(double value) {
        decayFactor_ = value;
    }
    /// Set input solution
    void setInputSolution(const double * solution, double objValue);
    /* Runs if bit set
        0 - before cuts at root node (or from doHeuristics)
        1 - during cuts at root
        2 - after root node cuts
        3 - after cuts at other nodes
        4 - during cuts at other nodes
            8 added if previous heuristic in loop found solution
     */
    inline void setWhereFrom(int value) {
        whereFrom_ = value;
    }
    inline int whereFrom() const {
        return whereFrom_;
    }
    /** Upto this depth we call the tree shallow and the heuristic can be called
        multiple times. That is, the test whether the current node is far from
        the others where the jeuristic was invoked will not be done, only the
        frequency will be tested. After that depth the heuristic will can be
        invoked only once per node, right before branching. That's when it'll be
        tested whether the heur should run at all. */
    inline void setShallowDepth(int value) {
        shallowDepth_ = value;
    }
    /** How often to invoke the heuristics in the shallow part of the tree */
    inline void setHowOftenShallow(int value) {
        howOftenShallow_ = value;
    }
    /** How "far" should this node be from every other where the heuristic was
        run in order to allow the heuristic to run in this node, too. Currently
        this is tested, but we may switch to avgDistanceToRun_ in the future. */
    inline void setMinDistanceToRun(int value) {
        minDistanceToRun_ = value;
    }

    /** Check whether the heuristic should run at all
        0 - before cuts at root node (or from doHeuristics)
        1 - during cuts at root
        2 - after root node cuts
        3 - after cuts at other nodes
        4 - during cuts at other nodes
            8 added if previous heuristic in loop found solution
    */
    virtual bool shouldHeurRun(int whereFrom);
    /** Check whether the heuristic should run this time */
    bool shouldHeurRun_randomChoice();
    void debugNodes();
    void printDistanceToNodes();
    /// how many times the heuristic has actually run
    inline int numRuns() const {
        return numRuns_;
    }

    /// How many times the heuristic could run
    inline int numCouldRun() const {
        return numCouldRun_;
    }
    /*! \brief Clone, but ...

      If type is
	- 0 clone the solver for the model,
	- 1 clone the continuous solver for the model
        - Add 2 to say without integer variables which are at low priority
        - Add 4 to say quite likely infeasible so give up easily (clp only).
    */
    OsiSolverInterface * cloneBut(int type);
protected:

    /// Model
    CbcModel * model_;
    /// When flag - 0 off, 1 at root, 2 other than root, 3 always
    int when_;
    /// Number of nodes in any sub tree
    int numberNodes_;
    /** Feasibility pump options , -1 is off
	>=0 for feasibility pump itself
        -2 quick proximity search
        -3 longer proximity search
    */
    int feasibilityPumpOptions_;
    /// Fraction of new(rows+columns)/old(rows+columns) before doing small branch and bound
    mutable double fractionSmall_;
    /// Thread specific random number generator
    CoinThreadRandom randomNumberGenerator_;
    /// Name for printing
    std::string heuristicName_;

    /// How often to do (code can change)
    mutable int howOften_;
    /// How much to increase how often
    double decayFactor_;
    /** Switches (does not apply equally to all heuristics)
        1 bit - stop once allowable gap on objective reached
        2 bit - always do given number of passes
        4 bit - weaken cutoff by 5% every 50 passes?
        8 bit - if has cutoff and suminf bobbling for 20 passes then
                first try halving distance to best possible then
                try keep halving distance to known cutoff
        16 bit - needs new solution to run
        1024 bit - stop all heuristics on max time
    */
    mutable int switches_;
    /* Runs if bit set
        0 - before cuts at root node (or from doHeuristics)
        1 - during cuts at root
        2 - after root node cuts
        3 - after cuts at other nodes
        4 - during cuts at other nodes
            8 added if previous heuristic in loop found solution
     */
    int whereFrom_;
    /** Upto this depth we call the tree shallow and the heuristic can be called
        multiple times. That is, the test whether the current node is far from
        the others where the jeuristic was invoked will not be done, only the
        frequency will be tested. After that depth the heuristic will can be
        invoked only once per node, right before branching. That's when it'll be
        tested whether the heur should run at all. */
    int shallowDepth_;
    /** How often to invoke the heuristics in the shallow part of the tree */
    int howOftenShallow_;
    /** How many invocations happened within the same node when in a shallow
        part of the tree. */
    int numInvocationsInShallow_;
    /** How many invocations happened when in the deep part of the tree. For
        every node we count only one invocation. */
    int numInvocationsInDeep_;
    /** After how many deep invocations was the heuristic run last time */
    int lastRunDeep_;
    /// how many times the heuristic has actually run
    int numRuns_;
    /** How "far" should this node be from every other where the heuristic was
        run in order to allow the heuristic to run in this node, too. Currently
        this is tested, but we may switch to avgDistanceToRun_ in the future. */
    int minDistanceToRun_;

    /// The description of the nodes where this heuristic has been applied
    CbcHeuristicNodeList runNodes_;

    /// How many times the heuristic could run
    int numCouldRun_;

    /// How many solutions the heuristic thought it got
    int numberSolutionsFound_;

    /// How many nodes the heuristic did this go
    mutable int numberNodesDone_;

    // Input solution - so can be used as seed
    double * inputSolution_;


#ifdef JJF_ZERO
    /// Lower bounds of last node where the heuristic found a solution
    double * lowerBoundLastNode_;
    /// Upper bounds of last node where the heuristic found a solution
    double * upperBoundLastNode_;
#endif
};
/** Rounding class
 */

class CbcRounding : public CbcHeuristic {
public:

    // Default Constructor
    CbcRounding ();

    // Constructor with model - assumed before cuts
    CbcRounding (CbcModel & model);

    // Copy constructor
    CbcRounding ( const CbcRounding &);

    // Destructor
    ~CbcRounding ();

    /// Assignment operator
    CbcRounding & operator=(const CbcRounding& rhs);

    /// Clone
    virtual CbcHeuristic * clone() const;
    /// Create C++ lines to get to current state
    virtual void generateCpp( FILE * fp) ;

    /// Resets stuff if model changes
    virtual void resetModel(CbcModel * model);

    /// update model (This is needed if cliques update matrix etc)
    virtual void setModel(CbcModel * model);

    using CbcHeuristic::solution ;
    /** returns 0 if no solution, 1 if valid solution
        with better objective value than one passed in
        Sets solution values if good, sets objective value (only if good)
        This is called after cuts have been added - so can not add cuts
    */
    virtual int solution(double & objectiveValue,
                         double * newSolution);
    /** returns 0 if no solution, 1 if valid solution
        with better objective value than one passed in
        Sets solution values if good, sets objective value (only if good)
        This is called after cuts have been added - so can not add cuts
        Use solutionValue rather than solvers one
    */
    virtual int solution(double & objectiveValue,
                         double * newSolution,
                         double solutionValue);
    /// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
    virtual void validate();


    /// Set seed
    void setSeed(int value) {
        seed_ = value;
    }
    /** Check whether the heuristic should run at all
        0 - before cuts at root node (or from doHeuristics)
        1 - during cuts at root
        2 - after root node cuts
        3 - after cuts at other nodes
        4 - during cuts at other nodes
            8 added if previous heuristic in loop found solution
    */
    virtual bool shouldHeurRun(int whereFrom);

protected:
    // Data

    // Original matrix by column
    CoinPackedMatrix matrix_;

    // Original matrix by
    CoinPackedMatrix matrixByRow_;

    // Down locks
    unsigned short * down_;

    // Up locks
    unsigned short * up_;

    // Equality locks
    unsigned short * equal_;

    // Seed for random stuff
    int seed_;
};

/** Partial solution class
    If user knows a partial solution this tries to get an integer solution
    it uses hotstart information
 */

class CbcHeuristicPartial : public CbcHeuristic {
public:

    // Default Constructor
    CbcHeuristicPartial ();

    /** Constructor with model - assumed before cuts
        Fixes all variables with priority <= given
        and does given number of nodes
    */
    CbcHeuristicPartial (CbcModel & model, int fixPriority = 10000, int numberNodes = 200);

    // Copy constructor
    CbcHeuristicPartial ( const CbcHeuristicPartial &);

    // Destructor
    ~CbcHeuristicPartial ();

    /// Assignment operator
    CbcHeuristicPartial & operator=(const CbcHeuristicPartial& rhs);

    /// Clone
    virtual CbcHeuristic * clone() const;
    /// Create C++ lines to get to current state
    virtual void generateCpp( FILE * fp) ;

    /// Resets stuff if model changes
    virtual void resetModel(CbcModel * model);

    /// update model (This is needed if cliques update matrix etc)
    virtual void setModel(CbcModel * model);

    using CbcHeuristic::solution ;
    /** returns 0 if no solution, 1 if valid solution
        with better objective value than one passed in
        Sets solution values if good, sets objective value (only if good)
        This is called after cuts have been added - so can not add cuts
    */
    virtual int solution(double & objectiveValue,
                         double * newSolution);
    /// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
    virtual void validate();


    /// Set priority level
    void setFixPriority(int value) {
        fixPriority_ = value;
    }

    /** Check whether the heuristic should run at all */
    virtual bool shouldHeurRun(int whereFrom);

protected:
    // Data

    // All variables with abs priority <= this will be fixed
    int fixPriority_;
};

/** heuristic - just picks up any good solution
    found by solver - see OsiBabSolver
 */

class CbcSerendipity : public CbcHeuristic {
public:

    // Default Constructor
    CbcSerendipity ();

    /* Constructor with model
    */
    CbcSerendipity (CbcModel & model);

    // Copy constructor
    CbcSerendipity ( const CbcSerendipity &);

    // Destructor
    ~CbcSerendipity ();

    /// Assignment operator
    CbcSerendipity & operator=(const CbcSerendipity& rhs);

    /// Clone
    virtual CbcHeuristic * clone() const;
    /// Create C++ lines to get to current state
    virtual void generateCpp( FILE * fp) ;

    /// update model
    virtual void setModel(CbcModel * model);

    using CbcHeuristic::solution ;
    /** returns 0 if no solution, 1 if valid solution.
        Sets solution values if good, sets objective value (only if good)
        We leave all variables which are at one at this node of the
        tree to that value and will
        initially set all others to zero.  We then sort all variables in order of their cost
        divided by the number of entries in rows which are not yet covered.  We randomize that
        value a bit so that ties will be broken in different ways on different runs of the heuristic.
        We then choose the best one and set it to one and repeat the exercise.

    */
    virtual int solution(double & objectiveValue,
                         double * newSolution);
    /// Resets stuff if model changes
    virtual void resetModel(CbcModel * model);

protected:
};

/** Just One class - this chooses one at random
 */

class CbcHeuristicJustOne : public CbcHeuristic {
public:

    // Default Constructor
    CbcHeuristicJustOne ();

    // Constructor with model - assumed before cuts
    CbcHeuristicJustOne (CbcModel & model);

    // Copy constructor
    CbcHeuristicJustOne ( const CbcHeuristicJustOne &);

    // Destructor
    ~CbcHeuristicJustOne ();

    /// Clone
    virtual CbcHeuristicJustOne * clone() const;

    /// Assignment operator
    CbcHeuristicJustOne & operator=(const CbcHeuristicJustOne& rhs);

    /// Create C++ lines to get to current state
    virtual void generateCpp( FILE * fp) ;

    /** returns 0 if no solution, 1 if valid solution
        with better objective value than one passed in
        Sets solution values if good, sets objective value (only if good)
        This is called after cuts have been added - so can not add cuts
        This does Fractional Diving
    */
    virtual int solution(double & objectiveValue,
                         double * newSolution);
    /// Resets stuff if model changes
    virtual void resetModel(CbcModel * model);

    /// update model (This is needed if cliques update matrix etc)
    virtual void setModel(CbcModel * model);
    /// Selects the next variable to branch on
    /** Returns true if all the fractional variables can be trivially
        rounded. Returns false, if there is at least one fractional variable
        that is not trivially roundable. In this case, the bestColumn
        returned will not be trivially roundable.
        This is dummy as never called
    */
    virtual bool selectVariableToBranch(OsiSolverInterface* /*solver*/,
                                        const double* /*newSolution*/,
                                        int& /*bestColumn*/,
                                        int& /*bestRound*/) {
        return true;
    }
    /// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
    virtual void validate();
    /// Adds an heuristic with probability
    void addHeuristic(const CbcHeuristic * heuristic, double probability);
    /// Normalize probabilities
    void normalizeProbabilities();
protected:
    // Data

    // Probability of running a heuristic
    double * probabilities_;

    // Heuristics
    CbcHeuristic ** heuristic_;

    // Number of heuristics
    int numberHeuristics_;

};

#endif