-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathanneal_test.go
69 lines (62 loc) · 1.57 KB
/
anneal_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
59
60
61
62
63
64
65
66
67
68
69
package hego
import (
"math"
"math/rand"
"testing"
)
type state float64
func (b state) Energy() float64 {
return float64(b * b)
}
func (b state) Neighbor() AnnealingState {
return b + state(rand.NormFloat64()*0.1)
}
func TestVerify(t *testing.T) {
settings := SASettings{}
settings.Temperature = 100.0
settings.AnnealingFactor = 0.9
err := settings.Verify()
if err != nil {
t.Error("verification should pass for temperature above 0 and annealing factor in (0,1]")
}
settings.Temperature = 0.0
err = settings.Verify()
if err == nil {
t.Error("verification should fail for temperature = 0.0")
}
settings.Temperature = 100.0
settings.AnnealingFactor = 1.1
err = settings.Verify()
if err == nil {
t.Error("verification should fail for annealing factor above 1")
}
settings.AnnealingFactor = 0.0
err = settings.Verify()
if err == nil {
t.Error("verification should fail for annealing factor <= 0")
}
}
// TestAnnealBit runs the AnnealBit method and checks for errors
func TestSA(t *testing.T) {
initialState := state(20.0)
settings := SASettings{}
res, err := SA(initialState, settings)
if err == nil {
t.Error("SA should fail with invalid settings")
}
settings.Temperature = 50.0
settings.AnnealingFactor = 0.99
settings.MaxIterations = 1000
settings.Verbose = 1
settings.KeepHistory = true
res, err = SA(initialState, settings)
if err != nil {
t.Errorf("Error while running Anneal main algorithm: %v", err)
}
if len(res.Energies) == 0 {
t.Error("Energies list should not be empty")
}
if math.Abs(res.Energy) > 0.5 {
t.Error("unexpected solution")
}
}