Skip to content

Commit

Permalink
increase and decrease support integer variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Yirmandias committed Oct 13, 2023
1 parent a5cbde3 commit 66a7bfa
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
6 changes: 3 additions & 3 deletions planning/grpc/server/src/chronicles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,12 @@ impl<'a> ChronicleFactory<'a> {
let operation = match kind {
EffectKind::Assign => EffectOp::Assign(value),
EffectKind::Increase => {
let value = IntCst::try_from(value).context("Increase effect require a constant value.")?;
let value = IAtom::try_from(value).context("Increase effect require an integer value.")?;
EffectOp::Increase(value)
}
EffectKind::Decrease => {
let value = IntCst::try_from(value).context("Decrease effect require a constant value.")?;
EffectOp::Increase(-value)
let value = IAtom::try_from(value).context("Decrease effect require an integer value.")?;
EffectOp::Decrease(value)
}
};
self.chronicle.effects.push(Effect {
Expand Down
44 changes: 32 additions & 12 deletions planning/planners/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,9 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> std::result::Result
}

// skip if it's two increases
if matches!(e1.operation, EffectOp::Increase(_)) && matches!(e2.operation, EffectOp::Increase(_)) {
if matches!(e1.operation, EffectOp::Increase(_) | EffectOp::Decrease(_))
&& matches!(e2.operation, EffectOp::Increase(_) | EffectOp::Decrease(_))
{
continue;
}

Expand Down Expand Up @@ -847,7 +849,9 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> std::result::Result
if !unifiable_sv(&solver.model, &cond.state_var, &eff.state_var) {
continue;
}
let EffectOp::Assign(effect_value) = eff.operation else { unreachable!() };
let EffectOp::Assign(effect_value) = eff.operation else {
unreachable!()
};
if !solver.model.unifiable(cond.value, effect_value) {
continue;
}
Expand Down Expand Up @@ -976,7 +980,7 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> std::result::Result
.iter()
.filter(|(_, prez, eff)| {
!solver.model.entails(!*prez)
&& matches!(eff.operation, EffectOp::Increase(_))
&& matches!(eff.operation, EffectOp::Increase(_) | EffectOp::Decrease(_))
&& is_integer(&eff.state_var)
})
.collect();
Expand All @@ -988,8 +992,12 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> std::result::Result

// Force the new assigned values to be in the state variable domain.
for &&(_, prez, eff) in &assignments {
let Type::Int { lb, ub } = eff.state_var.fluent.return_type() else { unreachable!() };
let EffectOp::Assign(val) = eff.operation else { unreachable!() };
let Type::Int { lb, ub } = eff.state_var.fluent.return_type() else {
unreachable!()
};
let EffectOp::Assign(val) = eff.operation else {
unreachable!()
};
let val: IAtom = val.try_into().expect("Not integer assignment to an int state variable");
solver.enforce(geq(val, lb), [prez]);
solver.enforce(leq(val, ub), [prez]);
Expand Down Expand Up @@ -1122,9 +1130,13 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> std::result::Result
.implies(prez_cond, solver.model.presence_literal(li_lit.variable())));

// Get the `ci_j*` value.
let EffectOp::Increase(eff_val) = eff.operation else { unreachable!() };
let ci: IAtom = eff_val.into();
(li_lit, ci)
let (ci, sign) = match eff.operation {
EffectOp::Increase(eff_val) => (eff_val, 1),
EffectOp::Decrease(eff_val) => (eff_val, -1),
_ => unreachable!(),
};
// let ci: IAtom = eff_val.into();
(li_lit, ci, sign)
})
.collect::<Vec<_>>()
})
Expand All @@ -1135,8 +1147,12 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> std::result::Result
// Create the sum.
let mut sum = LinearSum::zero();
sum += LinearSum::with_lit(ca, la);
for &(li, ci) in li_ci.iter() {
sum += LinearSum::with_lit(ci, li);
for &(li, ci, sign) in li_ci.iter() {
match sign {
-1 => sum -= LinearSum::with_lit(ci, li),
1 => sum += LinearSum::with_lit(ci, li),
_ => unreachable!(),
}
}
let cond_val =
IAtom::try_from(cond.value).expect("Condition value is not numeric for a numeric fluent");
Expand Down Expand Up @@ -1229,7 +1245,9 @@ fn create_la_vector_without_timepoints(
.implies(prez_cond, solver.model.presence_literal(la_lit.variable())));

// Get the `ca_j` variable.
let EffectOp::Assign(eff_val) = eff.operation else { unreachable!() };
let EffectOp::Assign(eff_val) = eff.operation else {
unreachable!()
};
let ca = IAtom::try_from(eff_val).expect("Try to assign a non-numeric value to a numeric fluent");

// Get the persistence timepoint of the effect `e_j`.
Expand Down Expand Up @@ -1279,7 +1297,9 @@ fn create_la_vector_with_timepoints(
.implies(prez_cond, solver.model.presence_literal(la_lit.variable())));

// Get the `ca_j` variable.
let EffectOp::Assign(eff_val) = eff.operation else { unreachable!() };
let EffectOp::Assign(eff_val) = eff.operation else {
unreachable!()
};
let ca = IAtom::try_from(eff_val).expect("Try to assign a non-numeric value to a numeric fluent");

// Get the persistence timepoint of the effect `e_j`.
Expand Down
20 changes: 14 additions & 6 deletions planning/planning/src/chronicles/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ pub struct Effect {
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum EffectOp {
Assign(Atom),
Increase(IntCst),
Increase(IAtom),
Decrease(IAtom),
}
impl EffectOp {
pub const TRUE_ASSIGNMENT: EffectOp = EffectOp::Assign(Atom::TRUE);
Expand All @@ -305,11 +306,11 @@ impl Debug for EffectOp {
EffectOp::Assign(val) => {
write!(f, ":= {val:?}")
}
EffectOp::Increase(val) if *val >= 0 => {
write!(f, "+= {val:?}")
}
EffectOp::Increase(val) => {
write!(f, "-= {}", -val)
write!(f, "+= {:?}", val)
}
EffectOp::Decrease(val) => {
write!(f, "-= {:?}", val)
}
}
}
Expand Down Expand Up @@ -352,9 +353,15 @@ impl Substitute for EffectOp {
match self {
EffectOp::Assign(val) => EffectOp::Assign(substitution.sub(*val)),
EffectOp::Increase(val) => {
let x: IntCst = *val; // guard: this will need substitution when val becomes a variable
let x = substitution.sub(Atom::from(*val)).try_into().unwrap();
//let x: IntCst = *val; // guard: this will need substitution when val becomes a variable
EffectOp::Increase(x)
}
EffectOp::Decrease(val) => {
let x = substitution.sub(Atom::from(*val)).try_into().unwrap();
//let x: IntCst = *val; // guard: this will need substitution when val becomes a variable
EffectOp::Decrease(x)
}
}
}
}
Expand Down Expand Up @@ -552,6 +559,7 @@ impl Chronicle {
match eff.operation {
EffectOp::Assign(x) => vars.add_atom(x),
EffectOp::Increase(x) => vars.add_atom(x),
EffectOp::Decrease(x) => vars.add_atom(x),
}
vars.add_sv(&eff.state_var)
}
Expand Down
9 changes: 7 additions & 2 deletions planning/planning/src/chronicles/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,15 @@ impl<'a> Printer<'a> {
match op {
EffectOp::Assign(value) => {
print!(" := ");
self.atom(*value)
self.atom(*value);
}
EffectOp::Increase(i) => {
print!(" += {i}")
print!(" += ");
self.atom((*i).into());
}
EffectOp::Decrease(i) => {
print!(" -= ");
self.atom((*i).into());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion planning/planning/src/parsing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ fn read_init(
if closed_world {
// closed world, every predicate that is not given a true value should be given a false value
// to do this, we rely on the classical classical planning state
let state_desc = World::new(context.model.get_symbol_table().deref().clone(), &context.fluents)?;
let state_desc = World::new(context.model.get_symbol_table().clone(), &context.fluents)?;
let mut s = state_desc.make_new_state();
for init in initial_facts {
let pred = read_sv(init, &state_desc)?;
Expand Down

0 comments on commit 66a7bfa

Please sign in to comment.