-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitness.go
47 lines (41 loc) · 1.19 KB
/
fitness.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
package ga
type FitnessFunction func(gene Genome) int
var DefaultFitnessFunc FitnessFunction = func(gene Genome) int {
count := 0
for _, i := range gene.Sequence {
if i == "1" {
count++
}
}
return count
}
// SetFitnessFunc changes the fitness function to the function specified
func (genA *GeneticAlgorithm) SetFitnessFunc(f FitnessFunction) {
genA.Fitness = f
}
// AverageFitness returns the average fitness of a [] Genome candidatePool
func (genA *GeneticAlgorithm) AverageFitness(candidatePool Population) int {
var average int = 0
for _, i := range candidatePool {
average += genA.Fitness(i)
}
return average / int(len(candidatePool))
}
// MaxFitness returns the highest fitness found in a [] Genome candidatePool
func (genA *GeneticAlgorithm) MaxFitnessCandidate(candidatePool Population) Genome {
var (
max int = 0
maxGene Genome
)
for _, i := range candidatePool {
if genA.Fitness(i) > max {
max = genA.Fitness(i)
maxGene = i
}
}
return maxGene
}
// MaxFitness returns the highest fitness found in a [] Genome candidatePool
func (genA *GeneticAlgorithm) MaxFitness(candidatePool Population) int {
return genA.Fitness(genA.MaxFitnessCandidate(candidatePool))
}