Skip to content

Commit

Permalink
fix(planning): correctly represent synthetic conditions introduced fo…
Browse files Browse the repository at this point in the history
…r numerics
  • Loading branch information
arbimo committed Dec 17, 2024
1 parent 744d539 commit dcc73e8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
4 changes: 1 addition & 3 deletions planning/planners/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,6 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> 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));
Expand All @@ -775,7 +774,7 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> 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,
Expand All @@ -784,7 +783,6 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> std::result::Result
value: var.into(),
},
));
next_cond_id += 1;
num_numeric_increase_coherence_constraints += 1;
}

Expand Down
7 changes: 5 additions & 2 deletions planning/planners/src/encode/symmetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 15 additions & 4 deletions planning/planners/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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),
}
}
}

Expand Down Expand Up @@ -111,7 +122,7 @@ pub fn conditions(pb: &FiniteProblem) -> impl Iterator<Item = (CondID, Lit, &Con
.conditions
.iter()
.enumerate()
.map(move |(cond_id, cond)| (CondID::new(instance_id, cond_id), ch.chronicle.presence, cond))
.map(move |(cond_id, cond)| (CondID::new_explicit(instance_id, cond_id), ch.chronicle.presence, cond))
})
}

Expand Down
7 changes: 6 additions & 1 deletion planning/planners/src/search/causal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use aries::model::lang::{SAtom, Type};
use aries::model::symbols::TypedSym;
use aries::solver::search::{Decision, SearchControl};
use aries::solver::stats::Stats;
use aries_planning::chronicles::analysis::CondOrigin;
use aries_planning::chronicles::{ChronicleInstance, Condition, Effect, FiniteProblem, StateVar, VarLabel};
use env_param::EnvParam;
use std::collections::HashSet;
Expand Down Expand Up @@ -36,7 +37,11 @@ impl ManualCausalSearch {
&self.problem.chronicles[chronicle_id]
}
fn cond(&self, cond_id: CondID) -> &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]
Expand Down
17 changes: 13 additions & 4 deletions planning/planning/src/chronicles/analysis/detrimental_supports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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),
},
}
}
Expand Down Expand Up @@ -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),
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion planning/planning/src/chronicles/analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dcc73e8

Please sign in to comment.