<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Optimize an ADFun Object Tape</title>
<meta name="description" id="description" content="Optimize an ADFun Object Tape"/>
<meta name="keywords" id="keywords" content=" optimize tape sequence operations speed memory Ndebug "/>
<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='_optimize_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="fun_check.cpp.xml" target="_top">Prev</a>
</td><td><a href="optimize.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>ADFun</option>
<option>optimize</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>ADFun-&gt;</option>
<option>Independent</option>
<option>FunConstruct</option>
<option>Dependent</option>
<option>abort_recording</option>
<option>seq_property</option>
<option>FunEval</option>
<option>Drivers</option>
<option>FunCheck</option>
<option>optimize</option>
<option>check_for_nan</option>
</select>
</td>
<td>
<select onchange='choose_down0(this)'>
<option>optimize-&gt;</option>
<option>optimize.cpp</option>
</select>
</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Syntax</option>
<option>Purpose</option>
<option>f</option>
<option>Improvements</option>
<option>---..Testing</option>
<option>Efficiency</option>
<option>Comparison Operators</option>
<option>Atomic Functions</option>
<option>---..rev_sparse_jac</option>
<option>---..nan</option>
<option>Checking Optimization</option>
<option>Example</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Optimize an ADFun Object Tape</big></big></b></center>
<br/>
<b><big><a name="Syntax" id="Syntax">Syntax</a></big></b>

<br/>

<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>.optimize()</span></font></code>



<br/>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
The operation sequence corresponding to an <a href="adfun.xml" target="_top"><span style='white-space: nowrap'>ADFun</span></a>
 object can
be very large and involve many operations; see the
size functions in <a href="seq_property.xml" target="_top"><span style='white-space: nowrap'>seq_property</span></a>
.
The 
<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>.optimize</span></font></code>
 procedure reduces the number of operations,
and thereby the time and the memory, required to
compute function and derivative values. 

<br/>
<br/>
<b><big><a name="f" id="f">f</a></big></b>
<br/>
The object 
<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;ADFun&lt;</span></font><i><font color="black"><span style='white-space: nowrap'>Base</span></font></i><font color="blue"><span style='white-space: nowrap'>&gt;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
<br/>
<b><big><a name="Improvements" id="Improvements">Improvements</a></big></b>
<br/>
You can see the reduction in number of variables in the operation sequence
by calling the function <a href="seq_property.xml#size_var" target="_top"><span style='white-space: nowrap'>f.size_var()</span></a>

before and after the optimization procedure.
Given that the optimization procedure takes time,
it may be faster to skip this optimize procedure and just compute
derivatives using the original operation sequence.

<br/>
<br/>
<b><a name="Improvements.Testing" id="Improvements.Testing">Testing</a></b>
<br/>
You can run the CppAD <a href="speed_main.xml" target="_top"><span style='white-space: nowrap'>speed</span></a>
 tests and see
the corresponding changes in number of variables and execution time; 
see <a href="cmake_check.xml" target="_top"><span style='white-space: nowrap'>cmake_check</span></a>
.

<br/>
<br/>
<b><big><a name="Efficiency" id="Efficiency">Efficiency</a></big></b>
<br/>
The <code><font color="blue">optimize</font></code> member function
may greatly reduce the number of variables 
in the operation sequence; see <a href="seq_property.xml#size_var" target="_top"><span style='white-space: nowrap'>size_var</span></a>
.
If a <a href="forward_zero.xml" target="_top"><span style='white-space: nowrap'>zero&#xA0;order&#xA0;forward</span></a>
 calculation is done during
the construction of 
<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i></code>
, it will require more memory
and time than required after the optimization procedure.
In addition, it will need to be redone.
For this reason, it is more efficient to use 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;ADFun&lt;</span></font><i><font color="black"><span style='white-space: nowrap'>Base</span></font></i><font color="blue"><span style='white-space: nowrap'>&gt;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>;<br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>.Dependent(</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>y</span></font></i><font color="blue"><span style='white-space: nowrap'>);<br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>.optimize();<br/>
</span></font></code>
instead of

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;ADFun&lt;</span></font><i><font color="black"><span style='white-space: nowrap'>Base</span></font></i><font color="blue"><span style='white-space: nowrap'>&gt;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>(</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>y</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>.optimize();<br/>
</span></font></code>
See the discussion about
<a href="funconstruct.xml#Sequence Constructor" target="_top"><span style='white-space: nowrap'>sequence&#xA0;constructors</span></a>
.

<br/>
<br/>
<b><big><a name="Comparison Operators" id="Comparison Operators">Comparison Operators</a></big></b>
<br/>
Any comparison operators that are in the tape are removed by this operation.
Hence the return value of <a href="comparechange.xml" target="_top"><span style='white-space: nowrap'>CompareChange</span></a>
 will always be zero
for an optimized tape (even if <code><font color="blue">NDEBUG</font></code> is not defined).

<br/>
<br/>
<b><big><a name="Atomic Functions" id="Atomic Functions">Atomic Functions</a></big></b>
<br/>
There are some subtitle issue with optimized <a href="atomic.xml" target="_top"><span style='white-space: nowrap'>atomic</span></a>
 functions

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>v</mi>
<mo stretchy="false">=</mo>
<mi mathvariant='italic'>g</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>u</mi>
<mo stretchy="false">)</mo>
</mrow></math>

:

<br/>
<br/>
<b><a name="Atomic Functions.rev_sparse_jac" id="Atomic Functions.rev_sparse_jac">rev_sparse_jac</a></b>
<br/>
The <a href="atomic_rev_sparse_jac.xml" target="_top"><span style='white-space: nowrap'>atomic_rev_sparse_jac</span></a>
 function is be used to determine
which components of 
<code><i><font color="black"><span style='white-space: nowrap'>u</span></font></i></code>
 affect the dependent variables of 
<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i></code>
.
The current setting of the 
<a href="atomic_option.xml#atomic_sparsity" target="_top"><span style='white-space: nowrap'>atomic_sparsity</span></a>
 pattern for each
atomic function is used to determine if the <code><font color="blue">bool</font></code> or 
<code><font color="blue">std::set&lt;size_t&gt;</font></code> version of <a href="atomic_rev_sparse_jac.xml" target="_top"><span style='white-space: nowrap'>atomic_rev_sparse_jac</span></a>
 is used.

<br/>
<br/>
<b><a name="Atomic Functions.nan" id="Atomic Functions.nan">nan</a></b>
<br/>
If 
<code><i><font color="black"><span style='white-space: nowrap'>u</span></font></i><font color="blue"><span style='white-space: nowrap'>[</span></font><i><font color="black"><span style='white-space: nowrap'>i</span></font></i><font color="blue"><span style='white-space: nowrap'>]</span></font></code>
 does not affect the value of
the dependent variables for 
<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i></code>
,
the value of 
<code><i><font color="black"><span style='white-space: nowrap'>u</span></font></i><font color="blue"><span style='white-space: nowrap'>[</span></font><i><font color="black"><span style='white-space: nowrap'>i</span></font></i><font color="blue"><span style='white-space: nowrap'>]</span></font></code>
 is set to <a href="nan.xml" target="_top"><span style='white-space: nowrap'>nan</span></a>
.


<br/>
<br/>
<b><big><a name="Checking Optimization" id="Checking Optimization">Checking Optimization</a></big></b>

<br/>
If <a href="faq.xml#Speed.NDEBUG" target="_top"><span style='white-space: nowrap'>NDEBUG</span></a>
 is not defined,
and <a href="size_order.xml" target="_top"><span style='white-space: nowrap'>f.size_order()</span></a>
 is greater than zero,
a <a href="forward_zero.xml" target="_top"><span style='white-space: nowrap'>forward_zero</span></a>
 calculation is done using the optimized version
of 
<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i></code>
 and the results are checked to see that they are
the same as before.
If they are not the same, the
<a href="errorhandler.xml" target="_top"><span style='white-space: nowrap'>ErrorHandler</span></a>
 is called with a known error message
related to 
<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>.optimize()</span></font></code>
.

<br/>
<br/>
<b><big><a name="Example" id="Example">Example</a></big></b>

<br/>
The file
<a href="optimize.cpp.xml" target="_top"><span style='white-space: nowrap'>optimize.cpp</span></a>

contains an example and test of this operation.
It returns true if it succeeds and false otherwise.


<hr/>Input File: cppad/local/optimize.hpp

</body>
</html>
