-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
52 lines (49 loc) · 1.14 KB
/
test.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
class Thing {
constructor(thing) {
if (thing) {
this.net = new Net(thing.net)
this.net.mutate(2)
} else {
this.net = new Net()
}
this.test()
}
fitness() {
return this.fit
}
test() {
this.fit = 0
for (var i = 0; i < 10; i++) {
var x = Math.random()*Math.PI*2
var y = Math.cos(x)
this.net.set('x', x)
this.net.tick()
var guess = this.net.val('y')
this.fit-= Math.abs(guess - y)
}
if (this.fit > -0.0001) {
this.fit+= 1/this.net.complexity()
}
}
reproduce() {
return new Thing(this)
}
}
window.onload = () => {
var p = []
while (p.length < 40) {
p.push(new Thing())
}
var gen = null
for (var i = 0; i < 100; i++) {
gen = gen ? gen.next() : new Generation(p)
if ((i+1) % 10 == 0) {
gen.stats()
console.log('round', i, gen.best.net.complexity(), gen.best.fitness())
}
}
gen.sort()
gen.stats()
window.best = gen.best
console.log(best)
}