Debian Codegraph @

Bonmin/src/Interfaces/BonTMINLP2TNLP.hpp

raw
1 // (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2004, 2006
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, Carnegie Mellon University,
7 // Carl D. Laird, Carnegie Mellon University,
8 // Andreas Waechter, International Business Machines Corporation
9 //
10 // Date : 12/01/2004
11
12 #ifndef __TMINLP2TNLP_HPP__
13 #define __TMINLP2TNLP_HPP__
14
15 #include "IpTNLP.hpp"
16 #include "BonTMINLP.hpp"
17 #include "IpSmartPtr.hpp"
18 #include "IpIpoptApplication.hpp"
19 #include "IpOptionsList.hpp"
20 #include "BonTypes.hpp"
21
22 namespace Bonmin
23 {
24 class IpoptInteriorWarmStarter;
25
26 /** This is an adapter class that converts a TMINLP to
27 * a TNLP to be solved by Ipopt. It allows an external
28 * caller to modify the bounds of variables, allowing
29 * the treatment of binary and integer variables as
30 * relaxed, or fixed
31 */
32 class TMINLP2TNLP : public Ipopt::TNLP
33 {
34 public:
35 /**@name Constructors/Destructors */
36 //@{
37 TMINLP2TNLP(const Ipopt::SmartPtr<TMINLP> tminlp
38 #ifdef WARM_STARTER
39 ,
40 const OptionsList& options
41 #endif
42 );
43
44 /** Copy Constructor
45 * \warning source and copy point to the same tminlp_.
46 */
47 TMINLP2TNLP(const TMINLP2TNLP&);
48
49 /** virtual copy .*/
50 virtual TMINLP2TNLP * clone() const{
51 return new TMINLP2TNLP(*this);}
52
53 /** Default destructor */
54 virtual ~TMINLP2TNLP();
55 //@}
56
57 /**@name Methods to modify the MINLP and form the NLP */
58 //@{
59
60 /** Get the number of variables */
61 inline Ipopt::Index num_variables() const
62 {
63 assert(x_l_.size() == x_u_.size());
64 return static_cast<int>(x_l_.size());
65 }
66
67 /** Get the number of constraints */
68 inline Ipopt::Index num_constraints() const
69 {
70 assert(g_l_.size() == g_u_.size());
71 return static_cast<int>(g_l_.size());
72 }
73 /** Get the nomber of nz in hessian */
74 Ipopt::Index nnz_h_lag()
75 {
76 return nnz_h_lag_;
77 }
78 /** Get the variable types */
79 const TMINLP::VariableType* var_types()
80 {
81 return &var_types_[0];
82 }
83
84 /** Get the current values for the lower bounds */
85 const Ipopt::Number* x_l()
86 {
87 return &x_l_[0];
88 }
89 /** Get the current values for the upper bounds */
90 const Ipopt::Number* x_u()
91 {
92 return &x_u_[0];
93 }
94
95 /** Get the original values for the lower bounds */
96 const Ipopt::Number* orig_x_l() const
97 {
98 return &orig_x_l_[0];
99 }
100 /** Get the original values for the upper bounds */
101 const Ipopt::Number* orig_x_u() const
102 {
103 return orig_x_u_();
104 }
105
106 /** Get the current values for constraints lower bounds */
107 const Ipopt::Number* g_l()
108 {
109 return g_l_();
110 }
111 /** Get the current values for constraints upper bounds */
112 const Ipopt::Number* g_u()
113 {
114 return g_u_();
115 }
116
117 /** get the starting primal point */
118 const Ipopt::Number * x_init() const
119 {
120 return x_init_();
121 }
122
123 /** get the user provided starting primal point */
124 const Ipopt::Number * x_init_user() const
125 {
126 return x_init_user_();
127 }
128
129 /** get the starting dual point */
130 const Ipopt::Number * duals_init() const
131 {
132 return duals_init_;
133 }
134
135 /** get the solution values */
136 const Ipopt::Number* x_sol() const
137 {
138 return x_sol_();
139 }
140
141 /** get the g solution (activities) */
142 const Ipopt::Number* g_sol() const
143 {
144 return g_sol_();
145 }
146
147 /** get the dual values */
148 const Ipopt::Number* duals_sol() const
149 {
150 return duals_sol_();
151 }
152
153 /** Get Optimization status */
154 Ipopt::SolverReturn optimization_status() const
155 {
156 return return_status_;
157 }
158
159 /** Get the objective value */
160 Ipopt::Number obj_value() const
161 {
162 return obj_value_;
163 }
164
165 /** Manually set objective value. */
166 void set_obj_value(Ipopt::Number value)
167 {
168 obj_value_ = value;
169 }
170
171 /** force solution to be fractionnal.*/
172 void force_fractionnal_sol();
173
174 /** Change the bounds on the variables */
175 void SetVariablesBounds(Ipopt::Index n,
176 const Ipopt::Number * x_l,
177 const Ipopt::Number * x_u);
178
179 /** Change the lower bound on the variables */
180 void SetVariablesLowerBounds(Ipopt::Index n,
181 const Ipopt::Number * x_l);
182
183 /** Change the upper bound on the variable */
184 void SetVariablesUpperBounds(Ipopt::Index n,
185 const Ipopt::Number * x_u);
186
187 /** Change the bounds on the variable */
188 void SetVariableBounds(Ipopt::Index var_no, Ipopt::Number x_l, Ipopt::Number x_u);
189
190 /** Change the lower bound on the variable */
191 void SetVariableLowerBound(Ipopt::Index var_no, Ipopt::Number x_l);
192
193 /** Change the upper bound on the variable */
194 void SetVariableUpperBound(Ipopt::Index var_no, Ipopt::Number x_u);
195
196 /** reset the starting point to original one. */
197 void resetStartingPoint();
198
199 /** set the starting point to x_init */
200 void setxInit(Ipopt::Index n,const Ipopt::Number* x_init);
201
202 /** set the dual starting point to duals_init */
203 void setDualsInit(Ipopt::Index n, const Ipopt::Number* duals_init);
204
205 /** xInit has been set?
206 * \return 0 if not, 1 if only primal 2 if primal dual.*/
207 int has_x_init(){
208 if(x_init_.empty()) return 0;
209 if(duals_init_) return 2;
210 return 1;
211 }
212 /** Set the contiuous solution */
213 void Set_x_sol(Ipopt::Index n, const Ipopt::Number* x_sol);
214
215 /** Set the contiuous dual solution */
216 void Set_dual_sol(Ipopt::Index n, const Ipopt::Number* dual_sol);
217
218 /** Change the type of the variable */
219 void SetVariableType(Ipopt::Index n, TMINLP::VariableType type);
220 //@}
221 /** Procedure to ouptut relevant informations to reproduce a sub-problem.
222 Compare the current problem to the problem to solve
223 and writes files with bounds which have changed and current starting point.
224 */
225 void outputDiffs(const std::string& probName, const std::string* varNames);
226
227 /**@name methods to gather information about the NLP */
228 //@{
229 /** This call is just passed onto the TMINLP object */
230 virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g,
231 Ipopt::Index& nnz_h_lag,
232 TNLP::IndexStyleEnum& index_style);
233
234 /** The caller is allowed to modify the bounds, so this
235 * method returns the internal bounds information
236 */
237 virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u,
238 Ipopt::Index m, Ipopt::Number* g_l, Ipopt::Number* g_u);
239
240 /** Returns the constraint linearity.
241 * array should be alocated with length at least m..*/
242 virtual bool get_constraints_linearity(Ipopt::Index m, LinearityType* const_types)
243 {
244 return tminlp_->get_constraints_linearity(m, const_types);
245 }
246
247 /** Returns the variables linearity.
248 * array should be alocated with length at least n..*/
249 virtual bool get_variables_linearity(Ipopt::Index n, LinearityType* var_types)
250 {
251 return tminlp_->get_variables_linearity(n, var_types);
252 }
253
254 /** returns true if objective is linear.*/
255 virtual bool hasLinearObjective(){return tminlp_->hasLinearObjective();}
256 /** Method called by Ipopt to get the starting point. The bools
257 * init_x and init_lambda are both inputs and outputs. As inputs,
258 * they indicate whether or not the algorithm wants you to
259 * initialize x and lambda respectively. If, for some reason, the
260 * algorithm wants you to initialize these and you cannot, set
261 * the respective bool to false.
262 */
263 virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x,
264 bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U,
265 Ipopt::Index m, bool init_lambda,
266 Ipopt::Number* lambda);
267
268 /** Method that returns scaling parameters.
269 */
270 virtual bool get_scaling_parameters(Ipopt::Number& obj_scaling,
271 bool& use_x_scaling, Ipopt::Index n,
272 Ipopt::Number* x_scaling,
273 bool& use_g_scaling, Ipopt::Index m,
274 Ipopt::Number* g_scaling);
275
276
277 /** Methat that returns an Ipopt IteratesVector that has the
278 * starting point for all internal varibles. */
279 virtual bool get_warm_start_iterate(Ipopt::IteratesVector& warm_start_iterate);
280
281 /** Returns the value of the objective function in x*/
282 virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
283 Ipopt::Number& obj_value);
284
285 /** Returns the vector of the gradient of
286 * the objective w.r.t. x */
287 virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
288 Ipopt::Number* grad_f);
289
290 /** Returns the vector of constraint values in x*/
291 virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
292 Ipopt::Index m, Ipopt::Number* g);
293
294 /** Returns the jacobian of the
295 * constraints. The vectors iRow and jCol only need to be set
296 * once. The first call is used to set the structure only (iRow
297 * and jCol will be non-NULL, and values will be NULL) For
298 * subsequent calls, iRow and jCol will be NULL. */
299 virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
300 Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index* iRow,
301 Ipopt::Index *jCol, Ipopt::Number* values);
302
303 /** compute the value of a single constraint */
304 virtual bool eval_gi(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
305 Ipopt::Index i, Ipopt::Number& gi);
306 /** compute the structure or values of the gradient for one
307 constraint */
308 virtual bool eval_grad_gi(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
309 Ipopt::Index i, Ipopt::Index& nele_grad_gi, Ipopt::Index* jCol,
310 Ipopt::Number* values);
311
312 /** Return the hessian of the
313 * lagrangian. The vectors iRow and jCol only need to be set once
314 * (during the first call). The first call is used to set the
315 * structure only (iRow and jCol will be non-NULL, and values
316 * will be NULL) For subsequent calls, iRow and jCol will be
317 * NULL. This matrix is symmetric - specify the lower diagonal
318 * only */
319 virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
320 Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda,
321 bool new_lambda, Ipopt::Index nele_hess,
322 Ipopt::Index* iRow, Ipopt::Index* jCol, Ipopt::Number* values);
323 //@}
324
325 /** @name Solution Methods */
326 //@{
327 /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
328 virtual void finalize_solution(Ipopt::SolverReturn status,
329 Ipopt::Index n, const Ipopt::Number* x, const Ipopt::Number* z_L, const Ipopt::Number* z_U,
330 Ipopt::Index m, const Ipopt::Number* g, const Ipopt::Number* lambda,
331 Ipopt::Number obj_value,
332 const Ipopt::IpoptData* ip_data,
333 Ipopt::IpoptCalculatedQuantities* ip_cq);
334 /** Intermediate Callback method for the user. Providing dummy
335 * default implementation. For details see IntermediateCallBack
336 * in IpNLP.hpp. */
337 virtual bool intermediate_callback(Ipopt::AlgorithmMode mode,
338 Ipopt::Index iter, Ipopt::Number obj_value,
339 Ipopt::Number inf_pr, Ipopt::Number inf_du,
340 Ipopt::Number mu, Ipopt::Number d_norm,
341 Ipopt::Number regularization_size,
342 Ipopt::Number alpha_du, Ipopt::Number alpha_pr,
343 Ipopt::Index ls_trials,
344 const Ipopt::IpoptData* ip_data,
345 Ipopt::IpoptCalculatedQuantities* ip_cq);
346 //@}
347
348 /** Method called to check wether a problem has still some variable not fixed. If there are no more
349 unfixed vars, checks wether the solution given by the bounds is feasible.*/
350
351 /** @name Methods for setting and getting the warm starter */
352 //@{
353 void SetWarmStarter(Ipopt::SmartPtr<IpoptInteriorWarmStarter> warm_starter);
354
355 Ipopt::SmartPtr<IpoptInteriorWarmStarter> GetWarmStarter();
356
357 //@}
358
359 /** Say if has a specific function to compute upper bounds*/
360 virtual bool hasUpperBoundingObjective(){
361 return tminlp_->hasUpperBoundingObjective();}
362
363 /** Evaluate the upper bounding function at given point and store the result.*/
364 double evaluateUpperBoundingFunction(const double * x);
365
366 /** \name Cuts management. */
367 /** Methods are not implemented at this point. But I need the interface.*/
368 //@{
369
370
371 /** Add some linear cuts to the problem formulation (not implemented yet in base class).*/
372 virtual void addCuts(unsigned int numberCuts, const OsiRowCut ** cuts){
373 if(numberCuts > 0)
374 throw CoinError("BonTMINLP2TNLP", "addCuts", "Not implemented");}
375
376
377 /** Add some cuts to the problem formulaiton (handles Quadratics).*/
378 virtual void addCuts(const OsiCuts &cuts){
379 if(cuts.sizeRowCuts() > 0 || cuts.sizeColCuts() > 0)
380 throw CoinError("BonTMINLP2TNLP", "addCuts", "Not implemented");}
381
382 /** Remove some cuts to the formulation */
383 virtual void removeCuts(unsigned int number ,const int * toRemove){
384 if(number > 0)
385 throw CoinError("BonTMINLP2TNLP", "removeCuts", "Not implemented");}
386
387 //@}
388
389
390 /** Access array describing constraint to which perspectives should be applied.*/
391 virtual const int * get_const_xtra_id() const{
392 return tminlp_->get_const_xtra_id();
393 }
394
395 /** Round and check the current solution, return norm inf of constraint violation.*/
396 double check_solution(OsiObject ** objects = 0, int nObjects = -1);
397 protected:
398 /** \name These should be modified in derived class to always maintain there correctness.
399 They are directly queried by OsiTMINLPInterface without virtual function for
400 speed.*/
401 /** @{ */
402 /// Types of the variable (TMINLP::CONTINUOUS, TMINLP::INTEGER, TMINLP::BINARY).
403 vector<TMINLP::VariableType> var_types_;
404 /// Current lower bounds on variables
405 vector<Ipopt::Number> x_l_;
406 /// Current upper bounds on variables
407 vector<Ipopt::Number> x_u_;
408 /// Original lower bounds on variables
409 vector<Ipopt::Number> orig_x_l_;
410 /// Original upper bounds on variables
411 vector<Ipopt::Number> orig_x_u_;
412 /// Lower bounds on constraints values
413 vector<Ipopt::Number> g_l_;
414 /// Upper bounds on constraints values
415 vector<Ipopt::Number> g_u_;
416 /// Initial primal point
417 vector<Ipopt::Number> x_init_;
418 /** Initial values for all dual multipliers (constraints then lower bounds then upper bounds) */
419 Ipopt::Number * duals_init_;
420 /// User-provideed initial prmal point
421 vector<Ipopt::Number> x_init_user_;
422 /// Optimal solution
423 vector<Ipopt::Number> x_sol_;
424 /// Activities of constraint g( x_sol_)
425 vector<Ipopt::Number> g_sol_;
426 /** Dual multipliers of constraints and bounds*/
427 vector<Ipopt::Number> duals_sol_;
428 /** @} */
429
430 /** Access number of entries in tminlp_ hessian*/
431 Ipopt::Index nnz_h_lag() const{
432 return nnz_h_lag_;}
433 /** Access number of entries in tminlp_ hessian*/
434 Ipopt::Index nnz_jac_g() const{
435 return nnz_jac_g_;}
436
437 /** Acces index_style.*/
438 TNLP::IndexStyleEnum index_style() const{
439 return index_style_;}
440 private:
441 /**@name Default Compiler Generated Methods
442 * (Hidden to avoid implicit creation/calling).
443 * These methods are not implemented and
444 * we do not want the compiler to implement
445 * them for us, so we declare them private
446 * and do not define them. This ensures that
447 * they will not be implicitly created/called. */
448 //@{
449 /** Default Constructor */
450 TMINLP2TNLP();
451
452 /** Overloaded Equals Operator */
453 TMINLP2TNLP& operator=(const TMINLP2TNLP&);
454 //@}
455
456 /** pointer to the tminlp that is being adapted */
457 Ipopt::SmartPtr<TMINLP> tminlp_;
458
459 /** @name Internal copies of data allowing caller to modify the MINLP */
460 //@{
461 /// Number of non-zeroes in the constraints jacobian.
462 Ipopt::Index nnz_jac_g_;
463 /// Number of non-zeroes in the lagrangian hessian
464 Ipopt::Index nnz_h_lag_;
465 /**index style (fortran or C)*/
466 TNLP::IndexStyleEnum index_style_;
467
468 /** Return status of the optimization process*/
469 Ipopt::SolverReturn return_status_;
470 /** Value of the optimal solution found by Ipopt */
471 Ipopt::Number obj_value_;
472 //@}
473
474 /** @name Warmstart object and related data */
475 //@{
476 /** Pointer to object that holds warmstart information */
477 Ipopt::SmartPtr<IpoptInteriorWarmStarter> curr_warm_starter_;
478 /** Value for a lower bound that denotes -infinity */
479 Ipopt::Number nlp_lower_bound_inf_;
480 /** Value for a upper bound that denotes infinity */
481 Ipopt::Number nlp_upper_bound_inf_;
482 /** Option from Ipopt - we currently use it to see if we want to
483 * use some clever warm start or just the last iterate from the
484 * previous run */
485 bool warm_start_entire_iterate_;
486 /** Do we need a new warm starter object */
487 bool need_new_warm_starter_;
488 //@}
489
490
491 /** Private method that throws an exception if the variable bounds
492 * are not consistent with the variable type */
493 void throw_exception_on_bad_variable_bound(Ipopt::Index i);
494
495 private:
496 // Delete all arrays
497 void gutsOfDelete();
498
499 /** Copies all the arrays.
500 \warning this and other should be two instances of the same problem
501 \warning AW: I am trying to mimic a copy construction for Cbc
502 use with great care not safe.
503 */
504 void gutsOfCopy(const TMINLP2TNLP &source);
505 };
506
507 } // namespace Ipopt
508
509 #endif