-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.d
311 lines (278 loc) · 11.3 KB
/
solution.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import std.random : uniform;
import std.range : lockstep, ElementType;
import std.traits : isArray;
//import orange.util.Reflection : nameOfFieldAt;
import std.string : appender;
import std.stdio : writeln;
import JCutils;
import yaml;
//be prepared for a mess if this becomes a class...parameters are expected
//to be moved by value!
struct Parameter(T, U) {
T value;
alias value this;
U mutability;
this(T value, U mutability) {
this.value = value;
this.mutability = mutability;
}
}
//To be templated with user-defined parameters class.
struct Init_params(T) {
mixin(gen_fields!T);
}
template gen_fields(T) {
const gen_fields = gen_fieldsImpl!(T, 0);
}
template gen_fieldsImpl(T, size_t i) {
static if(T.tupleof.length == 0) {
const gen_fieldsImpl = "";
}
else static if(T.tupleof.length -1 == i) {
const gen_fieldsImpl = InsertFirstDimension!(typeof(T.tupleof[i].value), 2).stringof
~ " " ~ nameOfFieldAt!(T,i) ~ "_range;\n" ~
InsertFirstDimension!(typeof(T.tupleof[i].value), 2).stringof
~ " " ~ nameOfFieldAt!(T,i) ~ "_mut_range;";
}
else {
const gen_fieldsImpl = InsertFirstDimension!(typeof(T.tupleof[i].value), 2).stringof
~ " " ~ nameOfFieldAt!(T,i) ~ "_range;\n" ~
InsertFirstDimension!(typeof(T.tupleof[i].value), 2).stringof
~ " " ~ nameOfFieldAt!(T,i) ~ "_mut_range;\n" ~
gen_fieldsImpl!(T, i+1);
}
}
//currently only works with one type at a time.
//only works with array types...
void read_cfg(Q,U,T)(ref T params, string filename) {
Node root = Loader(filename).load();
Node solution = root["solution"];
auto link = AAof!(U)(params);
static if(isArray!(ElementType!U)) {
pragma(msg,"array types");
int i=0; //has to be outside because solution is a tuple???
foreach(Node set; solution) {
foreach(string name, Node value; set) {
link[name~"_range"][i][0] = value["min"].as!Q;
link[name~"_range"][i][1] = value["max"].as!Q;
link[name~"_mut_range"][i][0] = value["mut_min"].as!Q;
link[name~"_mut_range"][i][1] = value["mut_max"].as!Q;
}
++i;
}
}
else {
pragma(msg,"scalar types");
auto set = solution[0];
foreach(string name, Node value; set) {
writeln(typeid(typeof(link[name~"_range"])));
writeln(name~"_range");
writeln(link);
link[name~"_range"][0] = value["min"].as!Q;
link[name~"_range"][1] = value["max"].as!Q;
link[name~"_mut_range"][0] = value["mut_min"].as!Q;
link[name~"_mut_range"][1] = value["mut_max"].as!Q;
}
}
}
//no support for any params with higher dimension than 1.
//please inherit from this and override anything you want to change
//in order specify the meaning of addition, division etc. on your
//parameters. Also, overriding mutate allows you to implement any
//different mutation style you want. You could even make this in to
//a basic whole population crossover GA by overriding mutate and average.
//please bare in mind that integer fields will be mutated. The meaning
//of the integer values should be somehow ordered otherwise small
//mutations could lead to drastic changes.
//could i have some form of default system where if an add operation
//is implemented for the parameters then use that one etc...?
class Solution (T){
T params;
int id;
double fitness;
Problem!T problem;
/* this(U=bool)(T params) {
this.params = params;
}*/
//full constructor including solution initialisation
//has to be a template to work around a bug in D
//mutabilities initialised to uniform. Is this right? It's gonna cause
//problems with array parameters...
//
//Assumes that there is only 1 tpye of parameter.!!!!!!!
//All the operator logic should be in the parameter class, not here
this(U=bool)(Problem!T problem, Init_params!(T) init_params, int id) {
this.problem = problem;
this.id = id;
//leave fitness as nan
static if(__traits(hasMember, T, "initialise")) {
pragma(msg,__traits(hasMember, T, "initialise"));
params.initialise(init_params);
}
else {
auto link = AAof!(typeof((Init_params!T).tupleof[0]))(init_params); //can only use 1 type!!!!!!!
foreach(uint i, ref param; params.tupleof) {
auto name = nameOfFieldAt!(T,i);
static if(isArray!(typeof(param.value))) {
double[] param_norm_vect = new double[param.value.length];
double[] mut_norm_vect = new double[param.mutability.length];
foreach(ref el_param_norm_vect, ref el_mut_norm_vect; lockstep(param_norm_vect, mut_norm_vect)) {
el_param_norm_vect = uniform!"[]"(link[name~"_range"][0][0],link[name~"_range"][0][1]);
el_mut_norm_vect = uniform!"[]"(link[name~"_mut_range"][0][0],link[name~"_mut_range"][0][1]);
}
param[] = param_norm_vect[];
param.mutability[] = mut_norm_vect[];
}
else {
param = uniform!"[]"(link[name~"_range"][0],link[name~"_range"][1]);
param.mutability = uniform!"[]"(link[name~"_mut_range"][0],link[name~"_mut_range"][1]);
}
}
}
// writeln(this);
}
//basic constructor for a blank sine_fit. All params initialised to 0
//has to be template to overcome bug in d
this(U=bool)() {
params = T.blank();
id = -1;
fitness = double.nan;
//problem left uninitialised. Is this ok?
}
//copy constructor. Again, has to be a bloody template.....
this(U=bool)(Solution a) {
params = a.params;
id = a.id;
fitness = a.fitness;
problem = a.problem;
}
//reroute for constructor from Population
//exands args to main constructor.
this(U...)(int id, U args) {
this(args[0], args[1], id);
}
void evaluate() {
fitness = problem.fitness_calc(params);
}
//no mutation of mutabilities atm....
void mutate(Solution parent) {
id = parent.id;
foreach(uint i, ref param; params.tupleof) {
//should user defined types which possess the right
//operator primitives be allowed here?
static assert(_is_arithmetic!(ElementTypeRecursive!(typeof(param.value))), "No default mutation
implementation for non-arithmetic types");
static if(isArray!(typeof(param.value))) {
double[] mut_vect = new double[param.value.length];
foreach(ref mut; mut_vect) {
mut = normal();
}
// writeln(mut_vect);
// writeln(nameOfFieldAt!(T,i)," ",parent.params.tupleof[i]);
param[] = parent.params.tupleof[i][] + mut_vect[]*parent.params.tupleof[i].mutability[];
// writeln(nameOfFieldAt!(T,i)," ",param);
}
else {
param = parent.params.tupleof[i] + normal()*parent.params.tupleof[i].mutability;
}
}
}
auto opOpAssign(string op, U)(U rhs) {
static if(!(op == "*" || op == "/"))
static assert("operation: " ~op~ " not supported");
foreach(uint i, ref param; params.tupleof) {
static if(isArray!(typeof(param.value))) {
static if(isArray!(U)) {
mixin("param[] " ~ op ~ "= rhs[];");
mixin("param.mutability[] " ~ op ~ "= rhs[];");
}
else {
mixin("param[] " ~ op ~ "= rhs;");
mixin("param.mutability[] " ~ op ~ "= rhs;");
}
}
else {
mixin("param " ~ op ~ "= rhs;");
mixin("param.mutability " ~ op ~ "= rhs;");
}
}
return this;
}
auto opOpAssign(string op, U:Solution!T)(U rhs) {
foreach(uint i, ref param; params.tupleof) {
static if(isArray!(typeof(param.value))) {
mixin("param[] " ~ op ~ "= rhs.params.tupleof[i][];");
mixin("param.mutability[] " ~ op ~ "= rhs.params.tupleof[i].mutability[];");
}
else {
mixin("param " ~ op ~ "= rhs.params.tupleof[i];");
mixin("param.mutability " ~ op ~ "= rhs.params.tupleof[i].mutability;");
}
}
return this;
}
auto opBinary(string op, U:Solution!T)(U rhs) {
U tmp = new U;
foreach(uint i, ref param; tmp.params.tupleof) {
static if(isArray!(typeof(param.value))) {
mixin("param[] = this.params.tupleof[i][] " ~ op ~ " rhs.params.tupleof[i][];");
mixin("param.mutability[] = this.params.tupleof[i].mutability[] "
~ op ~ " rhs.params.tupleof[i].mutability[];");
}
else {
mixin("param = this.params.tupleof[i] " ~ op ~ " rhs.params.tupleof[i];");
mixin("param.mutability = this.params.tupleof[i].mutability " ~ op ~ " rhs.params.tupleof[i].mutability;");
}
}
return tmp;
}
auto opBinary(string op, U)(U rhs) {
Solution!T tmp = new Solution!T;
foreach(uint i, ref param; tmp.params.tupleof) {
static if(isArray!(typeof(param.value))) {
static if(isArray!(U)) {
mixin("param[] = this.params.tupleof[i][] " ~ op ~ " rhs[];");
mixin("param.mutability[] = this.params.tupleof[i].mutability[] " ~ op ~ " rhs[];");
}
else {
mixin("param.mutability[] = this.params.tupleof[i].mutability[] " ~ op ~ " rhs;");
}
}
else {
mixin("param.mutability = this.params.tupleof[i].mutability " ~ op ~ " rhs;");
}
}
return tmp;
}
override int opCmp(Object o) {
auto test = this.fitness - (cast(Solution!T)o).fitness;
if(test<0)
return -1;
else if(test>0)
return 1;
else
return 0;
}
static auto average(Solution[] arr) {
Solution!T tmp = new Solution!T;
foreach(sol; arr) {
tmp += sol;
}
return tmp /= arr.length; //I guess this could be overidden to something interesting??
}
@property string csv_string() {
auto app = appender!string();
app.put(to!string(id) ~ "," ~ to!string(fitness) ~ ",");
foreach(param; params.tupleof)
app.put(to!string(param) ~ ",");
return app.data[0..$-1] ~ "\n";
}
override string toString() {
return this.csv_string;
}
}
//really need some unittests. No idea how considering how heavily templated and interdependant
//everything is...
class Problem(T) {
abstract double fitness_calc(T fit);
}