Skip to content

Commit

Permalink
feat(resource): support linear sum values
Browse files Browse the repository at this point in the history
  • Loading branch information
Shi-Raida committed Oct 16, 2023
1 parent 66a7bfa commit 9dee57d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 46 deletions.
4 changes: 2 additions & 2 deletions planning/grpc/server/src/chronicles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@ impl<'a> ChronicleFactory<'a> {
EffectKind::Assign => EffectOp::Assign(value),
EffectKind::Increase => {
let value = IAtom::try_from(value).context("Increase effect require an integer value.")?;
EffectOp::Increase(value)
EffectOp::Increase(LinearSum::from(value))
}
EffectKind::Decrease => {
let value = IAtom::try_from(value).context("Decrease effect require an integer value.")?;
EffectOp::Decrease(value)
EffectOp::Increase(-LinearSum::from(value))
}
};
self.chronicle.effects.push(Effect {
Expand Down
23 changes: 6 additions & 17 deletions planning/planners/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,7 @@ pub fn encode(pb: &FiniteProblem, metric: Option<Metric>) -> std::result::Result
}

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

Expand Down Expand Up @@ -980,7 +978,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(_) | EffectOp::Decrease(_))
&& matches!(eff.operation, EffectOp::Increase(_))
&& is_integer(&eff.state_var)
})
.collect();
Expand Down Expand Up @@ -1130,13 +1128,8 @@ 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 (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)
let EffectOp::Increase(eff_val) = eff.operation.clone() else { unreachable!() };
(li_lit, eff_val)
})
.collect::<Vec<_>>()
})
Expand All @@ -1147,12 +1140,8 @@ 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, sign) in li_ci.iter() {
match sign {
-1 => sum -= LinearSum::with_lit(ci, li),
1 => sum += LinearSum::with_lit(ci, li),
_ => unreachable!(),
}
for (li, ci) in li_ci.iter() {
sum += LinearSum::with_lit(ci.clone(), *li)
}
let cond_val =
IAtom::try_from(cond.value).expect("Condition value is not numeric for a numeric fluent");
Expand Down
43 changes: 22 additions & 21 deletions planning/planning/src/chronicles/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,10 @@ pub struct Effect {
pub operation: EffectOp,
}

#[derive(Clone, Copy, Eq, PartialEq)]
#[derive(Clone, Eq, PartialEq)]
pub enum EffectOp {
Assign(Atom),
Increase(IAtom),
Decrease(IAtom),
Increase(LinearSum),
}
impl EffectOp {
pub const TRUE_ASSIGNMENT: EffectOp = EffectOp::Assign(Atom::TRUE);
Expand All @@ -307,10 +306,7 @@ impl Debug for EffectOp {
write!(f, ":= {val:?}")
}
EffectOp::Increase(val) => {
write!(f, "+= {:?}", val)
}
EffectOp::Decrease(val) => {
write!(f, "-= {:?}", val)
write!(f, "+= {:?}", val.simplify())
}
}
}
Expand Down Expand Up @@ -352,16 +348,7 @@ impl Substitute for EffectOp {
fn substitute(&self, substitution: &impl Substitution) -> Self {
match self {
EffectOp::Assign(val) => EffectOp::Assign(substitution.sub(*val)),
EffectOp::Increase(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::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)
}
EffectOp::Increase(val) => EffectOp::Increase(substitution.sub_linear_sum(val)),
}
}
}
Expand Down Expand Up @@ -534,6 +521,21 @@ impl VarSet {
self.add_atom(*a)
}
}

fn add_linear_term(&mut self, term: &LinearTerm) {
self.add_atom(term.var());
self.add_atom(term.factor());
self.add_atom(term.denom());
self.add_lit(term.lit());
}

fn add_linear_sum(&mut self, sum: &LinearSum) {
self.add_atom(sum.constant());
self.add_atom(sum.denom());
for term in sum.terms() {
self.add_linear_term(term);
}
}
}

impl Chronicle {
Expand All @@ -556,10 +558,9 @@ impl Chronicle {
for eff in &self.effects {
vars.add_atom(eff.transition_start);
vars.add_atom(eff.persistence_start);
match eff.operation {
EffectOp::Assign(x) => vars.add_atom(x),
EffectOp::Increase(x) => vars.add_atom(x),
EffectOp::Decrease(x) => vars.add_atom(x),
match &eff.operation {
EffectOp::Assign(x) => vars.add_atom(*x),
EffectOp::Increase(x) => vars.add_linear_sum(x),
}
vars.add_sv(&eff.state_var)
}
Expand Down
8 changes: 2 additions & 6 deletions planning/planning/src/chronicles/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,9 @@ impl<'a> Printer<'a> {
print!(" := ");
self.atom(*value);
}
EffectOp::Increase(i) => {
EffectOp::Increase(sum) => {
print!(" += ");
self.atom((*i).into());
}
EffectOp::Decrease(i) => {
print!(" -= ");
self.atom((*i).into());
self.linear_sum(sum);
}
}
}
Expand Down

0 comments on commit 9dee57d

Please sign in to comment.