File tree Expand file tree Collapse file tree 3 files changed +9
-13
lines changed
examples/NeuroEvolution-flappy-bird Expand file tree Collapse file tree 3 files changed +9
-13
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class Bird {
5
5
this . brain = brain ;
6
6
} else {
7
7
this . brain = ml5 . neuralNetwork ( {
8
- inputs : 5 ,
8
+ inputs : 4 ,
9
9
outputs : [ "flap" , "no flap" ] ,
10
10
task : "classification" ,
11
11
neuroEvolution : true ,
@@ -29,7 +29,7 @@ class Bird {
29
29
think ( pipes ) {
30
30
let nextPipe = null ;
31
31
for ( let pipe of pipes ) {
32
- if ( pipe . x > this . x ) {
32
+ if ( pipe . x + pipe . w > this . x ) {
33
33
nextPipe = pipe ;
34
34
break ;
35
35
}
@@ -39,7 +39,6 @@ class Bird {
39
39
this . y / height ,
40
40
this . velocity / height ,
41
41
nextPipe . top / height ,
42
- nextPipe . bottom / height ,
43
42
( nextPipe . x - this . x ) / width ,
44
43
] ;
45
44
@@ -59,12 +58,11 @@ class Bird {
59
58
this . velocity += this . gravity ;
60
59
this . y += this . velocity ;
61
60
// Dampen velocity
62
- this . velocity *= 0.9 ;
61
+ this . velocity *= 0.95 ;
63
62
64
63
// Handle the "floor"
65
- if ( this . y > height ) {
66
- this . y = height ;
67
- this . velocity = 0 ;
64
+ if ( this . y > height || this . y < 0 ) {
65
+ this . alive = false ;
68
66
}
69
67
70
68
this . fitness ++ ;
Original file line number Diff line number Diff line change 1
1
class Pipe {
2
2
constructor ( ) {
3
- this . spacing = 75 ;
3
+ this . spacing = 100 ;
4
4
this . top = random ( height - this . spacing ) ;
5
5
this . bottom = this . top + this . spacing ;
6
6
this . x = width ;
Original file line number Diff line number Diff line change @@ -15,8 +15,8 @@ function draw() {
15
15
background ( 255 ) ;
16
16
17
17
for ( let i = pipes . length - 1 ; i >= 0 ; i -- ) {
18
- pipes [ i ] . show ( ) ;
19
18
pipes [ i ] . update ( ) ;
19
+ pipes [ i ] . show ( ) ;
20
20
if ( pipes [ i ] . offscreen ( ) ) {
21
21
pipes . splice ( i , 1 ) ;
22
22
}
@@ -35,15 +35,13 @@ function draw() {
35
35
}
36
36
}
37
37
38
- if ( frameCount % 75 == 0 ) {
38
+ if ( frameCount % 100 == 0 ) {
39
39
pipes . push ( new Pipe ( ) ) ;
40
40
}
41
41
42
42
if ( allBirdsDead ( ) ) {
43
43
normalizeFitness ( ) ;
44
44
reproduction ( ) ;
45
- pipes = [ ] ;
46
- pipes . push ( new Pipe ( ) ) ;
47
45
}
48
46
}
49
47
@@ -62,7 +60,7 @@ function reproduction() {
62
60
let parentA = weightedSelection ( ) ;
63
61
let parentB = weightedSelection ( ) ;
64
62
let child = parentA . brain . crossover ( parentB . brain ) ;
65
- child . mutate ( 0.1 ) ;
63
+ child . mutate ( 0.01 ) ;
66
64
nextBirds [ i ] = new Bird ( child ) ;
67
65
}
68
66
birds = nextBirds ;
You can’t perform that action at this time.
0 commit comments