| tmain.cpp - numeric - C++ library with numerical algorithms | |
| git clone git://src.adamsgaard.dk/numeric | |
| Log | |
| Files | |
| Refs | |
| LICENSE | |
| --- | |
| tmain.cpp (1845B) | |
| --- | |
| 1 #include <iostream> | |
| 2 #include <armadillo> | |
| 3 #include <functional> | |
| 4 #include "header.h" | |
| 5 #include "functions.h" | |
| 6 #include "downhill_simplex.h" | |
| 7 using namespace arma; | |
| 8 using namespace std; | |
| 9 | |
| 10 int main(int argc, char* argv[]) | |
| 11 { | |
| 12 // Namespace declarations | |
| 13 using std::cout; | |
| 14 | |
| 15 // Calculate machine precision | |
| 16 double eps = 1.0f; | |
| 17 while (1.0f + eps != 1.0f) | |
| 18 eps /= 2.0f; | |
| 19 | |
| 20 cout << "\n\033[1;36m## Minimization with downhill-simplex, part A ##\… | |
| 21 | |
| 22 // Try amoeba on Rosenbrock's valley function | |
| 23 cout << "\n\033[1;33m--- Rosenbrock's valley function ---\033[0m\n"; | |
| 24 int d = 2; | |
| 25 vec p(2); p[0]=5; p[1]=6; | |
| 26 p.print("Initial simplex is chosen around the point:"); | |
| 27 vector<vec> simplex; | |
| 28 for(int i=0; i<d+1; ++i) | |
| 29 simplex.push_back(p); | |
| 30 double dx = 1; | |
| 31 for(int i=0; i<d; ++i) | |
| 32 simplex[i][i] += dx; | |
| 33 amoeba A(rosenbrock, simplex); | |
| 34 ncalls = 0; | |
| 35 A.downhill(10.0f*eps); | |
| 36 A.low().print("Lowest point:"); | |
| 37 cout << "Amoeba converged after " << ncalls << " calls\n" | |
| 38 << "(Newton's root-finding method did this in 4181 calls)\n"; | |
| 39 | |
| 40 // Try amoeba on Himmelblau's function | |
| 41 cout << "\n\033[1;33m--- Himmelblau's function ---\033[0m\n"; | |
| 42 vec p2(2); p2[0]=5; p2[1]=6; | |
| 43 p2.print("Initial simplex is chosen around the point:"); | |
| 44 vector<vec> simplex2; | |
| 45 for(int i=0; i<d+1; ++i) | |
| 46 simplex2.push_back(p2); | |
| 47 double dx2 = 1; | |
| 48 for(int i=0; i<d; ++i) | |
| 49 simplex2[i][i] += dx2; | |
| 50 amoeba A2(himmelblau, simplex2); | |
| 51 ncalls = 0; | |
| 52 A2.downhill(10.0f*eps); | |
| 53 A2.low().print("Lowest point:"); | |
| 54 cout << "Amoeba converged after " << ncalls << " calls\n" | |
| 55 << "(Newton's root-finding method did this in 33 calls)\n"; | |
| 56 | |
| 57 // Return successfully | |
| 58 return 0; | |
| 59 } | |
| 60 | |
| 61 void check(const bool statement) | |
| 62 { | |
| 63 using std::cout; | |
| 64 if (statement == true) | |
| 65 cout << "\t\033[0;32mPassed\033[0m\n"; | |
| 66 else | |
| 67 cout << "\t\033[1;31mFail!!\033[0m\n"; | |
| 68 } |