浏览代码

Revert "fix 2 bugs"

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

+ 26 - 34
main.go

@@ -230,6 +230,9 @@ 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
233
 		if cumulativeScore >= bestTotalScore {
236
 		if cumulativeScore >= bestTotalScore {
234
 			bestTotalScore = cumulativeScore
237
 			bestTotalScore = cumulativeScore
235
 			// Go back to random depth and make a new change
238
 			// Go back to random depth and make a new change
@@ -243,6 +246,9 @@ func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
243
 	var rdepth int
246
 	var rdepth int
244
 	var fix bool
247
 	var fix bool
245
 	r := c.pickBestRide()
248
 	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
+	// }
246
 	if r != nil {
252
 	if r != nil {
247
 		rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, fromDepth)
253
 		rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, fromDepth)
248
 		// if depth == reverse depth, make a change
254
 		// if depth == reverse depth, make a change
@@ -251,8 +257,10 @@ func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
251
 			var r *Ride
257
 			var r *Ride
252
 			if fix {
258
 			if fix {
253
 				r = c.pickBestRide()
259
 				r = c.pickBestRide()
260
+				fmt.Printf("pickBestRide id %d depth %d\n", r.ID, depth)
254
 			} else {
261
 			} else {
255
 				r = c.pickRandomRide()
262
 				r = c.pickRandomRide()
263
+				fmt.Printf("pickRandomRide id %d depth %d\n", r.ID, depth)
256
 			}
264
 			}
257
 			// note fromDepth reset to depth
265
 			// note fromDepth reset to depth
258
 			rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, depth)
266
 			rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, depth)
@@ -356,13 +364,6 @@ type prioq struct {
356
 	bintree []*Car
364
 	bintree []*Car
357
 }
365
 }
358
 
366
 
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
-
366
 func (pq *prioq) Add(car *Car) {
367
 func (pq *prioq) Add(car *Car) {
367
 	pq.bintree = append(pq.bintree, car)
368
 	pq.bintree = append(pq.bintree, car)
368
 	pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
369
 	pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
@@ -370,7 +371,7 @@ func (pq *prioq) Add(car *Car) {
370
 	// Rebalance tree to respect invariant
371
 	// Rebalance tree to respect invariant
371
 	var i = len(pq.bintree) - 1
372
 	var i = len(pq.bintree) - 1
372
 	var p = (i - 1) / 2
373
 	var p = (i - 1) / 2
373
-	for p >= 0 && pq.bintree[p].greater(pq.bintree[i]) {
374
+	for p >= 0 && pq.bintree[p].Arrival > pq.bintree[i].Arrival {
374
 		pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
375
 		pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
375
 		pq.bintree[p].pqindex = p
376
 		pq.bintree[p].pqindex = p
376
 		pq.bintree[i].pqindex = i
377
 		pq.bintree[i].pqindex = i
@@ -379,22 +380,22 @@ func (pq *prioq) Add(car *Car) {
379
 	}
380
 	}
380
 }
381
 }
381
 
382
 
382
-func (pq *prioq) Pop() *Car {
383
+func (pq *prioq) RemoveAtIndex(k int) *Car {
383
 	if len(pq.bintree) == 0 {
384
 	if len(pq.bintree) == 0 {
384
 		return nil
385
 		return nil
385
 	}
386
 	}
386
 
387
 
387
-	if len(pq.bintree) == 1 {
388
-		elem := pq.bintree[0]
389
-		pq.bintree = pq.bintree[:0]
388
+	if k == len(pq.bintree)-1 {
389
+		elem := pq.bintree[k]
390
+		pq.bintree = pq.bintree[:k]
390
 		return elem
391
 		return elem
391
 	}
392
 	}
392
 
393
 
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
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
398
 	// Remove last element
399
 	// Remove last element
399
 	pq.bintree = pq.bintree[:len(pq.bintree)-1]
400
 	pq.bintree = pq.bintree[:len(pq.bintree)-1]
400
 
401
 
@@ -405,13 +406,14 @@ func (pq *prioq) Pop() *Car {
405
 
406
 
406
 	// Rebalance tree to respect invariant
407
 	// Rebalance tree to respect invariant
407
 	len := len(pq.bintree)
408
 	len := len(pq.bintree)
408
-	i, left, right := 0, 0, 0
409
+	i := k
410
+	left, right := 0, 0
409
 	for {
411
 	for {
410
 		left = 2*i + 1
412
 		left = 2*i + 1
411
 		right = 2*i + 2
413
 		right = 2*i + 2
412
 		if left < len && right < len { // Two children
414
 		if left < len && right < len { // Two children
413
-			if !pq.bintree[left].greater(pq.bintree[right]) {
414
-				if !pq.bintree[i].greater(pq.bintree[left]) {
415
+			if pq.bintree[left].Arrival <= pq.bintree[right].Arrival {
416
+				if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
415
 					break // Inferior to both children
417
 					break // Inferior to both children
416
 				} else {
418
 				} else {
417
 					pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
419
 					pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
@@ -420,7 +422,7 @@ func (pq *prioq) Pop() *Car {
420
 					i = left
422
 					i = left
421
 				}
423
 				}
422
 			} else {
424
 			} else {
423
-				if !pq.bintree[i].greater(pq.bintree[right]) {
425
+				if pq.bintree[i].Arrival <= pq.bintree[right].Arrival {
424
 					break // Inferior to both children
426
 					break // Inferior to both children
425
 				} else {
427
 				} else {
426
 					pq.bintree[i], pq.bintree[right] = pq.bintree[right], pq.bintree[i]
428
 					pq.bintree[i], pq.bintree[right] = pq.bintree[right], pq.bintree[i]
@@ -430,7 +432,7 @@ func (pq *prioq) Pop() *Car {
430
 				}
432
 				}
431
 			}
433
 			}
432
 		} else if left < len { // One child (left)
434
 		} else if left < len { // One child (left)
433
-			if !pq.bintree[i].greater(pq.bintree[left]) {
435
+			if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
434
 				break // Inferior to only child
436
 				break // Inferior to only child
435
 			}
437
 			}
436
 			pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
438
 			pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
@@ -446,18 +448,8 @@ func (pq *prioq) Pop() *Car {
446
 	return elem
448
 	return elem
447
 }
449
 }
448
 
450
 
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
451
+func (pq *prioq) Pop() *Car {
452
+	return pq.RemoveAtIndex(0)
461
 }
453
 }
462
 
454
 
463
 func (pq *prioq) empty() bool {
455
 func (pq *prioq) empty() bool {