-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsphere_II.cpp
More file actions
89 lines (78 loc) · 2.76 KB
/
Copy pathsphere_II.cpp
File metadata and controls
89 lines (78 loc) · 2.76 KB
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
#include <iostream>
#include <solver/common.h>
#include <solver/problem/bounded_problem_interface.h>
#include <solver/solver/local_search/gradient_descent_solver.h>
using namespace solver;
template<typename T>
class sphere : public bounded_problem_interface<T> {
public:
using Superclass = bounded_problem_interface<T>;
using typename Superclass::vector_t;
public:
explicit sphere(int dim) : Superclass(dim) {
this->setLowerBound(vector_t::Ones(dim) * -100.0);
this->setUpperBound(vector_t::Ones(dim) * 100.0);
}
T value(const vector_t &x) override {
T sum = 0.0;
for(int i = 0; i < x.size(); i++){
sum += x[i] * x[i];
}
return sum;
}
};
int main(){
solver::criteria<double> criteria_ = solver::criteria<double>::defaults();
criteria_.iterations = 1000;
criteria_.evaluations = 100000;
criteria_.gradNorm = -0.1;
criteria_.fx_is_know = true;
criteria_.fx_best = 0.0;
const size_t DIM = 10;
typedef double scalar;
typedef sphere<scalar> sphere;
typedef typename sphere::vector_t vector_t;
sphere f(DIM);
vector_t x_copy = 100 * vector_t::Random(DIM);
for(size_t i = 0; i < 4; i++) {
gradient_descent_solver<sphere> solver;
options<scalar> op = options<scalar>::defaults();
switch(i) {
case 0:{
op.set_line_search(line_search::AcceleratedStepSize);
cout << "Gradient Descent with Accelerated Step Size Line Search" << endl;
break;
}
case 1:{
cout << "Gradient Descent with Brent Line Search" << endl;
op.set_line_search(line_search::Brent);
break;
}
case 2:{
cout << "Gradient Descent with Golden Section Line Search" << endl;
op.set_line_search(line_search::GoldenSection);
break;
}
case 3:{
cout << "Gradient Descent with Interval Halving Line Search" << endl;
op.set_line_search(line_search::IntervalHalving);
break;
}
default:
cerr << "Selected Line Search Algorithm invalid." << endl;
}
vector_t x0 = x_copy;
solver.set_stop_criteria(criteria_);
solver.set_options(op);
solver.minimize(f, x0);
cout << "f in argmin: " << f(x0) << endl;
cout << "Solver status: " << solver.get_status() << endl << endl;
cout << "Final criteria dfvalues: " << endl << solver.criteria();
cout << "x0: [";
for (size_t j = 0; j < DIM - 1; j++) {
cout << x0[j] << ", ";
}
cout << x0[DIM - 1] << "]" << endl << endl << endl << endl;
}
return 0;
}