Prev Next Index-> contents reference index search external Up-> CppAD ipopt_solve ipopt_solve_get_started.cpp ipopt_solve_get_started.cpp Headings-> Purpose Configuration Requirement

$\newcommand{\W}[1]{ \; #1 \; } \newcommand{\R}[1]{ {\rm #1} } \newcommand{\B}[1]{ {\bf #1} } \newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} } \newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} } \newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} } \newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }$ This is cppad-20221105 documentation. Here is a link to its current documentation .
Nonlinear Programming Using CppAD and Ipopt: Example and Test

Purpose
This example program demonstrates how to use ipopt_solve to solve the example problem in the Ipopt documentation; i.e., the problem $$\begin{array}{lc} {\rm minimize \; } & x_1 * x_4 * (x_1 + x_2 + x_3) + x_3 \\ {\rm subject \; to \; } & x_1 * x_2 * x_3 * x_4 \geq 25 \\ & x_1^2 + x_2^2 + x_3^2 + x_4^2 = 40 \\ & 1 \leq x_1, x_2, x_3, x_4 \leq 5 \end{array}$$

Configuration Requirement
This example will be compiled and tested provided include_ipopt is on the cmake command line.
# include <cppad/ipopt/solve.hpp>

namespace {

class FG_eval {
public:
{   assert( fg.size() == 3 );
assert( x.size()  == 4 );

// Fortran style indexing
// f(x)
fg[0] = x1 * x4 * (x1 + x2 + x3) + x3;
// g_1 (x)
fg[1] = x1 * x2 * x3 * x4;
// g_2 (x)
fg[2] = x1 * x1 + x2 * x2 + x3 * x3 + x4 * x4;
//
return;
}
};
}

bool get_started(void)
{   bool ok = true;
size_t i;

// number of independent variables (domain dimension for f and g)
size_t nx = 4;
// number of constraints (range dimension for g)
size_t ng = 2;
// initial value of the independent variables
Dvector xi(nx);
xi[0] = 1.0;
xi[1] = 5.0;
xi[2] = 5.0;
xi[3] = 1.0;
// lower and upper limits for x
Dvector xl(nx), xu(nx);
for(i = 0; i < nx; i++)
{   xl[i] = 1.0;
xu[i] = 5.0;
}
// lower and upper limits for g
Dvector gl(ng), gu(ng);
gl[0] = 25.0;     gu[0] = 1.0e19;
gl[1] = 40.0;     gu[1] = 40.0;

// object that computes objective and constraints
FG_eval fg_eval;

// options
std::string options;
// turn off any printing
options += "Integer print_level  0\n";
options += "String  sb           yes\n";
// maximum number of iterations
options += "Integer max_iter     10\n";
// approximate accuracy in first order necessary conditions;
// see Mathematical Programming, Volume 106, Number 1,
// Pages 25-57, Equation (6)
options += "Numeric tol          1e-6\n";
// derivative testing
options += "String  derivative_test            second-order\n";
// maximum amount of random pertubation; e.g.,
// when evaluation finite diff

// place to return solution

// solve the problem
options, xi, xl, xu, gl, gu, fg_eval, solution
);
//
// Check some of the solution values
//
//
double check_x[]  = { 1.000000, 4.743000, 3.82115, 1.379408 };
double check_zl[] = { 1.087871, 0.,       0.,      0.       };
double check_zu[] = { 0.,       0.,       0.,      0.       };
double rel_tol    = 1e-6;  // relative tolerance
double abs_tol    = 1e-6;  // absolute tolerance
for(i = 0; i < nx; i++)
check_x[i],  solution.x[i],   rel_tol, abs_tol
);
}