-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottleneck.go
163 lines (145 loc) · 3.13 KB
/
bottleneck.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
package fsp
import (
"math"
"sort"
"time"
)
type Bottleneck struct {
graph Graph
currentBest Money
}
func NewBottleneck(g Graph) Bottleneck {
return Bottleneck{
graph,
Money(math.MaxInt32),
}
}
func (d Bottleneck) Name() string {
return "Bottleneck"
}
type byCost2 []Flight
func (f byCost2) Len() int {
return len(f)
}
func (f byCost2) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
func (f byCost2) Less(i, j int) bool {
return f[i].Cost < f[j].Cost
}
func (d Bottleneck) Solve(comm comm, problem Problem) {
flights := make([]*Flight, 0, problem.n)
visited := make(map[City]bool)
partial := partial{visited, flights, problem.n, 0}
btn := d.findBottlenecks(problem)
t := 30000.0 / float32(len(btn))
printInfo("Found", len(btn), "bottlenecks")
for _, b := range btn {
timePerBtn := t / float32(min(len(btn), 3))
printInfo("Testing", min(len(btn), 3), "flights in bottleneck")
sort.Sort(byCost2(b))
for _, f := range b {
partial.fly(&f)
tb := time.Duration(timePerBtn) * time.Millisecond
printInfo("running dfs from bottleneck for", tb)
d.dfs(comm, &partial, time.After(tb))
partial.backtrack()
}
}
}
type byCount [][]Flight
func (f byCount) Len() int {
return len(f)
}
func (f byCount) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
func (f byCount) Less(i, j int) bool {
return len(f[i]) < len(f[j])
}
type btnStat struct {
from [][]Flight
to [][]Flight
noBFrom []bool
noBTo []bool
cutoff int
}
func initB(n int) btnStat {
b := btnStat{}
b.from = make([][]Flight, n)
b.to = make([][]Flight, n)
b.cutoff = n / 4
for i := range b.from {
b.from[i] = make([]Flight, 0, b.cutoff)
b.to[i] = make([]Flight, 0, b.cutoff)
}
b.noBFrom = make([]bool, n)
b.noBTo = make([]bool, n)
return b
}
func (b *btnStat) add(f Flight) {
if !b.noBFrom[f.From] {
b.from[f.From] = append(b.from[f.From], f)
if len(b.from[f.From]) > b.cutoff {
b.from[f.From] = nil
b.noBFrom[f.From] = true
}
}
if !b.noBTo[f.To] {
b.to[f.To] = append(b.to[f.To], f)
if len(b.to[f.To]) > b.cutoff {
b.to[f.To] = nil
b.noBTo[f.To] = true
}
}
}
func (b btnStat) get() [][]Flight {
all := make([][]Flight, 0, len(b.from)+len(b.to))
for _, f := range b.from {
if f != nil && len(f) > 0 {
all = append(all, f)
}
}
for _, f := range b.to {
if f != nil && len(f) > 0 {
all = append(all, f)
}
}
sort.Sort(byCount(all))
return all
}
func (b *Bottleneck) findBottlenecks(p Problem) [][]Flight {
bs := initB(p.n)
for _, f := range p.flights {
if f.From == 0 || f.To == 0 {
continue
}
bs.add(f)
}
return bs.get()
}
func (b *Bottleneck) dfs(comm comm, partial *partial, timeout <-chan time.Time) bool {
if expired(timeout) {
return true
}
if partial.cost > b.currentBest {
return false
}
if partial.roundtrip() {
b.currentBest = comm.sendSolution(NewSolution(partial.solution()))
}
lf := partial.lastFlight()
if partial.hasVisited(lf.To) {
return false
}
dst := b.graph.fromDaySortedCost[lf.To][int(lf.Day+1)%b.graph.size]
for _, f := range dst {
partial.fly(f)
expired := b.dfs(comm, partial, timeout)
if expired {
return true
}
partial.backtrack()
}
return false
}