-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection.go
90 lines (71 loc) · 2.21 KB
/
selection.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
package generateplan
import "math/rand"
type Selection int
const (
FitnessProportial Selection = iota
StochasticUniversal
RankedBased
)
// FitnessProportial
func (algorithm *Algorithm) fitness_proportional_selection() []*Plan {
selection := []*Plan{}
for i := 0; i < algorithm.config.Population; i++ {
selection = append(selection, algorithm.selectPlanFitnessBased(rand.Float64()))
}
return selection
}
func (algorithm *Algorithm) selectPlanFitnessBased(selector float64) *Plan {
var aPropability float64 = 0
for _, plan := range algorithm.generation.plans {
aPropability += plan.fitness / algorithm.generation.fitness
if selector <= aPropability {
return plan
}
}
return nil
}
// StochasticUniversal
func (algorithm *Algorithm) stochastic_universal_selection() []*Plan {
selection := []*Plan{}
selector := rand.Float64()
for i := 0; i < algorithm.config.Population; i++ {
selection = append(selection, algorithm.selectPlanFitnessBased(selector))
selector += (1 / float64(algorithm.config.Population))
if selector >= 1 {
selector -= 1
}
}
return selection
}
// RankedBased
func (algorithm *Algorithm) ranked_based_selection() []*Plan {
rankedGeneration := algorithm.generation.sortPlans()
selection := []*Plan{}
for i := 0; i < algorithm.config.Population; i++ {
selection = append(selection, algorithm.selectPlanRankBased(rand.Float64(), rankedGeneration))
}
return selection
}
func (generation *Generation) sortPlans() *Generation {
newGen := newGeneration()
for _, plan := range generation.plans {
newGen.plans = append(newGen.plans, plan)
for i := len(newGen.plans) - 1; i > 0; i-- {
if newGen.plans[i].fitness > newGen.plans[i-1].fitness {
newGen.plans[i-1], newGen.plans[i] = newGen.plans[i], newGen.plans[i-1]
}
}
}
return newGen
}
func (algorithm *Algorithm) selectPlanRankBased(selector float64, rankedGeneration *Generation) *Plan {
var aPropability float64 = 0
for i, plan := range rankedGeneration.plans {
aPropability += 1 / float64(algorithm.config.Population) * (algorithm.config.SelectivePressure -
(2*algorithm.config.SelectivePressure-2)*(float64(i)/float64(algorithm.config.Population-1)))
if selector <= aPropability {
return plan
}
}
return nil
}