Ipopt
trunk
|
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 <assert.h> 00023 00024 #include <R.h> 00025 #include <Rdefines.h> 00026 #include <R_ext/Utils.h> 00027 // Rdefines.h is somewhat more higher level then Rinternal.h, and is preferred if the code might be shared with S at any stage. 00028 // Utils.h defines void R_CheckUserInterrupt(void); to allow user interuption from R 00029 00030 00031 class IpoptRNLP : public Ipopt::TNLP 00032 { 00033 SEXP R_environment; // this is the environment that the function gets called in. This environment can be used 00034 // to pass common data to the R functions. 00035 SEXP R_eval_f; // objective function 00036 SEXP R_eval_grad_f; // gradient of objective function 00037 00038 SEXP R_init_values; // vector with initial values, we get the Ipopt::Number of controls from the length of this vector 00039 00040 SEXP R_lower_bounds; // lower bounds of the control x 00041 SEXP R_upper_bounds; // upper bounds of the control x 00042 00043 SEXP R_eval_g; // function to evaluate constraints 00044 SEXP R_eval_jac_g; // function to evaluate jacobian of constraints 00045 SEXP R_eval_jac_g_structure; // list with non-zero elements in the Jacobian, this defines the sparse structure 00046 00047 SEXP R_constraint_lower_bounds; // lower bounds of the contraint function g() 00048 SEXP R_constraint_upper_bounds; // upper bounds of the contraint function g() 00049 00050 SEXP R_eval_h; // function to evaluate Hessian 00051 SEXP R_eval_h_structure; // list with non-zero elements of the Hessian, this defines the sparse structure 00052 00053 SEXP R_result_list; // structure that will contain the return values 00054 00055 bool d_hessian_approximation; // should we approximate the Hessian? default: false 00056 00057 int d_num_protected_members; // counter of the number of PROTECT calls of the SEXPs above 00058 public: 00060 IpoptRNLP(); 00061 00063 virtual ~IpoptRNLP(); 00064 00065 void set_R_environment( SEXP env ); 00066 00067 00068 void set_R_eval_f( SEXP f ); 00069 void set_R_eval_grad_f( SEXP f ); 00070 00071 void set_R_init_values( SEXP x0 ); 00072 void set_R_lower_bounds( SEXP lb ); 00073 void set_R_upper_bounds( SEXP ub ); 00074 00075 void set_R_eval_g( SEXP g ); 00076 void set_R_eval_jac_g( SEXP g ); 00077 void set_R_eval_jac_g_structure( SEXP s ); 00078 00079 void set_R_constraint_lower_bounds( SEXP lb ); 00080 void set_R_constraint_upper_bounds( SEXP ub ); 00081 00082 void set_R_eval_h( SEXP h ); 00083 void set_R_eval_h_structure( SEXP s ); 00084 00085 void set_hessian_approximation( bool b ); 00086 00087 SEXP get_R_result_list(); 00088 00092 virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g, 00093 Ipopt::Index& nnz_h_lag, IndexStyleEnum& Index_style); 00094 00096 virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u, 00097 Ipopt::Index m, Ipopt::Number* g_l, Ipopt::Number* g_u); 00098 00100 virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x, 00101 bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U, 00102 Ipopt::Index m, bool init_lambda, 00103 Ipopt::Number* lambda); 00104 00106 virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number& obj_value); 00107 00109 virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number* grad_f); 00110 00112 virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Index m, Ipopt::Number* g); 00113 00118 virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x, 00119 Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index* iRow, Ipopt::Index *jCol, 00120 Ipopt::Number* values); 00121 00126 virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x, 00127 Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda, 00128 bool new_lambda, Ipopt::Index nele_hess, Ipopt::Index* iRow, 00129 Ipopt::Index* jCol, Ipopt::Number* values); 00130 00132 00136 virtual void finalize_solution(Ipopt::SolverReturn status, 00137 Ipopt::Index n, const Ipopt::Number* x, const Ipopt::Number* z_L, const Ipopt::Number* z_U, 00138 Ipopt::Index m, const Ipopt::Number* g, const Ipopt::Number* lambda, 00139 Ipopt::Number obj_value, 00140 const Ipopt::IpoptData* ip_data, 00141 Ipopt::IpoptCalculatedQuantities* ip_cq); 00143 00144 private: 00156 // IpoptRNLP(); 00157 IpoptRNLP(const IpoptRNLP&); 00158 IpoptRNLP& operator=(const IpoptRNLP&); 00160 }; 00161 00162 00163 #endif