Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions generic_a_star/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ impl<

self.state = AStarState::Searching;

let is_continued_search = !self.closed_list.is_empty();
let mut last_node = None;
let mut target_identifier = None;
let mut target_cost = <Context::Node as AStarNode>::Cost::max_value();
Expand Down Expand Up @@ -391,23 +390,36 @@ impl<
if DEBUG_ASTAR && is_target {
trace!("Node {node} is target");
}
if is_target {
println!("Node {node} is target");
} else {
println!("Node {node}");
}
debug_assert!(!is_target || node.a_star_lower_bound().is_zero());

if self
.closed_list
.can_skip_node(&node, self.context.is_label_setting())
{
self.performance_counters.suboptimal_opened_nodes += 1;
let existing_cost = self.closed_list.get(node.identifier()).unwrap().cost();
let existing_secondary_maximisable_score = self
.closed_list
.get(node.identifier())
.unwrap()
.secondary_maximisable_score();

if is_target
&& (node.cost() < target_cost
|| (node.cost() == target_cost
&& (node.cost() < target_cost.min(existing_cost)
|| (node.cost() == target_cost.min(existing_cost)
&& node.secondary_maximisable_score()
> target_secondary_maximisable_score))
> target_secondary_maximisable_score
.max(existing_secondary_maximisable_score)))
{
if DEBUG_ASTAR {
trace!("Updating target to {node}");
}
println!("Updating target to {node}");
target_identifier = Some(node.identifier().clone());
target_cost = node.cost();
target_secondary_maximisable_score = node.secondary_maximisable_score();
Expand All @@ -420,14 +432,35 @@ impl<
self.closed_list.insert(node.identifier().clone(), node);
self.performance_counters.closed_nodes += 1;
debug_assert!(
previous_visit.is_none()
|| !self.context.is_label_setting()
|| is_continued_search,
previous_visit.is_none() || !self.context.is_label_setting(),
"Visited node again even though we are label setting:\nprevious: {}",
previous_visit.unwrap(),
);
break;
}
} else if is_target
&& (existing_cost < target_cost
|| (existing_cost == target_cost
&& node.secondary_maximisable_score()
> existing_secondary_maximisable_score))
{
let node = self.closed_list.get(node.identifier()).unwrap();
// Set target to existing node if it is better.
if DEBUG_ASTAR {
trace!("Updating target to {node}");
}
println!("Updating target to {node}");
target_identifier = Some(node.identifier().clone());
target_cost = node.cost();
target_secondary_maximisable_score = node.secondary_maximisable_score();

if self.context.is_label_setting() {
if DEBUG_ASTAR {
trace!("Context is label setting, so we return the first target found");
}
self.performance_counters.closed_nodes += 1;
break;
}
}

if DEBUG_ASTAR {
Expand Down Expand Up @@ -456,6 +489,7 @@ impl<
if DEBUG_ASTAR {
trace!("Updating target to {node}");
}
println!("Updating target to {node}");
target_identifier = Some(node.identifier().clone());
target_cost = node.cost();
target_secondary_maximisable_score = node.secondary_maximisable_score();
Expand All @@ -466,11 +500,7 @@ impl<
}
let previous_visit = self.closed_list.insert(node.identifier().clone(), node);
self.performance_counters.closed_nodes += 1;
debug_assert!(
previous_visit.is_none()
|| !self.context.is_label_setting()
|| is_continued_search
);
debug_assert!(previous_visit.is_none() || !self.context.is_label_setting());
break;
}
}
Expand All @@ -493,7 +523,12 @@ impl<
};

let cost = self.closed_list.get(&target_identifier).unwrap().cost();
debug_assert_eq!(cost, target_cost);
debug_assert_eq!(
cost,
target_cost,
"Target node has lower cost than target_cost:\nnode: {}\ntarget_cost: {target_cost}",
self.closed_list.get(&target_identifier).unwrap(),
);
self.state = AStarState::Terminated {
result: AStarResult::FoundTarget {
identifier: target_identifier.clone(),
Expand Down
10 changes: 8 additions & 2 deletions lib_ts_chainalign/src/exact_chaining/ts_12_jump/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<Cost: AStarCost> AStarContext for Context<'_, '_, '_, Cost> {
},
predecessor: None,
predecessor_alignment_type: None,
cost: Cost::zero(),
cost: self.costs.ts_base_cost,
match_run: 0,
}
}
Expand Down Expand Up @@ -198,7 +198,9 @@ impl<Cost: AStarCost> AStarContext for Context<'_, '_, '_, Cost> {

// Generate jump successors.
if is_primary {
let new_cost = *cost + self.costs.ts_base_cost;
// We do not count the jump costs here, because all paths anyways need to jump at some point.
// Instead, we count them at the start.
let new_cost = *cost;

// This generates too many jumps, most of these are gonna be much too far.
output.extend(
Expand Down Expand Up @@ -233,6 +235,10 @@ impl<Cost: AStarCost> AStarContext for Context<'_, '_, '_, Cost> {
fn memory_limit(&self) -> Option<usize> {
None
}

fn is_label_setting(&self) -> bool {
false
}
}

impl<Cost> Reset for Context<'_, '_, '_, Cost> {
Expand Down
26 changes: 3 additions & 23 deletions lib_ts_chainalign/src/exact_chaining/ts_34_jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,11 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
&a_star,
descendant_start,
start.ts_kind().unwrap(),
self.alignment_costs,
additional_primary_targets_output,
);
self.a_star_buffers = Some(a_star.into_buffers());

// The TS base cost is applied at the 12-jump, but we anyways apply it in this algorithm to make it label-setting if the base cost is non-zero.
// But since the 34-jump has zero cost, we subtract it again.
(
if cost == Cost::max_value() {
Cost::max_value()
} else {
cost - self.alignment_costs.ts_base_cost
},
alignment,
)
(cost, alignment)
}

/// Align from start until the cost limit is reached.
Expand All @@ -131,17 +121,14 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
);
let mut a_star = AStar::new_with_buffers(context, self.a_star_buffers.take().unwrap());
a_star.initialise();
// The TS base cost is applied at the 12-jump, but we anyways apply it in this algorithm to make it label-setting if the base cost is non-zero.
// But since the 34-jump has zero cost, we subtract it again.
a_star.search_until(|_, node| node.cost > cost_limit + self.alignment_costs.ts_base_cost);
a_star.search_until(|_, node| node.cost > cost_limit);

let descendant_start = start.secondary_ordinate_descendant().unwrap();
let ts_kind = start.ts_kind().unwrap();
Self::fill_additional_targets(
&a_star,
descendant_start,
ts_kind,
self.alignment_costs,
additional_primary_targets_output,
);
self.a_star_buffers = Some(a_star.into_buffers());
Expand All @@ -151,7 +138,6 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
a_star: &AStar<Context<Cost>>,
descendant_start: usize,
ts_kind: TsKind,
alignment_costs: &AlignmentCosts<Cost>,
additional_primary_targets_output: &mut impl Extend<(PrimaryAnchor, Cost)>,
) {
additional_primary_targets_output.extend(
Expand All @@ -173,13 +159,7 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
.map(|node| {
(
PrimaryAnchor::new_from_start(&node.identifier.coordinates()),
// The TS base cost is applied at the 12-jump, but we anyways apply it in this algorithm to make it label-setting if the base cost is non-zero.
// But since the 34-jump has zero cost, we subtract it again.
if node.cost == Cost::max_value() {
Cost::max_value()
} else {
node.cost - alignment_costs.ts_base_cost
},
node.cost,
)
}),
);
Expand Down
8 changes: 5 additions & 3 deletions lib_ts_chainalign/src/exact_chaining/ts_34_jump/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ impl<Cost: AStarCost> AStarContext for Context<'_, '_, '_, Cost> {

// Generate jump successors.
if is_secondary {
// TS base cost was applied at 12-jump already, but we anyways apply it to get a label-setting search if it's non-zero.
// We subtract it later in the super module.
let new_cost = *cost + self.costs.ts_base_cost;
let new_cost = *cost;

// This generates too many jumps, most of these are gonna be much too far.
output.extend(
Expand Down Expand Up @@ -242,6 +240,10 @@ impl<Cost: AStarCost> AStarContext for Context<'_, '_, '_, Cost> {
fn memory_limit(&self) -> Option<usize> {
None
}

fn is_label_setting(&self) -> bool {
false
}
}

impl<Cost> Reset for Context<'_, '_, '_, Cost> {
Expand Down