Browse Source

Revert "fix 2 bugs"

This reverts commit ac8d44f38040e7a7d83fd497c2256870392e2013.
Guillaume Koenig 7 years ago
parent
commit
98caa04389
1 changed files with 26 additions and 34 deletions
  1. 26 34
      main.go

+ 26 - 34
main.go

@@ -230,6 +230,9 @@ 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
233 236
 		if cumulativeScore >= bestTotalScore {
234 237
 			bestTotalScore = cumulativeScore
235 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 246
 	var rdepth int
244 247
 	var fix bool
245 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 252
 	if r != nil {
247 253
 		rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, fromDepth)
248 254
 		// if depth == reverse depth, make a change
@@ -251,8 +257,10 @@ func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
251 257
 			var r *Ride
252 258
 			if fix {
253 259
 				r = c.pickBestRide()
260
+				fmt.Printf("pickBestRide id %d depth %d\n", r.ID, depth)
254 261
 			} else {
255 262
 				r = c.pickRandomRide()
263
+				fmt.Printf("pickRandomRide id %d depth %d\n", r.ID, depth)
256 264
 			}
257 265
 			// note fromDepth reset to depth
258 266
 			rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, depth)
@@ -356,13 +364,6 @@ type prioq struct {
356 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 367
 func (pq *prioq) Add(car *Car) {
367 368
 	pq.bintree = append(pq.bintree, car)
368 369
 	pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
@@ -370,7 +371,7 @@ func (pq *prioq) Add(car *Car) {
370 371
 	// Rebalance tree to respect invariant
371 372
 	var i = len(pq.bintree) - 1
372 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 375
 		pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
375 376
 		pq.bintree[p].pqindex = p
376 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 384
 	if len(pq.bintree) == 0 {
384 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 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 399
 	// Remove last element
399 400
 	pq.bintree = pq.bintree[:len(pq.bintree)-1]
400 401
 
@@ -405,13 +406,14 @@ func (pq *prioq) Pop() *Car {
405 406
 
406 407
 	// Rebalance tree to respect invariant
407 408
 	len := len(pq.bintree)
408
-	i, left, right := 0, 0, 0
409
+	i := k
410
+	left, right := 0, 0
409 411
 	for {
410 412
 		left = 2*i + 1
411 413
 		right = 2*i + 2
412 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 417
 					break // Inferior to both children
416 418
 				} else {
417 419
 					pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
@@ -420,7 +422,7 @@ func (pq *prioq) Pop() *Car {
420 422
 					i = left
421 423
 				}
422 424
 			} else {
423
-				if !pq.bintree[i].greater(pq.bintree[right]) {
425
+				if pq.bintree[i].Arrival <= pq.bintree[right].Arrival {
424 426
 					break // Inferior to both children
425 427
 				} else {
426 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 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 436
 				break // Inferior to only child
435 437
 			}
436 438
 			pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
@@ -446,18 +448,8 @@ func (pq *prioq) Pop() *Car {
446 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 455
 func (pq *prioq) empty() bool {