-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfant.go
71 lines (62 loc) · 1.56 KB
/
fant.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
// Copyright 2012 - 2014 The Seriation Authors. All rights reserved. See the LICENSE file.
package ser
// Fast Ant System. Float64 version.
// E. D. Taillard 1998. "FANT: Fast ant system. Technical report IDSIA-46-98, IDSIA, Lugano.
import (
"math/rand"
)
// initTraceF64 does the (re-) initialization of the trace.
func initTraceF64(inc float64, trace Matrix64) {
n := trace.Rows()
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
trace[i][j] = inc
}
}
}
// updateTraceF64 updates the feromone trace.
func updateTraceF64(p, best_p IntVector, inc *float64, r float64, trace Matrix64) {
var i int
n := p.Len()
for i = 0; i < n && p[i] == best_p[i]; i++ { // skip
}
if i == n {
(*inc)++
initTraceF64(*inc, trace)
} else {
for i = 0; i < n; i++ {
trace[i][p[i]] += *inc
trace[i][best_p[i]] += r
}
}
}
// genTraceF64 generates a solution with probability of setting p[i] == j proportionnal to trace[i][j].
func genTraceF64(p IntVector, trace Matrix64) {
var target, sum float64
n := p.Len()
nexti := NewIntVector(n)
nextj := NewIntVector(n)
sum_trace := NewVector64(n)
nexti.Perm()
nextj.Perm()
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
sum_trace[i] += trace[i][j]
}
}
for i := 0; i < n; i++ {
// target = unif(0, sum_trace[nexti[i]]-1)
target = rand.Float64()*sum_trace[nexti[i]] - 1
j := i
sum = trace[nexti[i]][nextj[j]]
for sum < target {
j++
sum += trace[nexti[i]][nextj[j]]
}
p[nexti[i]] = nextj[j]
for k := i; k < n; k++ {
sum_trace[nexti[k]] -= trace[nexti[k]][nextj[j]]
}
nextj.Swap(j, i)
}
}