Skip to content

Commit

Permalink
feat(planning): add a new preprocesing step that rollup actions.
Browse files Browse the repository at this point in the history
It creates a new action that can be used to apply an arbitrary number of times the original action.
  • Loading branch information
arbimo committed Apr 18, 2024
1 parent 5590a2c commit 56d1530
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 36 deletions.
69 changes: 59 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion planning/grpc/server/src/chronicles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ impl<'a> ChronicleFactory<'a> {

pub fn build_template(self, label: String) -> Result<ChronicleTemplate, Error> {
Ok(ChronicleTemplate {
label: Some(label),
label: ChronicleLabel::Action(label),
parameters: self.variables,
chronicle: self.chronicle,
})
Expand Down
27 changes: 13 additions & 14 deletions planning/planners/src/encode/symmetry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::encode::analysis;
use crate::encoding::{ChronicleId, CondID, EffID, Encoding, Tag};
use crate::fmt::format_partial_name;
use analysis::CausalSupport;
use aries::core::Lit;
use aries::model::extensions::AssignmentExt;
Expand Down Expand Up @@ -95,6 +94,7 @@ fn add_plan_space_symmetry_breaking(pb: &FiniteProblem, model: &mut Model, encod

struct ActionOrigin {
template: usize,
#[allow(unused)]
gen: usize,
}
let actions: HashMap<ChronicleId, _> = pb
Expand All @@ -115,10 +115,7 @@ fn add_plan_space_symmetry_breaking(pb: &FiniteProblem, model: &mut Model, encod
_ => None,
})
.collect();
struct Cond {
cond_id: CondID,
lit: Lit,
}

type TemplateID = usize;
let templates = pb
.chronicles
Expand Down Expand Up @@ -186,15 +183,16 @@ fn add_plan_space_symmetry_breaking(pb: &FiniteProblem, model: &mut Model, encod
.sorted()
.collect();

if let Some(ch) = instances.first() {
let ch = &pb.chronicles[**ch];
let s = format_partial_name(&ch.chronicle.name, model).unwrap();
println!("{template_id} {s} ({})", instances.len());
for cond_id in conditions {
print_cond(*cond_id, pb, model);
println!();
}
}
// // detailed printing for debugging
// if let Some(ch) = instances.first() {
// let ch = &pb.chronicles[**ch];
// let s = format_partial_name(&ch.chronicle.name, model).unwrap();
// println!("{template_id} {s} ({})", instances.len());
// for cond_id in conditions {
// print_cond(*cond_id, pb, model);
// println!();
// }
// }

for (i, instance) in instances.iter().copied().enumerate() {
let mut clause = Vec::with_capacity(64);
Expand Down Expand Up @@ -229,6 +227,7 @@ fn add_plan_space_symmetry_breaking(pb: &FiniteProblem, model: &mut Model, encod
// std::process::exit(1)
}

#[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];
Expand Down
1 change: 1 addition & 0 deletions planning/planning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ streaming-iterator = "0.1.5"
regex = { workspace = true }
aries = { path = "../../solver" }
env_param = { path = "../../env_param" }
pathfinding = "4.9.1"
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ fn build_graph(pb: &Problem) {
writeln!(g, "digraph ops {{").unwrap();
for (i, ch) in pb.templates.iter().enumerate() {
println!("{:?}", ch.label);
writeln!(
g,
" {i} [shape=\"rectangle\", label=\"{}\"];",
ch.label.as_ref().unwrap()
)
.unwrap();
writeln!(g, " {i} [shape=\"rectangle\", label=\"{}\"];", &ch.label).unwrap();
println!(" cond:");
for cond in &ch.chronicle.conditions {
let gval = value_of(&cond.state_var.fluent, &cond.state_var.args, cond.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn hierarchy(pb: &Problem) -> HashMap<TemplateID, usize> {
println!("\nAction hierarchy: ");

for (template, lvl) in templates_lvl.iter().sorted_by_key(|(_template, lvl)| **lvl) {
println!(" [{lvl}] {}", pb.templates[*template].label.as_ref().unwrap())
println!(" [{lvl}] {}", pb.templates[*template].label)
}

templates_lvl
Expand Down
7 changes: 6 additions & 1 deletion planning/planning/src/chronicles/concrete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::fmt;
use itertools::Itertools;
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
Expand Down Expand Up @@ -119,7 +120,7 @@ impl Sub {
self.parameters.contains(&v)
}

fn add_untyped(&mut self, param: VarRef, instance: VarRef) -> Result<(), InvalidSubstitution> {
pub fn add_untyped(&mut self, param: VarRef, instance: VarRef) -> Result<(), InvalidSubstitution> {
if self.parameters.contains(&param) {
Err(InvalidSubstitution::DuplicatedEntry(param))
} else {
Expand Down Expand Up @@ -211,6 +212,10 @@ impl Sub {
}
Ok(sub)
}

pub fn replacement_vars(&self) -> impl Iterator<Item = VarRef> + '_ {
self.instances.iter().copied().unique()
}
}
#[derive(Debug)]
pub enum InvalidSubstitution {
Expand Down
21 changes: 19 additions & 2 deletions planning/planning/src/chronicles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use aries::model::symbols::{SymId, SymbolTable, TypedSym};
use aries::model::Model;
use aries::utils::input::Sym;
use env_param::EnvParam;
use std::fmt::Formatter;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::sync::Arc;

Expand Down Expand Up @@ -166,9 +166,26 @@ impl Ctx {
}
}

#[derive(Clone, Debug)]
pub enum ChronicleLabel {
/// Denotes an action with the given name
Action(String),
/// Rolled up version of the action
RolledAction(String),
}

impl Display for ChronicleLabel {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ChronicleLabel::Action(name) => write!(f, "{name}"),
ChronicleLabel::RolledAction(name) => write!(f, "{name}+"),
}
}
}

#[derive(Clone)]
pub struct ChronicleTemplate {
pub label: Option<String>,
pub label: ChronicleLabel,
pub parameters: Vec<Variable>,
pub chronicle: Chronicle,
}
Expand Down
Loading

0 comments on commit 56d1530

Please sign in to comment.