// Copyright (C) 2006 International Business Machines and others. // All Rights Reserved. // This code is published under the Common Public License. // // $Id: TutorialCpp_nlp.cpp 1097 2006-07-17 08:21:46Z andreasw $ // // Author: Andreas Waechter IBM 2006-07-16 // // This is part of the Ipopt tutorial at DIMACS's COIN-OR workshop. // // Implementation of the nlp class for execise problem // // param n := 4; // // var x {1..n} <= 0, >= -1.5, := -0.5; // // minimize obj: // sum{i in 1..n} (x[i]-1)^2; // ; // // subject to constr {i in 2..n-1}: // (x[i]^2+1.5*x[i]-i/n)*cos(x[i+1]) - x[i-1] = 0; // // The constant term "i/n" in the constraint is supposed to be input data // #include "TutorialCpp_nlp.hpp" // We use sin and cos #include using namespace Ipopt; // constructor TutorialCpp_NLP::TutorialCpp_NLP(Index N, const Number* a) : N_(N) { // Copy the values for the constants appearing in the constraints a_ = new Number[N_-2]; for (Index i=0; i1) { values[inz] -= lambda[i-2]*(x[i]*x[i] + 1.5*x[i] -a_[i-2])*cos(x[i]); } inz++; values[inz] = -lambda[i-1]*(2.*x[i]+1.5)*sin(x[i+1]); inz++; } values[inz] = obj_factor*2.; values[inz] -= lambda[n-3]*(x[n-1]*x[n-1] + 1.5*x[n-1] -a_[n-3])*cos(x[n-1]); inz++; assert(inz == nele_hess); } return true; } void TutorialCpp_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) { // here is where we would store the solution to variables, or write to a file, etc // so we could use the solution. // For this example, we write the solution to the console printf("\n\nSolution of the primal variables, x\n"); for (Index i=0; i