source: branches/devel/Bonmin/src/IpoptInterface/TMINLP.hpp @ 44

Last change on this file since 44 was 44, checked in by pbonami, 13 years ago

Can add linear cut to nonlinear formulation in Ipopt

  • Property svn:eol-style set to native
  • Property svn:keywords set to "Author Date Id Revision"
File size: 10.0 KB
Line 
1// (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2004
2// All Rights Reserved.
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    friend class TMINLP2TNLP;
58
59    /** Class to store sos constraints for model */
60    struct SosInfo
61    {
62      /** Number of SOS constraints.*/
63      int num;
64      /** Type of sos. At present Only type '1' SOS are supported by Cbc*/
65      char * types;
66      /** priorities of sos constraints.*/
67      int * priorities;
68     
69      /** \name Sparse storage of the elements of the SOS constraints.*/
70      /** @{ */
71      /** Total number of non zeroes in SOS constraints.*/
72      int numNz;
73      /** For 0 <= i < nums, start[i] gives the indice of indices and weights arrays at which the description of constraints i begins..*/ 
74      int * starts;
75      /** indices of elements belonging to the SOS.*/
76      int * indices;
77      /** weights of the elements of the SOS.*/
78      double * weights;
79      /** @} */
80      /** default constructor. */
81      SosInfo();
82      /** Copy constructor.*/
83      SosInfo(const SosInfo & source);
84     
85
86      /** destructor*/
87      ~SosInfo()
88      {
89        gutsOfDestructor();
90      }
91
92
93      /** Reset information */
94      void gutsOfDestructor();
95
96    };
97
98    /** Stores branching priorities information. */
99    struct BranchingInfo
100    {
101      /**number of variables*/
102      int size;
103      /** User set priorities on variables. */
104      int * priorities;
105      /** User set preferered branching direction. */
106      int * branchingDirections;
107      /** User set up pseudo costs.*/
108      double * upPsCosts;
109      /** User set down pseudo costs.*/
110      double * downPsCosts;
111      BranchingInfo():
112      size(0),
113      priorities(NULL),
114      branchingDirections(NULL),
115      upPsCosts(NULL),
116      downPsCosts(NULL)
117      {}
118      BranchingInfo(const BranchingInfo &other)
119      {
120        gutsOfDestructor();
121        size = other.size;
122        priorities = CoinCopyOfArray(other.priorities, size);
123        branchingDirections = CoinCopyOfArray(other.branchingDirections, size);
124        upPsCosts = CoinCopyOfArray(other.upPsCosts, size);
125        downPsCosts = CoinCopyOfArray(other.downPsCosts, size);
126      }
127      void gutsOfDestructor()
128      {
129      if (priorities != NULL) delete [] priorities;
130      priorities = NULL;
131      if (branchingDirections != NULL) delete [] branchingDirections; 
132      branchingDirections = NULL;
133      if (upPsCosts != NULL) delete [] upPsCosts;
134      upPsCosts = NULL;
135      if (downPsCosts != NULL) delete [] downPsCosts;
136      downPsCosts = NULL;
137      } 
138    };
139    /** Type of the variables.*/
140    enum VariableType
141    {
142      CONTINUOUS,
143      BINARY,
144      INTEGER
145    };
146
147    /** Type of the constraints*/
148    enum ConstraintType
149    {
150      LINEAR/** Constraint contains only linear terms.*/,
151      NON_LINEAR/**Constraint contains some non-linear terms.*/
152    };
153
154    /**@name Constructors/Destructors */
155    //@{
156    TMINLP();
157
158    /** Default destructor */
159    virtual ~TMINLP()
160    {}
161    //@}
162;
163
164    /**@name methods to gather information about the MINLP */
165    //@{
166    /** overload this method to return the number of variables
167     *  and constraints, and the number of non-zeros in the jacobian and
168     *  the hessian. */
169    virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
170        Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style)=0;
171
172    /** overload this method to set the variable type. The var_types
173     *  array will be allocated with length n. */
174    virtual bool get_var_types(Index n, VariableType* var_types)=0;
175
176    /** overload this method to set the constraint types (linear or not)*/
177    virtual bool get_constraints_types(Index m, ConstraintType* const_types)=0;
178
179    /** overload this method to return the information about the bound
180     *  on the variables and constraints. The value that indicates
181     *  that a bound does not exist is specified in the parameters
182     *  nlp_lower_bound_inf and nlp_upper_bound_inf.  By default,
183     *  nlp_lower_bound_inf is -1e19 and nlp_upper_bound_inf is
184     *  1e19.
185     *  An exception will be thrown if x_l and x_u are not 0,1 for binary variables
186     */
187    virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
188        Index m, Number* g_l, Number* g_u)=0;
189
190    /** overload this method to return the starting point. The bools
191     *  init_x and init_lambda are both inputs and outputs. As inputs,
192     *  they indicate whether or not the algorithm wants you to
193     *  initialize x and lambda respectively. If, for some reason, the
194     *  algorithm wants you to initialize these and you cannot, set
195     *  the respective bool to false.
196     */
197    virtual bool get_starting_point(Index n, bool init_x, Number* x,
198                                    bool init_z, Number* z_L, Number* z_U,
199        Index m, bool init_lambda,
200        Number* lambda)=0;
201
202    /** overload this method to return the value of the objective function */
203    virtual bool eval_f(Index n, const Number* x, bool new_x,
204        Number& obj_value)=0;
205
206    /** overload this method to return the vector of the gradient of
207     *  the objective w.r.t. x */
208    virtual bool eval_grad_f(Index n, const Number* x, bool new_x,
209        Number* grad_f)=0;
210
211    /** overload this method to return the vector of constraint values */
212    virtual bool eval_g(Index n, const Number* x, bool new_x,
213        Index m, Number* g)=0;
214
215    /** overload this method to return the jacobian of the
216     *  constraints. The vectors iRow and jCol only need to be set
217     *  once. The first call is used to set the structure only (iRow
218     *  and jCol will be non-NULL, and values will be NULL) For
219     *  subsequent calls, iRow and jCol will be NULL. */
220    virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
221        Index m, Index nele_jac, Index* iRow,
222        Index *jCol, Number* values)=0;
223
224    /** overload this method to return the hessian of the
225     *  lagrangian. The vectors iRow and jCol only need to be set once
226     *  (during the first call). The first call is used to set the
227     *  structure only (iRow and jCol will be non-NULL, and values
228     *  will be NULL) For subsequent calls, iRow and jCol will be
229     *  NULL. This matrix is symmetric - specify the lower diagonal
230     *  only */
231    virtual bool eval_h(Index n, const Number* x, bool new_x,
232        Number obj_factor, Index m, const Number* lambda,
233        bool new_lambda, Index nele_hess,
234        Index* iRow, Index* jCol, Number* values)=0;
235    //@}
236
237    /** @name Solution Methods */
238    //@{
239    /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
240    virtual void finalize_solution(SolverReturn status,
241        Index n, const Number* x, const Number* z_L, const Number* z_U,
242        Index m, const Number* g, const Number* lambda,
243        Number obj_value)=0;
244    //@}
245   
246    virtual const BranchingInfo * branchingInfo() const = 0;
247
248    /** Add some linear cuts to the problem formulation */
249   void addCuts(int numberCuts, const OsiRowCut ** cuts);
250 
251   /** Remove some cuts to the formulation */
252   void removeCuts(int number ,const int * toRemove);
253
254   /** remove the last number cuts.*/
255   void removeLastCuts(int number);
256
257   virtual const SosInfo * sosConstraints() const = 0;
258  private:
259    /**@name Default Compiler Generated Methods
260     * (Hidden to avoid implicit creation/calling).
261     * These methods are not implemented and
262     * we do not want the compiler to implement
263     * them for us, so we declare them private
264     * and do not define them. This ensures that
265     * they will not be implicitly created/called. */
266    //@{
267    /** Default Constructor */
268    //TMINLP();
269
270    /** Copy Constructor */
271    TMINLP(const TMINLP&);
272
273    /** Overloaded Equals Operator */
274    void operator=(const TMINLP&);
275    //@}
276
277      private:
278  /** resize arrays for linear cuts */
279  void resizeLinearCuts(int newNumberCuts, int newNnz);
280  /** columnindices of linear cuts. */
281   int * jCol_;
282  /** rows indices of linear cuts. */
283   int * iRow_;
284  /** elements of linear cuts.*/
285  double * elems_;
286  /** lower bounds for linear cuts. */
287  double * lower_;
288  /** upper bounds for linear cuts. */
289  double * upper_;
290  /** number of linear cuts.*/
291  int nLinearCuts_;
292  /** number of non-zeroes in linear cuts*/
293  int linearCutsNnz_;
294  /** storage size for linear cuts number of cuts.*/
295  int linearCutsCapacity_;
296  /** storage size for linear cuts number of nnz.*/
297  int linearCutsNnzCapacity_;
298  };
299
300} // namespace Ipopt
301
302#endif
Note: See TracBrowser for help on using the repository browser.