00001 #include <TooN/optimization/downhill_simplex.h> 00002 #include <iomanip> 00003 using namespace std; 00004 using namespace TooN; 00005 00006 00007 double sq(double x) 00008 { 00009 return x*x; 00010 } 00011 00012 double Rosenbrock(const Vector<2>& v) 00013 { 00014 return sq(1 - v[0]) + 100 * sq(v[1] - sq(v[0])); 00015 } 00016 00017 double Spiral(const Vector<2>& v) 00018 { 00019 double x = v[0]; 00020 double y = v[1]; 00021 return sin(20.0*sqrt(x*x+y*y)+2.0*atan(y/x))+2.0*x*x+2.0*y*y; 00022 } 00023 00024 00025 int main() 00026 { 00027 cout << setprecision(16); 00028 Vector<2> starting_point = makeVector(1.5, 1.5); 00029 00030 DownhillSimplex<2> dh_fixed(Spiral, starting_point, .001); 00031 00032 cout << "#> ignore" << endl; 00033 00034 while(dh_fixed.iterate(Spiral)) 00035 { 00036 cout << dh_fixed.get_simplex()[0] << dh_fixed.get_values()[0] << endl; 00037 cout << dh_fixed.get_simplex()[1] << dh_fixed.get_values()[1] << endl; 00038 cout << dh_fixed.get_simplex()[2] << dh_fixed.get_values()[2] << endl; 00039 cout << endl; 00040 } 00041 00042 cout << "#> resume" << endl; 00043 00044 cout << dh_fixed.get_simplex()[dh_fixed.get_best()] << endl 00045 << dh_fixed.get_values()[dh_fixed.get_best()] << endl; 00046 00047 DownhillSimplex<> dh_variable(Spiral, starting_point, .001); 00048 00049 while(dh_variable.iterate(Spiral)); 00050 00051 cout << dh_variable.get_simplex()[dh_variable.get_best()] << endl 00052 << dh_variable.get_values()[dh_variable.get_best()] << endl; 00053 } 00054 00055