Skip to content

Commit a34402c

Browse files
committed
modified: ES.d
modified: JCutils.d modified: Makefile modified: solution.d
1 parent 365a49d commit a34402c

File tree

4 files changed

+110
-45
lines changed

4 files changed

+110
-45
lines changed

ES.d

+9-6
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ class Population(T) {
8181
_sort();
8282

8383
if(memory) {
84-
history ~= class_arr_dup(solutions);
84+
history ~= class_arr_dup(solutions); //BAD VERY SLOW!!!
8585
if(history_best.length == 0)
8686
history_best ~= history[$-1][0..num_parents];
8787
else
8888
topN(history_best, history[$-1]); //copies topN from right to left.
8989
sort(history_best); //not strictly necessary, but it hardly
9090
//takes any time for small pops.
9191
}
92-
93-
writeln(history_best[0].fitness);
92+
// writeln();
93+
// writeln(history_best[0]);
9494

9595
select();
9696

@@ -178,7 +178,7 @@ class Population(T) {
178178
void evaluate() {
179179
//could use taskPool.map for this?
180180
foreach(ref solution; taskPool.parallel(solutions)) {
181-
// foreach(solution; solutions) {
181+
// foreach(solution; solutions) {
182182
solution.evaluate();
183183
}
184184
}
@@ -198,7 +198,6 @@ class Population(T) {
198198
void select() {
199199
if(cmp(style,"new") == 0) {
200200
parents[0] = child();
201-
// writeln(parents[0]);
202201
}
203202
else
204203
foreach(int i, ref parent; parents)
@@ -243,12 +242,16 @@ class Population(T) {
243242

244243
T child() {
245244
T[] to_average;
246-
if(memory)
245+
if(memory) {
246+
// writeln(history_best);
247+
// writeln("AVERAGE: ",T.average(history_best));
247248
return T.average(history_best);
249+
}
248250
if(!partitioned) {
249251
topN(solutions, num_parents);
250252
partitioned = true;
251253
}
254+
252255
return T.average(solutions[0..num_parents]);
253256
}
254257

JCutils.d

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import orange.util.Reflection : nameOfFieldAt;
1+
//import orange.util.Reflection : nameOfFieldAt;
22
import std.range : lockstep, ElementType;
33
import std.traits : isArray;
44
import std.conv : to;
@@ -194,12 +194,12 @@ template _is_floating(T) {
194194

195195
//wrapper struct for string_access.
196196
//sorts out the pointers.
197-
struct AAof (T) {
198-
T*[string] field_ptrs;
199-
this(U)(ref U obj) {
200-
field_ptrs = string_access!T(obj);
197+
struct AAof (U) {
198+
U*[string] field_ptrs;
199+
this(T)(ref T obj) {
200+
field_ptrs = string_access!U(obj);
201201
}
202-
auto ref opIndex(U : string)(U field) {
202+
auto ref opIndex(S : string)(S field) {
203203
return *(field_ptrs[field]);
204204
}
205205
}
@@ -212,6 +212,7 @@ struct AAof (T) {
212212
auto string_access(U,T)(ref T obj) {
213213
U*[string] dict;
214214
mixin(dictString!(T, U, "obj"));
215+
dict.rehash;
215216
return(dict);
216217
}
217218

@@ -249,3 +250,15 @@ template dictStringImpl (T, U, string name, size_t i) {
249250
const dictStringImpl = dictStringImpl!(T, U, name, i+1);
250251
}
251252
}
253+
254+
//Stolen from orange, For some reason it has all sorts of problems linking.
255+
template nameOfFieldAt (T, size_t position)
256+
{
257+
static assert (position < T.tupleof.length, format!(`The given position "`, position, `" is greater than the number of fields (`, T.tupleof.length, `) in the type "`, T, `"`));
258+
259+
static if (T.tupleof[position].stringof.length > T.stringof.length + 3)
260+
const nameOfFieldAt = T.tupleof[position].stringof[1 + T.stringof.length + 2 .. $];
261+
262+
else
263+
const nameOfFieldAt = "";
264+
}

Makefile

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
DC?=dmd
2-
3-
MAIN=ES.d JCutils.d solution.d test.d
2+
EXTRA?=
3+
MAIN?=test.d
4+
INCLUDES=ES.d JCutils.d solution.d
45

56
main:
6-
$(DC) $(MAIN) -L-ldyaml -L-lorange -w -m64 -gc -noboundscheck
7+
$(DC) $(MAIN) $(INCLUDES) -L-ldyaml -L-lorange -w -m64 -gc
8+
#-O -inline -release -noboundscheck $(EXTRA)
79

810
run: main
911
./ES

solution.d

+77-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import std.random : uniform;
2-
import std.range : lockstep;
2+
import std.range : lockstep, ElementType;
33
import std.traits : isArray;
4-
import orange.util.Reflection : nameOfFieldAt;
4+
//import orange.util.Reflection : nameOfFieldAt;
55
import std.string : appender;
66
import std.stdio : writeln;
77
import JCutils;
@@ -49,21 +49,38 @@ template gen_fieldsImpl(T, size_t i) {
4949
}
5050

5151
//currently only works with one type at a time.
52+
//only works with array types...
5253
void read_cfg(Q,U,T)(ref T params, string filename) {
5354
Node root = Loader(filename).load();
5455
Node solution = root["solution"];
5556

5657
auto link = AAof!(U)(params);
5758

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;
6570
}
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+
}
6784
}
6885
}
6986

@@ -96,6 +113,10 @@ class Solution (T){
96113
//has to be a template to work around a bug in D
97114
//mutabilities initialised to uniform. Is this right? It's gonna cause
98115
//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
99120
this(U=bool)(Problem!T problem, Init_params!(T) init_params, int id) {
100121
this.problem = problem;
101122
this.id = id;
@@ -106,8 +127,7 @@ class Solution (T){
106127
params.initialise(init_params);
107128
}
108129
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!!!!!!!
111131
foreach(uint i, ref param; params.tupleof) {
112132
auto name = nameOfFieldAt!(T,i);
113133

@@ -127,7 +147,7 @@ class Solution (T){
127147
}
128148
}
129149
}
130-
writeln(this);
150+
// writeln(this);
131151
}
132152

133153
//basic constructor for a blank sine_fit. All params initialised to 0
@@ -157,6 +177,7 @@ class Solution (T){
157177
fitness = problem.fitness_calc(params);
158178
}
159179

180+
//no mutation of mutabilities atm....
160181
void mutate(Solution parent) {
161182
id = parent.id;
162183
foreach(uint i, ref param; params.tupleof) {
@@ -169,7 +190,10 @@ class Solution (T){
169190
foreach(ref mut; mut_vect) {
170191
mut = normal();
171192
}
193+
// writeln(mut_vect);
194+
// writeln(nameOfFieldAt!(T,i)," ",parent.params.tupleof[i]);
172195
param[] = parent.params.tupleof[i][] + mut_vect[]*parent.params.tupleof[i].mutability[];
196+
// writeln(nameOfFieldAt!(T,i)," ",param);
173197
}
174198
else {
175199
param = parent.params.tupleof[i] + normal()*parent.params.tupleof[i].mutability;
@@ -178,38 +202,53 @@ class Solution (T){
178202
}
179203

180204
auto opOpAssign(string op, U)(U rhs) {
181-
static if(!(op == '*' || op == '/'))
205+
static if(!(op == "*" || op == "/"))
182206
static assert("operation: " ~op~ " not supported");
183207
foreach(uint i, ref param; params.tupleof) {
184208
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+
}
189217
}
190-
else
218+
else {
191219
mixin("param " ~ op ~ "= rhs;");
220+
mixin("param.mutability " ~ op ~ "= rhs;");
221+
}
192222
}
193223
return this;
194224
}
195225

196226
auto opOpAssign(string op, U:Solution!T)(U rhs) {
197227
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 {
201233
mixin("param " ~ op ~ "= rhs.params.tupleof[i];");
234+
mixin("param.mutability " ~ op ~ "= rhs.params.tupleof[i].mutability;");
235+
}
202236
}
203237
return this;
204238
}
205239

206240
auto opBinary(string op, U:Solution!T)(U rhs) {
207241
U tmp = new U;
208242
foreach(uint i, ref param; tmp.params.tupleof) {
209-
static if(isArray!(typeof(param.value)))
243+
static if(isArray!(typeof(param.value))) {
210244
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 {
212249
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+
}
213252
}
214253
return tmp;
215254
}
@@ -218,13 +257,17 @@ class Solution (T){
218257
Solution!T tmp = new Solution!T;
219258
foreach(uint i, ref param; tmp.params.tupleof) {
220259
static if(isArray!(typeof(param.value))) {
221-
static if(isArray!(U))
260+
static if(isArray!(U)) {
222261
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+
}
225267
}
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+
}
228271
}
229272
return tmp;
230273
}
@@ -240,7 +283,11 @@ class Solution (T){
240283
}
241284

242285
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??
244291
}
245292

246293
@property string csv_string() {

0 commit comments

Comments
 (0)