source: trunk/Cbc/src/CbcHeuristic.hpp @ 310

Last change on this file since 310 was 264, checked in by forrest, 14 years ago

for nonlinear

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#ifndef CbcHeuristic_H
4#define CbcHeuristic_H
5
6#include <string>
7#include <vector>
8#include "CoinPackedMatrix.hpp"
9#include "OsiCuts.hpp"
10
11class OsiSolverInterface;
12
13class CbcModel;
14
15//#############################################################################
16/** Heuristic base class */
17
18class CbcHeuristic {
19public:
20  // Default Constructor
21  CbcHeuristic ();
22
23  // Constructor with model - assumed before cuts
24  CbcHeuristic (CbcModel & model);
25
26  virtual ~CbcHeuristic();
27
28  /// update model (This is needed if cliques update matrix etc)
29  virtual void setModel(CbcModel * model);
30 
31  /// Clone
32  virtual CbcHeuristic * clone() const=0;
33
34  /// Resets stuff if model changes
35  virtual void resetModel(CbcModel * model)=0;
36
37  /** returns 0 if no solution, 1 if valid solution
38      with better objective value than one passed in
39      Sets solution values if good, sets objective value
40      This is called after cuts have been added - so can not add cuts
41  */
42  virtual int solution(double & objectiveValue,
43                       double * newSolution)=0;
44
45  /** returns 0 if no solution, 1 if valid solution, -1 if just
46      returning an estimate of best possible solution
47      with better objective value than one passed in
48      Sets solution values if good, sets objective value (only if nonzero code)
49      This is called at same time as cut generators - so can add cuts
50      Default is do nothing
51  */
52  virtual int solution(double & objectiveValue,
53                       double * newSolution,
54                       OsiCuts & cs) {return 0;};
55
56  /// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
57  virtual void validate() {};
58
59  /** Sets "when" flag - 0 off, 1 at root, 2 other than root, 3 always.
60      If 10 added then don't worry if validate says there are funny objects
61      as user knows it will be fine
62  */
63  inline void setWhen(int value)
64  { when_=value;};
65  /// Gets "when" flag - 0 off, 1 at root, 2 other than root, 3 always
66  inline int when() const
67  { return when_;};
68
69  /// Do mini branch and bound (return 1 if solution)
70  int smallBranchAndBound(OsiSolverInterface * solver,int numberNodes,
71                          double * newSolution, double & newSolutionValue,
72                          double cutoff , std::string name) const;
73
74protected:
75
76  /// Model
77  CbcModel * model_;
78  /// When flag - 0 off, 1 at root, 2 other than root, 3 always
79  int when_;
80private:
81 
82  /// Illegal Assignment operator
83  CbcHeuristic & operator=(const CbcHeuristic& rhs);
84 
85};
86
87/** Rounding class
88 */
89
90class CbcRounding : public CbcHeuristic {
91public:
92
93  // Default Constructor
94  CbcRounding ();
95
96  // Constructor with model - assumed before cuts
97  CbcRounding (CbcModel & model);
98 
99  // Copy constructor
100  CbcRounding ( const CbcRounding &);
101   
102  // Destructor
103  ~CbcRounding ();
104 
105  /// Clone
106  virtual CbcHeuristic * clone() const;
107
108  /// Resets stuff if model changes
109  virtual void resetModel(CbcModel * model);
110
111  /// update model (This is needed if cliques update matrix etc)
112  virtual void setModel(CbcModel * model);
113 
114  /** returns 0 if no solution, 1 if valid solution
115      with better objective value than one passed in
116      Sets solution values if good, sets objective value (only if good)
117      This is called after cuts have been added - so can not add cuts
118  */
119  virtual int solution(double & objectiveValue,
120                       double * newSolution);
121  /// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
122  virtual void validate();
123
124
125  /// Set seed
126  void setSeed(int value)
127  { seed_ = value;};
128
129protected:
130  // Data
131
132  // Original matrix by column
133  CoinPackedMatrix matrix_;
134
135  // Original matrix by
136  CoinPackedMatrix matrixByRow_;
137
138  // Seed for random stuff
139  int seed_;
140
141private:
142  /// Illegal Assignment operator
143  CbcRounding & operator=(const CbcRounding& rhs);
144};
145
146/** heuristic - just picks up any good solution
147    found by solver - see OsiBabSolver
148 */
149
150class CbcSerendipity : public CbcHeuristic {
151public:
152
153  // Default Constructor
154  CbcSerendipity ();
155
156  /* Constructor with model
157  */
158  CbcSerendipity (CbcModel & model);
159 
160  // Copy constructor
161  CbcSerendipity ( const CbcSerendipity &);
162   
163  // Destructor
164  ~CbcSerendipity ();
165 
166  /// Clone
167  virtual CbcHeuristic * clone() const;
168
169  /// update model
170  virtual void setModel(CbcModel * model);
171 
172  /** returns 0 if no solution, 1 if valid solution.
173      Sets solution values if good, sets objective value (only if good)
174      We leave all variables which are at one at this node of the
175      tree to that value and will
176      initially set all others to zero.  We then sort all variables in order of their cost
177      divided by the number of entries in rows which are not yet covered.  We randomize that
178      value a bit so that ties will be broken in different ways on different runs of the heuristic.
179      We then choose the best one and set it to one and repeat the exercise. 
180
181  */
182  virtual int solution(double & objectiveValue,
183                       double * newSolution);
184  /// Resets stuff if model changes
185  virtual void resetModel(CbcModel * model);
186
187protected:
188private:
189  /// Illegal Assignment operator
190  CbcSerendipity & operator=(const CbcSerendipity& rhs);
191};
192
193#endif
Note: See TracBrowser for help on using the repository browser.