1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /**
- * @file testWackerOptimization.cpp
- * @brief test the downhill simplex method on a toy example
- * @author Erik Rodner
- * @date 01/31/2012
- */
- #include "core/basics/Config.h"
- #include "optimization/DownhillSimplexOptimizer.h"
- #include "optimization/GoldenCutLineSearcher.h"
- #include "optimization/FileLog.h"
- using namespace std;
- using namespace NICE;
- const int dim = 1;
- class MyCostFunction : public CostFunction
- {
- public:
-
- MyCostFunction() : CostFunction(dim)
- {
- }
- virtual double evaluate(const optimization::matrix_type & x)
- {
- double f;
- cerr << x.rows() << " x " << x.cols() << endl;
- if ( x.rows() == 1 )
- {
- cerr << "current position: " << x[0][0] << endl;
- f = pow(x[0][0] - 0.3, 2.0);
- cerr << "function value: " << f << endl;
- } else {
- cerr << "current position: " << x[0][0] << " " << x[1][0] << endl;
- f = pow(x[0][0] - 0.3, 2.0) + pow( x[1][0] - 0.2, 2.0 );
- cerr << "function value: " << f << endl;
- }
- return f;
- }
- };
- /**
-
- test the downhill simplex method on a toy example
-
- */
- int main (int argc, char **argv)
- {
- std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
- Config conf ( argc, argv );
-
-
- CostFunction *func = new MyCostFunction();
- optimization::matrix_type initialParams (dim, 1);
- initialParams.Set(0.8);
- if ( dim == 2 )
- cerr << initialParams[0][0] << " " << initialParams[1][0] << endl;
- optimization::matrix_type scales (dim, 1);
- scales.Set(1.0);
- SimpleOptProblem optProblem ( func, initialParams, scales );
-
- bool useDownhill = conf.gB("main", "use_downhill", true );
- if ( useDownhill ) {
- DownhillSimplexOptimizer optimizer;
- optimizer.setMaxNumIter(true, conf.gI("main", "max_iterations", 10));
- optimizer.optimizeProb ( optProblem );
-
- /* Contraints are not working for DownhillSimplexOptimizer
- optProblem.setUpperBound(0, 1.0);
- optProblem.setLowerBound(0, 0.0);
- optProblem.setUpperBound(1, 1.0);
- optProblem.setLowerBound(1, 0.0);
- */
- } else {
- FileLog fl ("/tmp/optimizer.log");
- GoldenCutLineSearcher optimizer ( func, &fl );
- optimizer.setBounds(0.0, 1.0);
- optimizer.optimize();
- }
-
-
- delete func;
- return 0;
- }
|