This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
188 lines (150 loc) · 3.66 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
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
// Copyright 2017 Martin Planer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"runtime/pprof"
"sort"
"time"
)
const boardSize = 10
const popSize = 100
const generations = 2000
const mutateProb = 0.30
const fitChangeThreshold = 100
const fitChangeLength = 100
func main() {
stopProfiling := initProfiling()
rand.Seed(time.Now().UnixNano())
pop := make([]Board, 0, popSize)
for i := 0; i < popSize; i++ {
pop = append(pop, RandomBoard())
}
fit := FitnessTracker{
trackLength: 100,
avgChangeThreshold: 5,
}
// for gen := 0; gen < generations; gen++ {
// for SelectBestIndividual(pop).Fitness() < 0 {
gen := 1
for fit.IsImproving() {
best10 := SelectByFitness(pop, 10)
worthy := SelectByFitnessRoulette(pop, 20)
children := CrossoverRandom(worthy, 70)
Mutate(children)
pop = append(worthy, children...)
pop = append(pop, best10...)
best := SelectBestIndividual(pop)
fmt.Printf("Generation %04d: %d\n", gen, best.Fitness())
fit.Add(best.Fitness())
gen++
}
b := SelectBestIndividual(pop)
fmt.Println(b, b.Fitness())
exportBoard(b, "board.png")
// open image
open("board.png")
stopProfiling()
}
func initProfiling() func() {
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
return pprof.StopCPUProfile
}
// SelectByFitness returns the best n boards of the population,
// chosen by value of the fitness function. n must be greater than 0.
func SelectByFitness(pop []Board, n int) []Board {
sort.Sort(sort.Reverse(ByFitness(pop)))
return pop[0:n]
}
func SelectByFitnessRoulette(pop []Board, n int) []Board {
sort.Sort(sort.Reverse(ByFitness(pop)))
selected := make([]Board, 0, n)
fitSum := 0
leastFit := pop[len(pop)-1].Fitness()
fits := make([]int, len(pop))
if leastFit < 0 {
leastFit *= -1
} else {
leastFit = 0
}
for i := 0; i < len(pop); i++ {
fit := pop[i].Fitness() + leastFit + 1
fitSum += fit
fits[i] = fit
}
for len(selected) < n {
v := rand.Intn(fitSum) + 1
for j := 0; j < len(fits); j++ {
v -= fits[j]
if v <= 0 {
selected = append(selected, pop[j])
break
}
}
}
return selected
}
func SelectBestIndividual(pop []Board) Board {
return SelectByFitness(pop, 1)[0]
}
func CrossoverPerm(pop []Board) []Board {
perm := rand.Perm(len(pop))
children := make([]Board, 0, len(pop))
for i := 0; i < len(pop); i += 2 {
c1, c2 := pop[perm[i]].Crossover(pop[perm[i+1]])
children = append(children, c1, c2)
}
return children
}
func CrossoverRandom(pop []Board, n int) []Board {
children := make([]Board, 0, n)
for i := 0; i < n; i++ {
// Choose parents
p1 := rand.Intn(len(pop))
p2 := rand.Intn(len(pop))
c, _ := pop[p1].Crossover(pop[p2])
children = append(children, c)
}
return children
}
func Mutate(pop []Board) {
for i := 0; i < len(pop); i++ {
if rand.Float32() < mutateProb {
pop[i].Mutate()
}
}
}
//// SelectByFitness returns n boards of the population,
//// chosen by roulette wheel selection (ordered by value of the fitness function).
//func SelectByFitness(pop []Board, n int) []Board {
// sort.Sort(sort.Reverse(ByFitness(pop)))
// result = make([]Board, 0, n)
// fitSum := 0
// for _, b := range pop {
// fitSum += b.Fitness()
// }
// for
//}
type ByFitness []Board
func (b ByFitness) Len() int {
return len(b)
}
func (b ByFitness) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b ByFitness) Less(i, j int) bool {
return b[i].Fitness() < b[j].Fitness()
}