#include "hs071_nlp.hpp" using namespace Ipopt; /***************************************************** * here include my own dll for the function: * void trans_x2z(double *x, double *z, double *dzdx); * to calculate nonlinear vector function z=func(x) and the jacobian dz/dx *****************************************************/ #include #pragma comment(lib,"mydll.lib") HS071_NLP::HS071_NLP() { /***************************************************** * define the output variables z and dzdx, * which are public members of this object *****************************************************/ int mm = 2; // dimension of z int nn = 4 ; // dimension of x z=new double[mm]; dzdx=new double[mm*nn]; } HS071_NLP::~HS071_NLP(){} bool HS071_NLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, IndexStyleEnum& index_style) { n = 4; m = 1; nnz_jac_g = 4; nnz_h_lag = 0; index_style = TNLP::C_STYLE; return true; } bool HS071_NLP::get_bounds_info(Index n, Number* x_l, Number* x_u, Index m, Number* g_l, Number* g_u) { assert(n == 4); assert(m == 1); for (Index i=0; i<4; i++) {x_l[i] = 0; x_u[40+i] = 1; } for (Index i=0; i<1; i++) {g_l[i] = 0; g_u[i] = 1; } return true; } bool HS071_NLP::get_starting_point(Index n, bool init_x, Number* x, bool init_z, Number* z_L, Number* z_U, Index m, bool init_lambda, Number* lambda) { assert(init_x == true); assert(init_z == false); assert(init_lambda == false); for (Index i=0; i<4; i++) {x[i]=0.5; } return true; } // 1,returns the value of the objective function bool HS071_NLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value) { printf("\nA\n"); assert(n == 4); /***************************************************** * it is ok to call my own function to compute the z and dzdx *****************************************************/ obj_value = x[3]+z[1]; return true; } // 2,return the gradient of the objective function grad_{x} f(x) bool HS071_NLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f) { printf("\nB\n"); assert(n == 4); /***************************************************** * it is ok to call my own function to compute the z and dzdx *****************************************************/ double *temp=new double[4]; for(int i=0; i<3; i++) temp[i]= dzdx[i]; temp[3]= 1 + dzdx[3]; for(int i=0; i<4; i++) grad_f[i]= temp[i]; return true; } bool HS071_NLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g) { printf("\nC\n"); assert(n == 4); assert(m == 1); /***************************************************** * call my own function to compute the z and dzdx *****************************************************/ trans_x2z(x,z,dzdx); for(int j=0; j<1; j++) g[j]= z[1]; return true; } bool HS071_NLP::eval_jac_g(Index n, const Number* x, bool new_x, Index m, Index nele_jac, Index* iRow, Index *jCol, Number* values) {printf("\nD\n"); /***************************************************** * it is not ok to call my own function to compute the z and dzdx *****************************************************/ if (values == NULL) { iRow[0] = 0; jCol[0] =0; iRow[1] = 0; jCol[1] =1; iRow[2] = 0; jCol[2] =2; iRow[3] = 0; jCol[3] =3; } else for (int i=0; i<4; i++) values[i]= dzdx[4+i]; return true; } bool HS071_NLP::eval_h(Index n, const Number* x, bool new_x, Number obj_factor, Index m, const Number* lambda, bool new_lambda, Index nele_hess, Index* iRow, Index* jCol, Number* values) { return true; } void HS071_NLP::finalize_solution(SolverReturn status, Index n, const Number* x, const Number* z_L, const Number* z_U, Index m, const Number* g, const Number* lambda, Number obj_value, const IpoptData* ip_data, IpoptCalculatedQuantities* ip_cq) { printf("\n\nSolution of the primal variables, x\n"); for (Index i=0; i