-
Notifications
You must be signed in to change notification settings - Fork 0
/
aco_test.go
393 lines (350 loc) · 8.92 KB
/
aco_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Tests for the Ant System Algorithm described in Dorigo et al. 96
package aco
import (
"fmt"
"io/ioutil"
"math"
"os"
"testing"
"time"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func TestEqualTour(t *testing.T) {
vp := func(ind int) *Vertex {
v := new(Vertex)
v.Index = ind
return v
}
t.Run("SameTour", func(t *testing.T) {
a := Tour{vp(0), vp(1), vp(2)}
if !EqualTour(a, a) {
t.Fail()
}
})
t.Run("SameTourDifferentObject", func(t *testing.T) {
a := Tour{vp(0), vp(1), vp(2)}
b := Tour{a[0], a[1], a[2]}
if !EqualTour(a, b) {
t.Fail()
}
})
t.Run("SameTourDifferentOrder", func(t *testing.T) {
a := Tour{vp(0), vp(1), vp(2)}
b := Tour{a[1], a[2], a[0]}
if !EqualTour(a, b) {
t.Fail()
}
})
t.Run("DiffTourSameLen", func(t *testing.T) {
a := Tour{vp(0), vp(1), vp(2)}
b := Tour{a[1], vp(3), a[0]}
if EqualTour(a, b) {
t.Fail()
}
})
t.Run("DiffTourDiffLen", func(t *testing.T) {
a := Tour{vp(0), vp(1), vp(2), vp(4)}
b := Tour{a[1], vp(3), a[0]}
if EqualTour(a, b) {
t.Fail()
}
})
// Two Tours are equal if their order is in reverse, that is because our problem Graph is undirected.
t.Run("SameTourReversed", func(t *testing.T) {
a := Tour{vp(0), vp(1), vp(2), vp(3)}
b := Tour{a[3], a[2], a[1], a[0]}
if !EqualTour(a, b) {
t.Fail()
}
})
t.Run("SameTourReversedDiffOrder", func(t *testing.T) {
a := Tour{vp(0), vp(1), vp(2), vp(3), vp(4), vp(5)}
b := Tour{a[5], a[4], a[3], a[2], a[1], a[0]}
if !EqualTour(a, b) {
t.Fail()
}
})
}
func TestCloneGraph(t *testing.T) {
// create triangle graph
g := Graph{
Vertices: []Vertex{
{0, "0"},
{1, "1"},
{2, "2"},
},
Edges: [][]Edge{
{},
{{1, 1, 0}},
{{1, 1, 0}, {1, 1, 0}},
},
}
clonedG := cloneGraph(g)
for i := 0; i < len(g.Vertices); i++ {
if g.Vertices[i] != clonedG.Vertices[i] {
t.Fail()
}
for j := 0; j < len(g.Edges[i]); j++ {
if g.Edges[i][j] != clonedG.Edges[i][j] {
t.Fail()
}
}
}
}
// TODO
/*
func TestMoveToNextVertex(t *testing.T) {
t.Error("TODO implement")
}
*/
// TODO find a way to test replicability
func TestCheckFullyConnected(t *testing.T) {
t.Run("OneVertex", func(t *testing.T) {
// create triangle graph
oneVertexG := Graph{
Vertices: []Vertex{
{1, "1"},
},
Edges: [][]Edge{
{},
},
}
err := CheckFullyConnected(oneVertexG)
if err != nil {
t.Fatal(err)
}
})
t.Run("TwoVerticesNoEdge", func(t *testing.T) {
// create triangle graph
twoVertG := Graph{
Vertices: []Vertex{
{0, "0"},
{1, "1"},
},
Edges: [][]Edge{
{},
{},
},
}
err := CheckFullyConnected(twoVertG)
if err == nil {
t.Fail()
}
})
t.Run("TwoVerticesOneEdge", func(t *testing.T) {
// create triangle graph
twoVertG := Graph{
Vertices: []Vertex{
{0, "0"},
{1, "1"},
},
Edges: [][]Edge{
{},
{{1, 1, 0}},
},
}
err := CheckFullyConnected(twoVertG)
if err != nil {
t.Fail()
}
})
t.Run("ThreeVerticesTwoEdges", func(t *testing.T) {
// create triangle graph
threeVertG := Graph{
Vertices: []Vertex{
{0, "0"},
{1, "1"},
{2, "2"},
},
Edges: [][]Edge{
{},
{{1, 1, 0}},
{{1, 1, 0}},
},
}
err := CheckFullyConnected(threeVertG)
if err == nil {
t.Fail()
}
})
}
// TestTriangle tests the AS on a triangle graph. The triangle is the most trivial TSP and all ants will find the exact same tour, therefore the AS must terminate due to stagnation behaviour after the first cycle.
func TestTriangle(t *testing.T) {
// create triangle graph
triangleGraph := Graph{
Vertices: []Vertex{
{0, "0"},
{1, "1"},
{2, "2"},
},
Edges: [][]Edge{
{},
{{1, 1, 0}},
{{1, 1, 0}, {1, 1, 0}},
},
}
// TODO determine parameters
var NCmax int = 1 // check whether AS terminated with stagnation behaviour after exactly 1 cycle
var Q float64 = 1
var rho float64 = 0.5
var alpha float64 = 1
var beta float64 = 1
var seed int64 = 0
solution, stagnationBehaviour, err := AntSystemAlgorithm(
triangleGraph,
len(triangleGraph.Vertices),
NCmax,
Q,
rho,
alpha, beta,
LayTrailAntCycle,
seed,
os.Stdout,
)
if err != nil {
t.Error(err)
}
err = CheckSolutionValid(solution, triangleGraph)
if err != nil {
t.Fatal(err)
}
if !stagnationBehaviour {
t.Error("AntSystemAlgorithm should have terminated with stagnationBehaviour == true")
}
}
// TestSquare tests the AS on a square graph. The graph consists of four vertices arranged as a square and all of them are connected to eachother. There are exactly three possible tours for this TSP(without visiting any Vertex twice). Two tours involve both diagonals and one involves all boundaries of the square. The boundaries are the optimal solution, since they are shorter than the diagonals. The test expects that AS returns the square boundaries solution.
// Be aware that this test may fail, if all ants come up with the same non-border solution in a cycle. That would cause stagnation behaviour. Use a large number of ants to avoid make this event unlikely.
// TODO Convert this to a benchmark?
func TestSquare(t *testing.T) {
// The diagonals of a unit square have length Sqrt(2)
sqrt2 := math.Sqrt(2)
invSqrt2 := 1 / sqrt2
// create square graph
squareGraph := Graph{
Vertices: []Vertex{
// It would have been more intuitive if the square vertices were label as this:
// 0 1
// 2 3
// instead of
// 0 1
// 3 2
{0, "0"},
{1, "1"},
{2, "3"},
{3, "2"},
},
Edges: [][]Edge{
{},
{{1, 1, 0}},
{{sqrt2, invSqrt2, 0}, {1, 1, 0}},
{{1, 1, 0}, {sqrt2, invSqrt2, 0}, {1, 1, 0}},
},
}
fmt.Println(squareGraph)
var seed int64 = 0
// run AS
solution, _, err := ASBestParams(
squareGraph,
seed,
os.Stdout,
)
for i := 0; i < len(solution); i++ {
fmt.Println(*solution[i])
}
if err != nil {
t.Error(err)
}
err = CheckSolutionValid(solution, squareGraph)
if err != nil {
t.Fatal(err)
}
// check that AS returns the square boundaries solution.
// check that the solution Edges are in the right order.
// check that the solution Vertices are never visited twice.
sv := squareGraph.Vertices
expectedSol := Tour{&sv[0], &sv[1], &sv[2], &sv[3]}
if !EqualTour(solution, expectedSol) {
t.Errorf("want square border edges in solution:\n%v\ngot:\n%v\n", solution, expectedSol)
}
solTotLen := CompTotLength(squareGraph, solution)
if solTotLen > 4.5 {
t.Errorf("want total length ~= 4.0\ngot total length == %f\n", solTotLen)
}
}
// This function generates a Graph representing a equidistant grid of nxn
func generateGridGraph(nGridNodesPerDim int, dist float64) Graph {
nGridNodes := nGridNodesPerDim * nGridNodesPerDim
g := Graph{
Vertices: make([]Vertex, nGridNodes),
Edges: make([][]Edge, nGridNodes),
}
for vx := 0; vx < nGridNodesPerDim; vx++ {
for vy := 0; vy < nGridNodesPerDim; vy++ {
v := vx*nGridNodesPerDim + vy
g.Vertices[v].Index = v
g.Vertices[v].Label = fmt.Sprintf("%d", v)
g.Edges[v] = make([]Edge, v)
for e := 0; e < v; e++ {
aX := float64(vx) * dist
aY := float64(vy) * dist
bX := float64(g.Vertices[e].Index/nGridNodesPerDim) * dist
bY := float64(g.Vertices[e].Index%nGridNodesPerDim) * dist
g.Edges[v][e].Length = CompEuclid2dDist(aX, aY, bX, bY)
}
}
}
return g
}
// TestCompTotLength checks whether the function CompTotLength returns the rigth length
func TestCompTotLength(t *testing.T) {
nGridNodesPerDim := 4
var dist float64 = 10
// For a graph that is an equidistant grid of n x n fully connected vertices with distance d between
// neighbouring vertices, where n is even, an optimal solution has length: d * n * n.
g := generateGridGraph(nGridNodesPerDim, dist)
gv := func(i int) *Vertex {
return &g.Vertices[i]
}
optSol := Tour{
gv(0), gv(4), gv(8), gv(12),
gv(13), gv(9), gv(10), gv(14),
gv(15), gv(11), gv(7), gv(3),
gv(2), gv(6), gv(5), gv(1),
}
optSolLenInt := int(CompTotLength(g, optSol))
if optSolLenInt != 160 {
t.Errorf("want: %d\ngot: %d\n", 160, optSolLenInt)
}
}
func BenchmarkGrid(b *testing.B) {
// func TestGrid(b *testing.T) {
// TODO generalize to parameter
nGridNodesPerDim := 8
// For a graph that is an equidistant grid of n x n fully connected vertices with distance d between
// neighbouring vertices, where n is even, an optimal solution has length: d * n * n.
var dist float64 = 1
optLen := dist * float64(nGridNodesPerDim*nGridNodesPerDim)
// generate grid
g := generateGridGraph(nGridNodesPerDim, dist)
seed := time.Now().UTC().UnixNano()
var nAnts int = len(g.Vertices)
var NCmax int = 10000
var Q float64 = 100
var rho float64 = 0.5
var alpha float64 = 1
var beta float64 = 100
trailUpdateFunc := LayTrailAntCycle
solution, _, err := AntSystemAlgorithm(g, nAnts, NCmax, Q, rho, alpha, beta, trailUpdateFunc, seed, ioutil.Discard)
check(err)
solLen := CompTotLength(g, solution)
b.Logf("BestSol %f\n", solLen)
b.Logf("OptSol %f\n", optLen)
epsilon := 0.0000001
if solLen < optLen-epsilon || solLen > optLen+epsilon {
b.Fail()
}
}