1
1
import std.random : uniform;
2
- import std.range : lockstep;
2
+ import std.range : lockstep, ElementType ;
3
3
import std.traits : isArray;
4
- import orange.util.Reflection : nameOfFieldAt;
4
+ // import orange.util.Reflection : nameOfFieldAt;
5
5
import std.string : appender;
6
6
import std.stdio : writeln;
7
7
import JCutils;
@@ -49,21 +49,38 @@ template gen_fieldsImpl(T, size_t i) {
49
49
}
50
50
51
51
// currently only works with one type at a time.
52
+ // only works with array types...
52
53
void read_cfg (Q,U,T)(ref T params, string filename) {
53
54
Node root = Loader(filename).load();
54
55
Node solution = root[" solution" ];
55
56
56
57
auto link = AAof! (U)(params);
57
58
58
- int i=0 ; // has to be outside because solution is a tuple???
59
- foreach (Node set; solution) {
60
- foreach (string name, Node value; set) {
61
- link[name~ " _range" ][i][0 ] = value[" min" ].as! Q;
62
- link[name~ " _range" ][i][1 ] = value[" max" ].as! Q;
63
- link[name~ " _mut_range" ][i][0 ] = value[" mut_min" ].as! Q;
64
- link[name~ " _mut_range" ][i][1 ] = value[" mut_max" ].as! Q;
59
+ static if (isArray! (ElementType! U)) {
60
+ pragma (msg," array types" );
61
+ int i=0 ; // has to be outside because solution is a tuple???
62
+ foreach (Node set; solution) {
63
+ foreach (string name, Node value; set) {
64
+ link[name~ " _range" ][i][0 ] = value[" min" ].as! Q;
65
+ link[name~ " _range" ][i][1 ] = value[" max" ].as! Q;
66
+ link[name~ " _mut_range" ][i][0 ] = value[" mut_min" ].as! Q;
67
+ link[name~ " _mut_range" ][i][1 ] = value[" mut_max" ].as! Q;
68
+ }
69
+ ++ i;
65
70
}
66
- ++ i;
71
+ }
72
+ else {
73
+ pragma (msg," scalar types" );
74
+ auto set = solution[0 ];
75
+ foreach (string name, Node value; set) {
76
+ writeln(typeid (typeof (link[name~ " _range" ])));
77
+ writeln(name~ " _range" );
78
+ writeln(link);
79
+ link[name~ " _range" ][0 ] = value[" min" ].as! Q;
80
+ link[name~ " _range" ][1 ] = value[" max" ].as! Q;
81
+ link[name~ " _mut_range" ][0 ] = value[" mut_min" ].as! Q;
82
+ link[name~ " _mut_range" ][1 ] = value[" mut_max" ].as! Q;
83
+ }
67
84
}
68
85
}
69
86
@@ -96,6 +113,10 @@ class Solution (T){
96
113
// has to be a template to work around a bug in D
97
114
// mutabilities initialised to uniform. Is this right? It's gonna cause
98
115
// problems with array parameters...
116
+ //
117
+ // Assumes that there is only 1 tpye of parameter.!!!!!!!
118
+
119
+ // All the operator logic should be in the parameter class, not here
99
120
this (U=bool )(Problem! T problem, Init_params! (T) init_params, int id) {
100
121
this .problem = problem;
101
122
this .id = id;
@@ -106,8 +127,7 @@ class Solution (T){
106
127
params.initialise(init_params);
107
128
}
108
129
else {
109
- auto link = AAof! (double [2 ][1 ])(init_params);
110
-
130
+ auto link = AAof! (typeof ((Init_params! T).tupleof[0 ]))(init_params); // can only use 1 type!!!!!!!
111
131
foreach (uint i, ref param; params.tupleof) {
112
132
auto name = nameOfFieldAt! (T,i);
113
133
@@ -127,7 +147,7 @@ class Solution (T){
127
147
}
128
148
}
129
149
}
130
- writeln(this );
150
+ // writeln(this);
131
151
}
132
152
133
153
// basic constructor for a blank sine_fit. All params initialised to 0
@@ -157,6 +177,7 @@ class Solution (T){
157
177
fitness = problem.fitness_calc(params);
158
178
}
159
179
180
+ // no mutation of mutabilities atm....
160
181
void mutate (Solution parent) {
161
182
id = parent.id;
162
183
foreach (uint i, ref param; params.tupleof) {
@@ -169,7 +190,10 @@ class Solution (T){
169
190
foreach (ref mut; mut_vect) {
170
191
mut = normal();
171
192
}
193
+ // writeln(mut_vect);
194
+ // writeln(nameOfFieldAt!(T,i)," ",parent.params.tupleof[i]);
172
195
param[] = parent.params.tupleof[i][] + mut_vect[]* parent.params.tupleof[i].mutability[];
196
+ // writeln(nameOfFieldAt!(T,i)," ",param);
173
197
}
174
198
else {
175
199
param = parent.params.tupleof[i] + normal()* parent.params.tupleof[i].mutability;
@@ -178,38 +202,53 @@ class Solution (T){
178
202
}
179
203
180
204
auto opOpAssign (string op, U)(U rhs) {
181
- static if (! (op == ' * ' || op == ' / ' ))
205
+ static if (! (op == " * " || op == " / " ))
182
206
static assert (" operation: " ~ op~ " not supported" );
183
207
foreach (uint i, ref param; params.tupleof) {
184
208
static if (isArray! (typeof (param.value))) {
185
- static if (isArray! (U))
186
- mixin (" param[] " ~ op ~ " = rhs[];" );
187
- else
188
- mixin (" param[] " ~ op ~ " = rhs;" );
209
+ static if (isArray! (U)) {
210
+ mixin (" param[] " ~ op ~ " = rhs[];" );
211
+ mixin (" param.mutability[] " ~ op ~ " = rhs[];" );
212
+ }
213
+ else {
214
+ mixin (" param[] " ~ op ~ " = rhs;" );
215
+ mixin (" param.mutability[] " ~ op ~ " = rhs;" );
216
+ }
189
217
}
190
- else
218
+ else {
191
219
mixin (" param " ~ op ~ " = rhs;" );
220
+ mixin (" param.mutability " ~ op ~ " = rhs;" );
221
+ }
192
222
}
193
223
return this ;
194
224
}
195
225
196
226
auto opOpAssign (string op, U:Solution! T)(U rhs) {
197
227
foreach (uint i, ref param; params.tupleof) {
198
- static if (isArray! (typeof (param.value)))
199
- mixin (" param[] " ~ op ~ " = rhs.params.tupleof[i][];" );
200
- else
228
+ static if (isArray! (typeof (param.value))) {
229
+ mixin (" param[] " ~ op ~ " = rhs.params.tupleof[i][];" );
230
+ mixin (" param.mutability[] " ~ op ~ " = rhs.params.tupleof[i].mutability[];" );
231
+ }
232
+ else {
201
233
mixin (" param " ~ op ~ " = rhs.params.tupleof[i];" );
234
+ mixin (" param.mutability " ~ op ~ " = rhs.params.tupleof[i].mutability;" );
235
+ }
202
236
}
203
237
return this ;
204
238
}
205
239
206
240
auto opBinary (string op, U:Solution! T)(U rhs) {
207
241
U tmp = new U;
208
242
foreach (uint i, ref param; tmp.params.tupleof) {
209
- static if (isArray! (typeof (param.value)))
243
+ static if (isArray! (typeof (param.value))) {
210
244
mixin (" param[] = this.params.tupleof[i][] " ~ op ~ " rhs.params.tupleof[i][];" );
211
- else
245
+ mixin (" param.mutability[] = this.params.tupleof[i].mutability[] "
246
+ ~ op ~ " rhs.params.tupleof[i].mutability[];" );
247
+ }
248
+ else {
212
249
mixin (" param = this.params.tupleof[i] " ~ op ~ " rhs.params.tupleof[i];" );
250
+ mixin (" param.mutability = this.params.tupleof[i].mutability " ~ op ~ " rhs.params.tupleof[i].mutability;" );
251
+ }
213
252
}
214
253
return tmp;
215
254
}
@@ -218,13 +257,17 @@ class Solution (T){
218
257
Solution! T tmp = new Solution! T;
219
258
foreach (uint i, ref param; tmp.params.tupleof) {
220
259
static if (isArray! (typeof (param.value))) {
221
- static if (isArray! (U))
260
+ static if (isArray! (U)) {
222
261
mixin (" param[] = this.params.tupleof[i][] " ~ op ~ " rhs[];" );
223
- else
224
- mixin (" param[] = this.params.tupleof[i][] " ~ op ~ " rhs;" );
262
+ mixin (" param.mutability[] = this.params.tupleof[i].mutability[] " ~ op ~ " rhs[];" );
263
+ }
264
+ else {
265
+ mixin (" param.mutability[] = this.params.tupleof[i].mutability[] " ~ op ~ " rhs;" );
266
+ }
225
267
}
226
- else
227
- mixin (" param = this.params.tupleof[i] " ~ op ~ " rhs;" );
268
+ else {
269
+ mixin (" param.mutability = this.params.tupleof[i].mutability " ~ op ~ " rhs;" );
270
+ }
228
271
}
229
272
return tmp;
230
273
}
@@ -240,7 +283,11 @@ class Solution (T){
240
283
}
241
284
242
285
static auto average (Solution[] arr) {
243
- return mean (arr); // I guess this could be overidden to something interesting??
286
+ Solution! T tmp = new Solution! T;
287
+ foreach (sol; arr) {
288
+ tmp += sol;
289
+ }
290
+ return tmp /= arr.length; // I guess this could be overidden to something interesting??
244
291
}
245
292
246
293
@property string csv_string() {
0 commit comments