/build/buildd/coinor-ipopt-3.10.1/Ipopt/contrib/RInterface/src/IpoptRNLP.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Jelmer Ypma. All Rights Reserved.
00003  * This code is published under the Eclipse Public License.
00004  *
00005  * file:   IpoptRNLP.hpp
00006  * author: Jelmer Ypma
00007  * date:   18 April 2010
00008  *
00009  * This file defines a C++ class that derives from Ipopt::TNLP. The class
00010  * takes care of interaction between Ipopt and user-defined functions in R.
00011  *
00012  * Financial support of the UK Economic and Social Research Council 
00013  * through a grant (RES-589-28-0001) to the ESRC Centre for Microdata 
00014  * Methods and Practice (CeMMAP) is gratefully acknowledged.
00015  */
00016 
00017 #ifndef __IpoptRNLP_HPP__
00018 #define __IpoptRNLP_HPP__
00019 
00020 #include "IpTNLP.hpp"               // ISA TNLP
00021 
00022 #include <R.h>
00023 #include <Rdefines.h>
00024 #include <R_ext/Utils.h>
00025 // Rdefines.h is somewhat more higher level then Rinternal.h, and is preferred if the code might be shared with S at any stage.
00026 // Utils.h defines void R_CheckUserInterrupt(void); to allow user interuption from R
00027 
00028 
00029 class IpoptRNLP : public Ipopt::TNLP
00030 {
00031         SEXP R_environment;                 // this is the environment that the function gets called in. This environment can be used
00032                                     // to pass common data to the R functions.
00033         SEXP R_eval_f;                          // objective function
00034         SEXP R_eval_grad_f;                     // gradient of objective function
00035         
00036         SEXP R_init_values;                     // vector with initial values, we get the Ipopt::Number of controls from the length of this vector
00037         
00038         SEXP R_lower_bounds;            // lower bounds of the control x
00039         SEXP R_upper_bounds;            // upper bounds of the control x
00040         
00041         SEXP R_eval_g;                          // function to evaluate constraints
00042         SEXP R_eval_jac_g;                          // function to evaluate jacobian of constraints
00043         SEXP R_eval_jac_g_structure;    // list with non-zero elements in the Jacobian, this defines the sparse structure
00044         
00045         SEXP R_constraint_lower_bounds; // lower bounds of the contraint function g()
00046         SEXP R_constraint_upper_bounds; // upper bounds of the contraint function g()
00047         
00048         SEXP R_eval_h;                  // function to evaluate Hessian
00049         SEXP R_eval_h_structure;        // list with non-zero elements of the Hessian, this defines the sparse structure
00050         
00051         SEXP R_result_list;                             // structure that will contain the return values
00052         
00053         bool d_hessian_approximation;   // should we approximate the Hessian? default: false
00054         
00055     int d_num_protected_members;    // counter of the number of PROTECT calls of the SEXPs above
00056 public:
00058   IpoptRNLP();
00059 
00061   virtual ~IpoptRNLP();
00062 
00063   void set_R_environment( SEXP env );
00064   
00065   
00066   void set_R_eval_f( SEXP f );
00067   void set_R_eval_grad_f( SEXP f );
00068   
00069   void set_R_init_values( SEXP x0 );
00070   void set_R_lower_bounds( SEXP lb );
00071   void set_R_upper_bounds( SEXP ub );
00072   
00073   void set_R_eval_g( SEXP g );
00074   void set_R_eval_jac_g( SEXP g );
00075   void set_R_eval_jac_g_structure( SEXP s );
00076   
00077   void set_R_constraint_lower_bounds( SEXP lb );
00078   void set_R_constraint_upper_bounds( SEXP ub );
00079   
00080   void set_R_eval_h( SEXP h );
00081   void set_R_eval_h_structure( SEXP s );
00082   
00083   void set_hessian_approximation( bool b );
00084   
00085   SEXP get_R_result_list();
00086   
00090   virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g,
00091                             Ipopt::Index& nnz_h_lag, IndexStyleEnum& Index_style);
00092 
00094   virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u,
00095                                Ipopt::Index m, Ipopt::Number* g_l, Ipopt::Number* g_u);
00096 
00098   virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x,
00099                                   bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U,
00100                                   Ipopt::Index m, bool init_lambda,
00101                                   Ipopt::Number* lambda);
00102 
00104   virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number& obj_value);
00105 
00107   virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number* grad_f);
00108 
00110   virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Index m, Ipopt::Number* g);
00111 
00116   virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00117                           Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index* iRow, Ipopt::Index *jCol,
00118                           Ipopt::Number* values);
00119 
00124   virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
00125                       Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda,
00126                       bool new_lambda, Ipopt::Index nele_hess, Ipopt::Index* iRow,
00127                       Ipopt::Index* jCol, Ipopt::Number* values);
00128 
00130 
00134   virtual void finalize_solution(Ipopt::SolverReturn status,
00135                                  Ipopt::Index n, const Ipopt::Number* x, const Ipopt::Number* z_L, const Ipopt::Number* z_U,
00136                                  Ipopt::Index m, const Ipopt::Number* g, const Ipopt::Number* lambda,
00137                                  Ipopt::Number obj_value,
00138                                                  const Ipopt::IpoptData* ip_data,
00139                                                  Ipopt::IpoptCalculatedQuantities* ip_cq);
00141 
00142 private:
00154   //  IpoptRNLP();
00155   IpoptRNLP(const IpoptRNLP&);
00156   IpoptRNLP& operator=(const IpoptRNLP&);
00158 };
00159 
00160 
00161 #endif