use sas_plus::OpId;
use super::tables::Tables;
pub(super) fn forward(tables: &Tables, plan: &mut [OpId]) {
for i in 0..plan.len() {
let op = plan[i];
if !tables.black_eff[op].is_empty() {
continue;
}
let Some(needs) = &tables.needs[op] else {
continue;
};
let mut j = i;
while j > 0 && !achieves(tables, plan[j - 1], needs) {
plan.swap(j - 1, j);
j -= 1;
}
}
}
fn achieves(tables: &Tables, op: OpId, needs: &[usize]) -> bool {
tables.adds[op]
.iter()
.any(|f| needs.binary_search(f).is_ok())
}
#[cfg(test)]
mod tests {
use alloc::vec;
use alloc::vec::Vec;
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
use super::forward;
use crate::Relaxation;
use crate::red_black::tables::Tables;
#[test]
fn moves_red_operators_up_to_their_achievers() {
let op = |pre: Vec<Fact>, eff: Fact| Operator {
name: "op".into(),
pre,
eff: vec![Effect::new(eff)],
cost: 1,
};
let task = Task {
variables: (0..4)
.map(|i| Variable::new(alloc::format!("v{i}"), 2))
.collect(),
operators: vec![
op(vec![Fact::new(0, 0)], Fact::new(0, 1)),
op(vec![], Fact::new(1, 1)),
op(vec![Fact::new(0, 1)], Fact::new(2, 1)),
op(vec![Fact::new(1, 1)], Fact::new(3, 1)),
],
initial: State(vec![0; 4]),
goal: vec![],
axioms: vec![],
};
let relaxation = Relaxation::new(&task);
let tables = Tables::new(&task, &relaxation, &[0]);
let mut plan = vec![0, 2, 1, 3];
forward(&tables, &mut plan);
assert_eq!(plan, [1, 3, 0, 2]);
}
}