Vita
environment.cc
Go to the documentation of this file.
1
13#include "kernel/environment.h"
14
15namespace vita
16{
23{
24}
25
44{
45 mep.code_length = 100;
46 mep.patch_length = 1;
47 elitism = trilean::yes;
48 p_mutation = 0.04;
49 p_cross = 0.9;
51 dss = 1;
52 layers = 1;
53 individuals = 100;
56 mate_zone = 20;
57 generations = 100;
58 max_stuck_time = std::numeric_limits<unsigned>::max();
60
61 return *this;
62}
63
69void environment::xml(tinyxml2::XMLDocument *d) const
70{
71 Expects(!stat.summary_file.empty());
72
73 auto *root(d->RootElement());
74
75 auto *e_environment(d->NewElement("environment"));
76 root->InsertEndChild(e_environment);
77 set_text(e_environment, "layers", layers);
78 set_text(e_environment, "individuals", individuals);
79 set_text(e_environment, "min_individuals", min_individuals);
80 set_text(e_environment, "code_length", mep.code_length);
81 set_text(e_environment, "patch_length", mep.patch_length);
82 set_text(e_environment, "elitism", as_integer(elitism));
83 set_text(e_environment, "mutation_rate", p_mutation);
84 set_text(e_environment, "crossover_rate", p_cross);
85 set_text(e_environment, "brood_recombination", brood_recombination);
86 if (dss.has_value())
87 set_text(e_environment, "dss", *dss);
88 set_text(e_environment, "tournament_size", tournament_size);
89 set_text(e_environment, "mating_zone", mate_zone);
90 set_text(e_environment, "max_generations", generations);
91 set_text(e_environment, "max_stuck_time", *max_stuck_time);
92 if (validation_percentage.has_value())
93 set_text(e_environment, "validation_percentage", *validation_percentage);
94 set_text(e_environment, "cache_bits", cache_size); // size `1u<<cache_size`
95
96 auto *e_alps(d->NewElement("alps"));
97 e_environment->InsertEndChild(e_alps);
98 set_text(e_alps, "age_gap", alps.age_gap);
99 set_text(e_alps, "p_same_layer", alps.p_same_layer);
100
101 auto *e_team(d->NewElement("team"));
102 e_environment->InsertEndChild(e_team);
103 set_text(e_team, "individuals", team.individuals);
104
105 auto *e_statistics(d->NewElement("statistics"));
106 e_environment->InsertEndChild(e_statistics);
107 set_text(e_statistics, "directory", stat.dir);
108 set_text(e_statistics, "save_dynamics", stat.dynamic_file);
109 set_text(e_statistics, "save_layers", stat.layers_file);
110 set_text(e_statistics, "save_population", stat.population_file);
111 set_text(e_statistics, "save_summary", stat.summary_file);
112 set_text(e_statistics, "save_test", stat.test_file);
113 set_text(e_statistics, "individual_format", stat.ind_format);
114
115 auto *e_misc(d->NewElement("misc"));
116 e_environment->InsertEndChild(e_misc);
117 set_text(e_misc, "serialization_file", misc.serialization_file);
118}
119
127bool environment::is_valid(bool force_defined) const
128{
129 if (force_defined)
130 {
131 if (!mep.code_length)
132 {
133 vitaERROR << "Undefined code_length data member";
134 return false;
135 }
136
137 if (!mep.patch_length)
138 {
139 vitaERROR << "Undefined patch_length data member";
140 return false;
141 }
142
143 if (elitism == trilean::unknown)
144 {
145 vitaERROR << "Undefined elitism data member";
146 return false;
147 }
148
149 if (p_mutation < 0.0)
150 {
151 vitaERROR << "Undefined p_mutation data member";
152 return false;
153 }
154
155 if (p_cross < 0.0)
156 {
157 vitaERROR << "Undefined p_cross data member";
158 return false;
159 }
160
162 {
163 vitaERROR << "Undefined brood_recombination data member";
164 return false;
165 }
166
167 if (!layers)
168 {
169 vitaERROR << "Undefined layers data member";
170 return false;
171 }
172
173 if (!individuals)
174 {
175 vitaERROR << "Undefined `individuals` data member";
176 return false;
177 }
178
179 if (!min_individuals)
180 {
181 vitaERROR << "Undefined `min_individuals` data member";
182 return false;
183 }
184
185 if (!tournament_size)
186 {
187 vitaERROR << "Undefined tournament_size data member";
188 return false;
189 }
190
191 if (!mate_zone)
192 {
193 vitaERROR << "Undefined mate_zone data member";
194 return false;
195 }
196
197 if (!generations)
198 {
199 vitaERROR << "Undefined generations data member";
200 return false;
201 }
202
203 if (!max_stuck_time.has_value())
204 {
205 vitaERROR << "Undefined max_stuck_time data member";
206 return false;
207 }
208
209 if (!alps.age_gap)
210 {
211 vitaERROR << "Undefined age_gap parameter";
212 return false;
213 }
214
215 if (alps.p_same_layer < 0.0)
216 {
217 vitaERROR << "Undefined p_same_layer parameter";
218 return false;
219 }
220
221 if (!team.individuals)
222 {
223 vitaERROR << "Undefined team size parameter";
224 return false;
225 }
226 } // if (force_defined)
227
228 if (mep.code_length == 1)
229 {
230 vitaERROR << "`code_length` is too short";
231 return false;
232 }
233
234 if (mep.code_length && mep.patch_length
235 && mep.patch_length >= mep.code_length)
236 {
237 vitaERROR << "`patch_length` must be shorter than `code_length`";
238 return false;
239 }
240
241 if (p_mutation > 1.0)
242 {
243 vitaERROR << "`p_mutation` out of range";
244 return false;
245 }
246
247 if (p_cross > 1.0)
248 {
249 vitaERROR << "`p_cross` out of range";
250 return false;
251 }
252
253 if (validation_percentage.has_value() && *validation_percentage >= 100)
254 {
255 vitaERROR << "`validation_percentage` out of range";
256 return false;
257 }
258
259 if (dss.has_value() && *dss == 0)
260 {
261 vitaERROR << "`dss` out of range";
262 return false;
263 }
264
265 if (alps.p_same_layer > 1.0)
266 {
267 vitaERROR << "`p_same_layer` out of range";
268 return false;
269 }
270
271 if (min_individuals == 1)
272 {
273 vitaERROR << "At least 2 individuals for layer";
274 return false;
275 }
276
278 {
279 vitaERROR << "Too few individuals";
280 return false;
281 }
282
284 {
285 vitaERROR << "`tournament_size` (" << tournament_size
286 << ") cannot be greater than population size ("
287 << individuals << ")";
288 return false;
289 }
290
292 {
293 vitaERROR << "`tournament_size` (" << tournament_size
294 << ") cannot be greater than `mate_zone` (" << mate_zone
295 << ")";
296 return false;
297 }
298
299 if (stat.dir.has_filename())
300 {
301 vitaERROR << "`stat.dir` must contain a directory, not a file ("
302 << stat.dir << ")";
303 return false;
304 }
305
306 if (!stat.dynamic_file.empty() && !stat.dynamic_file.has_filename())
307 {
308 vitaERROR << "`stat.dynamic_file` must specify a file ("
309 << stat.dynamic_file << ")";
310 return false;
311 }
312
313 if (!stat.layers_file.empty() && !stat.layers_file.has_filename())
314 {
315 vitaERROR << "`stat.layers_file` must specify a file ("
316 << stat.layers_file << ")";
317 return false;
318 }
319
320 if (!stat.population_file.empty() && !stat.population_file.has_filename())
321 {
322 vitaERROR << "`stat.population_file` must specify a file ("
323 << stat.population_file << ")";
324 return false;
325 }
326
327 if (!stat.summary_file.empty() && !stat.summary_file.has_filename())
328 {
329 vitaERROR << "`stat.summary_file` must specify a file ("
330 << stat.summary_file << ")";
331 return false;
332 }
333
334 if (!stat.test_file.empty() && !stat.test_file.has_filename())
335 {
336 vitaERROR << "`stat.test_file` must specify a file ("
337 << stat.test_file << ")";
338 return false;
339 }
340
341 return true;
342}
343
344} // namespace vita
Dynamic training Subset Selection.
Definition: dss.h:38
Context object aggregating multiple related parameters into one structure.
Definition: environment.h:57
unsigned individuals
Number of individuals in a layer of the population.
Definition: environment.h:103
bool is_valid(bool) const
Definition: environment.cc:127
void xml(tinyxml2::XMLDocument *) const
Saves the environment in XML format.
Definition: environment.cc:69
facultative< unsigned, 0 > max_stuck_time
Used by some evolution strategies to stop a run when there aren't improvements within max_stuck_time ...
Definition: environment.h:208
unsigned mate_zone
This is used for the trivial geography scheme.
Definition: environment.h:197
unsigned generations
Maximun number of generations allowed before terminate a run.
Definition: environment.h:202
unsigned cache_size
2^cache_size is the number of elements of the cache.
Definition: environment.h:220
facultative< unsigned > validation_percentage
How much data should be reserved for the validation set? validation_percentage is the fraction of the...
Definition: environment.h:217
double p_mutation
Mutation rate probability.
Definition: environment.h:132
double p_cross
Crossover probability.
Definition: environment.h:137
unsigned min_individuals
Minimum number of individuals in a layer of the population.
Definition: environment.h:111
unsigned layers
Number of layers for the population.
Definition: environment.h:98
trilean elitism
An elitist algorithm is one that ALWAYS retains in the population the best individual found so far.
Definition: environment.h:116
environment()
Class constructor.
Definition: environment.cc:22
unsigned tournament_size
Size of the tournament to choose the parents from.
Definition: environment.h:172
environment & init()
Initialises the undefined parameters with "common" values.
Definition: environment.cc:43
unsigned brood_recombination
This parameter controls the brood recombination/selection level (1 to turn it off).
Definition: environment.h:160
A collection of cooperating individuals used as a member of vita::population.
Definition: team.h:54
Contains support functions for the ALPS algorithm.
The main namespace for the project.
std::size_t code_length
The number of genes (maximum length of an evolved program in the population).
Definition: environment.h:80
std::size_t patch_length
The number of symbols in the patch section (the tail section of the genome containing only terminals)...
Definition: environment.h:86
std::string serialization_file
Filename used for persistance.
Definition: environment.h:226
std::filesystem::path population_file
Name of the log file used to save population-specific information.
Definition: environment.h:248
std::filesystem::path dir
A base common path for the log files.
Definition: environment.h:234
std::filesystem::path dynamic_file
Name of the log file used to save real-time information.
Definition: environment.h:238
std::filesystem::path layers_file
Name of the log file used to save layer-specific information.
Definition: environment.h:242
std::filesystem::path summary_file
Name of the log file used to save a summary report.
Definition: environment.h:252
out::print_format_t ind_format
Default rendering format used to print an individual.
Definition: environment.h:259