forked from sudz123/Optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
156 lines (121 loc) · 5.93 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <Eigen/Dense>
#include "../Optimizer/optimizer"
using namespace std;
using namespace Eigen;
using namespace Optimizer;
double func (double x) {
// This function will return the square after adding 10 to the given number.
return pow(x + 10, 2);
}
double SumSquares (VectorXd x) {
return x.squaredNorm();
}
double Matyas (Vector2d x) {
return 0.26 * (pow(x(0),2) + pow(x(1),2)) - 0.48 * x(0) * x(1);
}
double Dixon (VectorXd x) {
double val = 0;
for (int i = 0; i < x.size() - 1; ++i)
val += 100 * pow(x(i + 1) - pow(x(i), 2), 2) + pow(x(i) - 1, 2);
return val;
}
double Himmelblau(Vector2d x)
{
return pow((pow(x(0),2) + x(1) -11),2) + pow((x(0) + pow(x(1),2) - 7),2);
}
double func_cust(Vector2d x) {
return pow(x(0)-10, 3) + pow(x(1)-20, 3);
}
double ineq1(Vector2d x){
return - (pow(x(0)-5, 2) + pow(x(1)-5, 2) + 100);
}
double ineq2(Vector2d x){
return - (pow(x(0)-5, 2) + pow(x(1)-5, 2) - 82.81);
}
int main () {
cout << "Using Function: (x + 10)^2 for single variable algorithms testing." << endl;
double ipt = 5.4;
cout << "Test Bounding Phase:" << endl;
Vector2d range = BoundingPhase(func, ipt);
cout << "Range from bounding Phase for initial point :" << ipt << endl;
cout << range << endl;
cout << "Test Exhaustive Search:" << endl;
range = Exhaustive(func, ipt);
cout << "Range from Exhaustive Search for initial point :" << ipt << endl;
cout << range << endl;
cout << "Derivatives at " << ipt << endl;
cout << Derivative(func, ipt) << endl;
cout << "Finding optimal point using above range for Newton Rapshon Method." << endl;
cout << "Optimal Point is: ";
cout << NewtonRapshon (func, range) << endl;
cout << "Finding optimal point using above range for Secant Method." << endl;
cout << "Optimal Point is: ";
cout << Secant (func, range) << endl;
cout << "Finding optimal point using above range for Golden Section Search Method." << endl;
cout << "Optimal Point is: ";
cout << GoldenSection (func, range) << endl;
cout << "Finding optimal point using above range for Interval Halving Method." << endl;
cout << "Optimal Point is: ";
cout << IntervalHalving (func, range) << endl;
cout << "Finding optimal point using above range for Fibonnaci Search Method." << endl;
cout << "Optimal Point is: ";
cout << Fibonacci (func, range) << endl;
cout << "Testing SVOptimize on (x + 10)^2 with initial point 5.4." << endl;
cout << "The Optimal Point obtained is: ";
cout << SVOptimize(func, 5.4) << endl;
cout << "Testing Bisection method on (x + 10)^2" << endl;
cout << "The Optimal Point obtained is: ";
cout << Bisection(func, Eigen::Vector2d(-20,1) ,0.001, 10000) << endl;
cout << "Testing Gradient function using SumSquares with 3 variables" << endl;
cout << Gradient(SumSquares, Vector3d(3, 3, 4)) << endl;
cout << "Testing Hessian function using SumSquares with 3 variables" << endl;
cout << Hessian(SumSquares, Vector3d(3, 3, 4)) << endl;
Vector3d x(4, -3, 7);
cout << "Testing MVOptimize on SumSquares with initial point (4, -3, 7)." << endl;
cout << "The Optimal Point obtained is: ";
cout << MVOptimize(SumSquares, x, 200, MVO::NEWTON) << endl;
cout << "Testing DFP on sum squared function with 3 variables and initial point (4, -3, 7)." << endl;
cout << "The Optimal Point obtained is: " << endl;
cout << DFP(SumSquares, x) << endl;
cout << "Testing Newton's method on sum squared function with 3 variables and initial point (4, -3, 7)." << endl;
cout << "The Optimal Point obtained is: " << endl;
cout << Newton(SumSquares, x) << endl;
cout << "Testing Cauchy's method on sum squared function with 3 variables and initial point (4, -3, 7)." << endl;
cout << "The Optimal Point obtained is: " << endl;
cout << Cauchy(SumSquares, x) << endl;
cout << "Testing Marquardt's method on sum squared function with 3 variables and initial point (4, -3, 7)." << endl;
cout << "The Optimal Point obtained is: " << endl;
cout << Marquardt(SumSquares, x) << endl;
cout << "Testing Conjugate Gradient method on sum squared function with 3 variables and initial point (4, -3, 7)." << endl;
cout << "The Optimal Point obtained is: " << endl;
cout << ConjugateGradient(SumSquares, x) << endl;
cout << "Testing Conjugate Gradient method on Matyas function with 2 variables and initial point (-3, 7)." << endl;
cout << "The Optimal Point obtained is: " << endl;
cout << ConjugateGradient(Matyas, Vector2d(-3, 7)) << endl;
cout << "Testing Conjugate Gradient method on Dixon function with initial point (-3, 8, 0)." << endl;
cout << "The Optimal Point obtained is: " << endl;
cout << ConjugateGradient(Dixon, Vector3d(-3, 8, 0), 30000) << endl;
cout << "Testing Simplex method on the Himmelblau function with two variables" << endl;
cout << "The Optimal Point obtained is: " << endl;
VectorXd vec(2);
vec(0) = 1;
vec(1) = 2;
cout << Simplex(Himmelblau,vec,10000, 2, 0.5) << endl;
cout << "Testing Penalty Function Method with custom function" << endl;
cout << "The Optimal Point obtained is: " << endl;
Vector2d x1(2, 2);
vector<std::function<double (Eigen::VectorXd)>> eq_const;
vector<std::function<double (Eigen::VectorXd)>> ineq_const{[](Vector2d x) { return pow(x(0) - 5, 2) + pow(x(1), 2) - 26; },
[](Vector2d x) { return x(0); }, [](Vector2d x) { return x(1); }};
cout << PenaltyConstrained(Himmelblau, x1, ineq_const, eq_const) << endl;
cout << "Testing Multiplier Constrained Method with custom function" << endl;
cout << "The Optimal Point obtained is: " << endl;
cout << MultiplierConstrained(Himmelblau, x1, ineq_const, eq_const) << endl;
cout << "Testing Data Save Methods" << endl;
Optimum point1;
point1.path = Hessian(SumSquares, Vector3d(3, 3, 4));
point1.SaveAsTxt();
point1.SaveAsCsv();
cout << "TEST SUCCESSFUL" << endl;
}