| 1 |
// (C) Copyright Carnegie Mellon University 2006 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// P. Bonami, Carnegie Mellon University |
| 7 |
// |
| 8 |
// Date : 03/17/2006 |
| 9 |
#ifndef MyTNLP_HPP |
| 10 |
#define MyTNLP_HPP |
| 11 |
#include "BonTMINLP.hpp" |
| 12 |
using namespace Ipopt; |
| 13 |
using namespace Bonmin; |
| 14 |
/** A C++ example for interfacing an MINLP with bonmin. |
| 15 |
* This class implements the following NLP : |
| 16 |
* \f[ |
| 17 |
\begin{array}{l} |
| 18 |
\min x_0 - x_1 - x_2 \\ |
| 19 |
\mbox{s.t}\\ |
| 20 |
(x_1 - \frac{1}{2})^2 + (x_2 - \frac{1}{2})^2 \leq \frac{1}{4} \\ |
| 21 |
x_0 - x_1 \leq 0 \\ |
| 22 |
x_1 + x_2 + x_3 \leq 2\\ |
| 23 |
x_0 \in \{0,1\}^n \; (x_1, x_2) \in R^2 \; x_3 \in N |
| 24 |
\end{array} |
| 25 |
\f] |
| 26 |
*/ |
| 27 |
|
| 28 |
class MyTMINLP : public TMINLP |
| 29 |
{ |
| 30 |
public: |
| 31 |
/// Default constructor. |
| 32 |
MyTMINLP(): |
| 33 |
printSol_(false){} |
| 34 |
|
| 35 |
/// virtual destructor. |
| 36 |
virtual ~MyTMINLP(){} |
| 37 |
|
| 38 |
|
| 39 |
/** Copy constructor.*/ |
| 40 |
MyTMINLP(const MyTMINLP &other): |
| 41 |
printSol_(other.printSol_){} |
| 42 |
/** Assignment operator. no data = nothing to assign*/ |
| 43 |
//MyTMINLP& operator=(const MyTMINLP&) {} |
| 44 |
|
| 45 |
|
| 46 |
/** \name Overloaded functions specific to a TMINLP.*/ |
| 47 |
//@{ |
| 48 |
/** Pass the type of the variables (INTEGER, BINARY, CONTINUOUS) to the optimizer. |
| 49 |
\param n size of var_types (has to be equal to the number of variables in the problem) |
| 50 |
\param var_types types of the variables (has to be filled by function). |
| 51 |
*/ |
| 52 |
virtual bool get_variables_types(Index n, VariableType* var_types); |
| 53 |
|
| 54 |
/** Pass info about linear and nonlinear variables.*/ |
| 55 |
virtual bool get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types); |
| 56 |
|
| 57 |
/** Pass the type of the constraints (LINEAR, NON_LINEAR) to the optimizer. |
| 58 |
\param m size of const_types (has to be equal to the number of constraints in the problem) |
| 59 |
\param const_types types of the constraints (has to be filled by function). |
| 60 |
*/ |
| 61 |
virtual bool get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types); |
| 62 |
//@} |
| 63 |
|
| 64 |
/** \name Overloaded functions defining a TNLP. |
| 65 |
* This group of function implement the various elements needed to define and solve a TNLP. |
| 66 |
* They are the same as those in a standard Ipopt NLP problem*/ |
| 67 |
//@{ |
| 68 |
/** Method to pass the main dimensions of the problem to Ipopt. |
| 69 |
\param n number of variables in problem. |
| 70 |
\param m number of constraints. |
| 71 |
\param nnz_jac_g number of nonzeroes in Jacobian of constraints system. |
| 72 |
\param nnz_h_lag number of nonzeroes in Hessian of the Lagrangean. |
| 73 |
\param index_style indicate wether arrays are numbered from 0 (C-style) or |
| 74 |
from 1 (Fortran). |
| 75 |
\return true in case of success.*/ |
| 76 |
virtual bool get_nlp_info(Index& n, Index&m, Index& nnz_jac_g, |
| 77 |
Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style); |
| 78 |
|
| 79 |
/** Method to pass the bounds on variables and constraints to Ipopt. |
| 80 |
\param n size of x_l and x_u (has to be equal to the number of variables in the problem) |
| 81 |
\param x_l lower bounds on variables (function should fill it). |
| 82 |
\param x_u upper bounds on the variables (function should fill it). |
| 83 |
\param m size of g_l and g_u (has to be equal to the number of constraints in the problem). |
| 84 |
\param g_l lower bounds of the constraints (function should fill it). |
| 85 |
\param g_u upper bounds of the constraints (function should fill it). |
| 86 |
\return true in case of success.*/ |
| 87 |
virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u, |
| 88 |
Index m, Number* g_l, Number* g_u); |
| 89 |
|
| 90 |
/** Method to to pass the starting point for optimization to Ipopt. |
| 91 |
\param init_x do we initialize primals? |
| 92 |
\param x pass starting primal points (function should fill it if init_x is 1). |
| 93 |
\param m size of lambda (has to be equal to the number of constraints in the problem). |
| 94 |
\param init_lambda do we initialize duals of constraints? |
| 95 |
\param lambda lower bounds of the constraints (function should fill it). |
| 96 |
\return true in case of success.*/ |
| 97 |
virtual bool get_starting_point(Index n, bool init_x, Number* x, |
| 98 |
bool init_z, Number* z_L, Number* z_U, |
| 99 |
Index m, bool init_lambda, |
| 100 |
Number* lambda); |
| 101 |
|
| 102 |
/** Method which compute the value of the objective function at point x. |
| 103 |
\param n size of array x (has to be the number of variables in the problem). |
| 104 |
\param x point where to evaluate. |
| 105 |
\param new_x Is this the first time we evaluate functions at this point? |
| 106 |
(in the present context we don't care). |
| 107 |
\param obj_value value of objective in x (has to be computed by the function). |
| 108 |
\return true in case of success.*/ |
| 109 |
virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value); |
| 110 |
|
| 111 |
/** Method which compute the gradient of the objective at a point x. |
| 112 |
\param n size of array x (has to be the number of variables in the problem). |
| 113 |
\param x point where to evaluate. |
| 114 |
\param new_x Is this the first time we evaluate functions at this point? |
| 115 |
(in the present context we don't care). |
| 116 |
\param grad_f gradient of objective taken in x (function has to fill it). |
| 117 |
\return true in case of success.*/ |
| 118 |
virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f); |
| 119 |
|
| 120 |
/** Method which compute the value of the functions defining the constraints at a point |
| 121 |
x. |
| 122 |
\param n size of array x (has to be the number of variables in the problem). |
| 123 |
\param x point where to evaluate. |
| 124 |
\param new_x Is this the first time we evaluate functions at this point? |
| 125 |
(in the present context we don't care). |
| 126 |
\param m size of array g (has to be equal to the number of constraints in the problem) |
| 127 |
\param grad_f values of the constraints (function has to fill it). |
| 128 |
\return true in case of success.*/ |
| 129 |
virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g); |
| 130 |
|
| 131 |
/** Method to compute the Jacobian of the functions defining the constraints. |
| 132 |
If the parameter values==NULL fill the arrays iCol and jRow which store the position of |
| 133 |
the non-zero element of the Jacobian. |
| 134 |
If the paramenter values!=NULL fill values with the non-zero elements of the Jacobian. |
| 135 |
\param n size of array x (has to be the number of variables in the problem). |
| 136 |
\param x point where to evaluate. |
| 137 |
\param new_x Is this the first time we evaluate functions at this point? |
| 138 |
(in the present context we don't care). |
| 139 |
\param m size of array g (has to be equal to the number of constraints in the problem) |
| 140 |
\param grad_f values of the constraints (function has to fill it). |
| 141 |
\return true in case of success.*/ |
| 142 |
virtual bool eval_jac_g(Index n, const Number* x, bool new_x, |
| 143 |
Index m, Index nele_jac, Index* iRow, Index *jCol, |
| 144 |
Number* values); |
| 145 |
|
| 146 |
/** Method to compute the Jacobian of the functions defining the constraints. |
| 147 |
If the parameter values==NULL fill the arrays iCol and jRow which store the position of |
| 148 |
the non-zero element of the Jacobian. |
| 149 |
If the paramenter values!=NULL fill values with the non-zero elements of the Jacobian. |
| 150 |
\param n size of array x (has to be the number of variables in the problem). |
| 151 |
\param x point where to evaluate. |
| 152 |
\param new_x Is this the first time we evaluate functions at this point? |
| 153 |
(in the present context we don't care). |
| 154 |
\param m size of array g (has to be equal to the number of constraints in the problem) |
| 155 |
\param grad_f values of the constraints (function has to fill it). |
| 156 |
\return true in case of success.*/ |
| 157 |
virtual bool eval_h(Index n, const Number* x, bool new_x, |
| 158 |
Number obj_factor, Index m, const Number* lambda, |
| 159 |
bool new_lambda, Index nele_hess, Index* iRow, |
| 160 |
Index* jCol, Number* values); |
| 161 |
|
| 162 |
|
| 163 |
/** Method called by Ipopt at the end of optimization.*/ |
| 164 |
virtual void finalize_solution(TMINLP::SolverReturn status, |
| 165 |
Index n, const Number* x, Number obj_value); |
| 166 |
|
| 167 |
//@} |
| 168 |
|
| 169 |
virtual const SosInfo * sosConstraints() const{return NULL;} |
| 170 |
virtual const BranchingInfo* branchingInfo() const{return NULL;} |
| 171 |
|
| 172 |
|
| 173 |
void printSolutionAtEndOfAlgorithm(){ |
| 174 |
printSol_ = true;} |
| 175 |
|
| 176 |
private: |
| 177 |
bool printSol_; |
| 178 |
}; |
| 179 |
|
| 180 |
#endif |