-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain.go
46 lines (40 loc) · 978 Bytes
/
brain.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
package main
import (
"github.com/gen2brain/raylib-go/raylib"
"math"
"math/rand"
)
type Brain struct {
directions []rl.Vector2
step int
}
func newBrain() Brain {
// rand.Seed(time.Now().UnixNano())
var b Brain
b.directions = make([]rl.Vector2, brainSize)
b.step = 0
b.randomize()
return b
}
func (b Brain) clone() Brain {
var newDirections []rl.Vector2
newDirections = make([]rl.Vector2, brainSize)
copy(newDirections, b.directions)
return Brain{newDirections, 0}
}
func (b *Brain) randomize() {
for i := range b.directions {
randomAngle := rand.Float64() * 2 * math.Pi
b.directions[i] = rl.Vector2{float32(math.Cos(randomAngle)), float32(math.Sin(randomAngle))}
}
}
func (b *Brain) mutate() {
mutationRate := 0.01
for i := range b.directions {
r := rand.Float64()
if mutationRate > r {
randomAngle := rand.Float64() * 2 * math.Pi
b.directions[i] = rl.Vector2{float32(math.Cos(randomAngle)), float32(math.Sin(randomAngle))}
}
}
}