-
Notifications
You must be signed in to change notification settings - Fork 60
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 👍