-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparison.cpp
More file actions
128 lines (105 loc) · 4.82 KB
/
comparison.cpp
File metadata and controls
128 lines (105 loc) · 4.82 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
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
/*
* Copyright Nick Thompson, 2023
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include <iostream>
#include <math.h>
#include <fstream>
#include "./optimization/jso.hpp"
#include "./optimization/jso_fix.hpp"
using boost::math::optimization::jso_parameters;
using boost::math::optimization::jso;
using boost::math::optimization::jso_fix_parameters;
using boost::math::optimization::jso_fix;
double rosenbrock(std::vector<double> const & x) {
double result = 0;
for (size_t i = 0; i < x.size() - 1; ++i) {
double tmp = x[i+1] - x[i]*x[i];
result += 100*tmp*tmp + (1-x[i])*(1-x[i]);
}
return result;
}
std::vector<double> test_jso_fix(size_t D = 20) {
auto de_params = jso_fix_parameters<std::vector<double>>();
const size_t dimension = D;
// Search on [0, 100]^dimension:
de_params.lower_bounds.resize(dimension, 0);
de_params.upper_bounds.resize(dimension, 100);
de_params.max_function_evaluations = std::ceil(10000 * D * std::log(D));
std::random_device rd;
std::mt19937_64 rng(rd());
std::vector<double> convergence_data;
auto generation_callback = [&convergence_data](double cost) {
convergence_data.push_back(cost);
};
std::atomic<bool>* stop_signal = nullptr;
std::atomic<double>* best_cost = nullptr;
std::vector<std::pair<std::vector<double>, double>>* population_history = nullptr;
auto local_minima = jso_fix(rosenbrock, de_params, rng,
std::numeric_limits<double>::quiet_NaN(),
stop_signal, best_cost, population_history, generation_callback);
return convergence_data;
}
std::vector<double> test_jso(size_t D = 20) {
auto de_params = jso_parameters<std::vector<double>>();
const size_t dimension = D;
// Search on [0, 100]^dimension:
de_params.lower_bounds.resize(dimension, 0);
de_params.upper_bounds.resize(dimension, 100);
de_params.max_function_evaluations = std::ceil(10000 * D * std::log(D));
std::random_device rd;
std::mt19937_64 rng(rd());
std::vector<double> convergence_data;
auto generation_callback = [&convergence_data](double cost) {
convergence_data.push_back(cost);
};
std::atomic<bool>* stop_signal = nullptr;
std::atomic<double>* best_cost = nullptr;
std::vector<std::pair<std::vector<double>, double>>* population_history = nullptr;
auto local_minima = jso(rosenbrock, de_params, rng,
std::numeric_limits<double>::quiet_NaN(),
stop_signal, best_cost, population_history, generation_callback);
return convergence_data;
}
int main() {
int N = 20;
std::vector<size_t> dimensions = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
for (size_t dimension : dimensions) {
std::vector<std::vector<double>> convergence_data_jso;
std::vector<std::vector<double>> convergence_data_jso_fix;
std::cout << "Testing with dimension: " << dimension << "\n";
for (size_t i = 0; i < N; ++i) {
std::cout << "Iteration: " << i << "\n";
convergence_data_jso.push_back(test_jso(dimension));
convergence_data_jso_fix.push_back(test_jso_fix(dimension));
}
// Write to a txt file, one file per dimension, named result_dimension.txt.
// The content should include generation, jso_mean, jso_min, jso_max, jso_fix_mean, jso_fix_min, jso_fix_max.
std::ofstream outfile("./data/result_" + std::to_string(dimension) + ".txt");
if (!outfile.is_open()) {
std::cerr << "Error: Unable to open file " << "result_" << dimension << ".txt" << std::endl;
}
size_t generation = convergence_data_jso.back().size();
for (size_t i = 0; i < generation; ++i) {
double jso_mean = 0, jso_fix_mean = 0;
double jso_min = std::numeric_limits<double>::max(), jso_fix_min = std::numeric_limits<double>::max();
double jso_max = std::numeric_limits<double>::min(), jso_fix_max = std::numeric_limits<double>::min();
for (size_t j = 0; j < N; ++j) {
jso_mean += convergence_data_jso[j][i];
jso_min = std::min(jso_min, convergence_data_jso[j][i]);
jso_max = std::max(jso_max, convergence_data_jso[j][i]);
jso_fix_mean += convergence_data_jso_fix[j][i];
jso_fix_min = std::min(jso_fix_min, convergence_data_jso_fix[j][i]);
jso_fix_max = std::max(jso_fix_max, convergence_data_jso_fix[j][i]);
}
jso_mean /= N;
jso_fix_mean /= N;
outfile << i << "," << jso_mean << "," << jso_min << "," << jso_max << ","
<< jso_fix_mean << "," << jso_fix_min << "," << jso_fix_max << "\n";
}
outfile.close();
}
return 0;
}