-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.js
83 lines (62 loc) · 1.47 KB
/
tester.js
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
var aai = require('./adaptive_ai').GA;
var org = require('./adaptive_ai').Org;
var nStates = 3;
var orgs = []
for (var i=0;i<10;i++) {
var organism = new org.Organism();
orgs.push(organism);
organism.setStateCount(nStates);
organism.setStateName(0, '.');
organism.setStateName(1, '-');
organism.setStateName(2, 'X');
organism.setSensorCount(1);
for (j=0; j<nStates; j++)
for (k=0; k<nStates; k++)
organism.setTransition(j, k, .2, [0]);
}
var gen = 0;
for (var i=0; i<100000; i++) {
update(i, false);
}
for (var i=0; i<10000; i++) {
update(i, true);
}
function update(i, outFlag) {
var mod = i%30;
if (i%100 === 99) {
gen += 1;
for (var j=0;j<10;j++) {
var org = orgs[j]
org.mutate();
}
}
var ostring = 'gen['+(gen+1)+']: ';
for (var j=0;j<10;j++) {
var org = orgs[j]
if (mod === 29) {
org.setSensorValue(0,100);
} else if (mod === 0) {
org.setSensorValue(0,0);
}
var idx = org.getCurrentState();
org.updateState();
if (outFlag === true)
ostring += organism.getStateName(idx) + '\t';
}
if (outFlag === true)
console.log(ostring);
}
function printGenome(genome) {
for (var c=0; c<genome.getChromosomeCount(); c++) {
var chrom = genome.getChromosome(c);
for (var g=0; g<chrom.getGeneCount(); g++) {
var gene = chrom.getGene(g);
var os = 'c'+c+' '+'g'+g+': ';
for (var i=0, l=gene._sequence.length; i<l; i++) {
os += gene._sequence[i].toFixed(4) + ' ';
}
console.log(os);
}
}
console.log('\n');
}