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<
Basef

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 the speed tests in the install instructions Examples and Tests list.

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<
Basef;
     
f.Dependent(xy);
     
f.optimize();
instead of
     ADFun<
Basef(xy)
     
f.optimize();
See the discussion about sequence constructors .

Comparison Operators
Any comparison operators that are in the tape are removed by this operation. Hence the return value of CompareChange will always be zero for an optimized tape (even if NDEBUG is not defined).

Checking Optimization
If NDEBUG is not defined, and f.size_taylor() is greater than zero, a ForwardZero 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