# source:trunk/Bonmin/src/IpoptInterface/TMINLP.hpp@1

Last change on this file since 1 was 1, checked in by andreasw, 13 years ago

imported initial code

• Property svn:eol-style set to native
• Property svn:keywords set to "Author Date Id Revision"
File size: 10.6 KB
Line
1// (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2004
3// This code is published under the Common 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 __TMINLP_HPP__
13#define __TMINLP_HPP__
14
15#include "IpUtils.hpp"
16#include "IpReferenced.hpp"
17#include "IpException.hpp"
18#include "IpAlgTypes.hpp"
19#include "CoinPackedMatrix.hpp"
20#include "OsiCuts.hpp"
21#include "IpTNLP.hpp"
22
23#include "CoinHelperFunctions.hpp"
24namespace Ipopt
25{
26
27  /** Base class for all MINLPs that use a standard triplet matrix form
28   *  and dense vectors.
29   *  The class TMINLP2TNLP allows the caller to produce a viable TNLP
30   *  from the MINLP (by relaxing binary and/or integers, or by
31   *  fixing them), which can then be solved by Ipopt.
32   *
33   *  This interface presents the problem form:
34   *  \f[
35   *  \begin{array}{rl}
36   *     &min f(x)\\
37   *
38   *     \mbox{s.t.}&\\
39   *      &   g^L <= g(x) <= g^U\\
40   *
41   *       &   x^L <=  x   <= x^U\\
42   *   \end{array}
43   *  \f]
44   *  Where each x_i is either a continuous, binary, or integer variable.
45   *  If x_i is binary, the bounds [xL,xU] are assumed to be [0,1].
46   *  In order to specify an equality constraint, set gL_i = gU_i =
47   *  rhs.  The value that indicates "infinity" for the bounds
48   *  (i.e. the variable or constraint has no lower bound (-infinity)
49   *  or upper bound (+infinity)) is set through the option
50   *  nlp_lower_bound_inf and nlp_upper_bound_inf.  To indicate that a
51   *  variable has no upper or lower bound, set the bound to
52   *  -ipopt_inf or +ipopt_inf respectively
53   */
54  class TMINLP : public ReferencedObject
55  {
56  public:
57    /**@name Constructors/Destructors */
58    //@{
59    TMINLP()
60    {}
61    ;
62
63    /** Default destructor */
64    virtual ~TMINLP()
65    {}
66    ;
67    //@}
68
69    /** Class to store sos constraints for model */
70    struct SosInfo
71    {
72      /** Number of SOS constraints.*/
73      int num;
74      /** Type of sos. At present Only type '1' SOS are supported by Cbc*/
75      char * types;
76      /** priorities of sos constraints.*/
77      int * priorities;
78
79      /** \name Sparse storage of the elements of the SOS constraints.*/
80      /** @{ */
81      /** Total number of non zeroes in SOS constraints.*/
82      int numNz;
83      /** For 0 <= i < nums, start[i] gives the indice of indices and weights arrays at which the description of constraints i begins..*/
84      int * starts;
85      /** indices of elements belonging to the SOS.*/
86      int * indices;
87      /** weights of the elements of the SOS.*/
88      double * weights;
89      /** @} */
90      /** default constructor. */
91      SosInfo():num(0), types(NULL), priorities(NULL), numNz(0), starts(NULL),
92             indices(NULL), weights(NULL)
93      {}
94      /** Copy constructor.*/
95      SosInfo(const SosInfo & source):num(source.num), types(NULL), priorities(NULL),
96          numNz(source.numNz), starts(NULL),indices(NULL),
97           weights(NULL)
98      {
99
100        if(num > 0) {
101          assert(source.types!=NULL);
102          assert(source.priorities!=NULL);
103          assert(source.starts!=NULL);
104          assert(source.indices!=NULL);
105          assert(source.weights!=NULL);
106          types = new char[num];
107          priorities = new int[num];
108          starts = new int[num + 1];
109          indices = new int[numNz];
110          weights = new double[numNz];
111          for(int i = 0 ; i < num ; i++) {
112            source.types[i] = types[i];
113            source.priorities[i] = priorities[i];
114            source.starts[i] = starts[i];
115          }
116          for(int i = 0 ; i < numNz ; i++) {
117            source.indices[i] = indices[i];
118            source.weights[i] = weights[i];
119          }
120        }
121        else {
122          assert(source.types==NULL);
123          assert(source.priorities==NULL);
124          assert(source.starts==NULL);
125          assert(source.indices==NULL);
126          assert(source.weights==NULL);
127        }
128
129      }
130
131      /** destructor*/
132      ~SosInfo()
133      {
134        gutsOfDestructor();
135      }
136
137
138      /** Reset information */
139      void gutsOfDestructor()
140      {
141        num = 0;
142        numNz = 0;
143        if(types) delete [] types;
144        types = NULL;
145        if(starts) delete [] starts;
146        starts = NULL;
147        if(indices) delete [] indices;
148        indices = NULL;
149        if(priorities) delete [] priorities;
150        priorities = NULL;
151        if(weights) delete [] weights;
152        weights = NULL;
153      }
154    };
155
156    /** Stores branching priorities information. */
157    struct BranchingInfo
158    {
159      /**number of variables*/
160      int size;
161      /** User set priorities on variables. */
162      int * priorities;
163      /** User set preferered branching direction. */
164      int * branchingDirections;
165      /** User set up pseudo costs.*/
166      double * upPsCosts;
167      /** User set down pseudo costs.*/
168      double * downPsCosts;
169      BranchingInfo():
170      size(0),
171      priorities(NULL),
172      branchingDirections(NULL),
173      upPsCosts(NULL),
174      downPsCosts(NULL)
175      {}
176      BranchingInfo(const BranchingInfo &other)
177      {
178        gutsOfDestructor();
179        size = other.size;
180        priorities = CoinCopyOfArray(other.priorities, size);
181        branchingDirections = CoinCopyOfArray(other.branchingDirections, size);
182        upPsCosts = CoinCopyOfArray(other.upPsCosts, size);
183        downPsCosts = CoinCopyOfArray(other.downPsCosts, size);
184      }
185      void gutsOfDestructor()
186      {
187      if (priorities != NULL) delete [] priorities;
188      priorities = NULL;
189      if (branchingDirections != NULL) delete [] branchingDirections;
190      branchingDirections = NULL;
191      if (upPsCosts != NULL) delete [] upPsCosts;
192      upPsCosts = NULL;
193      if (downPsCosts != NULL) delete [] downPsCosts;
194      downPsCosts = NULL;
195      }
196    };
197    /** Type of the variables.*/
198    enum VariableType
199    {
200      CONTINUOUS,
201      BINARY,
202      INTEGER
203    };
204
205    /** Type of the constraints*/
206    enum ConstraintType
207    {
208      LINEAR/** Constraint contains only linear terms.*/,
209      NON_LINEAR/**Constraint contains some non-linear terms.*/
210    };
211
212    /**@name methods to gather information about the MINLP */
213    //@{
214    /** overload this method to return the number of variables
215     *  and constraints, and the number of non-zeros in the jacobian and
216     *  the hessian. */
217    virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
218        Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style)=0;
219
220    /** overload this method to set the variable type. The var_types
221     *  array will be allocated with length n. */
222    virtual bool get_var_types(Index n, VariableType* var_types)=0;
223
224    /** overload this method to set the constraint types (linear or not)*/
225    virtual bool get_constraints_types(Index m, ConstraintType* const_types)=0;
226
227    /** overload this method to return the information about the bound
228     *  on the variables and constraints. The value that indicates
229     *  that a bound does not exist is specified in the parameters
230     *  nlp_lower_bound_inf and nlp_upper_bound_inf.  By default,
231     *  nlp_lower_bound_inf is -1e19 and nlp_upper_bound_inf is
232     *  1e19.
233     *  An exception will be thrown if x_l and x_u are not 0,1 for binary variables
234     */
235    virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
236        Index m, Number* g_l, Number* g_u)=0;
237
238    /** overload this method to return the starting point. The bools
239     *  init_x and init_lambda are both inputs and outputs. As inputs,
240     *  they indicate whether or not the algorithm wants you to
241     *  initialize x and lambda respectively. If, for some reason, the
242     *  algorithm wants you to initialize these and you cannot, set
243     *  the respective bool to false.
244     */
245    virtual bool get_starting_point(Index n, bool init_x, Number* x,
246        Index m, bool init_lambda,
247        Number* lambda)=0;
248
249    /** overload this method to return the value of the objective function */
250    virtual bool eval_f(Index n, const Number* x, bool new_x,
251        Number& obj_value)=0;
252
253    /** overload this method to return the vector of the gradient of
254     *  the objective w.r.t. x */
255    virtual bool eval_grad_f(Index n, const Number* x, bool new_x,
257
258    /** overload this method to return the vector of constraint values */
259    virtual bool eval_g(Index n, const Number* x, bool new_x,
260        Index m, Number* g)=0;
261
262    /** overload this method to return the jacobian of the
263     *  constraints. The vectors iRow and jCol only need to be set
264     *  once. The first call is used to set the structure only (iRow
265     *  and jCol will be non-NULL, and values will be NULL) For
266     *  subsequent calls, iRow and jCol will be NULL. */
267    virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
268        Index m, Index nele_jac, Index* iRow,
269        Index *jCol, Number* values)=0;
270
271    /** overload this method to return the hessian of the
272     *  lagrangian. The vectors iRow and jCol only need to be set once
273     *  (during the first call). The first call is used to set the
274     *  structure only (iRow and jCol will be non-NULL, and values
275     *  will be NULL) For subsequent calls, iRow and jCol will be
276     *  NULL. This matrix is symmetric - specify the lower diagonal
277     *  only */
278    virtual bool eval_h(Index n, const Number* x, bool new_x,
279        Number obj_factor, Index m, const Number* lambda,
280        bool new_lambda, Index nele_hess,
281        Index* iRow, Index* jCol, Number* values)=0;
282    //@}
283
284    /** @name Solution Methods */
285    //@{
286    /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
287    virtual void finalize_solution(SolverReturn status,
288        Index n, const Number* x, const Number* z_L, const Number* z_U,
289        Index m, const Number* g, const Number* lambda,
290        Number obj_value)=0;
291    //@}
292
293    virtual const BranchingInfo * branchingInfo() const = 0;
294
295    virtual const SosInfo * sosConstraints() const = 0;
296  private:
297    /**@name Default Compiler Generated Methods
298     * (Hidden to avoid implicit creation/calling).
299     * These methods are not implemented and
300     * we do not want the compiler to implement
301     * them for us, so we declare them private
302     * and do not define them. This ensures that
303     * they will not be implicitly created/called. */
304    //@{
305    /** Default Constructor */
306    //TMINLP();
307
308    /** Copy Constructor */
309    TMINLP(const TMINLP&);
310
311    /** Overloaded Equals Operator */
312    void operator=(const TMINLP&);
313    //@}
314
315
316  };
317
318} // namespace Ipopt
319
320#endif
Note: See TracBrowser for help on using the repository browser.