1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// Copyright (C) 2009 International Business Machines.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Author: Andreas Waechter IBM 2009-04-02
// This file is part of the Ipopt tutorial. It is the skeleton for
// the C++ implementation of the coding exercise problem (in AMPL
// formulation):
//
// 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"
#include <cassert>
#include <cstdio>
// We use sin and cos
#include <cmath>
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; i < N_ - 2; i++ )
{
a_[i] = a[i];
}
}
//destructor
TutorialCpp_NLP::~TutorialCpp_NLP()
{
// make sure we delete everything we allocated
delete[] a_;
}
// returns the size of the problem
bool TutorialCpp_NLP::get_nlp_info(
Index& n,
Index& m,
Index& nnz_jac_g,
Index& nnz_h_lag,
IndexStyleEnum& index_style
)
{
// number of variables is given in constructor
n =;
// we have N_-2 constraints
m =;
// each constraint has three nonzeros
nnz_jac_g =;
// We have the full diagonal, and the first off-diagonal except for
// the first and last variable
nnz_h_lag =;
// use the C style indexing (0-based) for the matrices
index_style = TNLP::C_STYLE;
return true;
}
// returns the variable bounds
bool TutorialCpp_NLP::get_bounds_info(
Index n,
Number* x_l,
Number* x_u,
Index m,
Number* g_l,
Number* g_u
)
{
// here, the n and m we gave IPOPT in get_nlp_info are passed back to us.
// If desired, we could assert to make sure they are what we think they are.
//assert(n == );
//assert(m == );
// HERE SET THE BOUNDS ON VARIABLE
return true;
}
// returns the initial point for the problem
bool TutorialCpp_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
)
{
// Here, we assume we only have starting values for x, if you code
// your own NLP, you can provide starting values for the dual variables
// if you wish
assert(init_x == true);
assert(init_z == false);
assert(init_lambda == false);
// initialize to the given starting point
#ifdef skip_me
/* If checking derivatives, if is useful to choose different values */
#endif
return true;
}
// returns the value of the objective function
// sum{i in 1..n} (x[i]-1)^2;
bool TutorialCpp_NLP::eval_f(
Index n,
const Number* x,
bool new_x,
Number& obj_value
)
{
// HERE COMPUTE VALUE OF OBJECTIVE FUNCTION
obj_value = ...
return true;
}
// return the gradient of the objective function grad_{x} f(x)
bool TutorialCpp_NLP::eval_grad_f(
Index n,
const Number* x,
bool new_x,
Number* grad_f
)
{
// HERE SET ALL VALUES OF GRADIENT IN grad_f
return true;
}
// return the value of the constraints: g(x)
// (x[j+1]^2+1.5*x[j+1]-a[j])*cos(x[j+2]) - x[j] = 0;
bool TutorialCpp_NLP::eval_g(
Index n,
const Number* x,
bool new_x,
Index m,
Number* g
)
{
// HERE COMPUTE VALUE OF ALL CONSTRAINTS IN g
return true;
}
// return the structure or values of the jacobian
bool TutorialCpp_NLP::eval_jac_g(
Index n,
const Number* x,
bool new_x,
Index m,
Index nele_jac,
Index* iRow,
Index* jCol,
Number* values
)
{
if( values == NULL )
{
// return the structure of the jacobian
// HERE FILL iRow and jCol
}
else
{
// return the values of the jacobian of the constraints
// HERE FILL values
}
return true;
}
//return the structure or values of the hessian
bool TutorialCpp_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
)
{
if( values == NULL )
{
// return the structure (lower or upper triangular part) of the
// Hessian of hte Lagrangian function
// HERE FILL iRow and jCol
}
else
{
// return the values of the Hessian of hte Lagrangian function
// HERE FILL values
}
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,
const IpoptData* ip_data,
IpoptCalculatedQuantities* ip_cq
)
{
// here is where we would store the solution to variables, or write
// to a file, etc so we could use the solution.
printf("\nWriting solution file solution.txt\n");
FILE* fp = fopen("solution.txt", "w");
// For this example, we write the solution to the console
fprintf(fp, "\n\nSolution of the primal variables, x\n");
for( Index i = 0; i < n; i++ )
{
fprintf(fp, "x[%d] = %e\n", (int)i, x[i]);
}
fprintf(fp, "\n\nSolution of the bound multipliers, z_L and z_U\n");
for( Index i = 0; i < n; i++ )
{
fprintf(fp, "z_L[%d] = %e\n", (int)i, z_L[i]);
}
for( Index i = 0; i < n; i++ )
{
fprintf(fp, "z_U[%d] = %e\n", (int)i, z_U[i]);
}
fprintf(fp, "\n\nObjective value\n");
fprintf(fp, "f(x*) = %e\n", obj_value);
fclose(fp);
}
|