Skip to content
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
35 changes: 23 additions & 12 deletions nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
22 changes: 18 additions & 4 deletions planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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()

Empty file.
5 changes: 5 additions & 0 deletions tests/test_nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down