use alloc::vec::Vec;
use sas_plus::{Fact, OpId, Value, VarId};
use crate::decoupled::{
Leaf, LeafAction, LeafEffect, LeafFact, LeafPart, Price, Step, UNREACHED, View,
};
use crate::search::simulation;
pub(super) struct LocalFacts {
pub(super) pre: Vec<(usize, usize)>,
pub(super) eff: Vec<(usize, usize)>,
}
impl LocalFacts {
pub(super) fn part(&self) -> LeafPart {
let fact = |&(slot, value): &(usize, usize)| LeafFact { slot, value };
LeafPart {
pre: self.pre.iter().map(fact).collect(),
eff: self
.eff
.iter()
.map(|f| LeafEffect {
center_cond: Vec::new(),
cond: Vec::new(),
fact: fact(f),
})
.collect(),
}
}
}
pub(super) struct ExplicitLeaf {
pub(super) leaf: Leaf,
pub(super) center_pres: Vec<Vec<(usize, Value)>>,
}
impl ExplicitLeaf {
pub(super) fn new(
vars: Vec<VarId>,
domains: Vec<usize>,
initial: &[usize],
goal: &[(usize, usize)],
) -> Self {
let mut leaf = Leaf::new(vars, domains, initial).expect("an enumerable leaf");
leaf.goal = goal
.iter()
.map(|&(slot, value)| LeafFact { slot, value })
.collect();
Self {
leaf,
center_pres: Vec::new(),
}
}
pub(super) fn add_action(
&mut self,
op: OpId,
cost: Price,
center_pre: Vec<(usize, Value)>,
facts: &LocalFacts,
) {
let mut center_facts: Vec<Fact> =
center_pre.iter().map(|&(s, v)| Fact::new(s, v)).collect();
center_facts.sort_unstable_by_key(|f| (f.var, f.value));
self.leaf.actions.push(LeafAction {
op,
cost,
center_pre: center_facts,
part: facts.part(),
});
self.center_pres.push(center_pre);
}
fn view(&self) -> View<'_> {
self.leaf.static_view()
}
pub(super) fn close(
&self,
seeds: &[Price],
active: super::Active<'_>,
costs: bool,
) -> (Vec<Price>, Vec<Step>) {
let mut prices = seeds.to_vec();
let steps = self
.view()
.close_by(&mut prices, costs, |a| active(&self.center_pres[a]));
(prices, steps)
}
pub(super) fn transfer(&self, part: &LeafPart, prices: &[Price]) -> Option<Vec<Price>> {
let next = self.view().transfer(part, prices);
next.iter().any(|&p| p != UNREACHED).then_some(next)
}
pub(super) fn source(
&self,
part: &LeafPart,
prices: &[Price],
target: usize,
price: Price,
) -> usize {
self.view().transfer_source(part, prices, target, price)
}
pub(super) fn goal_price(&self, prices: &[Price]) -> Option<Price> {
self.cheapest_goal(prices).map(|x| prices[x])
}
pub(super) fn cheapest_goal(&self, prices: &[Price]) -> Option<usize> {
let view = self.view();
reached(prices)
.filter(|&(x, _)| view.is_goal(x))
.min_by_key(|&(_, p)| p)
.map(|(x, _)| x)
}
pub(super) fn lower_fact_prices(&self, prices: &[Price], offsets: &[usize], out: &mut [Price]) {
self.view().lower_fact_prices(reached(prices), offsets, out);
}
pub(super) fn cheapest_goal_cost(&self, parts: &[&LeafPart]) -> Option<Price> {
simulation::cheapest_goal(&self.leaf, parts)
}
pub(super) fn slack(t: &[Price], n: &[Price]) -> Option<i128> {
let mut delta = i128::MIN;
for (x, p) in reached(n) {
if t[x] == UNREACHED {
return None;
}
delta = delta.max(i128::from(t[x]) - i128::from(p));
}
Some(delta)
}
}
fn reached(prices: &[Price]) -> impl Iterator<Item = (usize, Price)> + '_ {
prices
.iter()
.copied()
.enumerate()
.filter(|&(_, p)| p != UNREACHED)
}