tmain.B.cpp - numeric - C++ library with numerical algorithms
(HTM) git clone git://src.adamsgaard.dk/numeric
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tmain.B.cpp (2518B)
---
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 B ##\033[0m\n";
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 if (verbose == true)
27 p.print("Initial simplex is chosen around the point:");
28 vector<vec> simplex;
29 vector<vec> simplex_mod;
30 for(int i=0; i<d+1; ++i) {
31 simplex.push_back(p);
32 simplex_mod.push_back(p);
33 }
34 double dx = 1;
35 for(int i=0; i<d; ++i) {
36 simplex[i][i] += dx;
37 simplex_mod[i][i] += dx;
38 }
39 amoeba A(rosenbrock, simplex);
40 ncalls = 0;
41 A.downhill(10.0f*eps);
42 if (verbose == true)
43 A.low().print("Lowest point:");
44 cout << "Amoeba converged after " << ncalls << " calls\n";
45 amoeba A_mod(rosenbrock, simplex_mod);
46 ncalls = 0;
47 A_mod.downhill_mod(10.0f*eps);
48 if (verbose == true)
49 A_mod.low().print("Lowest point (modified downhill)");
50 cout << "Amoeba converged after " << ncalls << " calls with modified method\n";
51
52
53 // Try amoeba on Himmelblau's function
54 cout << "\n\033[1;33m--- Himmelblau's function ---\033[0m\n";
55 vec p2(2); p2[0]=5; p2[1]=6;
56 if (verbose == true)
57 p2.print("Initial simplex is chosen around the point:");
58 vector<vec> simplex2;
59 vector<vec> simplex2_mod;
60 for(int i=0; i<d+1; ++i) {
61 simplex2.push_back(p2);
62 simplex2_mod.push_back(p2);
63 }
64 double dx2 = 1;
65 for(int i=0; i<d; ++i) {
66 simplex2[i][i] += dx2;
67 simplex2_mod[i][i] += dx2;
68 }
69 amoeba A2(himmelblau, simplex2);
70 ncalls = 0;
71 A2.downhill(10.0f*eps);
72 if (verbose == true)
73 A2.low().print("Lowest point:");
74 cout << "Amoeba converged after " << ncalls << " calls\n";
75 amoeba A2_mod(himmelblau, simplex2_mod);
76 ncalls = 0;
77 A2_mod.downhill_mod(10.0f*eps);
78 if (verbose == true)
79 A2_mod.low().print("Lowest point (modified downhill)");
80 cout << "Amoeba converged after " << ncalls << " calls with modified method\n";
81
82 // Return successfully
83 return 0;
84 }
85
86 void check(const bool statement)
87 {
88 using std::cout;
89 if (statement == true)
90 cout << "\t\033[0;32mPassed\033[0m\n";
91 else
92 cout << "\t\033[1;31mFail!!\033[0m\n";
93 }