diff --git a/nlp.py b/nlp.py index ace6de90d..9f7c68ed3 100644 --- a/nlp.py +++ b/nlp.py @@ -239,17 +239,17 @@ def __repr__(self): E_Chomsky = Grammar('E_Prob_Chomsky', # A Grammar in Chomsky Normal Form - Rules( - S='NP VP', - NP='Article Noun | Adjective Noun', - VP='Verb NP | Verb Adjective', - ), - Lexicon( - Article='the | a | an', - Noun='robot | sheep | fence', - Adjective='good | new | sad', - Verb='is | say | are' - )) + Rules( + S='NP VP', + NP='Article Noun | Adjective Noun', + VP='Verb NP | Verb Adjective', + ), + Lexicon( + Article='the | a | an', + Noun='robot | sheep | fence', + Adjective='good | new | sad', + Verb='is | say | are' + )) E_Prob_Chomsky = ProbGrammar('E_Prob_Chomsky', # A Probabilistic Grammar in CNF ProbRules( @@ -263,7 +263,18 @@ def __repr__(self): Adjective='good [0.5] | new [0.2] | sad [0.3]', Verb='is [0.5] | say [0.3] | are [0.2]' )) - +E_Prob_Chomsky_ = ProbGrammar('E_Prob_Chomsky_', + ProbRules( + S='NP VP [1]', + NP='NP PP [0.4] | Noun Verb [0.6]', + PP='Preposition NP [1]', + VP='Verb NP [0.7] | VP PP [0.3]', + ), + ProbLexicon( + Noun='astronomers [0.18] | eyes [0.32] | stars [0.32] | telescopes [0.18]', + Verb='saw [0.5] | \'\' [0.5]', + Preposition='with [1]' + )) # ______________________________________________________________________________ # Chart Parsing diff --git a/planning.py b/planning.py index e31c8b3a3..e67616b4a 100644 --- a/planning.py +++ b/planning.py @@ -26,7 +26,7 @@ def act(self, action): """ Performs the action given as argument. Note that action is an Expr like expr('Remove(Glass, Table)') or expr('Eat(Sandwich)') - """ + """ action_name = action.op args = action.args list_action = first(a for a in self.actions if a.name == action_name) @@ -536,7 +536,7 @@ def double_tennis_problem(): expr('Partner(B, A)')] def goal_test(kb): - required = [expr('Goal(Returned(Ball))'), expr('At(a, RightNet)'), expr('At(a, LeftNet)')] + required = [expr('Returned(Ball)'), expr('At(a, LeftNet)'), expr('At(a, RightNet)')] return all(kb.ask(q) is not False for q in required) # Actions @@ -546,14 +546,14 @@ def goal_test(kb): precond_neg = [] effect_add = [expr("Returned(Ball)")] effect_rem = [] - hit = Action(expr("Hit(actor, Ball)"), [precond_pos, precond_neg], [effect_add, effect_rem]) + hit = Action(expr("Hit(actor, Ball, loc)"), [precond_pos, precond_neg], [effect_add, effect_rem]) # Go precond_pos = [expr("At(actor, loc)")] precond_neg = [] effect_add = [expr("At(actor, to)")] effect_rem = [expr("At(actor, loc)")] - go = Action(expr("Go(actor, to)"), [precond_pos, precond_neg], [effect_add, effect_rem]) + go = Action(expr("Go(actor, to, loc)"), [precond_pos, precond_neg], [effect_add, effect_rem]) return PDDL(init, [hit, go], goal_test) @@ -862,3 +862,17 @@ def goal_test(kb): return Problem(init, [add_engine1, add_engine2, add_wheels1, add_wheels2, inspect1, inspect2], goal_test, [job_group1, job_group2], resources) + + +def test_three_block_tower(): + p = three_block_tower() + assert p.goal_test() is False + solution = [expr("MoveToTable(C, A)"), + expr("Move(B, Table, C)"), + expr("Move(A, Table, B)")] + + for action in solution: + p.act(action) + + assert p.goal_test() + diff --git a/tests/.pytest_cache/v/cache/lastfailed b/tests/.pytest_cache/v/cache/lastfailed new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_nlp.py b/tests/test_nlp.py index 1d8320cdc..978685a4e 100644 --- a/tests/test_nlp.py +++ b/tests/test_nlp.py @@ -113,6 +113,11 @@ def test_CYK_parse(): P = CYK_parse(words, grammar) assert len(P) == 52 + grammar = nlp.E_Prob_Chomsky_ + words = ['astronomers', 'saw', 'stars'] + P = CYK_parse(words, grammar) + assert len(P) == 32 + # ______________________________________________________________________________ # Data Setup diff --git a/tests/test_planning.py b/tests/test_planning.py index 2c355f54c..c10c0e9ba 100644 --- a/tests/test_planning.py +++ b/tests/test_planning.py @@ -62,7 +62,19 @@ def test_spare_tire(): assert p.goal_test() +def test_double_tennis(): + p = double_tennis_problem() + assert p.goal_test() is False + + solution = [expr("Go(A, RightBaseLine, LeftBaseLine)"), + expr("Hit(A, Ball, RightBaseLine)"), + expr("Go(A, LeftNet, RightBaseLine)")] + + for action in solution: + p.act(action) + assert p.goal_test() + def test_three_block_tower(): p = three_block_tower() assert p.goal_test() is False