From dcc73e8aadc150e4eb705c034cb1da80d591aeb9 Mon Sep 17 00:00:00 2001 From: Arthur Bit-Monnot Date: Tue, 17 Dec 2024 13:51:27 +0100 Subject: [PATCH] fix(planning): correctly represent synthetic conditions introduced for numerics --- planning/planners/src/encode.rs | 4 +--- planning/planners/src/encode/symmetry.rs | 7 +++++-- planning/planners/src/encoding.rs | 19 +++++++++++++++---- planning/planners/src/search/causal.rs | 7 ++++++- .../analysis/detrimental_supports.rs | 17 +++++++++++++---- .../planning/src/chronicles/analysis/mod.rs | 2 +- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/planning/planners/src/encode.rs b/planning/planners/src/encode.rs index e6d88475..f8c23362 100644 --- a/planning/planners/src/encode.rs +++ b/planning/planners/src/encode.rs @@ -757,7 +757,6 @@ pub fn encode(pb: &FiniteProblem, metric: Option) -> std::result::Result let span = tracing::span!(tracing::Level::TRACE, "numeric increase coherence"); let _span = span.enter(); let mut num_numeric_increase_coherence_constraints = 0; - let mut next_cond_id = 10000; // TODO: use a proper ID for &(inc_id, prez, inc) in &incs { assert!(is_numeric(&inc.state_var)); @@ -775,7 +774,7 @@ pub fn encode(pb: &FiniteProblem, metric: Option) -> std::result::Result // Check that the state variable value is equals to the new variable `var`. // It will force the state variable to be in the bounds of the new variable after the increase. increase_coherence_conditions.push(( - CondID::new(inc_id.instance_id, next_cond_id), + CondID::new_post_increase(inc_id.instance_id, inc_id.eff_id), prez, Condition { start: inc.transition_end, @@ -784,7 +783,6 @@ pub fn encode(pb: &FiniteProblem, metric: Option) -> std::result::Result value: var.into(), }, )); - next_cond_id += 1; num_numeric_increase_coherence_constraints += 1; } diff --git a/planning/planners/src/encode/symmetry.rs b/planning/planners/src/encode/symmetry.rs index 52df5c13..31d8de1b 100644 --- a/planning/planners/src/encode/symmetry.rs +++ b/planning/planners/src/encode/symmetry.rs @@ -292,7 +292,10 @@ fn add_plan_space_symmetry_breaking(pb: &FiniteProblem, model: &mut Model, encod #[allow(unused)] fn print_cond(cid: CondID, pb: &FiniteProblem, model: &Model) { let ch = &pb.chronicles[cid.instance_id]; - let cond = &ch.chronicle.conditions[cid.cond_id]; - let s = model.shape.symbols.format(&[cond.state_var.fluent.sym]); + let state_var = match cid.cond_id { + analysis::CondOrigin::ExplicitCondition(cond_id) => &ch.chronicle.conditions[cond_id].state_var, + analysis::CondOrigin::PostIncrease(eff_id) => &ch.chronicle.effects[eff_id].state_var, + }; + let s = model.shape.symbols.format(&[state_var.fluent.sym]); print!(" {:?}:{}", ch.origin, s) } diff --git a/planning/planners/src/encoding.rs b/planning/planners/src/encoding.rs index 4b7143b4..eb78d286 100644 --- a/planning/planners/src/encoding.rs +++ b/planning/planners/src/encoding.rs @@ -1,5 +1,6 @@ use aries::model::lang::FAtom; use aries::{core::Lit, model::lang::Kind}; +pub use aries_planning::chronicles::analysis::CondOrigin; use aries_planning::chronicles::*; use env_param::EnvParam; use std::collections::{BTreeSet, HashSet}; @@ -16,11 +17,21 @@ pub struct CondID { /// Index of the instance in which the condition appears pub instance_id: ChronicleId, /// Index of the condition in the instance - pub cond_id: usize, + pub cond_id: CondOrigin, } + impl CondID { - pub fn new(instance_id: usize, cond_id: usize) -> Self { - Self { instance_id, cond_id } + pub fn new_explicit(instance_id: usize, cond_id: usize) -> Self { + Self { + instance_id, + cond_id: CondOrigin::ExplicitCondition(cond_id), + } + } + pub fn new_post_increase(instance_id: usize, eff_id: usize) -> Self { + Self { + instance_id, + cond_id: CondOrigin::PostIncrease(eff_id), + } } } @@ -111,7 +122,7 @@ pub fn conditions(pb: &FiniteProblem) -> impl Iterator &Condition { - &self.ch(cond_id.instance_id).chronicle.conditions[cond_id.cond_id] + if let CondOrigin::ExplicitCondition(cond_index) = cond_id.cond_id { + &self.ch(cond_id.instance_id).chronicle.conditions[cond_index] + } else { + panic!("Condition is implicit") + } } fn eff(&self, eff_id: EffID) -> &Effect { &self.ch(eff_id.instance_id).chronicle.effects[eff_id.eff_id] diff --git a/planning/planning/src/chronicles/analysis/detrimental_supports.rs b/planning/planning/src/chronicles/analysis/detrimental_supports.rs index 5268592a..64333ab8 100644 --- a/planning/planning/src/chronicles/analysis/detrimental_supports.rs +++ b/planning/planning/src/chronicles/analysis/detrimental_supports.rs @@ -175,7 +175,16 @@ pub struct TemplateCondID { /// id of the chronicle template in which the condition occurs pub template_id: usize, /// Index of the condition in the template's conditions - pub cond_id: usize, + pub cond_id: CondOrigin, +} + +#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Copy, Clone, Debug)] +pub enum CondOrigin { + /// This originates from the i-th condition of the chronicle + ExplicitCondition(usize), + /// This is a synthetic condition representing the value of the state variables after an increase effect. + /// It is the i-th effect of the chronicle. + PostIncrease(usize), } #[derive(Hash, Copy, Clone, Eq, PartialEq, Debug)] pub struct TemplateEffID { @@ -193,7 +202,7 @@ pub struct CausalSupport { } impl CausalSupport { - pub fn new(eff_template: usize, eff_id: usize, cond_template: usize, cond_id: usize) -> Self { + pub fn new(eff_template: usize, eff_id: usize, cond_template: usize, cond_id: CondOrigin) -> Self { Self { supporter: TemplateEffID { template_id: eff_template, @@ -213,7 +222,7 @@ impl CausalSupport { }, condition: TemplateCondID { template_id: cond.template_id, - cond_id: cond.cond_id, + cond_id: CondOrigin::ExplicitCondition(cond.cond_id), }, } } @@ -377,7 +386,7 @@ fn gather_detrimental_supports( }, condition: TemplateCondID { template_id: c.template_id, - cond_id: c.cond_id, + cond_id: CondOrigin::ExplicitCondition(c.cond_id), }, }); } diff --git a/planning/planning/src/chronicles/analysis/mod.rs b/planning/planning/src/chronicles/analysis/mod.rs index 2985c0e3..ffeada91 100644 --- a/planning/planning/src/chronicles/analysis/mod.rs +++ b/planning/planning/src/chronicles/analysis/mod.rs @@ -10,7 +10,7 @@ mod static_fluents; pub use crate::chronicles::analysis::features::*; use crate::chronicles::preprocessing::action_rolling::RollCompilation; -pub use detrimental_supports::{CausalSupport, TemplateCondID, TemplateEffID}; +pub use detrimental_supports::{CausalSupport, CondOrigin, TemplateCondID, TemplateEffID}; pub use static_fluents::is_static; pub type TemplateID = usize;