-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
85 lines (78 loc) · 2 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
package main
import (
"context"
"fmt"
"math/rand"
"github.com/soypat/mu8"
"github.com/soypat/mu8/genes"
"github.com/soypat/mu8/genetic"
)
const (
// [m/s]
gravity = 9.8
)
// Having constant length arrays lets us more dynamically program other aspects
// of this Genome. The process of cloning also becomes dead simple.
const Nstages = 2
var baseRocket = &rocket{
payloadMass: 10,
CD: 0.4,
stages: [Nstages]stage{
{
isp: 300,
massStruc: 200,
// The way this works is the first value is the starting value,
// other two are the permissible range the value may take during optimization.
massProp: genes.NewConstrainedFloat(0, 0, 3000),
deltaMass: genes.NewConstrainedFloat(0, 0, 100),
coastTime: genes.NewConstrainedFloat(0, 0, 300),
},
{
isp: 300,
massStruc: 20,
massProp: genes.NewConstrainedFloat(0, 0, 100),
deltaMass: genes.NewConstrainedFloat(0, 0, 3),
coastTime: genes.NewConstrainedFloat(0, 0, 300),
},
},
}
func main() {
const (
// How many iterations to print, spaced evenly between generations
// Do not set to less than 2.
Nprints = 10
// Number of generations to simulate.
Ngen = 4000
// Number of individuals in populations
Nindividuals = 8
// Polygamy (how many partners a rocket has)
polygamy = 2
// Mutation rate
mutrate = 0.1
)
type genoma = *rocket
src := rand.NewSource(2)
individuals := make([]*rocket, Nindividuals)
for i := range individuals {
clone := baseRocket.Clone()
mu8.Mutate(clone, src, 0.3)
individuals[i] = clone
}
pop := genetic.NewPopulation(individuals, src, func() *rocket { return baseRocket.Clone() })
for i := 0; i < Ngen; i++ {
err := pop.Advance(context.Background())
if err != nil {
panic(err)
}
err = pop.Selection(mutrate, polygamy)
if err != nil {
panic(err)
}
if i%(Ngen/(Nprints-1)) == 0 || i == Ngen-1 {
bestFitness := pop.ChampionFitness()
fmt.Printf("champHeight:%.3fkm\n", bestFitness)
}
}
best := pop.Champion()
fmt.Println("our champion:", best)
}