1 | // Copyright (C) 2006, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | |
---|
4 | #ifndef OsiSolverLink_H |
---|
5 | #define OsiSolverLink_H |
---|
6 | |
---|
7 | #include "OsiClpSolverInterface.hpp" |
---|
8 | #include "CoinModel.hpp" |
---|
9 | class OsiModel; |
---|
10 | class CoinPackedMatrix; |
---|
11 | class OsiLinkedBound; |
---|
12 | class OsiObject; |
---|
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 OsiSolverLink : 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 | OsiSolverLink (); |
---|
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 | OsiSolverLink( 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 | OsiSolverLink (const OsiSolverLink &); |
---|
69 | |
---|
70 | /// Assignment operator |
---|
71 | OsiSolverLink & operator=(const OsiSolverLink& rhs); |
---|
72 | |
---|
73 | /// Destructor |
---|
74 | virtual ~OsiSolverLink (); |
---|
75 | |
---|
76 | //@} |
---|
77 | |
---|
78 | |
---|
79 | /**@name Sets and Gets */ |
---|
80 | //@{ |
---|
81 | /// Add a bound modifier |
---|
82 | void addBoundModifier(bool upperBoundAffected, bool useUpperBound, int whichVariable, int whichVariableAffected, |
---|
83 | double multiplier=1.0); |
---|
84 | /// Update coefficients |
---|
85 | void updateCoefficients(ClpSimplex * solver, CoinPackedMatrix * matrix); |
---|
86 | /// Set special options |
---|
87 | inline void setSpecialOptions2(int value) |
---|
88 | { specialOptions2_=value;}; |
---|
89 | /// Get special options |
---|
90 | inline int specialOptions2() const |
---|
91 | { return specialOptions2_;}; |
---|
92 | /** Clean copy of matrix |
---|
93 | So we can add rows |
---|
94 | */ |
---|
95 | CoinPackedMatrix * cleanMatrix() const |
---|
96 | { return matrix_;}; |
---|
97 | //@} |
---|
98 | |
---|
99 | //--------------------------------------------------------------------------- |
---|
100 | |
---|
101 | protected: |
---|
102 | |
---|
103 | |
---|
104 | /**@name functions */ |
---|
105 | //@{ |
---|
106 | /// Do real work of initialize |
---|
107 | void initialize(ClpSimplex * & solver, OsiObject ** & object) const; |
---|
108 | /// Do real work of delete |
---|
109 | void gutsOfDestructor(bool justNullify=false); |
---|
110 | /// Do real work of copy |
---|
111 | void gutsOfCopy(const OsiSolverLink & rhs) ; |
---|
112 | //@} |
---|
113 | |
---|
114 | /**@name Private member data */ |
---|
115 | //@{ |
---|
116 | /** Clean copy of matrix |
---|
117 | Marked coefficients will be multiplied by L or U |
---|
118 | */ |
---|
119 | CoinPackedMatrix * matrix_; |
---|
120 | /// Model in CoinModel format |
---|
121 | CoinModel coinModel_; |
---|
122 | /// Number of variables in tightening phase |
---|
123 | int numberVariables_; |
---|
124 | /// Information |
---|
125 | OsiLinkedBound * info_; |
---|
126 | /** |
---|
127 | 0 bit (1) - don't do mini B&B |
---|
128 | */ |
---|
129 | int specialOptions2_; |
---|
130 | //@} |
---|
131 | }; |
---|
132 | /** |
---|
133 | List of bounds which depend on other bounds |
---|
134 | */ |
---|
135 | |
---|
136 | class OsiLinkedBound { |
---|
137 | |
---|
138 | public: |
---|
139 | //--------------------------------------------------------------------------- |
---|
140 | /**@name Action methods */ |
---|
141 | //@{ |
---|
142 | /// Update other bounds |
---|
143 | void updateBounds(ClpSimplex * solver); |
---|
144 | //@} |
---|
145 | |
---|
146 | |
---|
147 | /**@name Constructors and destructors */ |
---|
148 | //@{ |
---|
149 | /// Default Constructor |
---|
150 | OsiLinkedBound (); |
---|
151 | /// Useful Constructor |
---|
152 | OsiLinkedBound(OsiSolverInterface * model, int variable, |
---|
153 | int numberAffected, const int * positionL, |
---|
154 | const int * positionU, const double * multiplier); |
---|
155 | |
---|
156 | /// Copy constructor |
---|
157 | OsiLinkedBound (const OsiLinkedBound &); |
---|
158 | |
---|
159 | /// Assignment operator |
---|
160 | OsiLinkedBound & operator=(const OsiLinkedBound& rhs); |
---|
161 | |
---|
162 | /// Destructor |
---|
163 | ~OsiLinkedBound (); |
---|
164 | |
---|
165 | //@} |
---|
166 | |
---|
167 | /**@name Sets and Gets */ |
---|
168 | //@{ |
---|
169 | /// Get variable |
---|
170 | inline int variable() const |
---|
171 | { return variable_;}; |
---|
172 | /// Add a bound modifier |
---|
173 | void addBoundModifier(bool upperBoundAffected, bool useUpperBound, int whichVariable, |
---|
174 | double multiplier=1.0); |
---|
175 | //@} |
---|
176 | |
---|
177 | private: |
---|
178 | typedef struct { |
---|
179 | /* |
---|
180 | 0 - LB of variable affected |
---|
181 | 1 - UB of variable affected |
---|
182 | 2 - element in position (affected) affected |
---|
183 | */ |
---|
184 | unsigned int affect:2; |
---|
185 | unsigned int ubUsed:1; // nonzero if UB of this variable is used |
---|
186 | /* |
---|
187 | 0 - use x*multiplier |
---|
188 | 1 - use multiplier/x |
---|
189 | 2 - if UB use min of current upper and x*multiplier, if LB use max of current lower and x*multiplier |
---|
190 | */ |
---|
191 | unsigned int type:4; // type of computation |
---|
192 | unsigned int affected:25; // variable or element affected |
---|
193 | float multiplier; // to use in computation |
---|
194 | } boundElementAction; |
---|
195 | |
---|
196 | /**@name Private member data */ |
---|
197 | //@{ |
---|
198 | /// Pointer back to model |
---|
199 | OsiSolverInterface * model_; |
---|
200 | /// Variable |
---|
201 | int variable_; |
---|
202 | /// Number of variables/elements affected |
---|
203 | int numberAffected_; |
---|
204 | /// Maximum number of variables/elements affected |
---|
205 | int maximumAffected_; |
---|
206 | /// Actions |
---|
207 | boundElementAction * affected_; |
---|
208 | //@} |
---|
209 | }; |
---|
210 | #include "CbcHeuristic.hpp" |
---|
211 | /** heuristic - just picks up any good solution |
---|
212 | */ |
---|
213 | |
---|
214 | class CbcHeuristicDynamic3 : public CbcHeuristic { |
---|
215 | public: |
---|
216 | |
---|
217 | // Default Constructor |
---|
218 | CbcHeuristicDynamic3 (); |
---|
219 | |
---|
220 | /* Constructor with model |
---|
221 | */ |
---|
222 | CbcHeuristicDynamic3 (CbcModel & model); |
---|
223 | |
---|
224 | // Copy constructor |
---|
225 | CbcHeuristicDynamic3 ( const CbcHeuristicDynamic3 &); |
---|
226 | |
---|
227 | // Destructor |
---|
228 | ~CbcHeuristicDynamic3 (); |
---|
229 | |
---|
230 | /// Clone |
---|
231 | virtual CbcHeuristic * clone() const; |
---|
232 | |
---|
233 | /// update model |
---|
234 | virtual void setModel(CbcModel * model); |
---|
235 | |
---|
236 | /** returns 0 if no solution, 1 if valid solution. |
---|
237 | Sets solution values if good, sets objective value (only if good) |
---|
238 | We leave all variables which are at one at this node of the |
---|
239 | tree to that value and will |
---|
240 | initially set all others to zero. We then sort all variables in order of their cost |
---|
241 | divided by the number of entries in rows which are not yet covered. We randomize that |
---|
242 | value a bit so that ties will be broken in different ways on different runs of the heuristic. |
---|
243 | We then choose the best one and set it to one and repeat the exercise. |
---|
244 | |
---|
245 | */ |
---|
246 | virtual int solution(double & objectiveValue, |
---|
247 | double * newSolution); |
---|
248 | /// Resets stuff if model changes |
---|
249 | virtual void resetModel(CbcModel * model); |
---|
250 | |
---|
251 | protected: |
---|
252 | private: |
---|
253 | /// Illegal Assignment operator |
---|
254 | CbcHeuristicDynamic3 & operator=(const CbcHeuristicDynamic3& rhs); |
---|
255 | }; |
---|
256 | |
---|
257 | #endif |
---|