Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert tests to unittest #68

Merged
merged 3 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ If you plan to use PDDLGym for non-trivial domains, you will almost certainly ne

See `pddlgym/pddl/blocks.pddl` and `pddlgym/pddl/blocks/problem1.pddl` for an example to follow, where there are four action predicates: pickup, putdown, stack, and unstack.

## Running unit tests

From the root of the repo, run

```sh
python -m unittest discover pddlgym/tests/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirmed that this works as expected 👍

```

## Citation

Please use this bibtex if you want to cite this repository in your publications:
Expand Down
155 changes: 76 additions & 79 deletions pddlgym/tests/test_inference.py
Original file line number Diff line number Diff line change
@@ -1,112 +1,109 @@
from pddlgym.inference import find_satisfying_assignments
from pddlgym.structs import Predicate, Type, Not

import unittest

def test_prover():
TType = Type('t')
atom0, atom1, atom2 = TType('atom0'), TType('atom1'), TType('atom2')
var0, var1, var2, var3 = TType('Var0'), TType('Var1'), TType('Var2'), TType('Var3')
class TestProver(unittest.TestCase):
def test_prover(self):
TType = Type('t')
atom0, atom1, atom2 = TType('atom0'), TType('atom1'), TType('atom2')
var0, var1, var2, var3 = TType('Var0'), TType('Var1'), TType('Var2'), TType('Var3')

# Single predicate single arity test
predicate0 = Predicate('Predicate0', 1, [TType])
predicate1 = Predicate('Predicate1', 2, [TType, TType])
predicate2 = Predicate('Predicate2', 1, [TType])
# Single predicate single arity test
predicate0 = Predicate('Predicate0', 1, [TType])
predicate1 = Predicate('Predicate1', 2, [TType, TType])
predicate2 = Predicate('Predicate2', 1, [TType])

kb0 = [predicate0(atom0)]
assignments = find_satisfying_assignments(kb0, [predicate0(var0)])
assert len(assignments) == 1
assert len(assignments[0]) == 1
assert assignments[0][var0] == atom0
kb0 = [predicate0(atom0)]
assignments = find_satisfying_assignments(kb0, [predicate0(var0)])
assert len(assignments) == 1
assert len(assignments[0]) == 1
assert assignments[0][var0] == atom0

assignments = find_satisfying_assignments(kb0, [predicate0(var0), predicate0(var1)])
assert len(assignments) == 1
assignments = find_satisfying_assignments(kb0, [predicate0(var0), predicate0(var1)])
assert len(assignments) == 1

kb1 = [predicate0(atom0), predicate0(atom1)]
kb1 = [predicate0(atom0), predicate0(atom1)]

assignments = find_satisfying_assignments(kb1, [predicate0(var0)])
assert len(assignments) == 2
assignments = find_satisfying_assignments(kb1, [predicate0(var0)])
assert len(assignments) == 2

assignments = find_satisfying_assignments(kb1, [predicate0(var0), predicate0(var1)])
assert len(assignments) == 2
assignments = find_satisfying_assignments(kb1, [predicate0(var0), predicate0(var1)])
assert len(assignments) == 2

assignments = find_satisfying_assignments(kb1, [predicate0(var0), predicate0(var1), predicate0(var2)])
assert len(assignments) == 2
assignments = find_satisfying_assignments(kb1, [predicate0(var0), predicate0(var1), predicate0(var2)])
assert len(assignments) == 2

kb2 = [predicate0(atom0), predicate0(atom1), predicate0(atom2)]
kb2 = [predicate0(atom0), predicate0(atom1), predicate0(atom2)]

assignments = find_satisfying_assignments(kb2, [predicate0(var0)])
assert len(assignments) == 2
assignments = find_satisfying_assignments(kb2, [predicate0(var0)])
assert len(assignments) == 2

assignments = find_satisfying_assignments(kb2, [predicate0(var0), predicate0(var1)])
assert len(assignments) == 2
assignments = find_satisfying_assignments(kb2, [predicate0(var0), predicate0(var1)])
assert len(assignments) == 2

assignments = find_satisfying_assignments(kb2, [predicate0(var0), predicate0(var1), predicate0(var2)])
assert len(assignments) == 2
assignments = find_satisfying_assignments(kb2, [predicate0(var0), predicate0(var1), predicate0(var2)])
assert len(assignments) == 2

# Single predicate multiple arity test
kb3 = [predicate1(atom0, atom1), predicate1(atom1, atom2)]
# Single predicate multiple arity test
kb3 = [predicate1(atom0, atom1), predicate1(atom1, atom2)]

assignments = find_satisfying_assignments(kb3, [predicate1(var0, var1)])
assert len(assignments) == 2
assignments = find_satisfying_assignments(kb3, [predicate1(var0, var1)])
assert len(assignments) == 2

assignments = find_satisfying_assignments(kb3, [predicate1(var0, var1), predicate1(var1, var2)])
assert len(assignments) == 1
assignments = find_satisfying_assignments(kb3, [predicate1(var0, var1), predicate1(var1, var2)])
assert len(assignments) == 1

assignments = find_satisfying_assignments(kb3, [predicate1(var0, var1), predicate1(var1, var0)])
assert len(assignments) == 0
assignments = find_satisfying_assignments(kb3, [predicate1(var0, var1), predicate1(var1, var0)])
assert len(assignments) == 0

assignments = find_satisfying_assignments(kb3, [predicate1(var0, var1), predicate1(var2, var3)])
assert len(assignments) == 2
assignments = find_satisfying_assignments(kb3, [predicate1(var0, var1), predicate1(var2, var3)])
assert len(assignments) == 2

## Multiple predicate multiple arity test
kb4 = [predicate0(atom2), predicate1(atom0, atom1), predicate1(atom1, atom2)]
## Multiple predicate multiple arity test
kb4 = [predicate0(atom2), predicate1(atom0, atom1), predicate1(atom1, atom2)]

assignments = find_satisfying_assignments(kb4, [predicate1(var0, var1), predicate0(var1), predicate0(var0)])
assert len(assignments) == 0
assignments = find_satisfying_assignments(kb4, [predicate1(var0, var1), predicate0(var1), predicate0(var0)])
assert len(assignments) == 0

## Tricky case!
kb6 = [predicate0(atom0), predicate2(atom1), predicate1(atom0, atom2), predicate1(atom2, atom1)]
## Tricky case!
kb6 = [predicate0(atom0), predicate2(atom1), predicate1(atom0, atom2), predicate1(atom2, atom1)]

assignments = find_satisfying_assignments(kb6, [predicate0(var0), predicate2(var1), predicate1(var0, var1)])
assert len(assignments) == 0
assignments = find_satisfying_assignments(kb6, [predicate0(var0), predicate2(var1), predicate1(var0, var1)])
assert len(assignments) == 0

print("Pass.")
class TestNegativePreconditions(unittest.TestCase):
def test_negative_preconditions(self):
MoveableType = Type('moveable')
Holding = Predicate('Holding', 1, var_types=[MoveableType])
IsPawn = Predicate('IsPawn', 1, var_types=[MoveableType])
PutOn = Predicate('PutOn', 1, var_types=[MoveableType])
On = Predicate('On', 2, var_types=[MoveableType, MoveableType])

def test_negative_preconditions():
MoveableType = Type('moveable')
Holding = Predicate('Holding', 1, var_types=[MoveableType])
IsPawn = Predicate('IsPawn', 1, var_types=[MoveableType])
PutOn = Predicate('PutOn', 1, var_types=[MoveableType])
On = Predicate('On', 2, var_types=[MoveableType, MoveableType])
# ?x0 must bind to o0 and ?x1 must bind to o1, so ?x2 must bind to o2
conds = [ PutOn("?x0"), Holding("?x1"), IsPawn("?x2"), Not(On("?x2", "?x0")) ]
kb = { PutOn('o0'), IsPawn('o0'), IsPawn('o1'), IsPawn('o2'), Holding('o1'), }
assignments = find_satisfying_assignments(kb, conds, allow_redundant_variables=False)
assert len(assignments) == 1

# ?x0 must bind to o0 and ?x1 must bind to o1, so ?x2 must bind to o2
conds = [ PutOn("?x0"), Holding("?x1"), IsPawn("?x2"), Not(On("?x2", "?x0")) ]
kb = { PutOn('o0'), IsPawn('o0'), IsPawn('o1'), IsPawn('o2'), Holding('o1'), }
assignments = find_satisfying_assignments(kb, conds, allow_redundant_variables=False)
assert len(assignments) == 1
# should be the same, even though IsPawn("?x2") is removed
conds = [ PutOn("?x0"), Holding("?x1"), Not(On("?x2", "?x0")) ]
kb = { PutOn('o0'), IsPawn('o0'), IsPawn('o1'), IsPawn('o2'), Holding('o1'), }
assignments = find_satisfying_assignments(kb, conds, allow_redundant_variables=False)
assert len(assignments) == 1

# should be the same, even though IsPawn("?x2") is removed
conds = [ PutOn("?x0"), Holding("?x1"), Not(On("?x2", "?x0")) ]
kb = { PutOn('o0'), IsPawn('o0'), IsPawn('o1'), IsPawn('o2'), Holding('o1'), }
assignments = find_satisfying_assignments(kb, conds, allow_redundant_variables=False)
assert len(assignments) == 1
class TestZeroArityNegativePreconditions(unittest.TestCase):

print("Pass.")
def test_zero_arity_negative_preconditions(self):
MoveableType = Type('moveable')
Holding = Predicate('Holding', 1, var_types=[MoveableType])
HandEmpty = Predicate('HandEmpty', 0, var_types=[])

def test_zero_arity_negative_preconditions():
MoveableType = Type('moveable')
Holding = Predicate('Holding', 1, var_types=[MoveableType])
HandEmpty = Predicate('HandEmpty', 0, var_types=[])
conds = [ Holding("?x1"), Not(HandEmpty()) ]
kb = { Holding("a"), HandEmpty() }
assignments = find_satisfying_assignments(kb, conds, allow_redundant_variables=False)
assert len(assignments) == 0

conds = [ Holding("?x1"), Not(HandEmpty()) ]
kb = { Holding("a"), HandEmpty() }
assignments = find_satisfying_assignments(kb, conds, allow_redundant_variables=False)
assert len(assignments) == 0

print("Pass.")

if __name__ == "__main__":
test_prover()
test_negative_preconditions()
test_zero_arity_negative_preconditions()

unittest.main()
Loading