-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphenotype.js
More file actions
144 lines (119 loc) · 4.2 KB
/
phenotype.js
File metadata and controls
144 lines (119 loc) · 4.2 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
var phenotype = function (ph,opt) {
var opt = (opt) ? opt : {};
var rd = function(){ return Math.random(); };
var ind = function() {
this.it = 0;
this.adn = [];
this.dn = function(){
if (!this.adn[this.it]) { this.adn[this.it] = rd(); }
var r = this.adn[this.it]; this.it++;
return r;
};
this.rst = function(){ this.it = 0; };
};
var pheno = function(cr) {
cr.rst();
ph(cr.dn.bind(cr));
};
var calculateStep = function (cadnLength, genlength) {
for (var i = genlength; i > 0; i--) {
if (cadnLength % i == 0) {
return i;
}
}
return 1;
}
var setError = function(error) {
if (popsize != popu.length) {
popu.sort(function(a,b) { return a.err - b.err; });
if (popsize > popu.length) {
var nToAdd = popsize - popu.length;
for (var i = 0; i < nToAdd; i++) {
var nind = new ind();
nind.err = Infinity;
popu.push(nind);
}
} else if (popsize < popu.length) {
popu.splice(popsize, popu.length - popsize);
}
}
var cr = popu[creatid];
var errorNumber = Number(error);
if (errorNumber !== NaN) {
cr.err = errorNumber;
creatid++;
if (creatid >= popu.length) {
var cadn = popu[0].adn;
var nkill = ~~(popu.length*0.5);
var nomut = ~~(nkill*0.5);
var nnew = ~~(nkill*0.75);
popu.sort(function(a,b) { return a.err - b.err; });
popu.splice(popu.length - nkill, nkill);
var nrest = popu.length;
for (var i = 0; i < nkill; i++) {
var nind = new ind();
var step = calculateStep(cadn.length, genlength);
for (var z = 0; z < cadn.length; z+=step) {
var rind = ~~(nrest*Math.random());
for (var b = 0; b < step; b++) {
if (z+b < cadn.length) {
var v = popu[rind].adn[z+b];
if (i > nnew) {
v = Math.random();
} else if (i > nomut) {
v += (Math.random()-0.5)/mutation;
v = Math.max(Math.min(v,1),0);
}
nind.adn.push(v);
}
}
}
nind.err = lErr;
popu.push(nind);
}
creatid = 0;
progressRate = (prevError - popu[0].err) / prevError;
prevError = popu[0].err;
}
}
};
var evaluate = function() {
var cr = popu[creatid];
pheno(cr);
};
var lErr = Infinity;
var prevError = Infinity;
var progressRate = 0;
var genlength = (opt.genlength) ? opt.genlength : 1;
var popsize = (opt.popsize) ? opt.popsize : 100;
var mutation = (opt.mutation) ? opt.mutation : 1000;
var popu = [];
var creatid = 0;
for (let index = 0; index < popsize; index++) {
popu.push(new ind());
}
return {
score: function (fitness) {
setError(1/fitness);
},
error: function (err) {
setError(err);
},
run: function () {
evaluate();
},
save: function () {
popu.sort(function (a, b) {
return a.err - b.err;
});
return popu[0].adn;
},
load: function (adn) {
popu[0].adn = [];
for (let index = 0; index < adn.length; index++) {
const element = adn[index];
popu[0].adn.push(element);
}
}
};
};