-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathant.go
275 lines (254 loc) · 7.2 KB
/
ant.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
package fsp
import (
"fmt"
"math"
"math/rand"
//"os"
"time"
//"sort"
//"github.com/pkg/profile"
)
// Freaky engine finding pseudo-ant paths
type AntEngine struct {
graph Graph
seed int
}
var feromones []float32
type ant struct {
day Day
city City
total Money
visited []City
fis []FlightIndex
}
const EVAPORATE_P = 0.7 // percent to evaporate
const FEROM_C = 0.7
const PRICE_C = 2.0
const FEROMONE_WEIGHT = 0.9
var ANTS = 0
var ants []ant
var antSteps = 0
var antCurrentBest = Money(math.MaxInt32)
func (e AntEngine) Name() string {
return fmt.Sprintf("%s(%d)", "AntEngine", e.seed)
}
func (e AntEngine) Solve(comm comm, p Problem) {
//defer profile.Start(/*profile.MemProfile*/).Stop()
//fmt.Fprintf(os.Stderr, "") // TODO anti error, remove
if p.n < 200 {
rand.Seed(int64(e.seed) + time.Now().UTC().UnixNano())
feromones = make([]float32, len(p.flights))
antInit(p.n/2, p.n)
antSolver(p, e.graph, comm)
}
//comm.done()
}
func antInit(ant_n, problem_n int) {
ANTS = ant_n
ants = make([]ant, ant_n, ant_n)
for ai := range ants {
ants[ai].visited = make([]City, 0, problem_n)
ants[ai].fis = make([]FlightIndex, 0, problem_n)
}
}
func antSolver(problem Problem, graph Graph, comm comm) {
//solution := make([]Flight, 0, graph.size)
var maxTotal Money
antsFinished := 0
for {
maxTotal = 0
for ai := range ants {
for {
//printInfo("The chosen one", ai, ants[ai])
//printInfo("Ant:", ai)
fi, r := antFlight(problem, graph, ants[ai].visited, ants[ai].day, ants[ai].city)
antSteps++
if !r {
//printInfo("ant to die", ai, ants[ai].visited, "day", ants[ai].day, "city", ants[ai].city)
die(ai)
continue
}
//printInfo("FI:", fi)
flight := problem.flights[fi]
ants[ai].total += flight.Cost
ants[ai].day++
ants[ai].city = flight.To
if ants[ai].city == 0 { // ant has completed the route
if ants[ai].total > maxTotal {
maxTotal = ants[ai].total
}
break
} else {
ants[ai].visited = append(ants[ai].visited, ants[ai].city)
ants[ai].fis = append(ants[ai].fis, fi)
}
}
}
for ai := range ants { // ants finished
ants[ai].day = 0
evaporate(EVAPORATE_P)
// place the feromones
for _, fi := range ants[ai].fis {
feromones[fi] += float32(maxTotal) / float32(ants[ai].total)
}
ants[ai].total = 0
ants[ai].visited = ants[ai].visited[:0]
ants[ai].fis = ants[ai].fis[:0]
antsFinished++
}
if antsFinished > 100000/problem.n {
//printInfo("ants finished")
antsFinished = 0
//printInfo("Feromones:", feromones)
followAnts(problem, graph, comm)
//printInfo("antSteps:", antSteps)
}
}
}
func evaporate(x float32) {
mf := float32(0.0)
remain := 1.0 - x
for fi := range feromones {
feromones[fi] *= remain
if feromones[fi] > mf {
mf = feromones[fi]
}
}
//printInfo("Max feromone:", mf)
}
func followAnts(problem Problem, graph Graph, comm comm) {
solution := make([]Flight, 0, graph.size)
var price Money
var city City
for {
solution = solution[:0]
visited := make([]City, 0, MAX_CITIES)
city = City(0)
price = Money(0)
for d := 0; d < graph.size; d++ {
//printInfo("FA:")
fi, r := antFlight(problem, graph, visited, Day(d), city)
if !r {
return
}
price += problem.flights[fi].Cost
if price >= randomCurrentBest {
break
}
city = problem.flights[fi].To
visited = append(visited, city)
solution = append(solution, problem.flights[fi])
}
if len(solution) == graph.size && price < antCurrentBest {
antCurrentBest = price
comm.sendSolution(NewSolution(solution))
//printInfo("ant solution sent, price", price)
/*
printInfo("Stats:")
dg := make([]struct{maxF float32; flights,f25 int}, problem.n)
for _, dtfi := range graph.antsGraph {
for d, tfi := range dtfi {
for _, fi := range tfi {
if dg[int(d)].maxF < feromones[fi] {
dg[int(d)].maxF = feromones[fi]
}
dg[int(d)].flights += 1
}
}
}
for _, dtfi := range graph.antsGraph {
for d, tfi := range dtfi {
for _, fi := range tfi {
if dg[int(d)].maxF/4.0 < feromones[fi] {
dg[int(d)].f25 += 1
}
}
}
}
for d:=0; d<problem.n; d++ {
x := dg[int(d)]
printInfo("day", d, "max", x.maxF, "flights", x.flights, "flights>25%", x.f25)
}
*/
return
}
}
}
func die(ai int) {
//printInfo("ant", ai, "dying")
ants[ai].day = 0
ants[ai].city = 0
ants[ai].visited = ants[ai].visited[:0]
ants[ai].fis = ants[ai].fis[:0]
// keep current total cost for now; maybe add maximum flight cost or assign current worst running ant total
}
// ants don't fly
func antWeight(problem Problem, fi FlightIndex, flights int, avgCost float64, avgFeromones float32) float32 {
// feromones influence
price := problem.flights[fi].Cost
rel_price := avgCost / float64(price) // 1.0 for average, 2.0 for 2x better than average
//printInfo("xxx", avgFeromones, flights, feromones[fi], ANTS)
rel_feromones := 1.0
if avgFeromones > 0.0 {
rel_feromones = float64(avgFeromones) * (1.0 - FEROMONE_WEIGHT)
rel_feromones += float64(feromones[fi]/avgFeromones) * FEROMONE_WEIGHT
}
//fmt.Fprintf(os.Stderr, "rf avg %.2f cur %.2f res %.2f %v\n", avgFeromones, feromones[fi], rel_feromones, flights)
f := math.Pow(rel_feromones, FEROM_C)
// price influence
p := math.Pow(rel_price, PRICE_C)
var result float32 = float32(f * p)
//fmt.Fprintf(os.Stderr, "f/p: %.4f * %.2f = %.4f, (feromones %.2f/%.2f, cost %v, fi %v)\n", f, p, result, feromones[fi], rel_feromones, price, fi)
return result
}
// choose the flight ant will take
func antFlight(problem Problem, graph Graph, visited []City, day Day, city City) (FlightIndex, bool) {
// first, find all possible flights and construct random distribution
possible_flights := make([]FlightIndex, 0, MAX_CITIES)
var maxCost Money = 0 // needed to normalize costs
var sumCost Money = 0
var sumFeromones float32 = 0.0
//var mw float32 = 0.0
for _, fi := range graph.antsGraph[city][day] {
if contains(visited, problem.flights[fi].To) {
continue
}
possible_flights = append(possible_flights, fi)
cost := problem.flights[fi].Cost
if cost > maxCost {
maxCost = cost
}
sumCost += cost
sumFeromones += feromones[fi]
}
//printInfo("mw:", mw)
flightCnt := len(possible_flights)
var avgCost = float64(sumCost) / float64(flightCnt)
var avgFeromones = sumFeromones / float32(flightCnt)
// second, return that ant is stuck if no flight possible
if flightCnt == 0 {
return 0, false
}
// third, compute weights, we do in in extra cycle beacause of normalization
var fsum float32 = 0.0
thres := make([]float32, 0, MAX_CITIES+1) // array of thresholds
thres = append(thres, 0.0) // easier logic later if we always start with 0.0
for _, fi := range possible_flights {
// compute weight of the flight
// TODO scale according to average flight price
w := antWeight(problem, fi, flightCnt, avgCost, avgFeromones)
//if w > mw { mw = w }
fsum += w
thres = append(thres, fsum)
}
// fourth, choose flight randomly based on the distribution
r := rand.Float32() * fsum
result := flightCnt - 1
for i, f := range thres {
if r < f {
result = i - 1
break
}
}
return possible_flights[result], true
}