-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathant_colony_test.go
58 lines (53 loc) · 1.73 KB
/
ant_colony_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package hego
import "testing"
type ant []bool
func (a ant) Performance() float64 { return 1.0 }
func (a ant) DropPheromone(performance float64) {}
func (a ant) PerceivePheromone() []float64 { return []float64{1.0, 1.0} }
func (a ant) Evaporate(factor, min float64) {}
func (a ant) Step(next int) bool { return true }
func (a ant) Init() {}
func TestVerifyACOSettings(t *testing.T) {
settings := ACOSettings{}
err := settings.Verify()
if err == nil {
t.Error("verification should fail for invalid evaporation")
}
settings.Evaporation = 0.9
err = settings.Verify()
if err != nil {
t.Error("verification should pass for valid evaporation")
}
}
func TestACO(t *testing.T) {
settings := ACOSettings{}
pop := []Ant{
ant{true, true},
ant{true, true},
ant{true, true},
}
res, err := ACO(pop, settings)
if err == nil {
t.Error("ACO should fail when settings are invalid")
}
settings.Evaporation = 0.9
settings.MaxIterations = 10
settings.Verbose = 1
settings.KeepHistory = true
res, err = ACO(pop, settings)
if err != nil {
t.Errorf("ACO shoud not fail, got: %v", err)
}
if res.Iterations != settings.MaxIterations {
t.Errorf("result iterations unexpected, wanted %v got %v", settings.MaxIterations, res.Iterations)
}
if res.AveragePerformances[0] != pop[0].Performance() {
t.Error("all ants have the same performance, so average performance should be the same too")
}
if res.BestPerformances[0] != pop[0].Performance() {
t.Error("all ants have the same performance, so best performance should be the same too")
}
if res.BestAnts[res.Iterations-1].Performance() != pop[0].Performance() {
t.Error("best ant should have same performance as any other ant")
}
}