-
Notifications
You must be signed in to change notification settings - Fork 10
/
arbitrer.go
62 lines (51 loc) · 1.86 KB
/
arbitrer.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
package evoli
// ErrArbitration - must provide one or more individuals
var ErrArbitration = "ErrArbitration - must provide one or more individuals"
// Arbitrer - elect the winner between multiple participants
type Arbitrer interface {
Abritrate(participants ...Individual) (winner Individual, loosers []Individual)
}
func checkArbitrersParams(participants []Individual) {
if len(participants) < 1 {
panic(ErrArbitration)
}
}
type selecterBasedArbitrer struct {
Selecter
}
func (a selecterBasedArbitrer) Abritrate(participants ...Individual) (winner Individual, loosers []Individual) {
checkArbitrersParams(participants)
pop := NewPopulation(len(participants))
pop.Add(participants...)
selected, deads, _ := a.Select(pop, 1)
var deadsSlice []Individual
if deads != nil {
deadsSlice = deads.Slice()
}
defer selected.Close()
return selected.Get(0), deadsSlice
}
// NewProportionalToFitnessArbitrer - based on fitness value
func NewProportionalToFitnessArbitrer() Arbitrer {
return selecterBasedArbitrer{NewProportionalToFitnessSelecter()}
}
// NewProportionalToRankArbitrer - based on rank
func NewProportionalToRankArbitrer() Arbitrer {
return selecterBasedArbitrer{NewProportionalToRankSelecter()}
}
// NewStochasticUniversalSamplingArbitrer - based on fitness value
func NewStochasticUniversalSamplingArbitrer() Arbitrer {
return selecterBasedArbitrer{NewStochasticUniversalSamplingSelecter()}
}
// NewTournamentArbitrer - High Fitness increase chances to come out vcitorious from a duel
func NewTournamentArbitrer(p float64) Arbitrer {
return selecterBasedArbitrer{NewTournamentSelecter(p)}
}
// NewTruncationArbitrer - take the highest fitness
func NewTruncationArbitrer() Arbitrer {
return selecterBasedArbitrer{NewTruncationSelecter()}
}
// NewRandomArbitrer - choose randomly
func NewRandomArbitrer() Arbitrer {
return selecterBasedArbitrer{NewRandomSelecter()}
}