@@ -2,33 +2,49 @@ package scenes
2
2
3
3
import (
4
4
"math/rand"
5
- "os"
6
5
"strconv"
7
6
"time"
8
7
9
8
"github.com/andersondalmina/flappy-bird-neural-network/components"
10
9
"github.com/andersondalmina/flappy-bird-neural-network/neuralnetwork"
10
+ "github.com/andersondalmina/flappy-bird-neural-network/persist"
11
11
"github.com/faiface/pixel"
12
12
"github.com/faiface/pixel/pixelgl"
13
13
"golang.org/x/image/colornames"
14
14
)
15
15
16
- const datafile = "neuraldump_iahard.json"
17
-
18
16
type iaHard struct {
19
- pop * components.Population
20
- obstacles []components.Obstacle
17
+ generation int64
18
+ pop * components.Population
19
+ obstacles []components.Obstacle
21
20
}
22
21
23
22
// CreateIAHardScene create a scene when a machine plays
24
23
func CreateIAHardScene (gn int64 ) Scene {
24
+ return & iaHard {
25
+ generation : gn ,
26
+ }
27
+ }
28
+
29
+ func (s * iaHard ) Load () Scene {
30
+ datafile = "neuraldump_iahard.json"
31
+
32
+ var data [][][]float64
33
+ var err error
34
+
25
35
rand .Seed (time .Now ().UTC ().UnixNano ())
26
36
27
37
resetWallTime ()
28
38
29
- s := iaHard {
30
- pop : components .CreateNewPopulation (gn ),
31
- obstacles : make ([]components.Obstacle , 4 ),
39
+ s .pop = components .CreateNewPopulation (s .generation )
40
+ s .obstacles = make ([]components.Obstacle , 4 )
41
+
42
+ err = persist .CheckFileExists (datafile )
43
+ if err == nil {
44
+ err = persist .Load (datafile , & data )
45
+ if err != nil {
46
+ panic (err )
47
+ }
32
48
}
33
49
34
50
var n int64
@@ -38,27 +54,28 @@ func CreateIAHardScene(gn int64) Scene {
38
54
n = rand .Int63n (4 ) + 1
39
55
t = strconv .FormatInt (n , 10 )
40
56
neural := neuralnetwork .NewNeuralNetwork (neuralnetwork.Config {
41
- Inputs : 4 ,
42
- Layers : []int64 {4 , 20 , 2 },
57
+ Inputs : 3 ,
58
+ Layers : []int64 {10 , 20 , 1 },
43
59
})
44
60
ind = components .NewIndividual (components .NewBird (components .BirdX - rand .Float64 ()* 200 , components .Sprites ["bird1" + t ]), neural )
45
61
46
- _ , err := os .Stat (datafile )
47
- if gn == 1 && err == nil {
48
- ind .Neural ().ImportDump (datafile )
62
+ if s .generation == 1 {
63
+ ind .Neural ().UpdateWeights (data )
49
64
50
- } else if gn > 1 {
51
- ind .Neural ().SetWeights ( neuralnetwork . AjustWeight ( ind .Neural ().Weights () ))
65
+ } else if s . generation > 1 {
66
+ ind .Neural ().UpdateWeights ( ind .Neural ().Weights ())
52
67
}
53
68
54
69
s .pop .AddIndividual (ind )
55
70
}
56
71
57
- for i := 0 ; i < 4 ; i ++ {
72
+ for i := 0 ; i < 3 ; i ++ {
58
73
s .obstacles [i ] = components .NewPipe (components .WindowWidth + 320 * float64 (i ), (components .WindowHeight - components .PipeHeight * 2 )- rand .Float64 ()* 10 * components .PipeHeight )
59
74
}
60
75
61
- return & s
76
+ s .obstacles [3 ] = components .NewWall (components .WindowWidth + 320 * 3 )
77
+
78
+ return s
62
79
}
63
80
64
81
func (s * iaHard ) Run (win * pixelgl.Window ) Scene {
@@ -90,20 +107,21 @@ func (s *iaHard) Run(win *pixelgl.Window) Scene {
90
107
continue
91
108
}
92
109
93
- bInputs = make ([]float64 , 4 )
110
+ bInputs = make ([]float64 , 3 )
94
111
np = s .getBirdNextObstacle (bird )
95
112
bInputs [0 ] = np .GetX () - bird .X
96
113
bInputs [1 ] = np .GetY () - components .PipeHeight - bird .Y
97
114
bInputs [2 ] = np .GetType ()
98
- bInputs [3 ] = float64 (bird .GhostCountdown )
99
115
100
116
ind .SetInputs (bInputs )
101
117
102
118
result = ind .Neural ().Predict (bInputs )
103
119
104
120
if result [0 ] > 0 {
105
121
bird .Jump ()
106
- } else if result [1 ] > 0 {
122
+ }
123
+
124
+ if result [1 ] > 0 {
107
125
bird .UseGhost ()
108
126
}
109
127
@@ -130,17 +148,15 @@ func (s *iaHard) Run(win *pixelgl.Window) Scene {
130
148
}
131
149
}
132
150
133
- // if best.Bird().Points > bestPoints {
134
151
bestWeights = best .Neural ().Weights ()
135
152
bestPoints = best .Bird ().Points
136
- // }
137
153
138
154
err := best .Neural ().Dump (datafile )
139
155
if err != nil {
140
156
panic (err )
141
157
}
142
158
143
- return CreateIAHardScene (s .pop .Generation () + 1 )
159
+ return CreateIAHardScene (s .pop .Generation () + 1 ). Load ()
144
160
}
145
161
146
162
return s
0 commit comments