<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Nonlinear Programming Using CppAD and Ipopt: Example and Test</title> <meta name="description" id="description" content="Nonlinear Programming Using CppAD and Ipopt: Example and Test"/> <meta name="keywords" id="keywords" content=" example nonlinear programming ipopt Ad "/> <style type='text/css'> body { color : black } body { background-color : white } A:link { color : blue } A:visited { color : purple } A:active { color : purple } </style> <script type='text/javascript' language='JavaScript' src='_ipopt_get_started.cpp_xml.js'> </script> </head> <body> <table><tr> <td> <a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a> </td> <td><a href="ipopt_cppad_windows.xml" target="_top">Prev</a> </td><td><a href="ipopt_cppad_ode.xml" target="_top">Next</a> </td><td> <select onchange='choose_across0(this)'> <option>Index-></option> <option>contents</option> <option>reference</option> <option>index</option> <option>search</option> <option>external</option> </select> </td> <td> <select onchange='choose_up0(this)'> <option>Up-></option> <option>CppAD</option> <option>Example</option> <option>General</option> <option>ipopt_cppad_nlp</option> <option>ipopt_get_started.cpp</option> </select> </td> <td> <select onchange='choose_down3(this)'> <option>Example-></option> <option>General</option> <option>ExampleUtility</option> <option>ListAllExamples</option> <option>test_vector</option> </select> </td> <td> <select onchange='choose_down2(this)'> <option>General-></option> <option>ad_fun.cpp</option> <option>ad_in_c.cpp</option> <option>HesMinorDet.cpp</option> <option>HesLuDet.cpp</option> <option>ipopt_cppad_nlp</option> <option>Interface2C.cpp</option> <option>JacMinorDet.cpp</option> <option>JacLuDet.cpp</option> <option>mul_level</option> <option>OdeStiff.cpp</option> <option>ode_taylor.cpp</option> <option>ode_taylor_adolc.cpp</option> <option>StackMachine.cpp</option> </select> </td> <td> <select onchange='choose_down1(this)'> <option>ipopt_cppad_nlp-></option> <option>ipopt_cppad_windows</option> <option>ipopt_get_started.cpp</option> <option>ipopt_cppad_ode</option> </select> </td> <td>ipopt_get_started.cpp</td> <td> <select onchange='choose_current0(this)'> <option>Headings-></option> <option>Purpose</option> <option>Configuration Requirement</option> </select> </td> </tr></table><br/> <center><b><big><big>Nonlinear Programming Using CppAD and Ipopt: Example and Test</big></big></b></center> <br/> <b><big><a name="Purpose" id="Purpose">Purpose</a></big></b> <br/> This example program demonstrates how to use the class ipopt_cppad_nlp to solve the example problem in the Ipopt documentation; i.e., the problem <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow> <mtable rowalign="center" ><mtr><mtd columnalign="left" > <mrow><mstyle mathvariant='normal'><mi mathvariant='normal'>minimize</mi> <mspace width='.3em'/> </mstyle></mrow> </mtd><mtd columnalign="center" > <msub><mi mathvariant='italic'>x</mi> <mn>1</mn> </msub> <mo stretchy="false">*</mo> <msub><mi mathvariant='italic'>x</mi> <mn>4</mn> </msub> <mo stretchy="false">*</mo> <mo stretchy="false">(</mo> <msub><mi mathvariant='italic'>x</mi> <mn>1</mn> </msub> <mo stretchy="false">+</mo> <msub><mi mathvariant='italic'>x</mi> <mn>2</mn> </msub> <mo stretchy="false">+</mo> <msub><mi mathvariant='italic'>x</mi> <mn>3</mn> </msub> <mo stretchy="false">)</mo> <mo stretchy="false">+</mo> <msub><mi mathvariant='italic'>x</mi> <mn>3</mn> </msub> </mtd></mtr><mtr><mtd columnalign="left" > <mrow><mstyle mathvariant='normal'><mi mathvariant='normal'>subject</mi> <mspace width='.3em'/> <mi mathvariant='normal'>to</mi> <mspace width='.3em'/> </mstyle></mrow> </mtd><mtd columnalign="center" > <msub><mi mathvariant='italic'>x</mi> <mn>1</mn> </msub> <mo stretchy="false">*</mo> <msub><mi mathvariant='italic'>x</mi> <mn>2</mn> </msub> <mo stretchy="false">*</mo> <msub><mi mathvariant='italic'>x</mi> <mn>3</mn> </msub> <mo stretchy="false">*</mo> <msub><mi mathvariant='italic'>x</mi> <mn>4</mn> </msub> <mo stretchy="false">≥</mo> <mn>25</mn> </mtd></mtr><mtr><mtd columnalign="left" > </mtd><mtd columnalign="center" > <msubsup><mi mathvariant='italic'>x</mi> <mn>1</mn> <mn>2</mn> </msubsup> <mo stretchy="false">+</mo> <msubsup><mi mathvariant='italic'>x</mi> <mn>2</mn> <mn>2</mn> </msubsup> <mo stretchy="false">+</mo> <msubsup><mi mathvariant='italic'>x</mi> <mn>3</mn> <mn>2</mn> </msubsup> <mo stretchy="false">+</mo> <msubsup><mi mathvariant='italic'>x</mi> <mn>4</mn> <mn>2</mn> </msubsup> <mo stretchy="false">=</mo> <mn>40</mn> </mtd></mtr><mtr><mtd columnalign="left" > </mtd><mtd columnalign="center" > <mn>1</mn> <mo stretchy="false">≤</mo> <msub><mi mathvariant='italic'>x</mi> <mn>1</mn> </msub> <mo stretchy="false">,</mo> <msub><mi mathvariant='italic'>x</mi> <mn>2</mn> </msub> <mo stretchy="false">,</mo> <msub><mi mathvariant='italic'>x</mi> <mn>3</mn> </msub> <mo stretchy="false">,</mo> <msub><mi mathvariant='italic'>x</mi> <mn>4</mn> </msub> <mo stretchy="false">≤</mo> <mn>5</mn> </mtd></mtr></mtable> </mrow></math> <br/> <b><big><a name="Configuration Requirement" id="Configuration Requirement">Configuration Requirement</a></big></b> <br/> This example will be compiled and tested provided that the value <a href="installunix.xml#IpoptDir" target="_top"><span style='white-space: nowrap'>IpoptDir</span></a> is specified on the <a href="installunix.xml#Configure" target="_top"><span style='white-space: nowrap'>configure</span></a> command line. <code><font color="blue"> <pre style='display:inline'> # include "ipopt_cppad_nlp.hpp" namespace { class FG_info : public ipopt_cppad_fg_info { private: bool retape_; public: // derived class part of constructor FG_info(bool retape) : retape_ (retape) { } // Evaluation of the objective f(x), and constraints g(x) // using an Algorithmic Differentiation (AD) class. ADVector eval_r(size_t k, const ADVector& x) { ADVector fg(3); // Fortran style indexing ADNumber x1 = x[0]; ADNumber x2 = x[1]; ADNumber x3 = x[2]; ADNumber x4 = x[3]; // 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 fg; } bool retape(size_t k) { return retape_; } }; } bool ipopt_get_started(void) { bool ok = true; size_t j; // number of independent variables (domain dimension for f and g) size_t n = 4; // number of constraints (range dimension for g) size_t m = 2; // initial value of the independent variables NumberVector x_i(n); x_i[0] = 1.0; x_i[1] = 5.0; x_i[2] = 5.0; x_i[3] = 1.0; // lower and upper limits for x NumberVector x_l(n); NumberVector x_u(n); for(j = 0; j < n; j++) { x_l[j] = 1.0; x_u[j] = 5.0; } // lower and upper limits for g NumberVector g_l(m); NumberVector g_u(m); g_l[0] = 25.0; g_u[0] = 1.0e19; g_l[1] = 40.0; g_u[1] = 40.0; size_t icase; for(icase = 0; icase <= 1; icase++) { // Should ipopt_cppad_nlp retape the operation sequence for // every new x. Can test both true and false cases because // the operation sequence does not depend on x (for this case). bool retape = icase != 0; // object in derived class FG_info fg_info(retape); // create the Ipopt interface ipopt_cppad_solution solution; Ipopt::SmartPtr<Ipopt::TNLP> cppad_nlp = new ipopt_cppad_nlp( n, m, x_i, x_l, x_u, g_l, g_u, &fg_info, &solution ); // Create an instance of the IpoptApplication using Ipopt::IpoptApplication; Ipopt::SmartPtr<IpoptApplication> app = new IpoptApplication(); // turn off any printing app->Options()->SetIntegerValue("print_level", 0); // maximum number of iterations app->Options()->SetIntegerValue("max_iter", 10); // approximate accuracy in first order necessary conditions; // see Mathematical Programming, Volume 106, Number 1, // Pages 25-57, Equation (6) app->Options()->SetNumericValue("tol", 1e-9); // derivative testing app->Options()-> SetStringValue("derivative_test", "second-order"); // Initialize the IpoptApplication and process the options Ipopt::ApplicationReturnStatus status = app->Initialize(); ok &= status == Ipopt::Solve_Succeeded; // Run the IpoptApplication status = app->OptimizeTNLP(cppad_nlp); ok &= status == Ipopt::Solve_Succeeded; /* Check some of the solution values */ ok &= solution.status == ipopt_cppad_solution::success; // double check_x[] = { 1.000000, 4.743000, 3.82115, 1.379408 }; double check_z_l[] = { 1.087871, 0., 0., 0. }; double check_z_u[] = { 0., 0., 0., 0. }; double rel_tol = 1e-6; // relative tolerance double abs_tol = 1e-6; // absolute tolerance for(j = 0; j < n; j++) { ok &= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>( check_x[j], solution.x[j], rel_tol, abs_tol ); ok &= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>( check_z_l[j], solution.z_l[j], rel_tol, abs_tol ); ok &= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>( check_z_u[j], solution.z_u[j], rel_tol, abs_tol ); } } return ok; } </pre> </font></code> <hr/>Input File: ipopt_cppad/ipopt_get_started.cpp </body> </html>