-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcooperative_coevolution_random_solver.cpp
123 lines (105 loc) · 5.15 KB
/
cooperative_coevolution_random_solver.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
#include <decomposition/fast_interdependency_identification.h>
#include <decomposition/optimization_problem.h>
#include <decomposition/solver.h>
using namespace decompose;
using namespace std;
class problem_example : public optimization_problem {
public:
using super = optimization_problem;
explicit problem_example(size_t dimension, vector<scalar> lower_bound,
vector<scalar> upper_bound)
: super(dimension, std::move(lower_bound), std::move(upper_bound)) {}
scalar value(const vector<scalar> &x) override {
scalar sum = 0.0;
for(size_t i = 0; i < dimension; i++){
sum += pow(x[i], 2);
}
return sum;
}
};
class cooperative_co_evolution_random_solver : public solver {
public:
using super = solver;
cooperative_co_evolution_random_solver() : super() {}
cooperative_co_evolution_random_solver(criteria ¤t_criteria, criteria &stop_criteria, options &o) : super(current_criteria, stop_criteria, o) {}
void generate_random_solutions(optimization_problem &problem_, size_t index_sub_problem, vector<scalar> &x, scalar &fx, size_t number_samples){
vector<scalar> x_tmp = x;
set<size_t> sub_problem = problem_.get_problem_structure()[index_sub_problem];
for(size_t i = 1; i <= number_samples; i++){
for(unsigned long index_var : sub_problem){
uniform_real_distribution<scalar> dist(problem_.get_lower_bound()[index_var], problem_.get_upper_bound()[index_var]);
x_tmp[index_var] = dist(default_generator());
}
status status_ = check_convergence(stop_criteria, current_criteria);
if(status_ == status::Continue){
scalar fx_tmp = problem_.value(x_tmp);
current_criteria.evaluations++;
m_stats.push(fx_tmp, current_criteria.evaluations);
if(fx_tmp < fx){
fx = fx_tmp;
x = x_tmp;
}
}
else{
return;
}
}
}
void minimize(optimization_problem &problem_, vector<scalar> &x0) override{
const size_t number_samples = 100;
scalar best_fx = problem_.value(x0);
current_criteria.evaluations++;
m_stats.push(best_fx, current_criteria.evaluations);
bool is_known_problem_structure = problem_.is_known_problem_structure();
if(m_debug >= debug_level::Low){
cout << "Solver Initialization ..." << endl;
cout << "Iteration: " << current_criteria.iterations << " - Evaluations: " << current_criteria.evaluations << " - f(x0): " << best_fx << endl;
}
if(!is_known_problem_structure){
cerr << "Problem Structure is not Available." << endl;
return;
}
size_t index_sub_problem = 0;
status status_ = check_convergence(stop_criteria, current_criteria);
while(status_ == status::Continue){
generate_random_solutions(problem_, index_sub_problem, x0, best_fx, number_samples);
index_sub_problem = (index_sub_problem+1) % problem_.get_problem_structure().size();
current_criteria.iterations++;
if(m_debug >= debug_level::Low){
cout << "Iteration: " << current_criteria.iterations << " - Evaluations: " << current_criteria.evaluations << " - f(x0): " << best_fx << endl;
}
status_ = check_convergence(stop_criteria, current_criteria);
}
cout << endl << endl << "Solver Status: " << endl << get_status_string(status_) << endl;
cout << "F(x) of Best Solution: " << best_fx << endl << endl;
}
};
int main() {
const size_t dimension = 1000;
const scalar lower_bound = -5.0;
const scalar upper_bound = 10.0;
criteria current_, stop_;
options options_ = options::defaults();
stop_.evaluations = dimension * 1000;
stop_.iterations = 1000;
stop_.fx_is_know = true;
stop_.error_fx_best = 0.001;
stop_.fx_best = 0.0;
problem_example f(dimension, vector<scalar>(dimension, lower_bound), vector<scalar>(dimension, upper_bound));
fast_interdependency_identification method;
method.decompose(f, options_, current_);
cooperative_co_evolution_random_solver solver(current_, stop_, options_);
solver.set_debug(debug_level::None);
vector<scalar> x0(dimension);
for(size_t i = 0; i < dimension; i++){
uniform_real_distribution<scalar> dist(f.get_lower_bound()[i], f.get_upper_bound()[i]);
x0[i] = dist(default_generator());
}
solver.minimize(f, x0);
for(size_t i = 0; i < solver.get_stats().get_history().size(); i++){
cout << "Evaluation: " << solver.get_stats().get_history()[i].evaluation
<< " - F(x): " << solver.get_stats().get_history()[i].fx
<< " - Time: " << solver.get_stats().get_history()[i].time.count() << endl;
}
return 0;
}