-
Notifications
You must be signed in to change notification settings - Fork 1
/
ES.d
266 lines (235 loc) · 8.2 KB
/
ES.d
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
259
260
261
262
263
264
265
266
import std.algorithm : partialSort, topN, sort;
import std.stdio : writeln, stdout, File, write, writefln;
import std.parallelism : taskPool;
import std.file : mkdir, minPos, FileException;
import std.datetime;
import std.conv : to;
import std.math;
import std.array;
import std.string;
import JCutils;
import yaml;
import std.exception : enforce;
public import solution;
//processing topology info? Not really sure how
//Population class, holds the core runnings of the algorithm, including
//initialization of solutions;
class Population(T) {
T[][] history;
T[] history_best;
T[] solutions;
T[] parents;
int num_parents;
int num_offspring;
int pop_size;
int num_generations;
bool top_sorted = false;
bool full_sorted = false;
bool partitioned = false;
bool parent_list_up_to_date = false;
bool memory = false;
string style;
string foldername;
File fitness, means, bests;
//initialises the population
//initialise the solutions, allocate memory for parents etc...
this(U...)(U args, string cfg_fn) {
read_ES_config(cfg_fn);
solutions = new T[pop_size];
writefln("Initialising solutions: pop_size = %d init_num_parents = %d",
pop_size,num_parents);
foreach(int i, ref solution; solutions) {
solution = new T(i, args);
}
if(cmp(style,"new") == 0) {
writeln("using new style");
parents = new T[1];
num_offspring = pop_size;
memory = true;
}
else if(cmp(style,"old") == 0) {
writeln("using old style");
parents = new T[num_parents];
if(fmod(pop_size - num_parents,num_parents))
throw new Exception("pop_size - num_parents must be divisible by num_parents");
num_offspring = (pop_size - num_parents) / num_parents;
}
else
throw new Exception("style \""~style~"\" not supported");
writeln("num_offspring = ",num_offspring);
}
//runs the algorithm
void run() {
output_files_init();
//iterate over generations.
for(int i=1; i<=num_generations; ++i) {
write("\rgeneration: ",i," ");
stdout.flush();
evaluate();
_sort();
if(memory) {
history ~= class_arr_dup(solutions); //BAD VERY SLOW!!!
if(history_best.length == 0)
history_best ~= history[$-1][0..num_parents];
else
topN(history_best, history[$-1]); //copies topN from right to left.
sort(history_best); //not strictly necessary, but it hardly
//takes any time for small pops.
}
// writeln();
// writeln(history_best[0]);
select();
write_out(i);
replace();
}
//final eval and sort for results.
evaluate();
sort(solutions);
writeln();
}
void output_files_init() {
try mkdir("results");
catch(FileException e) {}
auto time_stamp = Clock.currTime().toISOExtString()[0..19];
foldername = "results/output_" ~ time_stamp;
mkdir(foldername);
mkdir(foldername~"/gens");
if(memory)
mkdir(foldername~"/bests");
fitness = File(foldername~"/fitness.txt","w");
bests = File(foldername~"/bests.txt","w");
if(memory)
means = File(foldername~"/means.txt","w");
}
void write_out(int gen_num) {
// writeln(solutions[0]);
auto record = File(foldername~"/gens/gen"~to!string(gen_num),"w");
record.write(csv_dump());
// writeln("\n",solutions);
if(memory) {
fitness.write(history_best[0].fitness,"\n");
means.write(parents[0].csv_string());
bests.write(history_best[0].csv_string);
auto best_pop = File(foldername~"/bests/best_asof_gen"~to!string(gen_num),"w");
best_pop.write(csv_dump(history_best));
}
else {
fitness.write(solutions[0].fitness,"\n");
bests.write(solutions[0].csv_string);
}
}
//dumps the all the solutions (paramters as csv, see
string csv_dump(T[] to_print) {
auto app = appender!string();
foreach(sol; to_print) {
app.put(sol.csv_string());
}
return app.data;
}
string csv_dump() {
return csv_dump(solutions);
}
//returns the best solutions to date
//might have side effects......
T[] best(int n=1) {
enforce(n>0);
T[] sols;
if(memory)
sols = history_best;
else
sols = solutions;
if(n==1) {
if(memory)
return history_best[0..1];
if(top_sorted)
return sols[0..1];
return minPos(sols)[0..1];
}
else {
partialSort(sols,n);
return(sols[0..n]);
}
}
//evalutes the entire population in parallel.
//need to optimise taskPool chunksize?
void evaluate() {
//could use taskPool.map for this?
foreach(ref solution; taskPool.parallel(solutions)) {
// foreach(solution; solutions) {
solution.evaluate();
}
}
//sorts the population by their fitness values. Smaller is better.
void _sort() {
// topN(solutions, num_parents); // has no sorting, only selects best
partialSort(solutions, num_parents);
assert(solutions[0] == minPos(solutions)[0]);
top_sorted = true; //added with partialSort()
partitioned = true;
}
//Selects the parents from the evaluated population
//parents MUST be unique!!!!
//assumes sorted
void select() {
if(cmp(style,"new") == 0) {
parents[0] = child();
}
else
foreach(int i, ref parent; parents)
parent = solutions[i];
//sort(parents); //this will need changing, ok for now as parents are
//already in order
parent_list_up_to_date = true;
}
//new simpler version of replace()
void replace() {
int skip = 0, par_ind = 0;
T to_skip = parents[skip];
T parent = parents[par_ind];
int children_so_far = 0;
auto end_of_parents = parents.length - 1; //not same as num_parents
//in new style unfortunately
foreach(int i, sol; solutions) {
//if we're on the first parent, skip it and start watching
//for the next one.
if(sol is to_skip) {
if(skip < end_of_parents)
to_skip = parents[++skip];
else
to_skip = null;
continue;
}
sol.mutate(parent); //modify sol based on parent
children_so_far++; //add one to the child counter
//check if given sol has had enough offspring
if(children_so_far >= num_offspring) {
if(par_ind < end_of_parents)
parent = parents[++par_ind]; //move to next parent
children_so_far = 0; //reset child counter
}
}
top_sorted = false;
full_sorted = false;
partitioned = false;
}
T child() {
T[] to_average;
if(memory) {
// writeln(history_best);
// writeln("AVERAGE: ",T.average(history_best));
return T.average(history_best);
}
if(!partitioned) {
topN(solutions, num_parents);
partitioned = true;
}
return T.average(solutions[0..num_parents]);
}
void read_ES_config(string filename) {
Node root = Loader(filename).load();
pop_size = root["pop_size"].as!int;
num_parents = root["num_parents"].as!int;
num_generations = root["num_generations"].as!int;
style = root["style"].as!string;
}
};