Optimize an ADFun Object Tape
Keywords: optimize adfun object tape sequence operations speed memory Ndebug syntax purpose f improvements testing efficiency atomic functions rev_sparse_jac nan checking optimization example

| Prev | Next |  |  |  |  |  |

Optimize an ADFun Object Tape
Syntax
`f.optimize()`
Purpose
The operation sequence corresponding to an ADFun object can be very large and involve many operations; see the size functions in seq_property . The `f.optimize` procedure reduces the number of operations, and thereby the time and the memory, required to compute function and derivative values.
f
The object `f` has prototype `
ADFun<Base> f
`
Improvements
You can see the reduction in number of variables in the operation sequence by calling the function f.size_var() 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.
Testing
You can run the CppAD speed tests and see the corresponding changes in number of variables and execution time; see cmake_check .
Efficiency
The `optimize` member function may greatly reduce the number of variables in the operation sequence; see size_var . If a zero order forward calculation is done during the construction of `f` , 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 `
ADFun<Base> f;
f.Dependent(x, y);
f.optimize();
` instead of `
ADFun<Base> f(x, y)
f.optimize();
` See the discussion about sequence constructors .
Atomic Functions
There are some subtitle issue with optimized atomic functions v = g ( u ) :
rev_sparse_jac
The atomic_rev_sparse_jac function is be used to determine which components of `u` affect the dependent variables of `f` . For each atomic operation, the current atomic_sparsity setting is used to determine if `pack_sparsity_enum`, `bool_sparsity_enum`, or `set_sparsity_enum` is used to determine dependency relations between argument and result variables.
nan
If `u[i]` does not affect the value of the dependent variables for `f` , the value of `u[i]` is set to nan .
Checking Optimization
If NDEBUG is not defined, and f.size_order() is greater than zero, a forward_zero calculation is done using the optimized version of `f` and the results are checked to see that they are the same as before. If they are not the same, the ErrorHandler is called with a known error message related to `f.optimize()` .
Example
The file optimize.cpp contains an example and test of this operation. It returns true if it succeeds and false otherwise.

Input File: cppad/local/optimize.hpp