-
Notifications
You must be signed in to change notification settings - Fork 1
/
knapsack_benchmark_test.go
42 lines (36 loc) · 1.11 KB
/
knapsack_benchmark_test.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
package genetic_test
import (
"testing"
"github.com/kklash/genetic"
)
func benchEvolveOnce(b *testing.B, selection genetic.SelectionFunc[*KnapsackSolution]) {
problem := knapsackSolutionFixtures[7].Problem // hardest problem
populationSize := 60
elitism := 2
population := genetic.NewPopulation(
populationSize,
problem.RandomSolution,
solutionCrossover,
genetic.StaticFitnessFunc(solutionFitness),
selection,
solutionMutation(0.02),
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
population.EvolveOnce(elitism)
}
}
func BenchmarkPopulation_EvolveOnce(b *testing.B) {
b.Run("TournamentSelection/poolsize=2", func(b *testing.B) {
benchEvolveOnce(b, genetic.TournamentSelection[*KnapsackSolution](2))
})
b.Run("TournamentSelection/poolsize=5", func(b *testing.B) {
benchEvolveOnce(b, genetic.TournamentSelection[*KnapsackSolution](5))
})
b.Run("TournamentSelection/poolsize=10", func(b *testing.B) {
benchEvolveOnce(b, genetic.TournamentSelection[*KnapsackSolution](10))
})
b.Run("RouletteSelection", func(b *testing.B) {
benchEvolveOnce(b, genetic.RouletteSelection[*KnapsackSolution])
})
}