-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
110 lines (86 loc) · 2.81 KB
/
main.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
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
#include "hyperdual.h"
#include "matrix.h"
#include "matrix_operation.h"
#include <algorithm>
#include <iomanip>
#include "problem.h"
#include "optimizer.h"
#include "time.h"
using namespace std;
//auto y = vars(1);
//return -exp(-(x-T(3.1))*(x-T(3.1)))+0.1*(x-T(0.4))*(x-T(0.4));
//return exp(-(x-T(3.1))*(x-T(3.1)))+x*y+(y-x)*(y-x);
//return x*x + y*x/2 + 10*sin((x+y)*(x+y)/30);
template <typename T>
struct MyFunctor {
virtual T operator()(const Matrix<T>& vars) const {
auto x = vars(0);
return (x-T(2.2))*x;
}
};
int main() {
{
time_t start_time, end_time;
start_time = clock();
CostGH<MyFunctor> cost(1);
BacktrackingStepSolver stepSolver(1, 0.5, 1e-4);
cost.variables(0) = 3.0f;
DFPRank1QuasiNewtonMethod opt(cost,stepSolver,1e-10);
opt.minimize();
end_time = clock();
cout<<cost.get_variables()<<endl;
cout<<"time used: "<<setprecision(9)<<(end_time-start_time+0.0)/CLOCKS_PER_SEC<<endl;
}
{
time_t start_time, end_time;
start_time = clock();
CostGH<MyFunctor> cost(1);
BacktrackingStepSolver stepSolver(1, 0.5, 1e-4);
cost.variables(0) = 3.0f;
BFGSRank2QuasiNewtonMethod opt(cost,stepSolver,1e-10);
opt.minimize();
end_time = clock();
cout<<cost.get_variables()<<endl;
cout<<"time used: "<<setprecision(9)<<(end_time-start_time+0.0)/CLOCKS_PER_SEC<<endl;
}
{
time_t start_time, end_time;
start_time = clock();
CostGH<MyFunctor> cost(1);
BacktrackingStepSolver stepSolver(1, 0.5, 1e-4);
cost.variables(0) = 3.0f;
SR1QuasiNewtonMethod opt(cost,stepSolver,1e-10);
opt.minimize();
end_time = clock();
cout<<cost.get_variables()<<endl;
cout<<"time used: "<<setprecision(9)<<(end_time-start_time+0.0)/CLOCKS_PER_SEC<<endl;
}
{
time_t start_time, end_time;
start_time = clock();
CostGH<MyFunctor> cost(1);
BacktrackingStepSolver stepSolver(1, 0.5, 1e-4);
cost.variables(0) = 3.0f;
NewtonMethod opt(cost,stepSolver,1e-9);
opt.minimize();
end_time = clock();
cout<<cost.get_variables()<<endl;
cout<<"time used: "<<setprecision(9)<<(end_time-start_time+0.0)/CLOCKS_PER_SEC<<endl;
}
{
time_t start_time, end_time;
start_time = clock();
CostGH<MyFunctor> cost(1);
cost.variables(0) = 3.0f;
ConjugateGradient opt(cost,1e-5);
opt.minimize();
end_time = clock();
cout<<cost.get_variables()<<endl;
cout<<"time used: "<<setprecision(9)<<(end_time-start_time+0.0)/CLOCKS_PER_SEC<<endl;
}
_CrtDumpMemoryLeaks();
}