use ferroplan::{solve, Mode, Options};
fn pddl3(dom: &str, prob: &str) -> ferroplan::Solution {
solve(
dom,
prob,
&Options {
mode: Mode::Pddl3,
threads: 1,
..Default::default()
},
)
.unwrap()
}
const MARK: &str = "(define (domain mk)
(:requirements :strips :typing :adl :fluents)
(:types item)
(:predicates (special ?x - item) (can ?x - item))
(:functions (total-cost))
(:action make :parameters (?x - item) :precondition (can ?x) :effect (special ?x)))";
#[test]
fn forall_preference_counts_violated_instances() {
let prob = "(define (problem p) (:domain mk)
(:objects a b c - item)
(:init (can a) (can b))
(:goal (forall (?x - item) (preference p (special ?x))))
(:metric minimize (is-violated p)))";
let sol = pddl3(MARK, prob);
assert!(sol.solved);
assert_eq!(sol.mode, Mode::Pddl3);
assert_eq!(
sol.plan.unwrap().metric,
Some(1.0),
"one unsatisfiable instance -> 1"
);
}
#[test]
fn forall_preference_all_satisfiable_is_zero() {
let prob = "(define (problem p) (:domain mk)
(:objects a b - item)
(:init (can a) (can b))
(:goal (forall (?x - item) (preference p (special ?x))))
(:metric minimize (is-violated p)))";
assert_eq!(pddl3(MARK, prob).plan.unwrap().metric, Some(0.0));
}
#[test]
fn weighted_forall_preferences() {
let dom = "(define (domain mk2)
(:requirements :strips :typing :adl :fluents)
(:types item)
(:predicates (special ?x - item) (gold ?x - item) (can ?x - item))
(:functions (total-cost))
(:action make :parameters (?x - item) :precondition (can ?x) :effect (special ?x)))";
let prob = "(define (problem p) (:domain mk2)
(:objects a b c - item)
(:init (can a) (can b))
(:goal (and (forall (?x - item) (preference p (special ?x)))
(forall (?x - item) (preference q (gold ?x)))))
(:metric minimize (+ (* 1 (is-violated p)) (* 10 (is-violated q)))))";
assert_eq!(pddl3(dom, prob).plan.unwrap().metric, Some(31.0));
}
const SOFTPRE: &str = "(define (domain sp)
(:requirements :strips :adl :fluents)
(:predicates (ready) (done))
(:functions (total-cost))
(:action go :parameters ()
:precondition (preference want (ready))
:effect (done)))";
#[test]
fn precondition_preference_charged_once_when_violated() {
let prob = "(define (problem p) (:domain sp)
(:init) (:goal (done)) (:metric minimize (is-violated want)))";
let sol = pddl3(SOFTPRE, prob);
assert!(sol.solved);
assert_eq!(sol.plan.unwrap().metric, Some(1.0));
}
#[test]
fn precondition_preference_free_when_satisfied() {
let prob = "(define (problem p) (:domain sp)
(:init (ready)) (:goal (done)) (:metric minimize (is-violated want)))";
assert_eq!(pddl3(SOFTPRE, prob).plan.unwrap().metric, Some(0.0));
}