Bonmin  1.7
MyTMINLP.cpp
Go to the documentation of this file.
00001 // (C) Copyright Carnegie Mellon University 2006
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // Authors :
00006 // P. Bonami, Carnegie Mellon University
00007 //
00008 // Date :  03/17/2006
00009 #include "MyTMINLP.hpp"
00010 #include "BonAmplInterface.hpp"
00011 
00012 bool 
00013 MyTMINLP::get_variables_types(Index n, VariableType* var_types)
00014 {
00015   var_types[0] = BINARY;
00016   var_types[1] = CONTINUOUS;
00017   var_types[2] = CONTINUOUS;
00018   var_types[3] = INTEGER;
00019   return true;
00020 }
00021 
00022 
00023 bool 
00024 MyTMINLP::get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types)
00025 {
00026   var_types[0] = Ipopt::TNLP::LINEAR;
00027   var_types[1] = Ipopt::TNLP::NON_LINEAR;
00028   var_types[2] = Ipopt::TNLP::NON_LINEAR;
00029   var_types[3] = Ipopt::TNLP::LINEAR;
00030   return true;
00031 }
00032 
00033 
00034 bool 
00035 MyTMINLP::get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types)
00036 {
00037   assert (m==3);
00038   const_types[0] = Ipopt::TNLP::NON_LINEAR;
00039   const_types[1] = Ipopt::TNLP::LINEAR;
00040   const_types[2] = Ipopt::TNLP::LINEAR;
00041   return true;
00042 }
00043 bool 
00044 MyTMINLP::get_nlp_info(Index& n, Index&m, Index& nnz_jac_g,
00045                        Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style)
00046 {
00047   n = 4;//number of variable
00048   m = 3;//number of constraints
00049   nnz_jac_g = 7;//number of non zeroes in Jacobian
00050   nnz_h_lag = 2;//number of non zeroes in Hessian of Lagrangean
00051   index_style = TNLP::FORTRAN_STYLE;
00052   return true;
00053 }
00054 
00055 bool 
00056 MyTMINLP::get_bounds_info(Index n, Number* x_l, Number* x_u,
00057                             Index m, Number* g_l, Number* g_u)
00058 {
00059   assert(n==4);
00060   assert(m==3);
00061   x_l[0] = 0.;
00062   x_u[0] = 1.;
00063   
00064   x_l[1] = 0.;
00065   x_u[1] = DBL_MAX;
00066   
00067   x_l[2] =0.;
00068   x_u[2] = DBL_MAX;
00069   
00070   x_l[3] = 0;
00071   x_u[3] = 5;
00072   
00073   g_l[0] = -DBL_MAX;
00074   g_u[0] = 1./4.;
00075 
00076   g_l[1] = -DBL_MAX;
00077   g_u[1] = 0;
00078   
00079   g_l[2] = -DBL_MAX;
00080   g_u[2] = 2;
00081   return true;
00082 }
00083 
00084 bool 
00085 MyTMINLP::get_starting_point(Index n, bool init_x, Number* x,
00086                              bool init_z, Number* z_L, Number* z_U,
00087                              Index m, bool init_lambda,
00088                              Number* lambda)
00089 {
00090   assert(n==4);
00091   assert(m==3);
00092   
00093   assert(init_x);
00094   assert(!init_lambda);
00095   x[0] = 0;
00096   x[1] = 0;
00097   x[2] = 0;
00098   x[3] = 0;
00099   return true;
00100 }
00101 
00102 bool 
00103 MyTMINLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value)
00104 {
00105   assert(n==4);
00106   obj_value = - x[0] - x[1] - x[2];
00107   return true;
00108 }
00109 
00110 bool
00111 MyTMINLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f)
00112 {
00113   assert(n==4);
00114   grad_f[0] = -1.;
00115   grad_f[1] = -1.;  
00116   grad_f[2] = -1.;
00117   grad_f[3] = 0.;
00118   return true;
00119 }
00120 
00121 bool
00122 MyTMINLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g)
00123 {
00124   assert(n==4);
00125   assert(m==3);
00126   
00127   g[0] = (x[1] - 1./2.)*(x[1] - 1./2.) + (x[2] - 1./2.)*(x[2] - 1./2.);
00128   g[1] = x[0] - x[1];
00129   g[2] = x[0] + x[2] + x[3];
00130   
00131   return true;
00132 }
00133 
00134 bool
00135 MyTMINLP::eval_jac_g(Index n, const Number* x, bool new_x,
00136                      Index m, Index nnz_jac, Index* iRow, Index *jCol,
00137                      Number* values)
00138 {
00139   assert(n==4);
00140   assert(nnz_jac == 7);
00141   if(values == NULL) {
00142     iRow[0] = 2;
00143     jCol[0] = 1;
00144 
00145     iRow[1] = 3;
00146     jCol[1] = 1;
00147     
00148     iRow[2] = 1;
00149     jCol[2] = 2;
00150     
00151     iRow[3] = 2;
00152     jCol[3] = 2;
00153     
00154     iRow[4] = 1;
00155     jCol[4] = 3;
00156         
00157     iRow[5] = 3;
00158     jCol[5] = 3;
00159     
00160     iRow[6] = 3;
00161     jCol[6] = 4;
00162     return true;
00163   }
00164   else {
00165     values[0] = 1.;
00166     values[1] = 1;
00167 
00168     values[2] = 2*x[1] - 1;
00169     values[3] = -1.;
00170 
00171     values[4] = 2*x[2] - 1;
00172     values[5] = 1.;
00173     
00174     values[6] = 1.;
00175     
00176     return true;
00177   }
00178 }
00179 
00180 bool
00181 MyTMINLP::eval_h(Index n, const Number* x, bool new_x,
00182                  Number obj_factor, Index m, const Number* lambda,
00183                  bool new_lambda, Index nele_hess, Index* iRow,
00184                  Index* jCol, Number* values)
00185 {
00186   assert (n==4);
00187   assert (m==3);
00188   assert(nele_hess==2);
00189   if(values==NULL)
00190   {
00191     iRow[0] = 2;
00192     jCol[0] = 2;
00193     
00194     iRow[1] = 3;
00195     jCol[1] = 3;
00196   }
00197   else {
00198     values[0] = 2*lambda[0];
00199     values[1] = 2*lambda[0];
00200   }
00201   return true;
00202 }
00203 
00204 void
00205 MyTMINLP::finalize_solution(TMINLP::SolverReturn status,
00206                             Index n, const Number* x, Number obj_value)
00207 {
00208   std::cout<<"Problem status: "<<status<<std::endl;
00209   std::cout<<"Objective value: "<<obj_value<<std::endl;
00210   if(printSol_ && x != NULL){
00211     std::cout<<"Solution:"<<std::endl;
00212     for(int i = 0 ; i < n ; i++){
00213       std::cout<<"x["<<i<<"] = "<<x[i];
00214       if(i < n-1) std::cout<<", ";}
00215     std::cout<<std::endl;
00216   }
00217 }