-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindividual.go
74 lines (63 loc) · 1.81 KB
/
individual.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
package evoli
import "sync"
// Individual refers to a potential solution to the problem we want to solve
type Individual interface {
Fitness() float64
SetFitness(float64)
Equal(Individual) bool
}
// individual https://en.wikipedia.org/wiki/individual
type individual struct {
fitness float64
best Individual
}
// NewIndividual is the constructor for individuals
func NewIndividual(fitness float64) Individual {
indiv := individual{
fitness,
nil,
}
return &indiv
}
// Fitness returns the strength of a individual regarding to its environement. Higher is stronger.
func (indiv *individual) Fitness() float64 {
return indiv.fitness
}
// SetFitness set the strength of a individual regarding to its environement. Higher is stronger.
func (indiv *individual) SetFitness(fitness float64) {
indiv.fitness = fitness
}
// Equal return true if indiv is equal to toBeCompared
func (indiv *individual) Equal(toBeCompared Individual) bool {
return indiv == toBeCompared
}
// individual https://en.wikipedia.org/wiki/individual
type individualSync struct {
individual
fitMut sync.RWMutex
bestMut sync.RWMutex
}
// NewIndividualSync is the constructor for threadsafe individuals
func NewIndividualSync(fitness float64) Individual {
indiv := individual{
fitness,
nil,
}
return &individualSync{
indiv,
sync.RWMutex{},
sync.RWMutex{},
}
}
// Fitness returns the strength of a individual regarding to its environement. Higher is stronger.
func (indiv *individualSync) Fitness() float64 {
indiv.fitMut.RLock()
defer indiv.fitMut.RUnlock()
return indiv.individual.Fitness()
}
// SetFitness set the strength of a individual regarding to its environement. Higher is stronger.
func (indiv *individualSync) SetFitness(Fitness float64) {
indiv.fitMut.Lock()
defer indiv.fitMut.Unlock()
indiv.individual.SetFitness(Fitness)
}