浏览代码

fix 2 bugs

Guillaume Koenig 7 年之前
父节点
当前提交
ac8d44f380
共有 1 个文件被更改,包括 34 次插入26 次删除
  1. 34 26
      main.go

+ 34 - 26
main.go

@@ -230,9 +230,6 @@ func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
230
 	if c == nil {
230
 	if c == nil {
231
 		// At this point we have a complete configuration
231
 		// At this point we have a complete configuration
232
 		fmt.Printf("score obtained: %d depth: %d\n", cumulativeScore, depth)
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
 		if cumulativeScore >= bestTotalScore {
233
 		if cumulativeScore >= bestTotalScore {
237
 			bestTotalScore = cumulativeScore
234
 			bestTotalScore = cumulativeScore
238
 			// Go back to random depth and make a new change
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
 	var rdepth int
243
 	var rdepth int
247
 	var fix bool
244
 	var fix bool
248
 	r := c.pickBestRide()
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
 	if r != nil {
246
 	if r != nil {
253
 		rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, fromDepth)
247
 		rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, fromDepth)
254
 		// if depth == reverse depth, make a change
248
 		// if depth == reverse depth, make a change
@@ -257,10 +251,8 @@ func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
257
 			var r *Ride
251
 			var r *Ride
258
 			if fix {
252
 			if fix {
259
 				r = c.pickBestRide()
253
 				r = c.pickBestRide()
260
-				fmt.Printf("pickBestRide id %d depth %d\n", r.ID, depth)
261
 			} else {
254
 			} else {
262
 				r = c.pickRandomRide()
255
 				r = c.pickRandomRide()
263
-				fmt.Printf("pickRandomRide id %d depth %d\n", r.ID, depth)
264
 			}
256
 			}
265
 			// note fromDepth reset to depth
257
 			// note fromDepth reset to depth
266
 			rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, depth)
258
 			rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, depth)
@@ -364,6 +356,13 @@ type prioq struct {
364
 	bintree []*Car
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
 func (pq *prioq) Add(car *Car) {
366
 func (pq *prioq) Add(car *Car) {
368
 	pq.bintree = append(pq.bintree, car)
367
 	pq.bintree = append(pq.bintree, car)
369
 	pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
368
 	pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
@@ -371,7 +370,7 @@ func (pq *prioq) Add(car *Car) {
371
 	// Rebalance tree to respect invariant
370
 	// Rebalance tree to respect invariant
372
 	var i = len(pq.bintree) - 1
371
 	var i = len(pq.bintree) - 1
373
 	var p = (i - 1) / 2
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
 		pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
374
 		pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
376
 		pq.bintree[p].pqindex = p
375
 		pq.bintree[p].pqindex = p
377
 		pq.bintree[i].pqindex = i
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
 	if len(pq.bintree) == 0 {
383
 	if len(pq.bintree) == 0 {
385
 		return nil
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
 		return elem
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
 	// Remove last element
398
 	// Remove last element
400
 	pq.bintree = pq.bintree[:len(pq.bintree)-1]
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
 	// Rebalance tree to respect invariant
406
 	// Rebalance tree to respect invariant
408
 	len := len(pq.bintree)
407
 	len := len(pq.bintree)
409
-	i := k
410
-	left, right := 0, 0
408
+	i, left, right := 0, 0, 0
411
 	for {
409
 	for {
412
 		left = 2*i + 1
410
 		left = 2*i + 1
413
 		right = 2*i + 2
411
 		right = 2*i + 2
414
 		if left < len && right < len { // Two children
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
 					break // Inferior to both children
415
 					break // Inferior to both children
418
 				} else {
416
 				} else {
419
 					pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
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
 					i = left
420
 					i = left
423
 				}
421
 				}
424
 			} else {
422
 			} else {
425
-				if pq.bintree[i].Arrival <= pq.bintree[right].Arrival {
423
+				if !pq.bintree[i].greater(pq.bintree[right]) {
426
 					break // Inferior to both children
424
 					break // Inferior to both children
427
 				} else {
425
 				} else {
428
 					pq.bintree[i], pq.bintree[right] = pq.bintree[right], pq.bintree[i]
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
 		} else if left < len { // One child (left)
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
 				break // Inferior to only child
434
 				break // Inferior to only child
437
 			}
435
 			}
438
 			pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
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
 	return elem
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
 func (pq *prioq) empty() bool {
463
 func (pq *prioq) empty() bool {