-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathea_main.cpp
More file actions
258 lines (208 loc) · 9.28 KB
/
ea_main.cpp
File metadata and controls
258 lines (208 loc) · 9.28 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
#include <print>
#include <sstream>
#include <fstream>
#include <nlohmann/json.hpp> //external dependency included here
#include <thread>
#include <util/enumutils.h>
#ifdef EA_PARALLEL
#include <tbb/global_control.h> //TBB as the backend for paralelism
#endif
#include <fitness/ObjectiveFunction.h>
#include <fitness/BinaryEncodedContinuousObjectiveFunction.h>
#include <problem/discrete/binary/onemax/OneMax.h>
#include <problem/continuous/Rastrigin.h>
#include <problem/continuous/Rosenbrock.h>
#include <problem/continuous/Sphere.h>
#include <problem/continuous/Ackley.h>
#include <problem/continuous/Griewank.h>
#include <problem/continuous/Schaffer2.h>
#include <problem/continuous/Schwefel.h>
#include <problem/continuous/Solomon.h>
#include <problem/continuous/XinSheYang2.h>
#include <statistics/DiversityMeasure.h>
#include <statistics/EntropyDiversity.h>
#include <config/EAConfiguration.h>
#include <base/EvolutionaryAlgorithm.h>
#include <statistics/VarianceDiversity.h>
using json = nlohmann::json;
ENUM(enumObjectiveFunctionType, int, ONEMAX, TRAP, MMDP, LEADING_ONES, TSP, LOP, SPHERE, BIN_SPHERE, ES_ESPHERE, ACKLEY, RASTRIGIN, ROSENBROCK, GRIEWANK, SCHAFFER2, SCHWEFEL, SOLOMON, XINSHEYANG2)
std::unique_ptr<ea::ObjectiveFunction> create(std::string &problem, const int & arg1, const double & arg2 ) {
size_t found = problem.find('-', 0);
if (found != std::string::npos)
problem.replace(found, 1, "_");
switch (enumObjectiveFunctionType::_from_string_nocase(problem.c_str())) {
case enumObjectiveFunctionType::ONEMAX:
return std::make_unique <ea::OneMax>(arg1);
case enumObjectiveFunctionType::ACKLEY:
return std::make_unique <ea::Ackley>(arg1, arg2);
case enumObjectiveFunctionType::RASTRIGIN:
return std::make_unique <ea::Rastrigin>(arg1, arg2);
case enumObjectiveFunctionType::ROSENBROCK:
return std::make_unique <ea::Rosenbrock>(arg1, arg2);
case enumObjectiveFunctionType::SPHERE:
return std::make_unique <ea::Sphere>(arg1, arg2);
case enumObjectiveFunctionType::GRIEWANK:
return std::make_unique <ea::Griewank>(arg1, arg2);
case enumObjectiveFunctionType::SCHAFFER2:
return std::make_unique <ea::Schaffer2>(arg1, arg2);
case enumObjectiveFunctionType::SCHWEFEL:
return std::make_unique <ea::Schwefel>(arg1, arg2);
case enumObjectiveFunctionType::SOLOMON:
return std::make_unique <ea::Solomon>(arg1, arg2);
case enumObjectiveFunctionType::XINSHEYANG2:
return std::make_unique <ea::XinSheYang2>(arg1, arg2);
/*
case enumObjectiveFunctionType::TRAP:
return new DeceptiveTrap(Integer.parseInt(problem.get(1)), Integer.parseInt(problem.get(2)));
case enumObjectiveFunctionType::MMDP:
return new MMDP(Integer.parseInt(problem.get(1)));
case enumObjectiveFunctionType::LEADING-ONES:
return new LeadingOnes(Integer.parseInt(problem.get(1)));
case enumObjectiveFunctionType::TSP:
ATSP atsp = new ATSP(Integer.parseInt(problem.get(1)));
return new TSPObjectiveFunction(atsp);
case enumObjectiveFunctionType::LOP:
LinearOrderingProblem lop = new LinearOrderingProblem(Integer.parseInt(problem.get(1)));
return new LOPObjectiveFunction(lop);
case enumObjectiveFunctionType::BIN-SPHERE:
return new BinaryEncodedContinuousObjectiveFunction(Integer.parseInt(problem.get(3)), new Sphere(Integer.parseInt(problem.get(1)), Double.parseDouble(problem.get(2))));
case enumObjectiveFunctionType::ES-SPHERE:
return new ESObjectiveFunction(new Sphere(Integer.parseInt(problem.get(1)), Double.parseDouble(problem.get(2))));
*/
default:
return nullptr;
}
}
/**
* Creates a diversity measure for a given problem
* @param problem name of the objective function
* @return a diversity measure suited to the objective function indicated
*/
/*
ENUM(enumDiversityMeasureType, int, ONEMAX, TRAP, MMDP, LEADING_ONES, TSP, LOP, SPHERE, BIN_SPHERE, ES_ESPHERE)
std::unique_ptr<ea::DiversityMeasure> createMeasure(std::string& problem) {
size_t found = problem.find('-', 0);
if (found != std::string::npos)
problem.replace(found, 1, "_");
switch (enumDiversityMeasureType::_from_string_nocase(problem.c_str())) {
case enumDiversityMeasureType::ONEMAX:
case enumDiversityMeasureType::TRAP:
case enumDiversityMeasureType::MMDP:
case enumDiversityMeasureType::LEADING_ONES:
case enumDiversityMeasureType::TSP:
case enumDiversityMeasureType::LOP:
case enumDiversityMeasureType::BIN_SPHERE:
return std::make_unique<ea::EntropyDiversity>();
case enumDiversityMeasureType::ES_SPHERE:
case enumDiversityMeasureType::SPHERE:
return std::make_unique<ea::VarianceDiversity>();
default:
return nullptr;
}
}
*/
//MAIN AVANZADO
struct Experiment {
int maxruns;
std::string problem;
int numvars;
float range;
int numbits;
};
void from_json(const json& j, Experiment & e) {
j.at("numruns").get_to(e.maxruns);
j.at("problem").get_to(e.problem);
j.at("numvars").get_to(e.numvars);
j.at("range").get_to(e.range);
j.at("numbits").get_to(e.numbits);
}
int main(int argc, char* argv[]) {
std::string expName = (argc < 2) ? "experiment.json" : argv[1];
#ifdef EA_PARALLEL
// Detecta el número de núcleos disponibles
int max_threads = std::thread::hardware_concurrency();
tbb::global_control global_limit(
tbb::global_control::max_allowed_parallelism,
max_threads/2
);
#endif
try {
//lee el json
std::ifstream expReader(expName);
if (expReader.fail()) {
std::cerr << "ERROR when opening " << expName << "\n";
return 1;
}
json je = json::parse(expReader);
expReader.close();
Experiment experiment = je.get<Experiment>();
std::println("{}",je);
std::string configf = (experiment.numbits > 0) ? "bitstring.json" : "numeric.json";
std::ifstream reader(configf);
if (reader.fail()) {
std::cerr << "ERROR when opening " << configf << "\n";
return 1;
}
json jc = json::parse(reader);
reader.close();
config::EAConfiguration conf = jc.get<config::EAConfiguration>();
long seed;
std::ifstream lastseedf("lastseed.txt");
if(lastseedf.fail()){
seed = conf.seed;
}
else {
lastseedf >> seed;
conf.seed = ++seed;
}
lastseedf.close();
int numruns = conf.numruns;
std::print("{}", conf);
ea::EvolutionaryAlgorithm myEA(conf);
auto cf = create(experiment.problem, experiment.numvars, experiment.range);
if (experiment.numbits > 0) {
ea::ContinuousObjectiveFunction * cf_cast = dynamic_cast<ea::ContinuousObjectiveFunction *>(cf.get());
if(cf_cast)
cf.release();
else
throw std::runtime_error("Function must be continuous for binary encoding");
auto unique_cf_cast = std::unique_ptr<ea::ContinuousObjectiveFunction>(cf_cast);
myEA.setObjectiveFunction(std::make_unique<ea::BinaryEncodedContinuousObjectiveFunction>(experiment.numbits, std::move(unique_cf_cast)));
myEA.setDiversityMeasure(std::make_unique<ea::EntropyDiversity>());
}
else {
myEA.setObjectiveFunction(std::move(cf));
myEA.setDiversityMeasure(std::make_unique<ea::VarianceDiversity>());
}
std::cout << "-------------------------------------------------------------------------------------\n";
std::cout << "Running the EA on " << experiment.problem << "(" << experiment.numvars << ", " << experiment.range << ")";
if (experiment.numbits > 0) {
std::cout << " binarized with " << experiment.numbits << " bits per variable";
}
else {
std::cout << " on a continuous domain";
}
std::cout << " with seed " << seed << "\n";
std::cout << "-------------------------------------------------------------------------------------\n";
for (int i = 0; i < numruns; i++) {
myEA.run();
std::cout << "\nRun " << i << ": " <<
myEA.stats.getTime(i) << "s\t" << myEA.stats.getBest(i).getFitness();
}
std::string statsfilename = std::format("{}-{}-{}-stats.json", experiment.problem, experiment.numvars, seed);
std::ofstream file(statsfilename);
std::format_to(std::ostreambuf_iterator<char>(file), "{}", myEA.stats);
file.close();
seed += numruns - 1;
if (seed < experiment.maxruns) {
std::ofstream seedFile("lastseed.txt");
seedFile << seed;
seedFile.close();
}
else {
std::remove("lastseed.txt");
}
}
catch (std::exception& e) { std::cout << e.what(); }
}