-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.go
312 lines (263 loc) · 9.84 KB
/
test.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"flag"
"fmt"
"strings"
"github.com/CRRDerek/Neuroevolution/classifiers"
"github.com/CRRDerek/Neuroevolution/evolution"
"github.com/CRRDerek/Neuroevolution/games"
)
func main() {
// Parse flags
game_name := flag.String("game", "Tic Tac Toe", "Name of game to be played. Currently Tic Tac Toe and Checkers are supported.")
filename := flag.String("filename", "None", "JSON file to load containing a neural network")
generations := flag.Int("generations", 128, "Number of generations to evolve before returning the best network")
max_games := flag.Int("maxgames", 1024, "Maximum number of games to play within a population")
max_streak := flag.Int("streak", 4, "Number of generations that achieve the maximum score before ending the algorithm early")
output := flag.String("output", "data\\results.json", "Name of a JSON file to write the results to")
population := flag.Int("population", 256, "Number of individuals in the population")
hiddenNeurons := flag.Int("hiddens", 256, "Number of hidden neurons. Recommend 56 for Tic Tac Toe.")
flag.Parse()
// Declare variables
var game games.Game
var playerMaker games.PlayerMaker
var humanPlayer games.Player
var inputs int
var outputs int
var network classifiers.Classifier
hiddens := *hiddenNeurons
// Configure the correct game functions and network size based on which game the user selected
if strings.ToLower(*game_name) == "tic tac toe" {
game = games.TicTacToe
playerMaker = games.TicTacToePlayerMaker
humanPlayer = games.HumanTicTacToePlayer
// Configure a network size for a Tic Tac Toe policy network
inputs = 28
outputs = 9
} else if strings.ToLower(*game_name) == "checkers" {
// Impose a 1024 turn limit so the games don't get stuck in a loop
game = games.MakeCheckers(1024)
playerMaker = games.CheckersPlayerMaker
humanPlayer = games.HumanCheckersPlayer
// Configure a network size for a Checkers policy network
inputs = 65
outputs = 24
} else {
fmt.Println("Please choose Tic Tac Toe or Checkers.")
return
}
// If there is a file specificed, load the file instead of evolving
if *filename != "None" {
var err error
network, err = classifiers.LoadJSON(*filename)
if err != nil {
fmt.Println("Error loading classifier: ", err)
return
}
} else {
// Seed the initial population
pop := make([]classifiers.Classifier, *population)
for i := 0; i < *population; i++ {
pop[i] = classifiers.RandomNetwork(inputs, hiddens, outputs)
}
network = evolution.EvolveAgents(game, playerMaker, *generations, *max_games, *max_streak, pop, evolution.Elimination_fitness)
// Save the results to a file
save(network, *output)
}
// Make a player out of the network
player := playerMaker(network)
// Play the game indefinitely
for {
victor := game(player, humanPlayer)
if victor == -1 {
fmt.Println("\n\nYou win!")
} else if victor == 0 {
fmt.Println("\n\nDraw!")
} else if victor == 1 {
fmt.Println("\n\nYou lose!")
}
}
}
func DemoTicTacToe() {
network, _ := classifiers.LoadJSON("data\\Final_TicTacToe.json")
ai_player := games.TicTacToePlayerMaker(network)
for {
victor := games.TicTacToe(ai_player, games.HumanTicTacToePlayer)
if victor == -1 {
fmt.Println("\n\nYou win!")
} else if victor == 0 {
fmt.Println("\n\nDraw!")
} else if victor == 1 {
fmt.Println("\n\nYou lose!")
}
}
}
// Seed a population of networks capable of learning XOR and then run neuroevolution
// on the XOR game.
func testXOR() {
// Seed the initial population
pop := make([]classifiers.Classifier, 100)
for i := 0; i < 100; i++ {
pop[i] = classifiers.RandomNetwork(3, 4, 1)
}
evolution.EvolveAgents(games.XorGame, games.XorGamePlayerMaker,
2000, 256, 10, pop, evolution.Elimination_fitness)
}
func testSaveJSON() {
evolved_agent := classifiers.RandomNetwork(28, 56, 9)
fmt.Println("Training complete!")
fmt.Println("Saving to file...")
err := evolved_agent.SaveJSON("data/testSave.json")
if err != nil {
fmt.Println("Error saving agent: ", err)
}
fmt.Println("Loading from file...")
// Load agent
loaded_agent, err := classifiers.LoadJSON("data/testSave.json")
if err != nil {
fmt.Println("Error saving agent: ", err)
}
// Play tic tac toe against the user
for {
victor := games.TicTacToe(games.TicTacToePlayerMaker(loaded_agent), games.HumanTicTacToePlayer)
if victor == -1 {
fmt.Println("\n\nYou win!")
} else if victor == 0 {
fmt.Println("\n\nDraw!")
} else if victor == 1 {
fmt.Println("\n\nYou lose!")
}
}
}
// Seed a population of networks capable of learning Tic Tac Toe (input size 28,
// output size 9) and run neuroevolution to produce an agent that has evolved to
// play tic tac toe.
//
// Run tic tac toe games against the user indefinitely once the evolved agent is ready.
func testTicTacToe() {
// Seed the initial population
pop_size := 256
pop := make([]classifiers.Classifier, pop_size)
for i := 0; i < pop_size; i++ {
pop[i] = classifiers.RandomNetwork(28, 56, 9)
}
// Evolve an agent capable of playing
evolved_agent := evolution.EvolveAgents(games.TicTacToe, games.TicTacToePlayerMaker,
64, 1024, 8, pop, evolution.Elimination_fitness)
fmt.Println("Training complete!")
save(evolved_agent, "data/TicTacToe.json")
// Play tic tac toe against the user
for {
victor := games.TicTacToe(games.TicTacToePlayerMaker(evolved_agent), games.HumanTicTacToePlayer)
if victor == -1 {
fmt.Println("\n\nYou win!")
} else if victor == 0 {
fmt.Println("\n\nDraw!")
} else if victor == 1 {
fmt.Println("\n\nYou lose!")
}
}
}
func testEvolveCheckersPolicyNetwork() {
testEvolveCheckers(games.CheckersPlayerMaker, "data/CheckersPolicyNetwork.json")
}
func testEvolveCheckersValueNetwork() {
testEvolveCheckers(games.ClassifierHeuristicPlayerMakerMaker(games.Checkers_make_move, games.CheckersTranslateInputs), "data/CheckersValueNetwork.json")
}
// Seed a population of networks capable of learning Checkers (input size 65,
// output size 24) and run neuroevolution to produce an agent that has evolved to
// play Checkers.
//
// Run Checkers games against the user indefinitely once the evolved agent is ready.
func testEvolveCheckers(playerMaker games.PlayerMaker, filename string) {
// Seed the initial population
pop_size := 256
pop := make([]classifiers.Classifier, pop_size)
for i := 0; i < pop_size; i++ {
pop[i] = classifiers.RandomNetwork(65, 256, 24)
}
// Run neuroevolution to produce an agent. The checkers games used by the
// evolutionary algorithm will be cut off after 100 moves to prevent
// random players from prolonging the game indefinitely.
evolved_agent := evolution.EvolveAgents(games.MakeCheckers(1024), playerMaker,
1024, 1024, 4, pop, evolution.Elimination_fitness) // Each member of the population will be tested at maximum 128 times.
// After 256 generations the algorithm concludes if it hasn't already spawned
// an agent that can win 128 times for 4 generations.
fmt.Println("Training complete!")
save(evolved_agent, filename)
// Play checkers against the user indefinitely
for {
victor := games.Checkers(playerMaker(evolved_agent), games.HumanCheckersPlayer)
if victor == -1 {
fmt.Println("\n\nYou win!")
} else if victor == 0 {
fmt.Println("\n\nDraw!")
} else if victor == 1 {
fmt.Println("\n\nYou lose!")
}
}
}
// Test the user interface of checkers against a random player
func testRandomCheckers() {
for {
victor := games.Checkers(games.HumanCheckersPlayer, games.RandomPlayer)
if victor == 1 {
fmt.Println("\n\nYou win!")
} else if victor == 0 {
fmt.Println("\n\nDraw!")
} else if victor == -1 {
fmt.Println("\n\nYou lose!")
}
}
}
// Test the user interface of checkers against a random player
func testDepthOneCheckers() {
for {
victor := games.Checkers(games.HumanCheckersPlayer, games.DepthOneSearchPlayerMaker(games.Checkers_heuristic, games.Checkers_make_move))
if victor == 1 {
fmt.Println("\n\nYou win!")
} else if victor == 0 {
fmt.Println("\n\nDraw!")
} else if victor == -1 {
fmt.Println("\n\nYou lose!")
}
}
}
func save(c classifiers.Classifier, filename string) {
err := c.SaveJSON(filename)
if err != nil {
fmt.Println("Error saving agent: ", err)
}
fmt.Println("Saved to file: ", filename)
}
func testEvolvedNetworks() {
// The first networks are all policy networks
network_a, _ := classifiers.LoadJSON("data\\Checkers_01232017_1.json")
player_a := games.CheckersPlayerMaker(network_a)
network_b, _ := classifiers.LoadJSON("data\\Checkers_01232017_2.json")
player_b := games.CheckersPlayerMaker(network_b)
network_c, _ := classifiers.LoadJSON("data\\Checkers_01242017_1.json")
player_c := games.CheckersPlayerMaker(network_c)
// These are value networks
network_d, _ := classifiers.LoadJSON("data\\Checkers_01242017_2.json")
player_d := games.ClassifierHeuristicPlayerMakerMaker(games.Checkers_make_move, games.CheckersTranslateInputs)(network_d)
network_e, _ := classifiers.LoadJSON("data\\Checkers_01242017_3.json")
player_e := games.ClassifierHeuristicPlayerMakerMaker(games.Checkers_make_move, games.CheckersTranslateInputs)(network_e)
players := []games.Player{player_a, player_b, player_c, player_d, player_e, games.RandomPlayer, games.DepthOneSearchPlayerMaker(games.Checkers_heuristic, games.Checkers_make_move)}
player_names := []string{"Policy Network A", "Policy Network B", "Policy Network C", "Value Network D", "Value Network E", "Random Player", "Depth One Heuristic Player"}
for i := 0; i < len(players); i++ {
score := 0
for j := 0; j < len(players); j++ {
victor := games.MakeCheckers(1024)(players[j], players[i])
if victor == -1 {
score += 1
fmt.Printf("%v defeated %v\n", player_names[i], player_names[j])
} else if victor == 1 {
fmt.Printf("%v was defeated by %v\n", player_names[i], player_names[j])
} else {
fmt.Printf("%v and %v tied!\n", player_names[i], player_names[j])
}
}
fmt.Printf("\n%v won %v games\n***\n\n", player_names[i], score)
}
}