-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (172 loc) · 4.54 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"regexp"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var isOneAndZero = regexp.MustCompile(`^[10]+$`).MatchString
func parseFlags() (size int, uniformRate, mutationRate float64, target []byte, elitist bool) {
flag.IntVar(&size, "size", 50, "sets the population size")
flag.Float64Var(&uniformRate, "uniform-rate", 0.5, "sets the crossover uniform rate")
flag.Float64Var(&mutationRate, "mutation-rate", 0.015, "sets the mutation rate")
flag.BoolVar(&elitist, "elitist", true, "sets elitism")
targetString := flag.String("target", "110011", "establishes the target gene sequence, 1s and 0s sequence")
flag.Parse()
if *targetString == "" || !isOneAndZero(*targetString) {
log.Fatal("invalid target")
}
target = stringToGenes(*targetString)
return
}
func stringToGenes(target string) []byte {
genes := make([]byte, len(target))
for i, c := range target {
if c == '1' {
genes[i] = 1
} else {
genes[i] = 0
}
}
return genes
}
func main() {
size, uniformRate, mutationRate, target, elitism := parseFlags()
pop := newInitializedPopulation(
size,
uniformRate,
mutationRate,
target,
elitism,
)
for pop.fittest().fitness() < len(pop.target) {
fmt.Printf("Generation: %d Fittest: %d\n%s\n\n", pop.generation, pop.fittest().fitness(), pop.fittest())
pop.evolve()
}
fmt.Println("Solution found!")
fmt.Println("Generations:", pop.generation)
fmt.Println("Genes:", pop.fittest())
}
type population struct {
individuals []*individual
target []byte
generation int
elitist bool
uniformRate float64
mutationRate float64
}
func newPopulation(size int, uniformRate, mutationRate float64, target []byte, elitist bool) *population {
return &population{
individuals: make([]*individual, size),
target: target,
elitist: elitist,
uniformRate: uniformRate,
mutationRate: mutationRate,
}
}
func newInitializedPopulation(size int, uniformRate, mutationRate float64, target []byte, elitist bool) *population {
pop := newPopulation(size, uniformRate, mutationRate, target, elitist)
for i := 0; i < size; i++ {
pop.individuals[i] = newIndividual(len(target), pop.fitness)
}
return pop
}
func (pop *population) evolve() {
evolved := newPopulation(len(pop.individuals), pop.uniformRate, pop.mutationRate, pop.target, pop.elitist)
startAt := 0
if pop.elitist {
evolved.individuals[0] = pop.fittest()
startAt = 1
}
for i := startAt; i < len(pop.individuals); i++ {
a := pop.individualByTournament(5)
b := pop.individualByTournament(5)
c := crossover(a, b, pop.uniformRate)
c.mutate(pop.mutationRate)
evolved.individuals[i] = c
}
pop.generation++
pop.individuals = evolved.individuals
}
func (pop *population) individualByTournament(size int) *individual {
tournament := newPopulation(size, pop.uniformRate, pop.mutationRate, pop.target, pop.elitist)
for i := 0; i < size; i++ {
tournament.individuals[i] = pop.individuals[rand.Intn(len(pop.individuals))]
}
return tournament.fittest()
}
func (pop *population) fittest() *individual {
fittest := pop.individuals[0]
for _, ind := range pop.individuals[1:] {
if fittest.fitness() < ind.fitness() {
fittest = ind
}
}
return fittest
}
func (pop *population) fitness(ind *individual) int {
if len(ind.genes) != len(pop.target) {
return 0
}
fitness := 0
for i := 0; i < len(pop.target); i++ {
if pop.target[i] == ind.genes[i] {
fitness++
}
}
return fitness
}
type individual struct {
genes []byte
fitnessFunc func(*individual) int
}
func newIndividual(size int, fitness func(*individual) int) *individual {
ind := &individual{
genes: make([]byte, size),
fitnessFunc: fitness,
}
for i := 0; i < size; i++ {
ind.genes[i] = byte(rand.Intn(2))
}
return ind
}
func crossover(a, b *individual, uniformRate float64) *individual {
if len(a.genes) != len(b.genes) {
return nil
}
c := newIndividual(len(a.genes), a.fitnessFunc)
for i := 0; i < len(a.genes); i++ {
if rand.Float64() <= uniformRate {
c.genes[i] = a.genes[i]
} else {
c.genes[i] = b.genes[i]
}
}
return c
}
func (ind *individual) fitness() int {
return ind.fitnessFunc(ind)
}
func (ind *individual) mutate(mutationRate float64) {
for i := 0; i < len(ind.genes); i++ {
if rand.Float64() <= mutationRate {
ind.genes[i] = byte(rand.Intn(2))
}
}
}
func (ind *individual) String() string {
s := []byte{}
for i := 0; i < len(ind.genes); i++ {
if ind.genes[i] == 0 {
s = append(s, []byte("0")...)
} else {
s = append(s, []byte("1")...)
}
}
return string(s)
}