1 | /* $Id: CbcHeuristicGreedy.hpp 1564 2010-12-30 17:45:15Z forrest $ */ |
---|
2 | // Copyright (C) 2005, International Business Machines |
---|
3 | // Corporation and others. All Rights Reserved. |
---|
4 | #ifndef CbcHeuristicGreedy_H |
---|
5 | #define CbcHeuristicGreedy_H |
---|
6 | |
---|
7 | #include "CbcHeuristic.hpp" |
---|
8 | /** Greedy heuristic classes |
---|
9 | */ |
---|
10 | |
---|
11 | class CbcHeuristicGreedyCover : public CbcHeuristic { |
---|
12 | public: |
---|
13 | |
---|
14 | // Default Constructor |
---|
15 | CbcHeuristicGreedyCover (); |
---|
16 | |
---|
17 | /* Constructor with model - assumed before cuts |
---|
18 | Initial version does not do Lps |
---|
19 | */ |
---|
20 | CbcHeuristicGreedyCover (CbcModel & model); |
---|
21 | |
---|
22 | // Copy constructor |
---|
23 | CbcHeuristicGreedyCover ( const CbcHeuristicGreedyCover &); |
---|
24 | |
---|
25 | // Destructor |
---|
26 | ~CbcHeuristicGreedyCover (); |
---|
27 | |
---|
28 | /// Clone |
---|
29 | virtual CbcHeuristic * clone() const; |
---|
30 | /// Assignment operator |
---|
31 | CbcHeuristicGreedyCover & operator=(const CbcHeuristicGreedyCover& rhs); |
---|
32 | /// Create C++ lines to get to current state |
---|
33 | virtual void generateCpp( FILE * fp) ; |
---|
34 | |
---|
35 | /// update model (This is needed if cliques update matrix etc) |
---|
36 | virtual void setModel(CbcModel * model); |
---|
37 | |
---|
38 | using CbcHeuristic::solution ; |
---|
39 | /** returns 0 if no solution, 1 if valid solution. |
---|
40 | Sets solution values if good, sets objective value (only if good) |
---|
41 | We leave all variables which are at one at this node of the |
---|
42 | tree to that value and will |
---|
43 | initially set all others to zero. We then sort all variables in order of their cost |
---|
44 | divided by the number of entries in rows which are not yet covered. We randomize that |
---|
45 | value a bit so that ties will be broken in different ways on different runs of the heuristic. |
---|
46 | We then choose the best one and set it to one and repeat the exercise. |
---|
47 | |
---|
48 | */ |
---|
49 | virtual int solution(double & objectiveValue, |
---|
50 | double * newSolution); |
---|
51 | /// Validate model i.e. sets when_ to 0 if necessary (may be NULL) |
---|
52 | virtual void validate() ; |
---|
53 | /// Resets stuff if model changes |
---|
54 | virtual void resetModel(CbcModel * model); |
---|
55 | /* Algorithm |
---|
56 | 0 - use current upper bounds |
---|
57 | 1 - use original upper bounds |
---|
58 | If 10 added perturb ratios more |
---|
59 | if 100 added round up all >=0.5 |
---|
60 | */ |
---|
61 | inline int algorithm() const { |
---|
62 | return algorithm_; |
---|
63 | } |
---|
64 | inline void setAlgorithm(int value) { |
---|
65 | algorithm_ = value; |
---|
66 | } |
---|
67 | // Only do this many times |
---|
68 | inline int numberTimes() const { |
---|
69 | return numberTimes_; |
---|
70 | } |
---|
71 | inline void setNumberTimes(int value) { |
---|
72 | numberTimes_ = value; |
---|
73 | } |
---|
74 | |
---|
75 | protected: |
---|
76 | /// Guts of constructor from a CbcModel |
---|
77 | void gutsOfConstructor(CbcModel * model); |
---|
78 | // Data |
---|
79 | |
---|
80 | // Original matrix by column |
---|
81 | CoinPackedMatrix matrix_; |
---|
82 | // original number of rows |
---|
83 | int originalNumberRows_; |
---|
84 | /* Algorithm |
---|
85 | 0 - use current upper bounds |
---|
86 | 1 - use original upper bounds |
---|
87 | If 10 added perturb ratios more |
---|
88 | */ |
---|
89 | int algorithm_; |
---|
90 | /// Do this many times |
---|
91 | int numberTimes_; |
---|
92 | |
---|
93 | }; |
---|
94 | |
---|
95 | |
---|
96 | class CbcHeuristicGreedyEquality : public CbcHeuristic { |
---|
97 | public: |
---|
98 | |
---|
99 | // Default Constructor |
---|
100 | CbcHeuristicGreedyEquality (); |
---|
101 | |
---|
102 | /* Constructor with model - assumed before cuts |
---|
103 | Initial version does not do Lps |
---|
104 | */ |
---|
105 | CbcHeuristicGreedyEquality (CbcModel & model); |
---|
106 | |
---|
107 | // Copy constructor |
---|
108 | CbcHeuristicGreedyEquality ( const CbcHeuristicGreedyEquality &); |
---|
109 | |
---|
110 | // Destructor |
---|
111 | ~CbcHeuristicGreedyEquality (); |
---|
112 | |
---|
113 | /// Clone |
---|
114 | virtual CbcHeuristic * clone() const; |
---|
115 | /// Assignment operator |
---|
116 | CbcHeuristicGreedyEquality & operator=(const CbcHeuristicGreedyEquality& rhs); |
---|
117 | /// Create C++ lines to get to current state |
---|
118 | virtual void generateCpp( FILE * fp) ; |
---|
119 | |
---|
120 | /// update model (This is needed if cliques update matrix etc) |
---|
121 | virtual void setModel(CbcModel * model); |
---|
122 | |
---|
123 | using CbcHeuristic::solution ; |
---|
124 | /** returns 0 if no solution, 1 if valid solution. |
---|
125 | Sets solution values if good, sets objective value (only if good) |
---|
126 | We leave all variables which are at one at this node of the |
---|
127 | tree to that value and will |
---|
128 | initially set all others to zero. We then sort all variables in order of their cost |
---|
129 | divided by the number of entries in rows which are not yet covered. We randomize that |
---|
130 | value a bit so that ties will be broken in different ways on different runs of the heuristic. |
---|
131 | We then choose the best one and set it to one and repeat the exercise. |
---|
132 | |
---|
133 | */ |
---|
134 | virtual int solution(double & objectiveValue, |
---|
135 | double * newSolution); |
---|
136 | /// Validate model i.e. sets when_ to 0 if necessary (may be NULL) |
---|
137 | virtual void validate() ; |
---|
138 | /// Resets stuff if model changes |
---|
139 | virtual void resetModel(CbcModel * model); |
---|
140 | /* Algorithm |
---|
141 | 0 - use current upper bounds |
---|
142 | 1 - use original upper bounds |
---|
143 | If 10 added perturb ratios more |
---|
144 | if 100 added round up all >=0.5 |
---|
145 | */ |
---|
146 | inline int algorithm() const { |
---|
147 | return algorithm_; |
---|
148 | } |
---|
149 | inline void setAlgorithm(int value) { |
---|
150 | algorithm_ = value; |
---|
151 | } |
---|
152 | // Fraction of rhs to cover before branch and cut |
---|
153 | inline void setFraction(double value) { |
---|
154 | fraction_ = value; |
---|
155 | } |
---|
156 | inline double fraction() const { |
---|
157 | return fraction_; |
---|
158 | } |
---|
159 | // Only do this many times |
---|
160 | inline int numberTimes() const { |
---|
161 | return numberTimes_; |
---|
162 | } |
---|
163 | inline void setNumberTimes(int value) { |
---|
164 | numberTimes_ = value; |
---|
165 | } |
---|
166 | protected: |
---|
167 | /// Guts of constructor from a CbcModel |
---|
168 | void gutsOfConstructor(CbcModel * model); |
---|
169 | // Data |
---|
170 | |
---|
171 | // Original matrix by column |
---|
172 | CoinPackedMatrix matrix_; |
---|
173 | // Fraction of rhs to cover before branch and cut |
---|
174 | double fraction_; |
---|
175 | // original number of rows |
---|
176 | int originalNumberRows_; |
---|
177 | /* Algorithm |
---|
178 | 0 - use current upper bounds |
---|
179 | 1 - use original upper bounds |
---|
180 | If 10 added perturb ratios more |
---|
181 | */ |
---|
182 | int algorithm_; |
---|
183 | /// Do this many times |
---|
184 | int numberTimes_; |
---|
185 | |
---|
186 | }; |
---|
187 | |
---|
188 | /** Greedy heuristic for SOS and L rows (and positive elements) |
---|
189 | */ |
---|
190 | |
---|
191 | class CbcHeuristicGreedySOS : public CbcHeuristic { |
---|
192 | public: |
---|
193 | |
---|
194 | // Default Constructor |
---|
195 | CbcHeuristicGreedySOS (); |
---|
196 | |
---|
197 | /* Constructor with model - assumed before cuts |
---|
198 | Initial version does not do Lps |
---|
199 | */ |
---|
200 | CbcHeuristicGreedySOS (CbcModel & model); |
---|
201 | |
---|
202 | // Copy constructor |
---|
203 | CbcHeuristicGreedySOS ( const CbcHeuristicGreedySOS &); |
---|
204 | |
---|
205 | // Destructor |
---|
206 | ~CbcHeuristicGreedySOS (); |
---|
207 | |
---|
208 | /// Clone |
---|
209 | virtual CbcHeuristic * clone() const; |
---|
210 | /// Assignment operator |
---|
211 | CbcHeuristicGreedySOS & operator=(const CbcHeuristicGreedySOS& rhs); |
---|
212 | /// Create C++ lines to get to current state |
---|
213 | virtual void generateCpp( FILE * fp) ; |
---|
214 | |
---|
215 | /// update model (This is needed if cliques update matrix etc) |
---|
216 | virtual void setModel(CbcModel * model); |
---|
217 | |
---|
218 | using CbcHeuristic::solution ; |
---|
219 | /** returns 0 if no solution, 1 if valid solution. |
---|
220 | Sets solution values if good, sets objective value (only if good) |
---|
221 | We leave all variables which are at one at this node of the |
---|
222 | tree to that value and will |
---|
223 | initially set all others to zero. We then sort all variables in order of their cost |
---|
224 | divided by the number of entries in rows which are not yet covered. We randomize that |
---|
225 | value a bit so that ties will be broken in different ways on different runs of the heuristic. |
---|
226 | We then choose the best one and set it to one and repeat the exercise. |
---|
227 | |
---|
228 | */ |
---|
229 | virtual int solution(double & objectiveValue, |
---|
230 | double * newSolution); |
---|
231 | /// Validate model i.e. sets when_ to 0 if necessary (may be NULL) |
---|
232 | virtual void validate() ; |
---|
233 | /// Resets stuff if model changes |
---|
234 | virtual void resetModel(CbcModel * model); |
---|
235 | /* Algorithm |
---|
236 | Bits |
---|
237 | 1 bit - use current model, otherwise original |
---|
238 | 2 - use current solution as starting point, otherwise pure greedy |
---|
239 | 4 - use duals to modify greedy |
---|
240 | 8 - use duals on GUB/SOS in special way |
---|
241 | */ |
---|
242 | inline int algorithm() const { |
---|
243 | return algorithm_; |
---|
244 | } |
---|
245 | inline void setAlgorithm(int value) { |
---|
246 | algorithm_ = value; |
---|
247 | } |
---|
248 | // Only do this many times |
---|
249 | inline int numberTimes() const { |
---|
250 | return numberTimes_; |
---|
251 | } |
---|
252 | inline void setNumberTimes(int value) { |
---|
253 | numberTimes_ = value; |
---|
254 | } |
---|
255 | |
---|
256 | protected: |
---|
257 | /// Guts of constructor from a CbcModel |
---|
258 | void gutsOfConstructor(CbcModel * model); |
---|
259 | // Data |
---|
260 | |
---|
261 | // Original RHS - if -1.0 then SOS otherwise <= value |
---|
262 | double * originalRhs_; |
---|
263 | // Original matrix by column |
---|
264 | CoinPackedMatrix matrix_; |
---|
265 | // original number of rows |
---|
266 | int originalNumberRows_; |
---|
267 | /* Algorithm |
---|
268 | */ |
---|
269 | int algorithm_; |
---|
270 | /// Do this many times |
---|
271 | int numberTimes_; |
---|
272 | |
---|
273 | }; |
---|
274 | |
---|
275 | |
---|
276 | #endif |
---|
277 | |
---|