-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (114 loc) · 2.98 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"time"
"./neural"
"./pong"
)
func genBlueprint(num_layers, inputs, outputs int) []int {
blueprint := make([]int, num_layers, num_layers)
blueprint[0] = inputs
for i := 1; i < num_layers-1; i++ {
blueprint[i] = 1
}
blueprint[len(blueprint)-1] = outputs
return blueprint
}
func testWorld() {
type pair struct {
score int
bounces int
nn neural.NeuralNet
}
num_networks := 2000
networks := make([]neural.NeuralNet, num_networks, num_networks)
blueprint := []int{10, 10}
activation := func(input []int) int {
max := input[0]
idx := 0
for i, cur := range input {
if cur > max {
max = cur
idx = i
}
}
return idx
}
ch := make(chan pair)
// monitoring:
first := true
last_score := 0
for i, _ := range networks {
go func(i int) {
networks[i] = neural.GenerateNN(blueprint, activation)
ch<-pair{}
}(i)
}
for i := 0; i < len(networks); i++ {
<-ch
}
for i:=0;; i++{
for _, network := range networks {
go func(nn neural.NeuralNet) {
w := pong.GenWorld(100, 200)
for ;; {
input := w.GetState()
filtered_input := []int{input[1], input[5]}
decision := nn.Process(filtered_input)
//fmt.Println("decision:", decision)
if !w.Tick(decision) {
ch<-pair{score: w.Score, bounces: w.Bounces, nn: nn}
return
}
}
}(network)
}
cur_result := <-ch
best_score, best_nn, best_bounces := cur_result.score, cur_result.nn, cur_result.bounces
for i := 1; i < num_networks; i++ {
cur_result := <-ch
cur_score, cur_nn, cur_bounces := cur_result.score, cur_result.nn, cur_result.bounces
if cur_score > best_score {
best_score, best_nn, best_bounces = cur_score, cur_nn, cur_bounces
}
}
// metrics
if first {
last_score = best_score
fmt.Println(i, ":", best_bounces, best_score)
first = false
} else if best_score > last_score {
last_score = best_score
fmt.Println(i, ":", best_bounces, best_score)
}
// breeding function
networks[0] = best_nn
for i := num_networks/10; i < num_networks; i++{
go func(i int) {
networks[i] = neural.GenerateNN(blueprint, activation)
ch<-pair{}
}(i)
}
for i := num_networks/10; i < num_networks; i++{
<-ch
}
for i := 1; i < num_networks; i++{
go func(i int) {
networks[i] = neural.Breed(best_nn, networks[i])
ch<-pair{}
}(i)
}
for i := 1; i < num_networks; i++{
<-ch
}
}
return
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
//testBreed()
//testDecision()
testWorld()
return
}