|
@@ -230,9 +230,6 @@ func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
|
230
|
230
|
if c == nil {
|
231
|
231
|
// At this point we have a complete configuration
|
232
|
232
|
fmt.Printf("score obtained: %d depth: %d\n", cumulativeScore, depth)
|
233
|
|
- // INVESTIGATE HERE : why do we have different scores ???
|
234
|
|
- // when we're only asking to fix 22 down in the stack
|
235
|
|
- return 8270, true
|
236
|
233
|
if cumulativeScore >= bestTotalScore {
|
237
|
234
|
bestTotalScore = cumulativeScore
|
238
|
235
|
// Go back to random depth and make a new change
|
|
@@ -246,9 +243,6 @@ func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
|
246
|
243
|
var rdepth int
|
247
|
244
|
var fix bool
|
248
|
245
|
r := c.pickBestRide()
|
249
|
|
- // if depth >= 252 && depth <= 258 {
|
250
|
|
- // fmt.Printf("yo picking id %d score %d c=%+v\n", r.ID, cumulativeScore, c)
|
251
|
|
- // }
|
252
|
246
|
if r != nil {
|
253
|
247
|
rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, fromDepth)
|
254
|
248
|
// if depth == reverse depth, make a change
|
|
@@ -257,10 +251,8 @@ func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
|
257
|
251
|
var r *Ride
|
258
|
252
|
if fix {
|
259
|
253
|
r = c.pickBestRide()
|
260
|
|
- fmt.Printf("pickBestRide id %d depth %d\n", r.ID, depth)
|
261
|
254
|
} else {
|
262
|
255
|
r = c.pickRandomRide()
|
263
|
|
- fmt.Printf("pickRandomRide id %d depth %d\n", r.ID, depth)
|
264
|
256
|
}
|
265
|
257
|
// note fromDepth reset to depth
|
266
|
258
|
rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, depth)
|
|
@@ -364,6 +356,13 @@ type prioq struct {
|
364
|
356
|
bintree []*Car
|
365
|
357
|
}
|
366
|
358
|
|
|
359
|
+func (c *Car) greater(c2 *Car) bool {
|
|
360
|
+ if c.Arrival == c2.Arrival {
|
|
361
|
+ return c.ID > c2.ID
|
|
362
|
+ }
|
|
363
|
+ return c.Arrival > c2.Arrival
|
|
364
|
+}
|
|
365
|
+
|
367
|
366
|
func (pq *prioq) Add(car *Car) {
|
368
|
367
|
pq.bintree = append(pq.bintree, car)
|
369
|
368
|
pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
|
|
@@ -371,7 +370,7 @@ func (pq *prioq) Add(car *Car) {
|
371
|
370
|
// Rebalance tree to respect invariant
|
372
|
371
|
var i = len(pq.bintree) - 1
|
373
|
372
|
var p = (i - 1) / 2
|
374
|
|
- for p >= 0 && pq.bintree[p].Arrival > pq.bintree[i].Arrival {
|
|
373
|
+ for p >= 0 && pq.bintree[p].greater(pq.bintree[i]) {
|
375
|
374
|
pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
|
376
|
375
|
pq.bintree[p].pqindex = p
|
377
|
376
|
pq.bintree[i].pqindex = i
|
|
@@ -380,22 +379,22 @@ func (pq *prioq) Add(car *Car) {
|
380
|
379
|
}
|
381
|
380
|
}
|
382
|
381
|
|
383
|
|
-func (pq *prioq) RemoveAtIndex(k int) *Car {
|
|
382
|
+func (pq *prioq) Pop() *Car {
|
384
|
383
|
if len(pq.bintree) == 0 {
|
385
|
384
|
return nil
|
386
|
385
|
}
|
387
|
386
|
|
388
|
|
- if k == len(pq.bintree)-1 {
|
389
|
|
- elem := pq.bintree[k]
|
390
|
|
- pq.bintree = pq.bintree[:k]
|
|
387
|
+ if len(pq.bintree) == 1 {
|
|
388
|
+ elem := pq.bintree[0]
|
|
389
|
+ pq.bintree = pq.bintree[:0]
|
391
|
390
|
return elem
|
392
|
391
|
}
|
393
|
392
|
|
394
|
|
- pq.bintree[k].pqindex = -1
|
395
|
|
- elem := pq.bintree[k]
|
396
|
|
- // Put last element at hole
|
397
|
|
- pq.bintree[k] = pq.bintree[len(pq.bintree)-1]
|
398
|
|
- pq.bintree[k].pqindex = k
|
|
393
|
+ pq.bintree[0].pqindex = -1
|
|
394
|
+ elem := pq.bintree[0]
|
|
395
|
+ // Put last element at root
|
|
396
|
+ pq.bintree[0] = pq.bintree[len(pq.bintree)-1]
|
|
397
|
+ pq.bintree[0].pqindex = 0
|
399
|
398
|
// Remove last element
|
400
|
399
|
pq.bintree = pq.bintree[:len(pq.bintree)-1]
|
401
|
400
|
|
|
@@ -406,14 +405,13 @@ func (pq *prioq) RemoveAtIndex(k int) *Car {
|
406
|
405
|
|
407
|
406
|
// Rebalance tree to respect invariant
|
408
|
407
|
len := len(pq.bintree)
|
409
|
|
- i := k
|
410
|
|
- left, right := 0, 0
|
|
408
|
+ i, left, right := 0, 0, 0
|
411
|
409
|
for {
|
412
|
410
|
left = 2*i + 1
|
413
|
411
|
right = 2*i + 2
|
414
|
412
|
if left < len && right < len { // Two children
|
415
|
|
- if pq.bintree[left].Arrival <= pq.bintree[right].Arrival {
|
416
|
|
- if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
|
|
413
|
+ if !pq.bintree[left].greater(pq.bintree[right]) {
|
|
414
|
+ if !pq.bintree[i].greater(pq.bintree[left]) {
|
417
|
415
|
break // Inferior to both children
|
418
|
416
|
} else {
|
419
|
417
|
pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
|
|
@@ -422,7 +420,7 @@ func (pq *prioq) RemoveAtIndex(k int) *Car {
|
422
|
420
|
i = left
|
423
|
421
|
}
|
424
|
422
|
} else {
|
425
|
|
- if pq.bintree[i].Arrival <= pq.bintree[right].Arrival {
|
|
423
|
+ if !pq.bintree[i].greater(pq.bintree[right]) {
|
426
|
424
|
break // Inferior to both children
|
427
|
425
|
} else {
|
428
|
426
|
pq.bintree[i], pq.bintree[right] = pq.bintree[right], pq.bintree[i]
|
|
@@ -432,7 +430,7 @@ func (pq *prioq) RemoveAtIndex(k int) *Car {
|
432
|
430
|
}
|
433
|
431
|
}
|
434
|
432
|
} else if left < len { // One child (left)
|
435
|
|
- if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
|
|
433
|
+ if !pq.bintree[i].greater(pq.bintree[left]) {
|
436
|
434
|
break // Inferior to only child
|
437
|
435
|
}
|
438
|
436
|
pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
|
|
@@ -448,8 +446,18 @@ func (pq *prioq) RemoveAtIndex(k int) *Car {
|
448
|
446
|
return elem
|
449
|
447
|
}
|
450
|
448
|
|
451
|
|
-func (pq *prioq) Pop() *Car {
|
452
|
|
- return pq.RemoveAtIndex(0)
|
|
449
|
+func (pq *prioq) RemoveAtIndex(k int) *Car {
|
|
450
|
+ if k == 0 {
|
|
451
|
+ return pq.Pop()
|
|
452
|
+ }
|
|
453
|
+ pq.bintree[k].pqindex = -1
|
|
454
|
+ elem := pq.bintree[k]
|
|
455
|
+ reassign := pq.bintree[k+1:]
|
|
456
|
+ pq.bintree = pq.bintree[:k]
|
|
457
|
+ for _, c := range reassign {
|
|
458
|
+ pq.Add(c)
|
|
459
|
+ }
|
|
460
|
+ return elem
|
453
|
461
|
}
|
454
|
462
|
|
455
|
463
|
func (pq *prioq) empty() bool {
|