-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcfs.go
239 lines (218 loc) · 6.71 KB
/
dcfs.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
package fsp
import (
"fmt"
"math"
"os"
"sort"
"strconv"
//"github.com/pkg/profile"
)
// Depth + Cheapest First Search engine
// a variant of greedy DFS using cheapest next flight first with heuristics based on average price for same flights on different days
type Dcfs struct {
graph Graph
skip int
}
var DcfsResultsCounter uint32
var DcfsBranchCounter []uint32
var dcfsCurrentBest = Money(math.MaxInt32)
// engine parms
var pMaxBranches = MAX_CITIES
var pDiscountWeight = float32(0.6)
var pNextAvgWeight = float32(-0.2)
var pMinDiscount = float32(-0.5)
var pDiscountThreshold = Money(0) // set as avg flight price in Solve()
func (e Dcfs) Name() string {
return fmt.Sprintf("%s(%d)", "Dcfs", e.skip)
}
func dcfsLoadEnvParams() {
var env string
env = os.Getenv("DCFS_MAX_BRANCHES")
if len(env) > 0 {
pMaxBranches, _ = strconv.Atoi(env)
}
env = os.Getenv("DCFS_DISC_W")
if len(env) > 0 {
tmp, _ := strconv.ParseFloat(env, 32)
pDiscountWeight = float32(tmp)
}
env = os.Getenv("DCFS_NEXT_AVG_W")
if len(env) > 0 {
tmp, _ := strconv.ParseFloat(env, 32)
pNextAvgWeight = float32(tmp)
}
env = os.Getenv("DCFS_MIN_DISC")
if len(env) > 0 {
tmp, _ := strconv.ParseFloat(env, 32)
pMinDiscount = float32(tmp)
}
env = os.Getenv("DCFS_DISC_THRESH")
if len(env) > 0 {
tmp, _ := strconv.Atoi(env)
pDiscountThreshold = Money(tmp)
}
}
func (e Dcfs) Solve(comm comm, p Problem) {
//defer profile.Start(/*profile.MemProfile*/).Stop()
DcfsBranchCounter = make([]uint32, e.graph.size+1)
pDiscountThreshold = Money(p.FlightStats().AvgPrice)
if p.n > 20 {
pMaxBranches = 2
} else {
pMaxBranches = p.n / 2
}
if e.skip > 0 && p.n < 20 {
return
}
dcfsLoadEnvParams()
dcfsSolver(e.graph, p.stats, comm, e.skip)
//comm.done()
}
type EvaluatedFlight struct {
flight Flight
value float32
}
type byValue []EvaluatedFlight
func (f byValue) Len() int {
return len(f)
}
func (f byValue) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
func (f byValue) Less(i, j int) bool {
return f[i].value < f[j].value
}
func dcfsInsertSortedFlight(slice []EvaluatedFlight, node EvaluatedFlight) []EvaluatedFlight {
l := len(slice)
if l == 0 {
return []EvaluatedFlight{node}
}
i := sort.Search(l, func(i int) bool { return slice[i].value > node.value })
//fmt.Println(i)
if i == 0 {
return append([]EvaluatedFlight{node}, slice...)
}
if i == -1 {
return append(slice[0:l], node)
}
//tail := append([]EvaluatedFlight{node}, slice[i:]...)
return append(slice[0:i], append([]EvaluatedFlight{node}, slice[i:l]...)...)
}
func dcfsInsertVisited(slice []City, c City) []City {
l := len(slice)
if l == 0 {
return []City{c}
}
i := sort.Search(l, func(i int) bool { return slice[i] > c })
if i == 0 {
return append([]City{c}, slice...)
}
if i == -1 {
return append(slice[0:l], c)
}
//tail := append([]EvaluatedFlight{node}, slice[i:]...)
return append(slice[0:i], append([]City{c}, slice[i:l]...)...)
}
func dcfsVisited(slice []City, c City) bool {
l := len(slice)
if l < 1 {
return false
}
p := sort.Search(l, func(i int) bool { return slice[i] >= c })
if p < l && slice[p] == c {
return true
}
return false
}
func dcfsSolver(graph Graph, stats FlightStatistics, comm comm, skip int) /*[]Flight*/ {
printInfo("starting dcfs solver", skip)
visited := make([]City, 0, MAX_CITIES)
solution := make([]Flight, 0, graph.size)
home := City(0)
day := Day(0)
price := Money(0)
dcfsIterate(solution, day, home, visited, graph, stats, price, comm, skip)
}
func dcfsIterate(partial []Flight, day Day, current City,
visited []City, graph Graph, stats FlightStatistics, price Money, comm comm, skip int) {
DcfsBranchCounter[day] += 1
if price >= dcfsCurrentBest {
// we have already got worse than best result, give it up, bro
DcfsResultsCounter++
return
}
if int(day) == graph.size {
DcfsResultsCounter++
if price < dcfsCurrentBest {
//dcfsCurrentBest = price
dcfsCurrentBest = comm.sendSolution(NewSolution(partial))
}
return
}
//fmt.Fprintln(os.Stderr, "I am at", current, "day is", day)
var current_deal float32
//var current_deal int32
possible_flights := make([]EvaluatedFlight, 0, MAX_CITIES)
for _, f := range graph.fromDaySortedCost[current][day] {
//printInfo(f)
if contains(visited, f.To) {
//if dcfsVisited(visited, f.To) {
continue
}
s := stats.ByDest[current][f.To]
discount := s.AvgPrice - float32(f.Cost)
discount_rate := discount / float32(f.Cost)
var s2 FlightStats
if day < Day(len(stats.ByDay[f.To])-1) {
s2 = stats.ByDay[f.To][day+1]
}
//if discount_rate < -0.3 {
if f.Cost > pDiscountThreshold && discount_rate < pMinDiscount {
// no discount, no deal, bro
continue
}
//current_deal = float32(f.Cost) - s.AvgPrice * discount // - NO NO NO
//current_deal = float32(f.Cost) * s.AvgPrice - s.AvgPrice * discount // (200, 300) = 39639, 51790
//current_deal = float32(f.Cost) - 0.3 * discount // (200, 300) = 40722, 51625
//current_deal = float32(f.Cost) - 0.6 * discount // (200, 300) = 40543, 48493
//current_deal = float32(f.Cost) - 0.9 * discount // (200, 300) = 40447, 50580, total: 189785
//current_deal = float32(f.Cost) // (200, 300) = NO, 48288, total: 189716
//current_deal = s.AvgPrice // (200, 300) = NO, NO, total: 193574
//current_deal = float32(f.Cost) * s.AvgPrice // (200, 300) = NO, NO total: 192248
//current_deal = float32(f.Cost) * s.AvgPrice + stats[f.To][current].AvgPrice * float32(f.Cost) // (200, 300) = NO, NO total: 192788
//current_deal = float32(f.Cost) - discount_rate * discount // (200, 300) = NO, 50481 total: 190949
//current_deal = -discount // no result total 194138
//current_deal = float32(f.Cost) - 0.6 * discount // (200, 300) = No, 48590, total: 187078 (disc rate < 0.3)
//current_deal = float32(f.Cost) - 0.6*discount // (200, 300) = 40505, 48493, total: 187010 (disc rate < 0.25, >650)
current_deal = float32(f.Cost) - pDiscountWeight*discount + pNextAvgWeight*s2.AvgPrice // (200, 300) = 40505, 48493, total: 187010 (disc rate < 0.25, >650)
//possible_flights = append(possible_flights, EvaluatedFlight{f, current_deal})
possible_flights = dcfsInsertSortedFlight(possible_flights, EvaluatedFlight{*f, current_deal})
}
//sort.Sort(byValue(possible_flights))
if len(possible_flights) == 0 {
return
}
if len(possible_flights) > pMaxBranches && day > 0 {
possible_flights = possible_flights[:pMaxBranches]
}
last_value := possible_flights[0].value
for i, f := range possible_flights {
if f.value-last_value > 30 {
return
}
if day == 0 && skip > i {
skip--
continue
}
dcfsIterate(append(partial, f.flight),
day+1,
f.flight.To,
append(visited, f.flight.To),
//dcfsInsertVisited(visited, f.flight.To),
graph, stats,
price+f.flight.Cost,
comm, skip)
last_value = f.value
}
return //[]Flight{}
}