<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Nonlinear Programming Retaping: Example and Test</title>
<meta name="description" id="description" content="Nonlinear Programming Retaping: Example and Test"/>
<meta name="keywords" id="keywords" content=" retape ipopt_solve "/>
<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_solve_retape.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_solve_get_started.cpp.xml" target="_top">Prev</a>
</td><td><a href="ipopt_solve_ode_inverse.cpp.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</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-&gt;</option>
<option>CppAD</option>
<option>ipopt_solve</option>
<option>ipopt_solve_retape.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>ipopt_solve-&gt;</option>
<option>ipopt_solve_get_started.cpp</option>
<option>ipopt_solve_retape.cpp</option>
<option>ipopt_solve_ode_inverse.cpp</option>
</select>
</td>
<td>ipopt_solve_retape.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Purpose</option>
</select>
</td>
</tr></table><br/>





<center><b><big><big>Nonlinear Programming Retaping: Example and Test</big></big></b></center>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
This example program demonstrates a case were the <code><font color="blue">ipopt::solve</font></code>
argument <a href="ipopt_solve.xml#options.Retape" target="_top"><span style='white-space: nowrap'>retape</span></a>
 should be true.

<code><font color="blue">
<pre style='display:inline'> 
# include &lt;cppad/ipopt/solve.hpp&gt;

namespace {
	using CppAD::AD;

	class FG_eval {
	public:
		typedef <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>( <a href="ad.xml" target="_top">AD</a>&lt;double&gt; ) ADvector;
		void operator()(ADvector&amp; fg, const ADvector&amp; x)
		{	assert( fg.size() == 1 );
			assert( x.size()  == 1 );

			// compute the Huber function using a conditional
			// statement that depends on the value of x.
			double eps = 0.1;
			if( abs(x[0]) &lt;= eps )
				fg[0] = x[0] * x[0] / (2.0 * eps);
			else
				fg[0] = abs(x[0]) - eps / 2.0;

			return;
		}
	};
}
	
bool retape(void)
{	bool ok = true;
	typedef <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>( double ) Dvector;

	// number of independent variables (domain dimension for f and g)
	size_t nx = 1;  
	// number of constraints (range dimension for g)
	size_t ng = 0;
	// initial value, lower and upper limits, for the independent variables
	Dvector xi(nx), xl(nx), xu(nx);
	xi[0] = 2.0;
	xl[0] = -1e+19;
	xu[0] = +1e+19;
	// lower and upper limits for g
	Dvector gl(ng), gu(ng);

	// object that computes objective and constraints
	FG_eval fg_eval;

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

	// place to return solution
	CppAD::ipopt::solve_result&lt;Dvector&gt; solution;

	// solve the problem
	CppAD::ipopt::solve&lt;Dvector, FG_eval&gt;(
		options, xi, xl, xu, gl, gu, fg_eval, solution
	);
	//
 	// Check some of the solution values
 	//
	ok &amp;= solution.status == CppAD::ipopt::solve_result&lt;Dvector&gt;::success;
	double rel_tol    = 1e-6;  // relative tolerance
	double abs_tol    = 1e-6;  // absolute tolerance
	ok &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>( solution.x[0], 0.0,  rel_tol, abs_tol);

	return ok;
}
</pre>

</font></code>


<hr/>Input File: example/ipopt_solve/retape.cpp

</body>
</html>
