Skip to content

Commit

Permalink
add some tests for building domains and problems with numeric fluents…
Browse files Browse the repository at this point in the history
… and action costs
  • Loading branch information
francescofuggitti committed Oct 10, 2023
1 parent 81b7ad1 commit bedbead
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
114 changes: 114 additions & 0 deletions tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,20 @@
from pddl.action import Action
from pddl.core import Domain
from pddl.exceptions import PDDLValidationError
from pddl.formatter import domain_to_string
from pddl.logic import Constant, Variable
from pddl.logic.base import And, Not
from pddl.logic.functions import Decrease
from pddl.logic.functions import EqualTo as FunctionEqualTo
from pddl.logic.functions import (
Function,
GreaterEqualThan,
GreaterThan,
Increase,
LesserEqualThan,
LesserThan,
TotalCost,
)
from pddl.logic.helpers import constants, variables
from pddl.logic.predicates import DerivedPredicate, Predicate
from pddl.parser.symbols import Symbols
Expand Down Expand Up @@ -85,6 +97,108 @@ def test_build_simple_domain():
assert domain


def test_build_simple_domain_with_derived_predicates():
"""Test a simple PDDL domain with derived predicates."""
x, y, z = variables("x y z")
p = Predicate("p", x, y, z)
q = Predicate("q")
r = Predicate("r")
dp = DerivedPredicate(p, And(q, r))
action_1 = Action("action_1", [x, y, z], precondition=p, effect=Not(p))
domain = Domain(
"simple_domain",
predicates={p},
derived_predicates={dp},
actions={action_1},
)
assert domain


def test_build_domain_with_numeric_fluents():
"""Test a PDDL domain with simple numeric fluents."""
x, y, z = variables("x y z")
p = Predicate("p", x, y, z)
q = Predicate("q")
r = Predicate("r")
func1 = Function("f1", x, y)
func2 = Function("f2")
func3 = Function("f3")
action_1 = Action(
"action_1",
[x, y, z],
precondition=p
& FunctionEqualTo(func1, 0)
& GreaterThan(func2, 1)
& LesserThan(func3, 5),
effect=Not(p) | q,
)
action_2 = Action(
"action_2",
[x, y, z],
precondition=r & GreaterEqualThan(func1, 1) & LesserEqualThan(func2, 5),
effect=Not(p) | q,
)
domain = Domain(
"domain_with_numeric",
predicates={p},
functions={func1, func2, func3},
actions={action_1, action_2},
)
assert domain


def test_build_domain_with_action_cost():
"""Test a PDDL domain with action costs."""
x, y, z = variables("x y z")
p = Predicate("p", x, y, z)
q = Predicate("q")
r = Predicate("r")
cost1 = Function("cost1", x, y)
cost2 = Function("cost2")
action_1 = Action(
"action_1",
[x, y, z],
precondition=p & FunctionEqualTo(cost1, 0) & GreaterThan(cost2, 1),
effect=Not(p) & Increase(cost1, 1),
)
action_2 = Action(
"action_2",
[x, y, z],
precondition=r & GreaterEqualThan(cost1, 1),
effect=(Not(p) | q) & Decrease(cost2, 1),
)
domain = Domain(
"domain_with_numeric",
predicates={p},
functions={cost1, cost2},
actions={action_1, action_2},
)
assert domain


def test_build_domain_with_total_cost():
"""Test a PDDL domain with total costs."""
x, y, z = variables("x y z")
p = Predicate("p", x, y, z)
q = Predicate("q")
total_cost = TotalCost()
action_1 = Action(
"action_1",
[x, y, z],
precondition=p & q,
effect=Not(p) & Increase(total_cost, 1),
)
domain = Domain(
"domain_with_total_cost",
requirements={Requirements.ACTION_COSTS},
predicates={p},
functions={total_cost},
actions={action_1},
)
print(domain_to_string(domain))
assert domain


def test_cycles_in_type_defs_not_allowed() -> None:
"""Test that type defs with cycles are not allowed."""
with pytest.raises(
Expand Down
65 changes: 65 additions & 0 deletions tests/test_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@

from pddl.core import Domain, Problem
from pddl.logic.base import And, Not
from pddl.logic.functions import EqualTo as FunctionEqualTo
from pddl.logic.functions import (
Function,
GreaterEqualThan,
GreaterThan,
LesserEqualThan,
LesserThan,
Metric,
TotalCost,
)
from pddl.logic.helpers import constants, variables
from pddl.logic.predicates import Predicate
from tests.conftest import pddl_objects_problems
Expand Down Expand Up @@ -81,3 +91,58 @@ def test_build_simple_problem():
goal=p & q,
)
assert problem


def test_build_problem_with_metric():
"""Test a PDDL problem with metric."""
x, y = variables("x y")
o1, o2 = constants("o1 o2")
p = Predicate("p", x, y)
q = Predicate("q")
total_cost = TotalCost()
problem = Problem(
"simple_problem",
domain_name="simple_domain",
objects=[o1, o2],
init={p, Not(q), FunctionEqualTo(total_cost, 0)},
goal=p & q,
metric=Metric([total_cost]),
)
assert problem


def test_build_problem_with_metric_list():
"""Test a PDDL problem with two functions and metric."""
x, y = variables("x y")
o1, o2 = constants("o1 o2")
p = Predicate("p", x, y)
q = Predicate("q")
cost1 = Function("cost1", x, y)
cost2 = Function("cost2")
problem = Problem(
"simple_problem",
domain_name="simple_domain",
objects=[o1, o2],
init={p, Not(q), FunctionEqualTo(cost1, 0), FunctionEqualTo(cost2, 1)},
goal=p & q & GreaterEqualThan(cost1, 3) & LesserEqualThan(cost2, 10),
metric=Metric([cost1, cost2], Metric.MAXIMIZE),
)
assert problem


def test_build_problem_with_numeric_goal():
"""Test a PDDL problem with numeric fluents in goal."""
x, y = variables("x y")
o1, o2 = constants("o1 o2")
p = Predicate("p", x, y)
q = Predicate("q")
cost1 = Function("cost1", x, y)
cost2 = Function("cost2")
problem = Problem(
"simple_problem",
domain_name="simple_domain",
objects=[o1, o2],
init={p, Not(q), FunctionEqualTo(cost1, 0), FunctionEqualTo(cost2, 10)},
goal=p & q & GreaterThan(cost1, 3) & LesserThan(cost2, 10),
)
assert problem

0 comments on commit bedbead

Please sign in to comment.