1 | // (C) Copyright International Business Machines Corporation and |
---|
2 | // Carnegie Mellon University 2004, 2007 |
---|
3 | // |
---|
4 | // All Rights Reserved. |
---|
5 | // This code is published under the Common Public License. |
---|
6 | // |
---|
7 | // Authors : |
---|
8 | // Pierre Bonami, Carnegie Mellon University, |
---|
9 | // Carl D. Laird, Carnegie Mellon University, |
---|
10 | // Andreas Waechter, International Business Machines Corporation |
---|
11 | // |
---|
12 | // Date : 12/01/2004 |
---|
13 | |
---|
14 | #ifndef __TMINLP_HPP__ |
---|
15 | #define __TMINLP_HPP__ |
---|
16 | |
---|
17 | #include "IpUtils.hpp" |
---|
18 | #include "IpReferenced.hpp" |
---|
19 | #include "IpException.hpp" |
---|
20 | #include "IpAlgTypes.hpp" |
---|
21 | #include "CoinPackedMatrix.hpp" |
---|
22 | #include "OsiCuts.hpp" |
---|
23 | #include "IpTNLP.hpp" |
---|
24 | #include "CoinError.hpp" |
---|
25 | |
---|
26 | #include "CoinHelperFunctions.hpp" |
---|
27 | using namespace Ipopt; |
---|
28 | namespace Bonmin |
---|
29 | { |
---|
30 | DECLARE_STD_EXCEPTION(TMINLP_INVALID); |
---|
31 | DECLARE_STD_EXCEPTION(TMINLP_INVALID_VARIABLE_BOUNDS); |
---|
32 | |
---|
33 | /** Base class for all MINLPs that use a standard triplet matrix form |
---|
34 | * and dense vectors. |
---|
35 | * The class TMINLP2TNLP allows the caller to produce a viable TNLP |
---|
36 | * from the MINLP (by relaxing binary and/or integers, or by |
---|
37 | * fixing them), which can then be solved by Ipopt. |
---|
38 | * |
---|
39 | * This interface presents the problem form: |
---|
40 | * \f[ |
---|
41 | * \begin{array}{rl} |
---|
42 | * &min f(x)\\ |
---|
43 | * |
---|
44 | * \mbox{s.t.}&\\ |
---|
45 | * & g^L <= g(x) <= g^U\\ |
---|
46 | * |
---|
47 | * & x^L <= x <= x^U\\ |
---|
48 | * \end{array} |
---|
49 | * \f] |
---|
50 | * Where each x_i is either a continuous, binary, or integer variable. |
---|
51 | * If x_i is binary, the bounds [xL,xU] are assumed to be [0,1]. |
---|
52 | * In order to specify an equality constraint, set gL_i = gU_i = |
---|
53 | * rhs. The value that indicates "infinity" for the bounds |
---|
54 | * (i.e. the variable or constraint has no lower bound (-infinity) |
---|
55 | * or upper bound (+infinity)) is set through the option |
---|
56 | * nlp_lower_bound_inf and nlp_upper_bound_inf. To indicate that a |
---|
57 | * variable has no upper or lower bound, set the bound to |
---|
58 | * -ipopt_inf or +ipopt_inf respectively |
---|
59 | */ |
---|
60 | class TMINLP : public Ipopt::ReferencedObject |
---|
61 | { |
---|
62 | public: |
---|
63 | friend class TMINLP2TNLP; |
---|
64 | /** Return statuses of algorithm.*/ |
---|
65 | enum SolverReturn{ |
---|
66 | SUCCESS, |
---|
67 | INFEASIBLE, |
---|
68 | LIMIT_EXCEEDED, |
---|
69 | MINLP_ERROR}; |
---|
70 | /** Class to store sos constraints for model */ |
---|
71 | struct SosInfo |
---|
72 | { |
---|
73 | /** Number of SOS constraints.*/ |
---|
74 | int num; |
---|
75 | /** Type of sos. At present Only type '1' SOS are supported by Cbc*/ |
---|
76 | char * types; |
---|
77 | /** priorities of sos constraints.*/ |
---|
78 | int * priorities; |
---|
79 | |
---|
80 | /** \name Sparse storage of the elements of the SOS constraints.*/ |
---|
81 | /** @{ */ |
---|
82 | /** Total number of non zeroes in SOS constraints.*/ |
---|
83 | int numNz; |
---|
84 | /** For 0 <= i < nums, start[i] gives the indice of indices and weights arrays at which the description of constraints i begins..*/ |
---|
85 | int * starts; |
---|
86 | /** indices of elements belonging to the SOS.*/ |
---|
87 | int * indices; |
---|
88 | /** weights of the elements of the SOS.*/ |
---|
89 | double * weights; |
---|
90 | /** @} */ |
---|
91 | /** default constructor. */ |
---|
92 | SosInfo(); |
---|
93 | /** Copy constructor.*/ |
---|
94 | SosInfo(const SosInfo & source); |
---|
95 | |
---|
96 | |
---|
97 | /** destructor*/ |
---|
98 | ~SosInfo() |
---|
99 | { |
---|
100 | gutsOfDestructor(); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /** Reset information */ |
---|
105 | void gutsOfDestructor(); |
---|
106 | |
---|
107 | }; |
---|
108 | |
---|
109 | /** Stores branching priorities information. */ |
---|
110 | struct BranchingInfo |
---|
111 | { |
---|
112 | /**number of variables*/ |
---|
113 | int size; |
---|
114 | /** User set priorities on variables. */ |
---|
115 | int * priorities; |
---|
116 | /** User set preferered branching direction. */ |
---|
117 | int * branchingDirections; |
---|
118 | /** User set up pseudo costs.*/ |
---|
119 | double * upPsCosts; |
---|
120 | /** User set down pseudo costs.*/ |
---|
121 | double * downPsCosts; |
---|
122 | BranchingInfo(): |
---|
123 | size(0), |
---|
124 | priorities(NULL), |
---|
125 | branchingDirections(NULL), |
---|
126 | upPsCosts(NULL), |
---|
127 | downPsCosts(NULL) |
---|
128 | {} |
---|
129 | BranchingInfo(const BranchingInfo &other) |
---|
130 | { |
---|
131 | gutsOfDestructor(); |
---|
132 | size = other.size; |
---|
133 | priorities = CoinCopyOfArray(other.priorities, size); |
---|
134 | branchingDirections = CoinCopyOfArray(other.branchingDirections, size); |
---|
135 | upPsCosts = CoinCopyOfArray(other.upPsCosts, size); |
---|
136 | downPsCosts = CoinCopyOfArray(other.downPsCosts, size); |
---|
137 | } |
---|
138 | void gutsOfDestructor() |
---|
139 | { |
---|
140 | if (priorities != NULL) delete [] priorities; |
---|
141 | priorities = NULL; |
---|
142 | if (branchingDirections != NULL) delete [] branchingDirections; |
---|
143 | branchingDirections = NULL; |
---|
144 | if (upPsCosts != NULL) delete [] upPsCosts; |
---|
145 | upPsCosts = NULL; |
---|
146 | if (downPsCosts != NULL) delete [] downPsCosts; |
---|
147 | downPsCosts = NULL; |
---|
148 | } |
---|
149 | ~BranchingInfo() |
---|
150 | { |
---|
151 | gutsOfDestructor(); |
---|
152 | } |
---|
153 | }; |
---|
154 | |
---|
155 | /** Class to store perturbation radii for variables in the model */ |
---|
156 | class PerturbInfo |
---|
157 | { |
---|
158 | public: |
---|
159 | /** default constructor. */ |
---|
160 | PerturbInfo() : |
---|
161 | perturb_radius_(NULL) |
---|
162 | {} |
---|
163 | |
---|
164 | /** destructor*/ |
---|
165 | ~PerturbInfo() |
---|
166 | { |
---|
167 | delete [] perturb_radius_; |
---|
168 | } |
---|
169 | |
---|
170 | /** Method for setting the perturbation radii. */ |
---|
171 | void SetPerturbationArray(Index numvars, const double* perturb_radius); |
---|
172 | |
---|
173 | /** Method for getting the array for the perturbation radii in |
---|
174 | * order to use the values. */ |
---|
175 | const double* GetPerturbationArray() const { |
---|
176 | return perturb_radius_; |
---|
177 | } |
---|
178 | |
---|
179 | private: |
---|
180 | /** Copy constructor.*/ |
---|
181 | PerturbInfo(const PerturbInfo & source); |
---|
182 | |
---|
183 | /** Perturbation radii for all variables. A negative value |
---|
184 | * means that the radius has not been given. If the pointer is |
---|
185 | * NULL, then no variables have been assigned a perturbation |
---|
186 | * radius. */ |
---|
187 | double* perturb_radius_; |
---|
188 | }; |
---|
189 | |
---|
190 | /** Type of the variables.*/ |
---|
191 | enum VariableType |
---|
192 | { |
---|
193 | CONTINUOUS, |
---|
194 | BINARY, |
---|
195 | INTEGER |
---|
196 | }; |
---|
197 | |
---|
198 | /**@name Constructors/Destructors */ |
---|
199 | //@{ |
---|
200 | TMINLP(); |
---|
201 | |
---|
202 | /** Default destructor */ |
---|
203 | virtual ~TMINLP() |
---|
204 | {} |
---|
205 | //@} |
---|
206 | |
---|
207 | /**@name methods to gather information about the MINLP */ |
---|
208 | //@{ |
---|
209 | /** overload this method to return the number of variables |
---|
210 | * and constraints, and the number of non-zeros in the jacobian and |
---|
211 | * the hessian. */ |
---|
212 | virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, |
---|
213 | Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style)=0; |
---|
214 | |
---|
215 | /** overload this method to return scaling parameters. This is |
---|
216 | * only called if the options are set to retrieve user scaling. |
---|
217 | * There, use_x_scaling (or use_g_scaling) should get set to true |
---|
218 | * only if the variables (or constraints) are to be scaled. This |
---|
219 | * method should return true only if the scaling parameters could |
---|
220 | * be provided. |
---|
221 | */ |
---|
222 | virtual bool get_scaling_parameters(Number& obj_scaling, |
---|
223 | bool& use_x_scaling, Index n, |
---|
224 | Number* x_scaling, |
---|
225 | bool& use_g_scaling, Index m, |
---|
226 | Number* g_scaling) |
---|
227 | { |
---|
228 | return false; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | /** overload this method to set the variable type. The var_types |
---|
233 | * array will be allocated with length n. */ |
---|
234 | virtual bool get_variables_types(Index n, VariableType* var_types)=0; |
---|
235 | |
---|
236 | /** overload this method to return the constraint linearity. |
---|
237 | * array should be alocated with length at least n.*/ |
---|
238 | virtual bool get_constraints_linearity(Index m, |
---|
239 | Ipopt::TNLP::LinearityType* const_types) = 0; |
---|
240 | |
---|
241 | /** overload this method to return the information about the bound |
---|
242 | * on the variables and constraints. The value that indicates |
---|
243 | * that a bound does not exist is specified in the parameters |
---|
244 | * nlp_lower_bound_inf and nlp_upper_bound_inf. By default, |
---|
245 | * nlp_lower_bound_inf is -1e19 and nlp_upper_bound_inf is |
---|
246 | * 1e19. |
---|
247 | * An exception will be thrown if x_l and x_u are not 0,1 for binary variables |
---|
248 | */ |
---|
249 | virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u, |
---|
250 | Index m, Number* g_l, Number* g_u)=0; |
---|
251 | |
---|
252 | /** overload this method to return the starting point. The bools |
---|
253 | * init_x and init_lambda are both inputs and outputs. As inputs, |
---|
254 | * they indicate whether or not the algorithm wants you to |
---|
255 | * initialize x and lambda respectively. If, for some reason, the |
---|
256 | * algorithm wants you to initialize these and you cannot, set |
---|
257 | * the respective bool to false. |
---|
258 | */ |
---|
259 | virtual bool get_starting_point(Index n, bool init_x, Number* x, |
---|
260 | bool init_z, Number* z_L, Number* z_U, |
---|
261 | Index m, bool init_lambda, |
---|
262 | Number* lambda)=0; |
---|
263 | |
---|
264 | /** overload this method to return the value of the objective function */ |
---|
265 | virtual bool eval_f(Index n, const Number* x, bool new_x, |
---|
266 | Number& obj_value)=0; |
---|
267 | |
---|
268 | /** overload this method to return the vector of the gradient of |
---|
269 | * the objective w.r.t. x */ |
---|
270 | virtual bool eval_grad_f(Index n, const Number* x, bool new_x, |
---|
271 | Number* grad_f)=0; |
---|
272 | |
---|
273 | /** overload this method to return the vector of constraint values */ |
---|
274 | virtual bool eval_g(Index n, const Number* x, bool new_x, |
---|
275 | Index m, Number* g)=0; |
---|
276 | |
---|
277 | /** overload this method to return the jacobian of the |
---|
278 | * constraints. The vectors iRow and jCol only need to be set |
---|
279 | * once. The first call is used to set the structure only (iRow |
---|
280 | * and jCol will be non-NULL, and values will be NULL) For |
---|
281 | * subsequent calls, iRow and jCol will be NULL. */ |
---|
282 | virtual bool eval_jac_g(Index n, const Number* x, bool new_x, |
---|
283 | Index m, Index nele_jac, Index* iRow, |
---|
284 | Index *jCol, Number* values)=0; |
---|
285 | |
---|
286 | /** overload this method to return the hessian of the |
---|
287 | * lagrangian. The vectors iRow and jCol only need to be set once |
---|
288 | * (during the first call). The first call is used to set the |
---|
289 | * structure only (iRow and jCol will be non-NULL, and values |
---|
290 | * will be NULL) For subsequent calls, iRow and jCol will be |
---|
291 | * NULL. This matrix is symmetric - specify the lower diagonal |
---|
292 | * only */ |
---|
293 | virtual bool eval_h(Index n, const Number* x, bool new_x, |
---|
294 | Number obj_factor, Index m, const Number* lambda, |
---|
295 | bool new_lambda, Index nele_hess, |
---|
296 | Index* iRow, Index* jCol, Number* values)=0; |
---|
297 | /** Compute the value of a single constraint. The constraint |
---|
298 | * number is i (starting counting from 0. */ |
---|
299 | virtual bool eval_gi(Index n, const Number* x, bool new_x, |
---|
300 | Index i, Number& gi) |
---|
301 | { |
---|
302 | std::cerr << "Method eval_gi not overloaded from TMINLP\n"; |
---|
303 | return false; |
---|
304 | } |
---|
305 | /** Compute the structure or values of the gradient for one |
---|
306 | * constraint. The constraint * number is i (starting counting |
---|
307 | * from 0. Other things are like with eval_jac_g. */ |
---|
308 | virtual bool eval_grad_gi(Index n, const Number* x, bool new_x, |
---|
309 | Index i, Index& nele_grad_gi, Index* jCol, |
---|
310 | Number* values) |
---|
311 | { |
---|
312 | std::cerr << "Method eval_grad_gi not overloaded from TMINLP\n"; |
---|
313 | return false; |
---|
314 | } |
---|
315 | //@} |
---|
316 | |
---|
317 | /** @name Solution Methods */ |
---|
318 | //@{ |
---|
319 | /** This method is called when the algorithm is complete so the TNLP can store/write the solution */ |
---|
320 | virtual void finalize_solution(TMINLP::SolverReturn status, |
---|
321 | Index n, const Number* x, Number obj_value) =0; |
---|
322 | //@} |
---|
323 | |
---|
324 | virtual const BranchingInfo * branchingInfo() const = 0; |
---|
325 | |
---|
326 | /** Add some linear cuts to the problem formulation */ |
---|
327 | void addCuts(int numberCuts, const OsiRowCut ** cuts); |
---|
328 | |
---|
329 | /** Remove some cuts to the formulation */ |
---|
330 | void removeCuts(int number ,const int * toRemove); |
---|
331 | |
---|
332 | /** remove the last number cuts.*/ |
---|
333 | void removeLastCuts(int number); |
---|
334 | |
---|
335 | virtual const SosInfo * sosConstraints() const = 0; |
---|
336 | |
---|
337 | virtual const PerturbInfo* perturbInfo() const |
---|
338 | { |
---|
339 | return NULL; |
---|
340 | } |
---|
341 | |
---|
342 | /** Say if has a specific function to compute upper bounds*/ |
---|
343 | virtual bool hasUpperBoundingObjective(){ |
---|
344 | return false;} |
---|
345 | |
---|
346 | /** overload this method to return the value of an alternative objective function for |
---|
347 | upper bounding (to use it hasUpperBoundingObjective should return true).*/ |
---|
348 | virtual bool eval_upper_bound_f(Index n, const Number* x, |
---|
349 | Number& obj_value){} |
---|
350 | private: |
---|
351 | /**@name Default Compiler Generated Methods |
---|
352 | * (Hidden to avoid implicit creation/calling). |
---|
353 | * These methods are not implemented and |
---|
354 | * we do not want the compiler to implement |
---|
355 | * them for us, so we declare them private |
---|
356 | * and do not define them. This ensures that |
---|
357 | * they will not be implicitly created/called. */ |
---|
358 | //@{ |
---|
359 | /** Default Constructor */ |
---|
360 | //TMINLP(); |
---|
361 | |
---|
362 | /** Copy Constructor */ |
---|
363 | TMINLP(const TMINLP&); |
---|
364 | |
---|
365 | /** Overloaded Equals Operator */ |
---|
366 | void operator=(const TMINLP&); |
---|
367 | //@} |
---|
368 | |
---|
369 | /** resize arrays for linear cuts */ |
---|
370 | void resizeLinearCuts(int newNumberCuts, int newNnz); |
---|
371 | /** columnindices of linear cuts. */ |
---|
372 | int * jCol_; |
---|
373 | /** rows indices of linear cuts. */ |
---|
374 | int * iRow_; |
---|
375 | /** elements of linear cuts.*/ |
---|
376 | double * elems_; |
---|
377 | /** lower bounds for linear cuts. */ |
---|
378 | double * lower_; |
---|
379 | /** upper bounds for linear cuts. */ |
---|
380 | double * upper_; |
---|
381 | /** number of linear cuts.*/ |
---|
382 | int nLinearCuts_; |
---|
383 | /** number of non-zeroes in linear cuts*/ |
---|
384 | int linearCutsNnz_; |
---|
385 | /** storage size for linear cuts number of cuts.*/ |
---|
386 | int linearCutsCapacity_; |
---|
387 | /** storage size for linear cuts number of nnz.*/ |
---|
388 | int linearCutsNnzCapacity_; |
---|
389 | }; |
---|
390 | |
---|
391 | } // namespace Ipopt |
---|
392 | |
---|
393 | #endif |
---|