1 | // Copyright (C) 2006, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | |
---|
4 | #ifndef CbcSolverLink_H |
---|
5 | #define CbcSolverLink_H |
---|
6 | |
---|
7 | #include "OsiClpSolverInterface.hpp" |
---|
8 | #include "CoinModel.hpp" |
---|
9 | class CbcModel; |
---|
10 | class CoinPackedMatrix; |
---|
11 | class CbcLinkedBound; |
---|
12 | class CbcObject; |
---|
13 | //############################################################################# |
---|
14 | |
---|
15 | /** |
---|
16 | |
---|
17 | This is to allow the user to replace initialSolve and resolve |
---|
18 | This version changes coefficients |
---|
19 | */ |
---|
20 | |
---|
21 | class CbcSolverLink : public OsiClpSolverInterface { |
---|
22 | |
---|
23 | public: |
---|
24 | //--------------------------------------------------------------------------- |
---|
25 | /**@name Solve methods */ |
---|
26 | //@{ |
---|
27 | /// Solve initial LP relaxation |
---|
28 | virtual void initialSolve(); |
---|
29 | |
---|
30 | /// Resolve an LP relaxation after problem modification |
---|
31 | virtual void resolve(); |
---|
32 | |
---|
33 | /** |
---|
34 | Problem specific |
---|
35 | Returns -1 if node fathomed and no solution |
---|
36 | 0 if did nothing |
---|
37 | 1 if node fathomed and solution |
---|
38 | allFixed is true if all LinkedBound variables are fixed |
---|
39 | */ |
---|
40 | virtual int fathom(bool allFixed) {return 0;}; |
---|
41 | //@} |
---|
42 | |
---|
43 | |
---|
44 | /**@name Constructors and destructors */ |
---|
45 | //@{ |
---|
46 | /// Default Constructor |
---|
47 | CbcSolverLink (); |
---|
48 | |
---|
49 | /** This creates from a coinModel object |
---|
50 | |
---|
51 | if errors.then number of sets is -1 |
---|
52 | |
---|
53 | This creates linked ordered sets information. It assumes - |
---|
54 | |
---|
55 | for product terms syntax is yy*f(zz) |
---|
56 | also just f(zz) is allowed |
---|
57 | and even a constant |
---|
58 | |
---|
59 | modelObject not const as may be changed as part of process. |
---|
60 | */ |
---|
61 | CbcSolverLink( CoinModel & modelObject); |
---|
62 | // real constructor for gdb |
---|
63 | void gdb( CoinModel & modelObject); |
---|
64 | /// Clone |
---|
65 | virtual OsiSolverInterface * clone(bool copyData=true) const; |
---|
66 | |
---|
67 | /// Copy constructor |
---|
68 | CbcSolverLink (const CbcSolverLink &); |
---|
69 | |
---|
70 | /// Assignment operator |
---|
71 | CbcSolverLink & operator=(const CbcSolverLink& rhs); |
---|
72 | |
---|
73 | /// Destructor |
---|
74 | virtual ~CbcSolverLink (); |
---|
75 | |
---|
76 | //@} |
---|
77 | |
---|
78 | |
---|
79 | /**@name Sets and Gets */ |
---|
80 | //@{ |
---|
81 | /// Setup |
---|
82 | void initialize(CbcModel * model, const CoinPackedMatrix * matrix, |
---|
83 | int numberVariables, const int * which, |
---|
84 | const int *starts, const int * positionL, |
---|
85 | const int * positionU); |
---|
86 | /** This creates a solver instance and information and passes to CbcModel |
---|
87 | Granularity can be given for each set |
---|
88 | Negative values mean S1 type grid (using abs(value). |
---|
89 | */ |
---|
90 | void initialize ( CbcModel * model, bool top=true); |
---|
91 | /// revert to previous solver |
---|
92 | void revert(); |
---|
93 | /// Add a bound modifier |
---|
94 | void addBoundModifier(bool upperBoundAffected, bool useUpperBound, int whichVariable, int whichVariableAffected, |
---|
95 | double multiplier=1.0); |
---|
96 | /// Objective value of best solution found internally |
---|
97 | inline double bestObjectiveValue() const |
---|
98 | { return bestObjectiveValue_;}; |
---|
99 | /// Best solution found internally |
---|
100 | const double * bestSolution() const |
---|
101 | { return bestSolution_;}; |
---|
102 | /// Set special options |
---|
103 | inline void setSpecialOptions2(int value) |
---|
104 | { specialOptions2_=value;}; |
---|
105 | /// Get special options |
---|
106 | inline int specialOptions2() const |
---|
107 | { return specialOptions2_;}; |
---|
108 | /// Number of Linked Ordered Sets |
---|
109 | inline int numberSets() const |
---|
110 | { return numberSets_;}; |
---|
111 | /// Sets granularity |
---|
112 | void setGranularity(double top, double bottom, |
---|
113 | const double * topG = NULL, const double * bottomG = NULL); |
---|
114 | //@} |
---|
115 | |
---|
116 | //--------------------------------------------------------------------------- |
---|
117 | |
---|
118 | protected: |
---|
119 | |
---|
120 | |
---|
121 | /**@name functions */ |
---|
122 | //@{ |
---|
123 | /// Do real work of initialize |
---|
124 | void initialize(ClpSimplex * & solver, CbcObject ** & object) const; |
---|
125 | /// Do real work of delete |
---|
126 | void gutsOfDestructor(bool justNullify=false); |
---|
127 | /// Do real work of copy |
---|
128 | void gutsOfCopy(const CbcSolverLink & rhs) ; |
---|
129 | //@} |
---|
130 | |
---|
131 | /**@name Private member data */ |
---|
132 | //@{ |
---|
133 | /// Pointer back to model |
---|
134 | CbcModel * model_; |
---|
135 | /** Clean copy of matrix |
---|
136 | Marked coefficients will be multiplied by L or U |
---|
137 | */ |
---|
138 | CoinPackedMatrix * matrix_; |
---|
139 | /// Number of variables in tightening phase |
---|
140 | int numberVariables_; |
---|
141 | /// Information |
---|
142 | CbcLinkedBound * info_; |
---|
143 | /// Objective value of best solution found internally |
---|
144 | double bestObjectiveValue_; |
---|
145 | /// Best solution found internally |
---|
146 | double * bestSolution_; |
---|
147 | /** |
---|
148 | 0 bit (1) - don't do mini B&B |
---|
149 | */ |
---|
150 | int specialOptions2_; |
---|
151 | /// Number of Sets |
---|
152 | int numberSets_; |
---|
153 | /// Number of nonlinear terms |
---|
154 | int numberNonlinear_; |
---|
155 | /// Start of each linked ordered set |
---|
156 | int * startSet_; |
---|
157 | /// Which row is nonlinear definition row |
---|
158 | int * definitionRow_; |
---|
159 | /// Which row is linear definition row (-1 for just f(z), -2 for numeric) |
---|
160 | int * definitionRowL_; |
---|
161 | /// Nonlinear column |
---|
162 | int * functionColumn_; |
---|
163 | /// Linear column |
---|
164 | int * definitionColumnL_; |
---|
165 | /// Which row is usage row |
---|
166 | int * usageRow_; |
---|
167 | /// Convexity row |
---|
168 | int * convexityRow_; |
---|
169 | /// Copy of truncated model |
---|
170 | ClpSimplex * simplexModel_; |
---|
171 | /// Copy of original model |
---|
172 | ClpSimplex * savedModel_; |
---|
173 | /// Which variables of original were nonlinear |
---|
174 | int * which_; |
---|
175 | /// Copy of CoinModel |
---|
176 | CoinModel coinModel_; |
---|
177 | /// Granularity at top of tree |
---|
178 | double * granularityTop_; |
---|
179 | /// Granularity at bottom of tree (when all integers gone) |
---|
180 | double * granularityBottom_; |
---|
181 | //@} |
---|
182 | }; |
---|
183 | /** |
---|
184 | List of bounds which depend on other bounds |
---|
185 | */ |
---|
186 | |
---|
187 | class CbcLinkedBound { |
---|
188 | |
---|
189 | public: |
---|
190 | //--------------------------------------------------------------------------- |
---|
191 | /**@name Action methods */ |
---|
192 | //@{ |
---|
193 | /// Update other bounds |
---|
194 | void updateBounds(ClpSimplex * solver); |
---|
195 | /// Update coefficients |
---|
196 | void updateCoefficients(ClpSimplex * solver, CoinPackedMatrix * matrix); |
---|
197 | /// Fix variables in LOS |
---|
198 | void fixLOS(ClpSimplex * solver); |
---|
199 | /// Update this variables bounds from reference row |
---|
200 | void updateBoundsForThis(ClpSimplex * solver); |
---|
201 | //@} |
---|
202 | |
---|
203 | |
---|
204 | /**@name Constructors and destructors */ |
---|
205 | //@{ |
---|
206 | /// Default Constructor |
---|
207 | CbcLinkedBound (); |
---|
208 | /// Useful Constructor |
---|
209 | CbcLinkedBound(CbcModel * model, int variable, |
---|
210 | int numberAffected, const int * positionL, |
---|
211 | const int * positionU, const double * multiplier); |
---|
212 | |
---|
213 | /// Copy constructor |
---|
214 | CbcLinkedBound (const CbcLinkedBound &); |
---|
215 | |
---|
216 | /// Assignment operator |
---|
217 | CbcLinkedBound & operator=(const CbcLinkedBound& rhs); |
---|
218 | |
---|
219 | /// Destructor |
---|
220 | ~CbcLinkedBound (); |
---|
221 | |
---|
222 | //@} |
---|
223 | |
---|
224 | /**@name Sets and Gets */ |
---|
225 | //@{ |
---|
226 | /// Get variable |
---|
227 | inline int variable() const |
---|
228 | { return variable_;}; |
---|
229 | /// Add a bound modifier |
---|
230 | void addBoundModifier(bool upperBoundAffected, bool useUpperBound, int whichVariable, |
---|
231 | double multiplier=1.0); |
---|
232 | /// Add a bound modifier |
---|
233 | void addCoefficientModifier( bool useUpperBound, int position, |
---|
234 | double multiplier=1.0); |
---|
235 | /// Get useObject |
---|
236 | inline int useObject() const |
---|
237 | { return useObject_;}; |
---|
238 | /// Set useObject |
---|
239 | inline void setUseObject(int value) |
---|
240 | { useObject_ = value;}; |
---|
241 | //@} |
---|
242 | |
---|
243 | private: |
---|
244 | typedef struct { |
---|
245 | /* |
---|
246 | 0 - LB of variable affected |
---|
247 | 1 - UB of variable affected |
---|
248 | 2 - element in position (affected) affected |
---|
249 | */ |
---|
250 | unsigned int affect:2; |
---|
251 | unsigned int ubUsed:1; // nonzero if UB of this variable is used |
---|
252 | /* |
---|
253 | 0 - use x*multiplier |
---|
254 | 1 - use multiplier/x |
---|
255 | 2 - if UB use min of current upper and x*multiplier, if LB use max of current lower and x*multiplier |
---|
256 | */ |
---|
257 | unsigned int type:4; // type of computation |
---|
258 | unsigned int affected:25; // variable or element affected |
---|
259 | float multiplier; // to use in computation |
---|
260 | } boundElementAction; |
---|
261 | |
---|
262 | /**@name Private member data */ |
---|
263 | //@{ |
---|
264 | /// Pointer back to model |
---|
265 | CbcModel * model_; |
---|
266 | /// Variable |
---|
267 | int variable_; |
---|
268 | /// Number of variables/elements affected |
---|
269 | int numberAffected_; |
---|
270 | /// Maximum number of variables/elements affected |
---|
271 | int maximumAffected_; |
---|
272 | /// Actions |
---|
273 | boundElementAction * affected_; |
---|
274 | /// If >=0 object number which can be used to determine bounds on variable |
---|
275 | int useObject_; |
---|
276 | /// Number of sets for variable fixing |
---|
277 | int numberSets_; |
---|
278 | /// Which objects whose reference row entry can be used |
---|
279 | int * whichSets_; |
---|
280 | //@} |
---|
281 | }; |
---|
282 | #include "CbcHeuristic.hpp" |
---|
283 | /** heuristic - just picks up any good solution |
---|
284 | */ |
---|
285 | |
---|
286 | class CbcHeuristicDynamic2 : public CbcHeuristic { |
---|
287 | public: |
---|
288 | |
---|
289 | // Default Constructor |
---|
290 | CbcHeuristicDynamic2 (); |
---|
291 | |
---|
292 | /* Constructor with model |
---|
293 | */ |
---|
294 | CbcHeuristicDynamic2 (CbcModel & model); |
---|
295 | |
---|
296 | // Copy constructor |
---|
297 | CbcHeuristicDynamic2 ( const CbcHeuristicDynamic2 &); |
---|
298 | |
---|
299 | // Destructor |
---|
300 | ~CbcHeuristicDynamic2 (); |
---|
301 | |
---|
302 | /// Clone |
---|
303 | virtual CbcHeuristic * clone() const; |
---|
304 | |
---|
305 | /// update model |
---|
306 | virtual void setModel(CbcModel * model); |
---|
307 | |
---|
308 | /** returns 0 if no solution, 1 if valid solution. |
---|
309 | Sets solution values if good, sets objective value (only if good) |
---|
310 | We leave all variables which are at one at this node of the |
---|
311 | tree to that value and will |
---|
312 | initially set all others to zero. We then sort all variables in order of their cost |
---|
313 | divided by the number of entries in rows which are not yet covered. We randomize that |
---|
314 | value a bit so that ties will be broken in different ways on different runs of the heuristic. |
---|
315 | We then choose the best one and set it to one and repeat the exercise. |
---|
316 | |
---|
317 | */ |
---|
318 | virtual int solution(double & objectiveValue, |
---|
319 | double * newSolution); |
---|
320 | /// Resets stuff if model changes |
---|
321 | virtual void resetModel(CbcModel * model); |
---|
322 | |
---|
323 | protected: |
---|
324 | private: |
---|
325 | /// Illegal Assignment operator |
---|
326 | CbcHeuristicDynamic2 & operator=(const CbcHeuristicDynamic2& rhs); |
---|
327 | }; |
---|
328 | |
---|
329 | #endif |
---|