@@ -331,7 +331,6 @@ impl<
331331
332332 self . state = AStarState :: Searching ;
333333
334- let is_continued_search = !self . closed_list . is_empty ( ) ;
335334 let mut last_node = None ;
336335 let mut target_identifier = None ;
337336 let mut target_cost = <Context :: Node as AStarNode >:: Cost :: max_value ( ) ;
@@ -391,23 +390,36 @@ impl<
391390 if DEBUG_ASTAR && is_target {
392391 trace ! ( "Node {node} is target" ) ;
393392 }
393+ if is_target {
394+ println ! ( "Node {node} is target" ) ;
395+ } else {
396+ println ! ( "Node {node}" ) ;
397+ }
394398 debug_assert ! ( !is_target || node. a_star_lower_bound( ) . is_zero( ) ) ;
395399
396400 if self
397401 . closed_list
398402 . can_skip_node ( & node, self . context . is_label_setting ( ) )
399403 {
400404 self . performance_counters . suboptimal_opened_nodes += 1 ;
405+ let existing_cost = self . closed_list . get ( node. identifier ( ) ) . unwrap ( ) . cost ( ) ;
406+ let existing_secondary_maximisable_score = self
407+ . closed_list
408+ . get ( node. identifier ( ) )
409+ . unwrap ( )
410+ . secondary_maximisable_score ( ) ;
401411
402412 if is_target
403- && ( node. cost ( ) < target_cost
404- || ( node. cost ( ) == target_cost
413+ && ( node. cost ( ) < target_cost. min ( existing_cost )
414+ || ( node. cost ( ) == target_cost. min ( existing_cost )
405415 && node. secondary_maximisable_score ( )
406- > target_secondary_maximisable_score) )
416+ > target_secondary_maximisable_score
417+ . max ( existing_secondary_maximisable_score) ) )
407418 {
408419 if DEBUG_ASTAR {
409420 trace ! ( "Updating target to {node}" ) ;
410421 }
422+ println ! ( "Updating target to {node}" ) ;
411423 target_identifier = Some ( node. identifier ( ) . clone ( ) ) ;
412424 target_cost = node. cost ( ) ;
413425 target_secondary_maximisable_score = node. secondary_maximisable_score ( ) ;
@@ -420,14 +432,35 @@ impl<
420432 self . closed_list . insert ( node. identifier ( ) . clone ( ) , node) ;
421433 self . performance_counters . closed_nodes += 1 ;
422434 debug_assert ! (
423- previous_visit. is_none( )
424- || !self . context. is_label_setting( )
425- || is_continued_search,
435+ previous_visit. is_none( ) || !self . context. is_label_setting( ) ,
426436 "Visited node again even though we are label setting:\n previous: {}" ,
427437 previous_visit. unwrap( ) ,
428438 ) ;
429439 break ;
430440 }
441+ } else if is_target
442+ && ( existing_cost < target_cost
443+ || ( existing_cost == target_cost
444+ && node. secondary_maximisable_score ( )
445+ > existing_secondary_maximisable_score) )
446+ {
447+ let node = self . closed_list . get ( node. identifier ( ) ) . unwrap ( ) ;
448+ // Set target to existing node if it is better.
449+ if DEBUG_ASTAR {
450+ trace ! ( "Updating target to {node}" ) ;
451+ }
452+ println ! ( "Updating target to {node}" ) ;
453+ target_identifier = Some ( node. identifier ( ) . clone ( ) ) ;
454+ target_cost = node. cost ( ) ;
455+ target_secondary_maximisable_score = node. secondary_maximisable_score ( ) ;
456+
457+ if self . context . is_label_setting ( ) {
458+ if DEBUG_ASTAR {
459+ trace ! ( "Context is label setting, so we return the first target found" ) ;
460+ }
461+ self . performance_counters . closed_nodes += 1 ;
462+ break ;
463+ }
431464 }
432465
433466 if DEBUG_ASTAR {
@@ -456,6 +489,7 @@ impl<
456489 if DEBUG_ASTAR {
457490 trace ! ( "Updating target to {node}" ) ;
458491 }
492+ println ! ( "Updating target to {node}" ) ;
459493 target_identifier = Some ( node. identifier ( ) . clone ( ) ) ;
460494 target_cost = node. cost ( ) ;
461495 target_secondary_maximisable_score = node. secondary_maximisable_score ( ) ;
@@ -466,11 +500,7 @@ impl<
466500 }
467501 let previous_visit = self . closed_list . insert ( node. identifier ( ) . clone ( ) , node) ;
468502 self . performance_counters . closed_nodes += 1 ;
469- debug_assert ! (
470- previous_visit. is_none( )
471- || !self . context. is_label_setting( )
472- || is_continued_search
473- ) ;
503+ debug_assert ! ( previous_visit. is_none( ) || !self . context. is_label_setting( ) ) ;
474504 break ;
475505 }
476506 }
@@ -493,7 +523,12 @@ impl<
493523 } ;
494524
495525 let cost = self . closed_list . get ( & target_identifier) . unwrap ( ) . cost ( ) ;
496- debug_assert_eq ! ( cost, target_cost) ;
526+ debug_assert_eq ! (
527+ cost,
528+ target_cost,
529+ "Target node has lower cost than target_cost:\n node: {}\n target_cost: {target_cost}" ,
530+ self . closed_list. get( & target_identifier) . unwrap( ) ,
531+ ) ;
497532 self . state = AStarState :: Terminated {
498533 result : AStarResult :: FoundTarget {
499534 identifier : target_identifier. clone ( ) ,
0 commit comments