From 5fb6861459c4c3afebe74aad9684ca205c273b87 Mon Sep 17 00:00:00 2001 From: Vaishali Sharma Date: Wed, 7 Mar 2018 17:35:41 +0530 Subject: [PATCH 1/6] added test case for CYK_parse --- agents.ipynb | 18 +++++++++++++++--- nlp.py | 13 ++++++++++++- tests/.pytest_cache/v/cache/lastfailed | 4 ++++ tests/test_agents.py | 2 ++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 tests/.pytest_cache/v/cache/lastfailed diff --git a/agents.ipynb b/agents.ipynb index ed6920bd0..88fdb3465 100644 --- a/agents.ipynb +++ b/agents.ipynb @@ -150,9 +150,21 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'Agent' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mclass\u001b[0m \u001b[0mBlindDog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAgent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mlocation\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmovedown\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlocation\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mNameError\u001b[0m: name 'Agent' is not defined" + ] + } + ], "source": [ "class BlindDog(Agent):\n", " location = 1\n", @@ -1252,7 +1264,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4rc1" + "version": "3.6.4" } }, "nbformat": 4, diff --git a/nlp.py b/nlp.py index ace6de90d..9434663e5 100644 --- a/nlp.py +++ b/nlp.py @@ -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 NNP [1]', + VP='Verb NP [0.7] | VP PP [0.3]', + ), + ProbLexicon( + Noun='astronomers [0.18] | ears [0.16] | stars [0.32] | telescopes [0.18] | \'\' [0.16]', + Verb='saw [1]', + Preposition='with [1]' + )) # ______________________________________________________________________________ # Chart Parsing diff --git a/tests/.pytest_cache/v/cache/lastfailed b/tests/.pytest_cache/v/cache/lastfailed new file mode 100644 index 000000000..41735f061 --- /dev/null +++ b/tests/.pytest_cache/v/cache/lastfailed @@ -0,0 +1,4 @@ +{ + "test_learning.py": true, + "test_text.py": true +} \ No newline at end of file diff --git a/tests/test_agents.py b/tests/test_agents.py index caefe61d4..8b68379f2 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -1,3 +1,5 @@ +import sys +sys.path.insert(0,'./../') import random from agents import Direction from agents import Agent From 46871ca2c4f2400fba81d61a545e312157ae5733 Mon Sep 17 00:00:00 2001 From: Vaishali Sharma Date: Wed, 7 Mar 2018 17:55:00 +0530 Subject: [PATCH 2/6] added testcase for CYK_parse --- nlp.py | 6 +++--- tests/test_nlp.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/nlp.py b/nlp.py index 9434663e5..b0256d559 100644 --- a/nlp.py +++ b/nlp.py @@ -267,12 +267,12 @@ def __repr__(self): ProbRules( S='NP VP [1]', NP='NP PP [0.4] | Noun Verb [0.6]', - PP='Preposition NNP [1]', + PP='Preposition NP [1]', VP='Verb NP [0.7] | VP PP [0.3]', ), ProbLexicon( - Noun='astronomers [0.18] | ears [0.16] | stars [0.32] | telescopes [0.18] | \'\' [0.16]', - Verb='saw [1]', + Noun='astronomers [0.18] | eyes [0.32] | stars [0.32] | telescopes [0.18]', + Verb='saw [0.5] | \'\' [0.5]', Preposition='with [1]' )) 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 From 7601e1a88b65a9241b16d2f29c91964d0497b7ab Mon Sep 17 00:00:00 2001 From: Vaishali Sharma Date: Wed, 7 Mar 2018 17:58:10 +0530 Subject: [PATCH 3/6] corrected spacing --- nlp.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/nlp.py b/nlp.py index b0256d559..d98f1bb86 100644 --- a/nlp.py +++ b/nlp.py @@ -264,17 +264,17 @@ def __repr__(self): 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]' - )) + 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 From 55f6af6cda4f11bd7d3bbc6b4d6f192091725b39 Mon Sep 17 00:00:00 2001 From: Vaishali Sharma Date: Wed, 7 Mar 2018 21:49:22 +0530 Subject: [PATCH 4/6] fixed issues like alignment, missing comma etc. --- agents.ipynb | 44 ++++++++++---------------- nlp.py | 28 ++++++++-------- tests/.pytest_cache/v/cache/lastfailed | 4 --- tests/test_agents.py | 2 -- 4 files changed, 30 insertions(+), 48 deletions(-) diff --git a/agents.ipynb b/agents.ipynb index 88fdb3465..65878bbab 100644 --- a/agents.ipynb +++ b/agents.ipynb @@ -4,6 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "\n", "# AGENT #\n", "\n", "An agent, as defined in 2.1 is anything that can perceive its environment through sensors, and act upon that environment through actuators based on its agent program. This can be a dog, robot, or even you. As long as you can perceive the environment and act on it, you are an agent. This notebook will explain how to implement a simple agent, create an environment, and create a program that helps the agent act on the environment based on its percepts.\n", @@ -17,6 +18,7 @@ "cell_type": "code", "execution_count": 1, "metadata": { + "collapsed": true, "scrolled": true }, "outputs": [], @@ -80,7 +82,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Food(Thing):\n", @@ -150,21 +154,11 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'Agent' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mclass\u001b[0m \u001b[0mBlindDog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAgent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mlocation\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmovedown\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlocation\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mNameError\u001b[0m: name 'Agent' is not defined" - ] - } - ], + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], "source": [ "class BlindDog(Agent):\n", " location = 1\n", @@ -175,14 +169,12 @@ " def eat(self, thing):\n", " '''returns True upon success or False otherwise'''\n", " if isinstance(thing, Food):\n", - " #print(\"Dog: Ate food at {}.\".format(self.location))\n", " return True\n", " return False\n", " \n", " def drink(self, thing):\n", " ''' returns True upon success or False otherwise'''\n", " if isinstance(thing, Water):\n", - " #print(\"Dog: Drank water at {}.\".format(self.location))\n", " return True\n", " return False\n", " \n", @@ -468,7 +460,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "from random import choice\n", @@ -499,14 +493,12 @@ " def eat(self, thing):\n", " '''returns True upon success or False otherwise'''\n", " if isinstance(thing, Food):\n", - " #print(\"Dog: Ate food at {}.\".format(self.location))\n", " return True\n", " return False\n", " \n", " def drink(self, thing):\n", " ''' returns True upon success or False otherwise'''\n", " if isinstance(thing, Water):\n", - " #print(\"Dog: Drank water at {}.\".format(self.location))\n", " return True\n", " return False\n", " \n", @@ -558,11 +550,9 @@ " if action == 'turnright':\n", " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", " agent.turn(Direction.R)\n", - " #print('now facing {}'.format(agent.direction.direction))\n", " elif action == 'turnleft':\n", " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", " agent.turn(Direction.L)\n", - " #print('now facing {}'.format(agent.direction.direction))\n", " elif action == 'moveforward':\n", " loc = copy.deepcopy(agent.location) # find out the target location\n", " if agent.direction.direction == Direction.R:\n", @@ -573,7 +563,6 @@ " loc[1] += 1\n", " elif agent.direction.direction == Direction.U:\n", " loc[1] -= 1\n", - " #print('{} at {} facing {}'.format(agent, loc, agent.direction.direction))\n", " if self.is_inbounds(loc):# move only if the target is a valid location\n", " print('{} decided to move {}wards at location: {}'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", " agent.moveforward()\n", @@ -676,11 +665,9 @@ " if action == 'turnright':\n", " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", " agent.turn(Direction.R)\n", - " #print('now facing {}'.format(agent.direction.direction))\n", " elif action == 'turnleft':\n", " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", " agent.turn(Direction.L)\n", - " #print('now facing {}'.format(agent.direction.direction))\n", " elif action == 'moveforward':\n", " loc = copy.deepcopy(agent.location) # find out the target location\n", " if agent.direction.direction == Direction.R:\n", @@ -691,7 +678,6 @@ " loc[1] += 1\n", " elif agent.direction.direction == Direction.U:\n", " loc[1] -= 1\n", - " #print('{} at {} facing {}'.format(agent, loc, agent.direction.direction))\n", " if self.is_inbounds(loc):# move only if the target is a valid location\n", " print('{} decided to move {}wards at location: {}'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", " agent.moveforward()\n", @@ -1169,7 +1155,9 @@ { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "from ipythonblocks import BlockGrid\n", diff --git a/nlp.py b/nlp.py index d98f1bb86..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( @@ -264,14 +264,14 @@ def __repr__(self): Verb='is [0.5] | say [0.3] | are [0.2]' )) E_Prob_Chomsky_ = ProbGrammar('E_Prob_Chomsky_', - ProbRules( + 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], + ProbLexicon( + Noun='astronomers [0.18] | eyes [0.32] | stars [0.32] | telescopes [0.18]', Verb='saw [0.5] | \'\' [0.5]', Preposition='with [1]' )) diff --git a/tests/.pytest_cache/v/cache/lastfailed b/tests/.pytest_cache/v/cache/lastfailed index 41735f061..e69de29bb 100644 --- a/tests/.pytest_cache/v/cache/lastfailed +++ b/tests/.pytest_cache/v/cache/lastfailed @@ -1,4 +0,0 @@ -{ - "test_learning.py": true, - "test_text.py": true -} \ No newline at end of file diff --git a/tests/test_agents.py b/tests/test_agents.py index 8b68379f2..caefe61d4 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -1,5 +1,3 @@ -import sys -sys.path.insert(0,'./../') import random from agents import Direction from agents import Agent From adb536f49e48a1f4893c492ae28a23ce3a517707 Mon Sep 17 00:00:00 2001 From: Vaishali Sharma Date: Mon, 12 Mar 2018 17:25:03 +0530 Subject: [PATCH 5/6] test case for double tennis problem --- planning.py | 23 +++++++++++++++++++---- tests/test_planning.py | 12 ++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/planning.py b/planning.py index 4c02c3d72..a3ba82f3a 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) @@ -74,6 +74,7 @@ def substitute(self, e, args): def check_precond(self, kb, args): """Checks if the precondition is satisfied in the current state""" # check for positive clauses + #print (self.precond_pos[0].__eq__(expr('At(actor, loc)')), self.precond_neg) for clause in self.precond_pos: if self.substitute(clause, args) not in kb.clauses: return False @@ -536,7 +537,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 +547,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 +863,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/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 From edded08133a750f74a88fa2d2f4c6dc36320455d Mon Sep 17 00:00:00 2001 From: Vaishali Sharma Date: Thu, 15 Mar 2018 12:02:33 +0530 Subject: [PATCH 6/6] Update planning.py removed commented print statements. --- planning.py | 1 - 1 file changed, 1 deletion(-) diff --git a/planning.py b/planning.py index d63cc8ddb..e67616b4a 100644 --- a/planning.py +++ b/planning.py @@ -74,7 +74,6 @@ def substitute(self, e, args): def check_precond(self, kb, args): """Checks if the precondition is satisfied in the current state""" # check for positive clauses - #print (self.precond_pos[0].__eq__(expr('At(actor, loc)')), self.precond_neg) for clause in self.precond_pos: if self.substitute(clause, args) not in kb.clauses: return False