Bonmin  1.7
BonTMINLP.hpp
Go to the documentation of this file.
00001 // (C) Copyright International Business Machines Corporation and
00002 // Carnegie Mellon University 2004, 2007
00003 //
00004 // All Rights Reserved.
00005 // This code is published under the Common Public License.
00006 //
00007 // Authors :
00008 // Pierre Bonami, Carnegie Mellon University,
00009 // Carl D. Laird, Carnegie Mellon University,
00010 // Andreas Waechter, International Business Machines Corporation
00011 //
00012 // Date : 12/01/2004
00013 
00014 #ifndef __TMINLP_HPP__
00015 #define __TMINLP_HPP__
00016 
00017 #include "IpUtils.hpp"
00018 #include "IpReferenced.hpp"
00019 #include "IpException.hpp"
00020 #include "IpAlgTypes.hpp"
00021 #include "CoinPackedMatrix.hpp"
00022 #include "OsiCuts.hpp"
00023 #include "IpTNLP.hpp"
00024 #include "CoinError.hpp"
00025 #include "CoinHelperFunctions.hpp"
00026 
00027 namespace Bonmin
00028 {
00029   DECLARE_STD_EXCEPTION(TMINLP_INVALID);
00030   DECLARE_STD_EXCEPTION(TMINLP_INVALID_VARIABLE_BOUNDS);
00031 
00059   class TMINLP : public Ipopt::ReferencedObject
00060   {
00061   public:
00062     friend class TMINLP2TNLP;
00064     enum SolverReturn{
00065       SUCCESS,
00066       INFEASIBLE,
00067       CONTINUOUS_UNBOUNDED,
00068       LIMIT_EXCEEDED,
00069       USER_INTERRUPT,
00070       MINLP_ERROR};
00072     struct SosInfo
00073     {
00075       int num;
00077       char * types;
00079       int * priorities;
00080       
00084       int numNz;
00086       int * starts;
00088       int * indices;
00090       double * weights;
00093       SosInfo();
00095       SosInfo(const SosInfo & source);
00096       
00097 
00099       ~SosInfo()
00100       {
00101         gutsOfDestructor();
00102       }
00103 
00104 
00106       void gutsOfDestructor();
00107 
00108     };
00109 
00111     struct BranchingInfo
00112     {
00114       int size;
00116       int * priorities;
00118       int * branchingDirections;
00120       double * upPsCosts;
00122       double * downPsCosts;
00123       BranchingInfo():
00124       size(0),
00125       priorities(NULL),
00126       branchingDirections(NULL),
00127       upPsCosts(NULL),
00128       downPsCosts(NULL)
00129       {}
00130       BranchingInfo(const BranchingInfo &other)
00131       {
00132         gutsOfDestructor();
00133         size = other.size;
00134         priorities = CoinCopyOfArray(other.priorities, size);
00135         branchingDirections = CoinCopyOfArray(other.branchingDirections, size);
00136         upPsCosts = CoinCopyOfArray(other.upPsCosts, size);
00137         downPsCosts = CoinCopyOfArray(other.downPsCosts, size);
00138       }
00139       void gutsOfDestructor()
00140       {
00141       if (priorities != NULL) delete [] priorities;
00142       priorities = NULL;
00143       if (branchingDirections != NULL) delete [] branchingDirections;  
00144       branchingDirections = NULL;
00145       if (upPsCosts != NULL) delete [] upPsCosts;
00146       upPsCosts = NULL;
00147       if (downPsCosts != NULL) delete [] downPsCosts;
00148       downPsCosts = NULL;
00149       }
00150       ~BranchingInfo()
00151       {
00152         gutsOfDestructor();
00153       }
00154     };
00155 
00157     class PerturbInfo
00158     {
00159     public:
00161       PerturbInfo() :
00162         perturb_radius_(NULL)
00163       {}
00164 
00166       ~PerturbInfo()
00167       {
00168         delete [] perturb_radius_;
00169       }
00170 
00172       void SetPerturbationArray(Ipopt::Index numvars, const double* perturb_radius);
00173 
00176       const double* GetPerturbationArray() const {
00177         return perturb_radius_;
00178       }
00179 
00180     private:
00182       PerturbInfo(const PerturbInfo & source);
00183 
00188       double* perturb_radius_;
00189     };
00190 
00192     enum VariableType
00193     {
00194       CONTINUOUS,
00195       BINARY,
00196       INTEGER
00197     };
00198 
00201     TMINLP();
00202 
00204     virtual ~TMINLP();
00206 
00212     virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g,
00213         Ipopt::Index& nnz_h_lag, Ipopt::TNLP::IndexStyleEnum& index_style)=0;
00214 
00222     virtual bool get_scaling_parameters(Ipopt::Number& obj_scaling,
00223                                         bool& use_x_scaling, Ipopt::Index n,
00224                                         Ipopt::Number* x_scaling,
00225                                         bool& use_g_scaling, Ipopt::Index m,
00226                                         Ipopt::Number* g_scaling)
00227     {
00228       return false;
00229     }
00230 
00231 
00234     virtual bool get_variables_types(Ipopt::Index n, VariableType* var_types)=0;
00235 
00238     virtual bool get_variables_linearity(Ipopt::Index n, 
00239                                            Ipopt::TNLP::LinearityType* var_types) = 0;
00240 
00243     virtual bool get_constraints_linearity(Ipopt::Index m, 
00244                                            Ipopt::TNLP::LinearityType* const_types) = 0;
00245 
00254     virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u,
00255         Ipopt::Index m, Ipopt::Number* g_l, Ipopt::Number* g_u)=0;
00256 
00264     virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x,
00265                                     bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U,
00266         Ipopt::Index m, bool init_lambda,
00267         Ipopt::Number* lambda)=0;
00268 
00270     virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00271         Ipopt::Number& obj_value)=0;
00272 
00275     virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00276         Ipopt::Number* grad_f)=0;
00277 
00279     virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00280         Ipopt::Index m, Ipopt::Number* g)=0;
00281 
00287     virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00288         Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index* iRow,
00289         Ipopt::Index *jCol, Ipopt::Number* values)=0;
00290 
00298     virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00299         Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda,
00300         bool new_lambda, Ipopt::Index nele_hess,
00301         Ipopt::Index* iRow, Ipopt::Index* jCol, Ipopt::Number* values)=0;
00304     virtual bool eval_gi(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00305                          Ipopt::Index i, Ipopt::Number& gi)
00306     {
00307       std::cerr << "Method eval_gi not overloaded from TMINLP\n";
00308       throw -1;
00309     }
00313     virtual bool eval_grad_gi(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00314                               Ipopt::Index i, Ipopt::Index& nele_grad_gi, Ipopt::Index* jCol,
00315                               Ipopt::Number* values)
00316     {
00317       std::cerr << "Method eval_grad_gi not overloaded from TMINLP\n";
00318       throw -1;
00319     }
00321 
00325     virtual void finalize_solution(TMINLP::SolverReturn status,
00326                                    Ipopt::Index n, const Ipopt::Number* x, Ipopt::Number obj_value) =0;
00328     
00329     virtual const BranchingInfo * branchingInfo() const = 0;
00330 
00331     virtual const SosInfo * sosConstraints() const = 0;
00332 
00333     virtual const PerturbInfo* perturbInfo() const
00334     {
00335       return NULL;
00336     }
00337 
00339     virtual bool hasUpperBoundingObjective(){
00340       return false;}
00341     
00344     virtual bool eval_upper_bound_f(Ipopt::Index n, const Ipopt::Number* x,
00345                                     Ipopt::Number& obj_value){ return false; }
00346 
00348    enum Convexity {
00349      Convex,
00350      NonConvex,
00351      SimpleConcave};
00352 
00355    struct MarkedNonConvex {
00357          MarkedNonConvex():
00358          cIdx(-1), cRelaxIdx(-1){}
00360       int cIdx;
00362          int cRelaxIdx;};
00366    struct SimpleConcaveConstraint{
00368          SimpleConcaveConstraint():
00369            xIdx(-1), yIdx(-1), cIdx(-1){}
00371       int xIdx;
00373          int yIdx;
00375          int cIdx;};
00377     virtual bool get_constraint_convexities(int m, TMINLP::Convexity * constraints_convexities)const {
00378       CoinFillN(constraints_convexities, m, TMINLP::Convex);
00379       return true;}
00381   virtual bool get_number_nonconvex(int & number_non_conv, int & number_concave) const{
00382     number_non_conv = 0;
00383     number_concave = 0;
00384     return true;} 
00386   virtual bool get_constraint_convexities(int number_non_conv, MarkedNonConvex * non_convs) const{
00387     assert(number_non_conv == 0);
00388     return true;}
00390   virtual bool get_simple_concave_constraints(int number_concave, SimpleConcaveConstraint * simple_concave) const{
00391     assert(number_concave == 0);
00392     return true;}
00393 
00395   virtual bool hasLinearObjective(){return false;}
00396 
00398   bool hasGeneralInteger();
00399 
00401   virtual const int * get_const_xtra_id() const{
00402     return NULL;
00403   }
00404   protected:
00408     TMINLP(const TMINLP&);
00409 
00411     void operator=(const TMINLP&);
00413 
00414   private:
00415   };
00416 
00417 } // namespace Ipopt
00418 
00419 #endif
00420