use std::collections::HashMap;
use pddl_runtime::{Comparison, Expr, Formula};
use crate::{
AtomicFormula, BinaryComparison, BinaryOp, FluentExpression, FunctionHead, GoalDescription,
InitElement, Literal, MetricFluentExpression, MultiOp, Name, Optimization,
PreconditionGoalDescription, PreconditionGoalDescriptions, PreferenceGoalDescription, Problem,
Term, Type, TypedVariables,
};
#[derive(Debug)]
pub struct Signature {
pub domain: &'static str,
pub types: &'static [TypeSignature],
pub predicates: &'static [Symbol],
pub functions: &'static [Symbol],
pub constants: &'static [(&'static str, usize)],
}
#[derive(Debug)]
pub struct TypeSignature {
pub name: &'static str,
pub parent: Option<usize>,
}
#[derive(Debug)]
pub struct Symbol {
pub name: &'static str,
pub args: &'static [usize],
}
#[derive(Debug, Clone)]
pub struct Loaded {
pub names: Vec<String>,
pub starts: Vec<u32>,
pub ends: Vec<u32>,
pub constants: Vec<u32>,
pub facts: Vec<(usize, Vec<u32>)>,
pub values: Vec<(usize, Vec<u32>, f64)>,
pub goal: Formula,
pub minimize_total_cost: bool,
}
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum LoadError {
#[error("the problem is for domain `{found}`, not `{expected}`")]
WrongDomain {
expected: &'static str,
found: String,
},
#[error("unknown type `{0}`")]
UnknownType(String),
#[error("object `{0}` has more than one type")]
MultipleTypes(String),
#[error("unknown object `{0}`")]
UnknownObject(String),
#[error("unknown predicate or function `{0}`")]
UnknownSymbol(String),
#[error("`{0}` has the wrong number of arguments")]
WrongArity(String),
#[error("`{object}` is not of the type `{expected}` that `{symbol}` takes")]
WrongType {
symbol: String,
object: String,
expected: &'static str,
},
#[error("unknown variable `{0}`")]
UnknownVariable(String),
#[error("unsupported in a problem: {0}")]
Unsupported(&'static str),
#[error("`{function}` is {value}, but action costs must be non-negative integers")]
InvalidCost {
function: String,
value: f64,
},
}
fn lc(name: &str) -> String {
name.to_lowercase()
}
pub fn load(signature: &Signature, problem: &Problem) -> Result<Loaded, LoadError> {
if lc(problem.domain()) != signature.domain {
return Err(LoadError::WrongDomain {
expected: signature.domain,
found: problem.domain().to_string(),
});
}
if !problem.constraints().is_empty() {
return Err(LoadError::Unsupported("constraints"));
}
let minimize_total_cost = match problem.metric_spec() {
None => false,
Some(metric) => match (metric.optimization(), metric.expression()) {
(Optimization::Minimize, MetricFluentExpression::Function(symbol, args))
if args.is_empty() && lc(symbol) == "total-cost" =>
{
true
}
_ => {
return Err(LoadError::Unsupported(
"metrics other than minimizing total-cost",
))
}
},
};
let loader = Loader::new(signature, problem)?;
let mut facts = Vec::new();
let mut values = Vec::new();
for element in problem.init().iter() {
match element {
InitElement::Literal(Literal::AtomicFormula(AtomicFormula::Predicate(atom))) => {
let names: Vec<&Name> = atom.values().iter().collect();
let (pred, args) = loader.symbol(true, atom.predicate(), &names)?;
facts.push((pred, args));
}
InitElement::Literal(Literal::NotAtomicFormula(AtomicFormula::Predicate(_))) => {}
InitElement::Literal(_) => {
return Err(LoadError::Unsupported("equality in the initial state"))
}
InitElement::At(..) => return Err(LoadError::Unsupported("timed initial literals")),
InitElement::IsValue(head, value) => {
if lc(head.symbol()) == "total-cost" && head.names().is_empty() {
continue; }
let names: Vec<&Name> = head.names().iter().collect();
let (func, args) = loader.symbol(false, head.symbol(), &names)?;
values.push((func, args, **value));
}
InitElement::IsObject(..) => return Err(LoadError::Unsupported("object fluents")),
}
}
let goal = loader.goals(problem.goals(), &HashMap::new())?;
Ok(Loaded {
names: loader.names,
starts: loader.starts,
ends: loader.ends,
constants: loader.constants,
facts,
values,
goal,
minimize_total_cost,
})
}
type Scope = HashMap<String, u32>;
type OnBinding<'f> = dyn FnMut(&Scope) -> Result<(), LoadError> + 'f;
struct Loader<'a> {
signature: &'a Signature,
names: Vec<String>,
numbers: HashMap<String, u32>,
starts: Vec<u32>,
ends: Vec<u32>,
constants: Vec<u32>,
}
impl<'a> Loader<'a> {
fn new(signature: &'a Signature, problem: &Problem) -> Result<Self, LoadError> {
let type_index = |ty: &Type, object: &str| -> Result<usize, LoadError> {
let name = match ty {
Type::Exactly(name) => lc(name),
Type::EitherOf(names) if names.len() == 1 => lc(&names[0]),
Type::EitherOf(_) => return Err(LoadError::MultipleTypes(object.to_string())),
};
signature
.types
.iter()
.position(|t| t.name == name)
.ok_or(LoadError::UnknownType(name))
};
let mut declared: Vec<(String, usize)> = signature
.constants
.iter()
.map(|&(name, ty)| (name.to_string(), ty))
.collect();
for typed in problem.objects().iter() {
let name = lc(typed.value());
let ty = type_index(typed.type_(), &name)?;
match declared.iter().find(|(n, _)| *n == name) {
Some(&(_, other)) if other == ty => {} Some(_) => return Err(LoadError::MultipleTypes(name)),
None => declared.push((name, ty)),
}
}
let count = signature.types.len();
let mut starts = vec![0; count];
let mut ends = vec![0; count];
let mut names = Vec::new();
for (t, start) in starts.iter_mut().enumerate() {
*start = u32::try_from(names.len()).expect("fewer than 2^32 objects");
names.extend(
declared
.iter()
.filter(|(_, ty)| *ty == t)
.map(|(n, _)| n.clone()),
);
ends[t] = u32::try_from(names.len()).expect("fewer than 2^32 objects");
}
for t in (0..count).rev() {
if let Some(parent) = signature.types[t].parent {
ends[parent] = ends[parent].max(ends[t]);
}
}
let numbers: HashMap<String, u32> = names
.iter()
.enumerate()
.map(|(i, n)| {
(
n.clone(),
u32::try_from(i).expect("fewer than 2^32 objects"),
)
})
.collect();
let constants = signature
.constants
.iter()
.map(|(name, _)| numbers[*name])
.collect();
Ok(Self {
signature,
names,
numbers,
starts,
ends,
constants,
})
}
fn object(&self, name: &str) -> Result<u32, LoadError> {
let name = lc(name);
self.numbers
.get(&name)
.copied()
.ok_or(LoadError::UnknownObject(name))
}
fn symbol(
&self,
predicate: bool,
name: &str,
args: &[&Name],
) -> Result<(usize, Vec<u32>), LoadError> {
let args = args
.iter()
.map(|a| self.object(a))
.collect::<Result<Vec<_>, _>>()?;
self.check(predicate, name, args)
}
fn check(
&self,
predicate: bool,
name: &str,
args: Vec<u32>,
) -> Result<(usize, Vec<u32>), LoadError> {
let name = lc(name);
let symbols = if predicate {
self.signature.predicates
} else {
self.signature.functions
};
let index = symbols
.iter()
.position(|s| s.name == name)
.ok_or_else(|| LoadError::UnknownSymbol(name.clone()))?;
let symbol = &symbols[index];
if symbol.args.len() != args.len() {
return Err(LoadError::WrongArity(name));
}
for (&arg, &ty) in args.iter().zip(symbol.args) {
if !(self.starts[ty]..self.ends[ty]).contains(&arg) {
return Err(LoadError::WrongType {
symbol: name,
object: self.names[arg as usize].clone(),
expected: self.signature.types[ty].name,
});
}
}
Ok((index, args))
}
fn term(&self, term: &Term, scope: &HashMap<String, u32>) -> Result<u32, LoadError> {
match term {
Term::Name(name) => self.object(name),
Term::Variable(var) => {
let name = lc(var);
scope
.get(&name)
.copied()
.ok_or(LoadError::UnknownVariable(name))
}
Term::Function(_) => Err(LoadError::Unsupported("object fluents")),
}
}
fn each_binding(
&self,
vars: &TypedVariables,
scope: &HashMap<String, u32>,
f: &mut OnBinding<'_>,
) -> Result<(), LoadError> {
let mut domains = Vec::new();
for var in vars.iter() {
let mut objects = Vec::new();
for ty in var.type_().clone() {
let name = lc(&ty);
let t = self
.signature
.types
.iter()
.position(|t| t.name == name)
.ok_or(LoadError::UnknownType(name))?;
objects.extend(self.starts[t]..self.ends[t]);
}
objects.sort_unstable();
objects.dedup();
domains.push((lc(var.value()), objects));
}
bind(&domains, &mut scope.clone(), f)
}
fn goals(
&self,
goals: &PreconditionGoalDescriptions,
scope: &HashMap<String, u32>,
) -> Result<Formula, LoadError> {
let mut parts = Vec::new();
for goal in goals {
match goal {
PreconditionGoalDescription::Preference(PreferenceGoalDescription::Goal(gd)) => {
parts.push(self.goal(gd, scope)?);
}
PreconditionGoalDescription::Preference(PreferenceGoalDescription::Preference(
_,
)) => return Err(LoadError::Unsupported("preferences")),
PreconditionGoalDescription::Forall(vars, inner) => {
self.each_binding(vars, scope, &mut |s| {
parts.push(self.goals(inner, s)?);
Ok(())
})?;
}
}
}
Ok(flatten_and(parts))
}
fn goal(
&self,
gd: &GoalDescription,
scope: &HashMap<String, u32>,
) -> Result<Formula, LoadError> {
Ok(match gd {
GoalDescription::AtomicFormula(formula)
| GoalDescription::Literal(Literal::AtomicFormula(formula)) => {
self.atom(formula, scope)?
}
GoalDescription::Literal(Literal::NotAtomicFormula(formula)) => {
Formula::Not(Box::new(self.atom(formula, scope)?))
}
GoalDescription::Not(inner) => Formula::Not(Box::new(self.goal(inner, scope)?)),
GoalDescription::And(parts) => flatten_and(
parts
.iter()
.map(|p| self.goal(p, scope))
.collect::<Result<_, _>>()?,
),
GoalDescription::Or(parts) => Formula::Or(
parts
.iter()
.map(|p| self.goal(p, scope))
.collect::<Result<_, _>>()?,
),
GoalDescription::Imply(a, b) => Formula::Or(vec![
Formula::Not(Box::new(self.goal(a, scope)?)),
self.goal(b, scope)?,
]),
GoalDescription::Exists(vars, inner) => {
let mut parts = Vec::new();
self.each_binding(vars, scope, &mut |s| {
parts.push(self.goal(inner, s)?);
Ok(())
})?;
Formula::Or(parts)
}
GoalDescription::ForAll(vars, inner) => {
let mut parts = Vec::new();
self.each_binding(vars, scope, &mut |s| {
parts.push(self.goal(inner, s)?);
Ok(())
})?;
flatten_and(parts)
}
GoalDescription::FluentComparison(comparison) => Formula::Compare(
match comparison.comparison() {
BinaryComparison::LessThan => Comparison::Less,
BinaryComparison::LessThanOrEqual => Comparison::LessOrEqual,
BinaryComparison::Equal => Comparison::Equal,
BinaryComparison::GreaterOrEqual => Comparison::GreaterOrEqual,
BinaryComparison::GreaterThan => Comparison::Greater,
},
self.expr(comparison.first(), scope)?,
self.expr(comparison.second(), scope)?,
),
})
}
fn atom(
&self,
formula: &AtomicFormula<Term>,
scope: &HashMap<String, u32>,
) -> Result<Formula, LoadError> {
Ok(match formula {
AtomicFormula::Equality(eq) => match (eq.first(), eq.second()) {
(Term::Function(a), Term::Function(b)) => Formula::Compare(
Comparison::Equal,
self.fluent(a.symbol(), a.terms(), scope)?,
self.fluent(b.symbol(), b.terms(), scope)?,
),
(a, b) => {
if self.term(a, scope)? == self.term(b, scope)? {
Formula::True
} else {
Formula::False
}
}
},
AtomicFormula::Predicate(atom) => {
let args = atom
.values()
.iter()
.map(|t| self.term(t, scope))
.collect::<Result<Vec<_>, _>>()?;
match self.check(true, atom.predicate(), args) {
Ok((pred, args)) => Formula::Atom {
pred,
args: args.into(),
},
Err(LoadError::WrongType { .. }) => Formula::False,
Err(e) => return Err(e),
}
}
})
}
fn fluent(
&self,
symbol: &str,
terms: &[Term],
scope: &HashMap<String, u32>,
) -> Result<Expr, LoadError> {
let args = terms
.iter()
.map(|t| self.term(t, scope))
.collect::<Result<Vec<_>, _>>()?;
let (func, args) = self.check(false, symbol, args)?;
Ok(Expr::Fluent {
func,
args: args.into(),
})
}
fn expr(
&self,
expr: &FluentExpression,
scope: &HashMap<String, u32>,
) -> Result<Expr, LoadError> {
let boxed = |e: &FluentExpression| self.expr(e, scope).map(Box::new);
Ok(match expr {
FluentExpression::Number(n) => Expr::Number(**n),
FluentExpression::Function(head) => {
let (symbol, terms): (&Name, &[Term]) = match head {
FunctionHead::Simple(symbol) => (symbol, &[]),
FunctionHead::WithTerms(symbol, terms) => (symbol, terms),
};
self.fluent(symbol, terms, scope)?
}
FluentExpression::Negative(inner) => Expr::Neg(boxed(inner)?),
FluentExpression::BinaryOp(op, a, b) => {
let (a, b) = (boxed(a)?, boxed(b)?);
match op {
BinaryOp::Addition => Expr::Add(a, b),
BinaryOp::Subtraction => Expr::Sub(a, b),
BinaryOp::Multiplication => Expr::Mul(a, b),
BinaryOp::Division => Expr::Div(a, b),
}
}
FluentExpression::MultiOp(op, first, rest) => {
let mut acc = self.expr(first, scope)?;
for e in rest {
let e = boxed(e)?;
acc = match op {
MultiOp::Addition => Expr::Add(Box::new(acc), e),
MultiOp::Multiplication => Expr::Mul(Box::new(acc), e),
};
}
acc
}
})
}
}
fn bind(
domains: &[(String, Vec<u32>)],
scope: &mut HashMap<String, u32>,
f: &mut OnBinding<'_>,
) -> Result<(), LoadError> {
let Some(((name, objects), rest)) = domains.split_first() else {
return f(scope);
};
let shadowed = scope.get(name).copied();
for &object in objects {
scope.insert(name.clone(), object);
bind(rest, scope, f)?;
}
match shadowed {
Some(object) => scope.insert(name.clone(), object),
None => scope.remove(name),
};
Ok(())
}
fn flatten_and(parts: Vec<Formula>) -> Formula {
let mut flat = Vec::new();
for part in parts {
match part {
Formula::And(inner) => flat.extend(inner),
other => flat.push(other),
}
}
Formula::And(flat)
}
#[cfg(all(test, feature = "parser"))]
mod tests {
use super::*;
use crate::Parser;
static SIGNATURE: Signature = Signature {
domain: "d",
types: &[
TypeSignature {
name: "object",
parent: None,
},
TypeSignature {
name: "vehicle",
parent: Some(0),
},
TypeSignature {
name: "truck",
parent: Some(1),
},
TypeSignature {
name: "place",
parent: Some(0),
},
],
predicates: &[Symbol {
name: "at",
args: &[1, 3],
}],
functions: &[Symbol {
name: "fuel",
args: &[1],
}],
constants: &[("depot", 3)],
};
fn load_str(text: &str) -> Result<Loaded, LoadError> {
load(&SIGNATURE, &Problem::from_str(text).unwrap())
}
#[test]
fn numbers_subtypes_inside_their_supertypes() {
let loaded = load_str(
"(define (problem p) (:domain d)
(:objects t1 - truck v1 - vehicle shop - place t2 - truck depot - place)
(:init (at t1 depot) (= (fuel t2) 3) (= (total-cost) 0))
(:goal (and (at t2 shop) (forall (?v - vehicle) (>= (fuel ?v) 1))))
(:metric minimize (total-cost)))",
)
.unwrap();
assert_eq!(loaded.names, ["v1", "t1", "t2", "depot", "shop"]);
assert_eq!(loaded.starts, [0, 0, 1, 3]);
assert_eq!(loaded.ends, [5, 3, 3, 5]);
assert_eq!(loaded.constants, [3]);
assert_eq!(loaded.facts, [(0, vec![1, 3])]);
assert_eq!(loaded.values, [(0, vec![2], 3.0)]);
assert!(loaded.minimize_total_cost);
let Formula::And(goal) = &loaded.goal else {
panic!("{:?}", loaded.goal)
};
assert_eq!(goal.len(), 4, "{goal:?}");
}
#[test]
fn rejects_problems_that_do_not_fit() {
let problem = |objects: &str, init: &str, goal: &str| {
load_str(&format!(
"(define (problem p) (:domain d) (:objects {objects}) (:init {init}) (:goal {goal}))"
))
};
assert_eq!(
problem("x - robot", "", "(and)").unwrap_err(),
LoadError::UnknownType("robot".into())
);
assert_eq!(
problem("x - (either truck place)", "", "(and)").unwrap_err(),
LoadError::MultipleTypes("x".into())
);
assert_eq!(
problem("depot - truck", "", "(and)").unwrap_err(),
LoadError::MultipleTypes("depot".into())
);
assert_eq!(
problem("t - truck", "(at t nowhere)", "(and)").unwrap_err(),
LoadError::UnknownObject("nowhere".into())
);
assert_eq!(
problem("t - truck", "(at depot t)", "(and)").unwrap_err(),
LoadError::WrongType {
symbol: "at".into(),
object: "depot".into(),
expected: "vehicle"
}
);
assert_eq!(
problem("t - truck", "(on t)", "(and)").unwrap_err(),
LoadError::UnknownSymbol("on".into())
);
assert_eq!(
problem("t - truck", "(at t)", "(and)").unwrap_err(),
LoadError::WrongArity("at".into())
);
assert_eq!(
problem("t - truck", "(at 5 (at t depot))", "(and)").unwrap_err(),
LoadError::Unsupported("timed initial literals")
);
assert_eq!(
problem("t - truck", "", "(at ?x depot)").unwrap_err(),
LoadError::UnknownVariable("x".into())
);
assert!(matches!(
load_str("(define (problem p) (:domain other) (:init) (:goal (and)))"),
Err(LoadError::WrongDomain { .. })
));
assert_eq!(
load_str("(define (problem p) (:domain d) (:init) (:goal (and)) (:metric maximize (total-cost)))")
.unwrap_err(),
LoadError::Unsupported("metrics other than minimizing total-cost")
);
}
#[test]
fn reads_every_init_and_goal_form() {
let loaded = load_str(
"(define (problem p) (:domain d) (:objects t - truck depot - place)
(:init (at t depot) (not (at t depot)) (= (fuel t) 2))
(:goal (and (not (at t depot))
(forall (?v - vehicle) (exists (?p - place) (at ?v ?p)))
(<= (- (fuel t)) (* (fuel t) 2 3))
(= (+ (fuel t) 1 1) 4)
(> (/ (fuel t) 2) 0))))",
)
.unwrap();
assert_eq!(loaded.names, ["t", "depot"]);
let fact = |_: usize, _: &[u32]| true;
let value = |_: usize, _: &[u32]| Some(2.0);
let Formula::And(goal) = &loaded.goal else {
panic!("{:?}", loaded.goal)
};
assert!(!goal[0].holds(&fact, &value));
assert!(goal[1..].iter().all(|g| g.holds(&fact, &value)), "{goal:?}");
}
#[test]
fn reads_an_equality_of_two_functions_as_numeric() {
let loaded = load_str(
"(define (problem p) (:domain d) (:objects t u - truck)
(:init) (:goal (= (fuel t) (fuel u))))",
)
.unwrap();
let value = |_: usize, args: &[u32]| Some(f64::from(args[0]));
assert!(!loaded.goal.holds(&|_, _| false, &value));
assert!(loaded.goal.holds(&|_, _| false, &|_, _| Some(1.0)));
}
#[test]
fn rejects_unsupported_problem_constructs() {
let problem = |rest: &str| {
load_str(&format!(
"(define (problem p) (:domain d) (:objects t - truck) {rest})"
))
};
assert_eq!(
problem("(:init) (:goal (and)) (:constraints (always (at t depot)))").unwrap_err(),
LoadError::Unsupported("constraints")
);
assert_eq!(
problem("(:init (= (owner t) depot)) (:goal (and))").unwrap_err(),
LoadError::Unsupported("object fluents")
);
assert_eq!(
problem("(:init) (:goal (preference g (at t depot)))").unwrap_err(),
LoadError::Unsupported("preferences")
);
assert_eq!(
problem("(:init) (:goal (exists (?x - robot) (at t ?x)))").unwrap_err(),
LoadError::UnknownType("robot".into())
);
assert_eq!(
problem("(:init) (:goal (at t nowhere))").unwrap_err(),
LoadError::UnknownObject("nowhere".into())
);
assert_eq!(
problem("(:init (= (speed t) 1)) (:goal (and))").unwrap_err(),
LoadError::UnknownSymbol("speed".into())
);
}
#[test]
fn grounds_goal_connectives() {
let loaded = load_str(
"(define (problem p) (:domain d) (:objects t - truck)
(:init (at t depot))
(:goal (and (or (at t depot) (not (= t depot)))
(imply (at t depot) (exists (?p - place) (at t ?p)))
(at depot depot)
(< (- (* 2 (fuel t) 1) (/ 4 (fuel t))) (+ 1 (fuel t))))))",
)
.unwrap();
let fact = |pred: usize, args: &[u32]| {
loaded
.facts
.iter()
.any(|(p, a)| *p == pred && a.as_slice() == args)
};
let Formula::And(parts) = &loaded.goal else {
panic!()
};
assert_eq!(parts[2], Formula::False);
assert!(parts[0].holds(&fact, &|_, _| None));
assert!(parts[1].holds(&fact, &|_, _| None));
assert!(parts[3].holds(&fact, &|_, _| Some(2.0)));
assert!(!parts[3].holds(&fact, &|_, _| None));
}
}