-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreactorgen.js
More file actions
390 lines (359 loc) · 12.5 KB
/
reactorgen.js
File metadata and controls
390 lines (359 loc) · 12.5 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
var reactorsim = require('ic2-reactor-sim');
var scoreReactor = require('./scoring');
var Mutations = require('./mutation');
var gridUtils = require('./grid');
var config = require('./genetic-config');
var makeGrid = require('./grid').makeGrid;
var weightedRand = require('./weighted-rand');
var async = require('async');
var fs = require('fs');
var reactorIdCtr = 1;
var curGeneration = 0;
var populations = {};
for(var curPop in config.populations) populations[curPop] = new GenPopulation();
function GenReactor(grid, results, score) {
this.grid = grid;
this.results = results;
this.score = score;
this.id = reactorIdCtr++;
}
function GenFamily(members, props) {
var self = this;
this.members = []; // sorted from highest (at index 0) to lowest
this.lastGenerationScoreChange = curGeneration;
if(Array.isArray(members)) {
members.forEach(function(m) {
self.addMember(m, members.length);
});
}
if(props) {
for(var key in props) {
if(props[key] !== undefined) this[key] = props[key];
}
}
}
GenFamily.prototype.addMember = function(member, maxMembers) {
if(!maxMembers) maxMembers = 1000000000;
var i;
var oldMaxScore = this.getMaxScore();
var firstMember = (this.members.length === 0);
for(i = 0; i < this.members.length; ++i) {
if(this.members[i].score <= member.score) break;
}
if(i < maxMembers) {
this.members.splice(i, 0, member);
}
if(this.members.length > maxMembers) {
this.members = this.members.slice(0, maxMembers);
}
var newMaxScore = this.getMaxScore();
var scoreDiffThreshold = 0.00001;
if(oldMaxScore < newMaxScore && newMaxScore - oldMaxScore >= scoreDiffThreshold && !firstMember) {
this.lastGenerationScoreChange = curGeneration;
}
};
GenFamily.prototype.getMaxScore = function() {
if(!this.members.length) return 0;
return this.members[0].score;
};
GenFamily.prototype.resortMembers = function() {
this.members.sort(function(a, b) {
return b.score - a.score;
});
};
function GenFamilySet() {
this.families = []; // store in order from highest score to lowest score
}
GenFamilySet.prototype.addFamily = function(family) {
this.families.push(family);
};
GenFamilySet.prototype.resortFamilies = function(maxFamilies, phaseConfig, onlySort) {
if(!maxFamilies) maxFamilies = 1000000000;
this.families.sort(function(a, b) {
return b.getMaxScore() - a.getMaxScore();
});
if(onlySort) return;
if(this.families.length > maxFamilies) {
this.families = this.families.slice(0, maxFamilies);
}
// Check for stale pruning
if(phaseConfig && phaseConfig.stalePruning && phaseConfig.stalePruning.pruneStaleFamilies) {
var newFamilies = [];
this.families.forEach(function(family, familyIdx) {
var staleGenerations = curGeneration - family.lastGenerationScoreChange;
if(staleGenerations > phaseConfig.stalePruning.staleGenerations && familyIdx >= phaseConfig.stalePruning.keepTopFamilies) {
console.log('Pruning stale family');
} else {
newFamilies.push(family);
}
});
this.families = newFamilies;
}
};
function GenPopulation() {
this.familySet = new GenFamilySet();
this.curPhase = 0;
this.curPhaseGeneration = 0;
}
function randGrid() {
var dims = reactorsim.getDimensions(config.reactorSize);
var comps = [];
for(var i = 0; i < dims.width * dims.height; ++i) {
comps.push(weightedRand(config.componentWeights));
}
var grid = makeGrid(dims.width, dims.height, comps);
return grid;
}
function writeState() {
console.log('Saving state ...');
var obj = {
reactorIdCtr: reactorIdCtr,
curGeneration: curGeneration,
populations: populations
};
fs.writeFileSync('./current-state.json', JSON.stringify(obj, null, 2));
console.log('Done.');
}
function loadState(cb) {
if(fs.existsSync('./current-state.json')) {
var loadedPops = 0;
var loadedFams = 0;
var loadedMems = 0;
console.log('Loading saved state ...');
var state = require('./current-state.json');
curGeneration = state.curGeneration;
reactorIdCtr = state.reactorIdCtr;
for(var popId in state.populations) {
loadedPops++;
var statePop = state.populations[popId];
populations[popId] = new GenPopulation();
var newPop = populations[popId];
newPop.curPhase = statePop.curPhase;
newPop.curPhaseGeneration = statePop.curPhaseGeneration;
statePop.familySet.families.forEach(function(stateFam) {
loadedFams++;
var newFam = new GenFamily();
newFam.lastGenerationScoreChange = stateFam.lastGenerationScoreChange;
stateFam.members.forEach(function(stateMem) {
var newMem = new GenReactor(makeGrid(stateMem.grid.width, stateMem.grid.height, stateMem.grid.data), stateMem.results, stateMem.score);
newMem.id = stateMem.id;
newFam.addMember(newMem);
loadedMems++;
});
newPop.familySet.addFamily(newFam);
});
populations[popId] = newPop;
}
console.log('Loaded ' + loadedPops + ' populations, ' + loadedFams + ' families, ' + loadedMems + ' members.');
console.log('Rescoring loaded reactors ...');
rerunAll(cb);
} else {
cb();
}
}
function rerunAll(cb) {
async.eachSeries(Object.keys(populations), function(popId, cb) {
var pop = populations[popId];
async.eachSeries(pop.familySet.families, function(family, cb) {
async.eachSeries(family.members, function(member, cb) {
reactorsim.runSimulation(member.grid, function(error, simResults) {
if(error) return cb(error);
var phaseConfig = config.populations[popId].phases[pop.curPhase];
var score = scoreReactor(phaseConfig, member.grid, simResults);
member.results = simResults;
member.score = score;
cb();
});
}, function() {
family.resortMembers();
cb();
});
}, function() {
pop.familySet.resortFamilies(null, null, true);
cb();
});
}, function() {
cb();
});
}
function runGeneration(cb) {
console.log('Generating reactors ...');
var i;
async.eachSeries(Object.keys(populations), function(populationId, cb) {
console.log('Generating reactors for ' + populationId + ' ...');
var population = populations[populationId];
var popConfig = config.populations[populationId];
var popPhases = popConfig.phases;
// Check if switching to next phase
if(population.curPhaseGeneration >= popConfig.phases[population.curPhase].numGenerations) {
population.curPhase++;
population.curPhaseGeneration = 0;
if(population.curPhase >= popConfig.phases.length) population.curPhase = 0;
}
var phaseConfig = popConfig.phases[population.curPhase];
var mutations = new Mutations(phaseConfig);
var nRandFamilies = (curGeneration === 0) ? phaseConfig.algorithm.randomFamiliesInitialGeneration : phaseConfig.algorithm.randomFamiliesPerGeneration;
var reactorsToRun = [];
// Generate mutated reactors
population.familySet.families.forEach(function(family) {
family.members.forEach(function(member) {
for(i = 0; i < phaseConfig.algorithm.offspringPerMember; ++i) {
var newGrid = mutations.mutate(member.grid);
reactorsToRun.push({
family: family,
grid: newGrid
});
}
});
});
// Generate random families
for(i = 0; i < nRandFamilies; ++i) {
reactorsToRun.push({
family: null,
grid: randGrid()
});
}
function hybridFamilyProps(fam1, fam2) {
function min(a, b) {
return (a < b) ? a : b;
}
var ret = {};
if(phaseConfig.stalePruning && phaseConfig.stalePruning.preserveStaleCounterOnHybrid) {
ret.lastGenerationScoreChange = min(fam1.lastGenerationScoreChange, fam2.lastGenerationScoreChange);
}
return ret;
}
// Generate same-family hybrids
for(i = 0; i < phaseConfig.algorithm.sameFamilyHybrids; ++i) {
var family = population.familySet.families[Math.floor(Math.random() * population.familySet.families.length)];
if(family && family.members.length > 1) {
var hybridMember1 = family.members[Math.floor(Math.random() * family.members.length)];
var hybridMember2;
do {
hybridMember2 = family.members[Math.floor(Math.random() * family.members.length)];
} while(hybridMember2 === hybridMember1);
reactorsToRun.push({
family: family,
grid: mutations.hybrid(hybridMember1.grid, hybridMember2.grid)
});
}
}
// Generate cross-family hybrids
for(i = 0; i < phaseConfig.algorithm.crossFamilyHybrids; ++i) {
if(population.familySet.families.length > 1) {
var parentFamily1 = population.familySet.families[Math.floor(Math.random() * population.familySet.families.length)];
var parentFamily2;
do {
parentFamily2 = population.familySet.families[Math.floor(Math.random() * population.familySet.families.length)];
} while(parentFamily1 === parentFamily2);
var hybridMember1 = parentFamily1.members[Math.floor(Math.random() * parentFamily1.members.length)];
var hybridMember2 = parentFamily2.members[Math.floor(Math.random() * parentFamily2.members.length)];
if(hybridMember1 && hybridMember2) {
reactorsToRun.push({
family: null,
grid: mutations.hybrid(hybridMember1.grid, hybridMember2.grid),
familyProperties: hybridFamilyProps(parentFamily1, parentFamily2)
});
}
}
}
// Generate cross-population hybrids
for(i = 0; i < phaseConfig.algorithm.crossPopulationHybrids; ++i) {
if(!phaseConfig.hybridization.crossPopulationHybridWeights) throw "Missing weights";
var popId1 = weightedRand(phaseConfig.hybridization.crossPopulationHybridWeights);
var pop1 = populations[popId1];
var popId2 = weightedRand(phaseConfig.hybridization.crossPopulationHybridWeights);
var pop2 = populations[popId2];
if(popId1 == popId2 && pop1.familySet.families.length < 2) continue;
if(pop1.familySet.families.length < 1 || pop2.familySet.families.length < 1) continue;
var parentFamily1 = pop1.familySet.families[Math.floor(Math.random() * pop1.familySet.families.length)];
var parentFamily2;
do {
parentFamily2 = pop2.familySet.families[Math.floor(Math.random() * pop2.familySet.families.length)];
} while(popId1 === popId2 && parentFamily1 === parentFamily2);
var hybridMember1 = parentFamily1.members[Math.floor(Math.random() * parentFamily1.members.length)];
var hybridMember2 = parentFamily2.members[Math.floor(Math.random() * parentFamily2.members.length)];
if(hybridMember1 && hybridMember2) {
reactorsToRun.push({
family: null,
grid: mutations.hybrid(hybridMember1.grid, hybridMember2.grid),
familyProperties: hybridFamilyProps(parentFamily1, parentFamily2)
});
}
}
// Add promoted reactors, and prune the population families
if(phaseConfig.promotions && curGeneration % phaseConfig.promotions.promotionInterval == phaseConfig.promotions.promotionInterval - 1) {
phaseConfig.promotions.promotedPopulations.forEach(function(promPopId) {
var promPop = populations[promPopId];
promPop.familySet.resortFamilies(phaseConfig.promotions.prePromotionPruneFamilies);
promPop.familySet.families.slice(0, phaseConfig.promotions.numPromotionsPerPopulation).forEach(function(family) {
if(family.members.length) {
reactorsToRun.push({
family: null,
grid: family.members[0].grid
});
}
});
});
}
// Run each reactor and add the results to the state
console.log('Running simulations (' + reactorsToRun.length + ') ...');
async.eachLimit(reactorsToRun, 16, function(reac, cb) {
reactorsim.runSimulation(reac.grid, function(error, simResults) {
if(error) return cb(error);
var score = scoreReactor(phaseConfig, reac.grid, simResults);
var reactor = new GenReactor(reac.grid, simResults, score);
if(reac.family) {
reac.family.addMember(reactor, phaseConfig.algorithm.membersPerFamily);
} else {
population.familySet.addFamily(new GenFamily([reactor], reac.familyProperties));
}
cb();
});
}, function(error) {
if(error) throw error;
// Resort and truncate families
population.familySet.resortFamilies(phaseConfig.algorithm.families, phaseConfig);
population.curPhaseGeneration++;
cb();
});
}, function() {
curGeneration++;
writeState();
cb();
});
}
function run() {
loadState(function() {
async.whilst(function() {
return true;
}, function(cb) {
console.log('=== Running generation ' + curGeneration + ' ===');
runGeneration(function() {
console.log('Top Result:');
var bestFam = populations[config.resultPopulation].familySet.families[0];
if(bestFam) {
var best = bestFam.members[0];
if(best) {
gridUtils.printGrid(best.grid);
console.log(best.results);
} else {
console.log('None yet.');
}
} else {
console.log('None yet.');
}
cb();
});
}, function() {
console.log('Done.');
});
});
}
exports.run = run;
exports.getStatus = function() {
return {
curGeneration: curGeneration,
populations: populations
};
};