-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexhaustive.go
416 lines (343 loc) · 8.03 KB
/
exhaustive.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
"fmt"
"math"
"github.com/gosuri/uiprogress"
)
// 1: [0,1,2,0]
// 2: 0210
func exhaustiveSearch(src, n int, dist [][]int) {
fmt.Println("exhaustive search, there are ", fac(n-1), " paths to check")
perms := genPermutations(src, n)
var path []int
var shortest int = math.MaxInt32
for _, perm := range perms {
// Check if path ist shorter than path
d := calcPathDist(perm, dist)
if d < shortest {
shortest = d
path = perm
}
}
fmt.Println("shortest path is ", path, "len", shortest)
}
// Do not preallocate all possible paths, be merciful to your memory.
func betteExhaustiveSearch(src, n int, dist [][]int) {
fmt.Println("better exhaustive search, there are ", fac(n-1), " paths to check")
var path []int
var shortest int = math.MaxInt32
// Fancy
uiprogress.Start() // start rendering
bar := uiprogress.AddBar(fac(n - 1)) // Add a new bar
// optionally, append and prepend completion and elapsed time
bar.AppendCompleted()
// bar.PrependElapsed()
bar.AppendElapsed()
// Indices without/start/end
left := sliceWithoutSrc(src, n)
for perm := range permutations(left) {
bar.Incr()
// Make a path
a := make([]int, n+1)
a[0] = src
a[n] = src
k := 1
for i := 0; i < len(perm); i++ {
a[k] = perm[i]
k++
}
// Check it
d := calcPathDist(a, dist)
if d < shortest {
shortest = d
path = a
}
}
fmt.Println("shortest path is ", path, "len", shortest)
}
//uses parallel permutator, otherwise identical to betteExhaustiveSearch
func betteExhaustiveSearch2(src, n int, dist [][]int) {
fmt.Println("better exhaustive search, there are ", fac(n-1), " paths to check")
var path []int
var shortest int = math.MaxInt32
// Fancy
uiprogress.Start() // start rendering
bar := uiprogress.AddBar(fac(n - 1)) // Add a new bar
// optionally, append and prepend completion and elapsed time
bar.AppendCompleted()
// bar.PrependElapsed()
bar.AppendElapsed()
// Indices without/start/end
left := sliceWithoutSrc(src, n)
for perm := range parallelPermutations(left, 20) {
bar.Incr()
// Make a path
a := make([]int, n+1)
a[0] = src
a[n] = src
k := 1
for i := 0; i < len(perm); i++ {
a[k] = perm[i]
k++
}
// Check it
d := calcPathDist(a, dist)
if d < shortest {
shortest = d
path = a
}
}
fmt.Println("shortest path is ", path, "len", shortest)
}
//runs fastest if workerCnt==1 :(
func parallelExhaustiveSearch(workerCnt, src, n int, dist [][]int) {
fmt.Println("parallel exhaustive search, there are ", fac(n-1), " paths to check")
fmt.Println(workerCnt, " workers started")
var path []int
var shortest = math.MaxInt32
// Fancy
//uiprogress.Start() // start rendering
//bar := uiprogress.AddBar(fac(n - 1)) // Add a new bar
// optionally, append and prepend completion and elapsed time
//bar.AppendCompleted()
// bar.PrependElapsed()
//bar.AppendElapsed()
// Indices without/start/end
left := sliceWithoutSrc(src, n)
//startWorkers
in := parallelPermutations(left, workerCnt*2)
outs := make([]chan []int, workerCnt)
for i := 0; i < workerCnt; i++ {
outs[i] = exhaustiveWorker(i, src, n, dist, in)
}
for i := 0; i < workerCnt; i++ {
short := <-outs[i] //blocking, unbuffered out chan
d := calcPathDist(short, dist)
if d < shortest {
shortest = d
path = short
}
}
fmt.Println("shortest path is ", path, "len", shortest)
}
func exhaustiveWorker(worker, src, n int, dist [][]int, in <-chan []int) (out chan []int) {
out = make(chan []int, 0)
go func() {
shortest := math.MaxInt32
var path []int
cnt := 0
for perm := range in {
cnt++
if cnt%1000000 == 0 && worker == 0 {
//bar.Incr()
fmt.Println(len(in))
}
// Make a path
a := make([]int, n+1)
a[0] = src
a[n] = src
copy(a[1:], perm)
// Check it
d := calcPathDist(a, dist)
if d < shortest {
shortest = d
path = a
}
}
out <- path
fmt.Println("shortest path worker ", worker, " is ", path, "len", shortest)
}()
return out
}
func parallelExhaustiveSearch2(workerCnt, src, n int, dist [][]int) {
fmt.Println("parallel exhaustive search, there are ", fac(n-1), " paths to check")
fmt.Println(workerCnt, " workers started")
//c chan []int, inputs []int
inputs := sliceWithoutSrc(src, n)
c := make([]chan []int, len(inputs))
for i := 0; i < len(inputs); i++ {
c[i] = make(chan []int)
temp := make([]int, len(inputs)-1)
copy(temp, inputs[:i])
copy(temp[i:], inputs[i+1:])
go func(t []int, ou chan []int, in int) {
permutate3(ou, t, in, dist)
}(temp, c[i], inputs[i])
}
var path []int
var shortest = math.MaxInt32
for i := 0; i < len(inputs); i++ {
aChan := c[i]
aPath := <-aChan
d := calcPathDist(aPath, dist)
if d < shortest {
shortest = d
path = aPath
}
}
fmt.Println("shortest path is ", path, "len", shortest)
return
}
// This does work, but since all possible paths are generated beforehand
// we may run into a little RAM overflow if the problem is too big...
// In my case (32gb) if n>13
func genPermutations(start int, n int) [][]int {
a := make([][]int, fac(n-1))
// Indices without/start/end
var left []int
for i := 0; i < n; i++ {
if i == start {
continue
}
left = append(left, i)
}
tmp := 0
for perm := range permutations(left) {
a[tmp] = make([]int, n+1)
a[tmp][0] = start
a[tmp][n] = start
k := 1
for i := 0; i < len(perm); i++ {
a[tmp][k] = perm[i]
k++
}
tmp++
}
return a
}
func fac(n int) (result int) {
if n > 0 {
result = n * fac(n-1)
return result
}
return 1
}
func sliceWithoutSrc(v, n int) []int {
var left []int
for i := 0; i < n; i++ {
if i == v {
continue
}
left = append(left, i)
}
return left
}
func permutations(data []int) <-chan []int {
c := make(chan []int)
go func(c chan []int) {
defer close(c)
permutate(c, data)
}(c)
return c
}
func permutate(c chan []int, inputs []int) {
output := make([]int, len(inputs))
copy(output, inputs)
c <- output
size := len(inputs)
p := make([]int, size+1)
for i := 0; i < size+1; i++ {
p[i] = i
}
for i := 1; i < size; {
p[i]--
j := 0
if i%2 == 1 {
j = p[i]
}
tmp := inputs[j]
inputs[j] = inputs[i]
inputs[i] = tmp
output := make([]int, len(inputs))
copy(output, inputs)
c <- output
for i = 1; p[i] == 0; i++ {
p[i] = i
}
}
}
func permutateParallel(c chan []int, inputs []int) {
finisher := make(chan bool)
for i := 0; i < len(inputs); i++ {
temp := make([]int, len(inputs)-1)
copy(temp, inputs[:i])
copy(temp[i:], inputs[i+1:])
//fmt.Println(temp, i)
go permutate2(c, temp, inputs[i], finisher)
}
for i := 0; i < len(inputs); i++ {
<-finisher
}
return
}
func permutate2(c chan []int, inputs []int, first int, finisher chan bool) {
output := make([]int, len(inputs))
copy(output, inputs)
c <- output
size := len(inputs)
p := make([]int, size+1)
for i := 0; i < size+1; i++ {
p[i] = i
}
for i := 1; i < size; {
p[i]--
j := 0
if i%2 == 1 {
j = p[i]
}
inputs[j], inputs[i] = inputs[i], inputs[j]
output := make([]int, len(inputs)+1)
copy(output[1:], inputs)
output[0] = first
c <- output
for i = 1; p[i] == 0; i++ {
p[i] = i
}
}
finisher <- true
}
func permutate3(c chan []int, inputs []int, first int, dist [][]int) {
var path []int
var shortest = math.MaxInt32
d := calcPathDist(inputs, dist)
if d < shortest {
shortest = d
output := make([]int, len(inputs)+1)
copy(output[1:], inputs)
output[0] = first
path = output
}
size := len(inputs)
p := make([]int, size+1)
for i := 0; i < size+1; i++ {
p[i] = i
}
for i := 1; i < size; {
p[i]--
j := 0
if i%2 == 1 {
j = p[i]
}
inputs[j], inputs[i] = inputs[i], inputs[j]
d := calcPathDist(inputs, dist)
if d < shortest {
shortest = d
output := make([]int, len(inputs)+1)
copy(output[1:], inputs)
output[0] = first
path = output
}
for i = 1; p[i] == 0; i++ {
p[i] = i
}
}
c <- path
}
func parallelPermutations(data []int, buffer int) <-chan []int {
c := make(chan []int, buffer)
go func(c chan []int) {
defer close(c)
permutateParallel(c, data)
}(c)
return c
}