Ipopt  3.12.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
IpoptRNLP.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Jelmer Ypma. All Rights Reserved.
3  * This code is published under the Eclipse Public License.
4  *
5  * file: IpoptRNLP.hpp
6  * author: Jelmer Ypma
7  * date: 18 April 2010
8  *
9  * This file defines a C++ class that derives from Ipopt::TNLP. The class
10  * takes care of interaction between Ipopt and user-defined functions in R.
11  *
12  * Financial support of the UK Economic and Social Research Council
13  * through a grant (RES-589-28-0001) to the ESRC Centre for Microdata
14  * Methods and Practice (CeMMAP) is gratefully acknowledged.
15  */
16 
17 #ifndef __IpoptRNLP_HPP__
18 #define __IpoptRNLP_HPP__
19 
20 #include "IpTNLP.hpp" // ISA TNLP
21 
22 #include <assert.h>
23 
24 #include <R.h>
25 #include <Rdefines.h>
26 #include <R_ext/Utils.h>
27 // Rdefines.h is somewhat more higher level then Rinternal.h, and is preferred if the code might be shared with S at any stage.
28 // Utils.h defines void R_CheckUserInterrupt(void); to allow user interuption from R
29 
30 
31 class IpoptRNLP : public Ipopt::TNLP
32 {
33  SEXP R_environment; // this is the environment that the function gets called in. This environment can be used
34  // to pass common data to the R functions.
35  SEXP R_eval_f; // objective function
36  SEXP R_eval_grad_f; // gradient of objective function
37 
38  SEXP R_init_values; // vector with initial values, we get the Ipopt::Number of controls from the length of this vector
39 
40  SEXP R_lower_bounds; // lower bounds of the control x
41  SEXP R_upper_bounds; // upper bounds of the control x
42 
43  SEXP R_eval_g; // function to evaluate constraints
44  SEXP R_eval_jac_g; // function to evaluate jacobian of constraints
45  SEXP R_eval_jac_g_structure; // list with non-zero elements in the Jacobian, this defines the sparse structure
46 
47  SEXP R_constraint_lower_bounds; // lower bounds of the contraint function g()
48  SEXP R_constraint_upper_bounds; // upper bounds of the contraint function g()
49 
50  SEXP R_eval_h; // function to evaluate Hessian
51  SEXP R_eval_h_structure; // list with non-zero elements of the Hessian, this defines the sparse structure
52 
53  SEXP R_result_list; // structure that will contain the return values
54 
55  bool d_hessian_approximation; // should we approximate the Hessian? default: false
56 
57  int d_num_protected_members; // counter of the number of PROTECT calls of the SEXPs above
58 public:
60  IpoptRNLP();
61 
63  virtual ~IpoptRNLP();
64 
65  void set_R_environment( SEXP env );
66 
67 
68  void set_R_eval_f( SEXP f );
69  void set_R_eval_grad_f( SEXP f );
70 
71  void set_R_init_values( SEXP x0 );
72  void set_R_lower_bounds( SEXP lb );
73  void set_R_upper_bounds( SEXP ub );
74 
75  void set_R_eval_g( SEXP g );
76  void set_R_eval_jac_g( SEXP g );
77  void set_R_eval_jac_g_structure( SEXP s );
78 
79  void set_R_constraint_lower_bounds( SEXP lb );
80  void set_R_constraint_upper_bounds( SEXP ub );
81 
82  void set_R_eval_h( SEXP h );
83  void set_R_eval_h_structure( SEXP s );
84 
85  void set_hessian_approximation( bool b );
86 
87  SEXP get_R_result_list();
88 
92  virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g,
93  Ipopt::Index& nnz_h_lag, IndexStyleEnum& Index_style);
94 
96  virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u,
98 
100  virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x,
101  bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U,
102  Ipopt::Index m, bool init_lambda,
103  Ipopt::Number* lambda);
104 
106  virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number& obj_value);
107 
109  virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number* grad_f);
110 
112  virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Index m, Ipopt::Number* g);
113 
118  virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
120  Ipopt::Number* values);
121 
126  virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
127  Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda,
128  bool new_lambda, Ipopt::Index nele_hess, Ipopt::Index* iRow,
129  Ipopt::Index* jCol, Ipopt::Number* values);
130 
132 
136  virtual void finalize_solution(Ipopt::SolverReturn status,
137  Ipopt::Index n, const Ipopt::Number* x, const Ipopt::Number* z_L, const Ipopt::Number* z_U,
138  Ipopt::Index m, const Ipopt::Number* g, const Ipopt::Number* lambda,
139  Ipopt::Number obj_value,
140  const Ipopt::IpoptData* ip_data,
143 
144 private:
156  // IpoptRNLP();
157  IpoptRNLP(const IpoptRNLP&);
158  IpoptRNLP& operator=(const IpoptRNLP&);
160 };
161 
162 
163 #endif
Number * x
Input: Starting point Output: Optimal solution.
void set_R_lower_bounds(SEXP lb)
Class for all IPOPT specific calculated quantities.
SEXP R_eval_g
Definition: IpoptRNLP.hpp:43
SEXP R_eval_h
Definition: IpoptRNLP.hpp:50
virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number *x_l, Ipopt::Number *x_u, Ipopt::Index m, Ipopt::Number *g_l, Ipopt::Number *g_u)
Method to return the bounds for my problem.
Number Number Index m
Number of constraints.
void set_R_eval_jac_g(SEXP g)
SEXP R_lower_bounds
Definition: IpoptRNLP.hpp:40
SEXP R_upper_bounds
Definition: IpoptRNLP.hpp:41
SEXP R_init_values
Definition: IpoptRNLP.hpp:38
Number Number * g
Values of constraint at final point (output only - ignored if set to NULL)
SEXP R_environment
Definition: IpoptRNLP.hpp:33
int d_num_protected_members
Definition: IpoptRNLP.hpp:57
SEXP R_eval_h_structure
Definition: IpoptRNLP.hpp:51
double Number
Type of all numbers.
Definition: IpTypes.hpp:17
void set_R_eval_f(SEXP f)
bool d_hessian_approximation
Definition: IpoptRNLP.hpp:55
void set_R_eval_jac_g_structure(SEXP s)
void set_R_upper_bounds(SEXP ub)
SEXP R_eval_grad_f
Definition: IpoptRNLP.hpp:36
virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number *x, bool init_z, Ipopt::Number *z_L, Ipopt::Number *z_U, Ipopt::Index m, bool init_lambda, Ipopt::Number *lambda)
Method to return the starting point for the algorithm.
virtual bool get_nlp_info(Ipopt::Index &n, Ipopt::Index &m, Ipopt::Index &nnz_jac_g, Ipopt::Index &nnz_h_lag, IndexStyleEnum &Index_style)
Method to return some info about the nlp.
virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Method to return: 1) The structure of the jacobian (if &quot;values&quot; is NULL) 2) The values of the jacobia...
void set_R_eval_grad_f(SEXP f)
SEXP R_constraint_lower_bounds
Definition: IpoptRNLP.hpp:47
IpoptRNLP & operator=(const IpoptRNLP &)
SolverReturn
enum for the return from the optimize algorithm (obviously we need to add more)
Definition: IpAlgTypes.hpp:22
Number Number Index Number Number Index nele_jac
Number of non-zero elements in constraint Jacobian.
virtual bool eval_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number &obj_value)
Method to return the objective value.
void set_R_environment(SEXP env)
Class to organize all the data required by the algorithm.
Definition: IpIpoptData.hpp:83
virtual bool eval_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Number *g)
Method to return the constraint residuals.
void set_R_eval_h(SEXP h)
void set_hessian_approximation(bool b)
void set_R_eval_h_structure(SEXP s)
int Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:19
IpoptRNLP()
default constructor
Number Number Index Number Number Index Index nele_hess
Number of non-zero elements in Hessian of Lagrangian.
SEXP R_constraint_upper_bounds
Definition: IpoptRNLP.hpp:48
void set_R_constraint_lower_bounds(SEXP lb)
void set_R_eval_g(SEXP g)
void set_R_constraint_upper_bounds(SEXP ub)
virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number *grad_f)
Method to return the gradient of the objective.
IndexStyleEnum
overload this method to return the number of variables and constraints, and the number of non-zeros i...
Definition: IpTNLP.hpp:80
virtual void finalize_solution(Ipopt::SolverReturn status, Ipopt::Index n, const Ipopt::Number *x, const Ipopt::Number *z_L, const Ipopt::Number *z_U, Ipopt::Index m, const Ipopt::Number *g, const Ipopt::Number *lambda, Ipopt::Number obj_value, const Ipopt::IpoptData *ip_data, Ipopt::IpoptCalculatedQuantities *ip_cq)
This method is called when the algorithm is complete so the TNLP can store/write the solution...
Base class for all NLP&#39;s that use standard triplet matrix form and dense vectors. ...
Definition: IpTNLP.hpp:50
SEXP R_eval_jac_g
Definition: IpoptRNLP.hpp:44
virtual ~IpoptRNLP()
default destructor
SEXP R_eval_jac_g_structure
Definition: IpoptRNLP.hpp:45
SEXP R_eval_f
Definition: IpoptRNLP.hpp:35
SEXP R_result_list
Definition: IpoptRNLP.hpp:53
virtual bool eval_h(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number *lambda, bool new_lambda, Ipopt::Index nele_hess, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Method to return: 1) The structure of the hessian of the lagrangian (if &quot;values&quot; is NULL) 2) The valu...
SEXP get_R_result_list()
void set_R_init_values(SEXP x0)