From 9d272b73dacef0520709e9340498935206b02ee3 Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:26:13 +0530 Subject: [PATCH 01/15] Update test_agents.py pep8 changes, showed flake8 errors --- tests/test_agents.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/test_agents.py b/tests/test_agents.py index 89ee3fcf3..7d251f547 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -1,21 +1,23 @@ from agents import Direction + def test_move_forward(): d = Direction("up") - l1 = d.move_forward((0,0)) - assert l1 == (0,-1) + l1 = d.move_forward((0, 0)) + assert l1 == (0, -1) d = Direction(Direction.R) - l1 = d.move_forward((0,0)) - assert l1 == (1,0) + l1 = d.move_forward((0, 0)) + assert l1 == (1, 0) d = Direction(Direction.D) - l1 = d.move_forward((0,0)) - assert l1 == (0,1) + l1 = d.move_forward((0, 0)) + assert l1 == (0, 1) d = Direction("left") - l1 = d.move_forward((0,0)) - assert l1 == (-1,0) - l2 = d.move_forward((1,0)) - assert l2 == (0,0) + l1 = d.move_forward((0, 0)) + assert l1 == (-1, 0) + l2 = d.move_forward((1, 0)) + assert l2 == (0, 0) + def test_add(): d = Direction(Direction.U) l1 = d + "right" @@ -36,5 +38,4 @@ def test_add(): l1 = d + Direction.R l2 = d + Direction.L assert l1.direction == Direction.U - assert l2.direction == Direction.D #fixed - + assert l2.direction == Direction.D # fixed From 1581f7a72b3f91267e5a87fcfffa0f07d0140beb Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:32:01 +0530 Subject: [PATCH 02/15] Update test_agents.py --- tests/test_agents.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_agents.py b/tests/test_agents.py index 7d251f547..aece69d7f 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -17,7 +17,6 @@ def test_move_forward(): l2 = d.move_forward((1, 0)) assert l2 == (0, 0) - def test_add(): d = Direction(Direction.U) l1 = d + "right" @@ -38,4 +37,4 @@ def test_add(): l1 = d + Direction.R l2 = d + Direction.L assert l1.direction == Direction.U - assert l2.direction == Direction.D # fixed + assert l2.direction == Direction.D From 7f9d0534f52faa3f137c5a14f3ed45a75080bf52 Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:34:40 +0530 Subject: [PATCH 03/15] Update test_agents.py --- tests/test_agents.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_agents.py b/tests/test_agents.py index aece69d7f..20aa33af6 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -16,7 +16,8 @@ def test_move_forward(): assert l1 == (-1, 0) l2 = d.move_forward((1, 0)) assert l2 == (0, 0) - + + def test_add(): d = Direction(Direction.U) l1 = d + "right" @@ -37,4 +38,4 @@ def test_add(): l1 = d + Direction.R l2 = d + Direction.L assert l1.direction == Direction.U - assert l2.direction == Direction.D + assert l2.direction == Direction.D From 594736d3cb27954e496f3f171fcb41ade76078ca Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:38:02 +0530 Subject: [PATCH 04/15] Update test_agents.py --- tests/test_agents.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_agents.py b/tests/test_agents.py index 20aa33af6..2512ccddf 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -16,8 +16,8 @@ def test_move_forward(): assert l1 == (-1, 0) l2 = d.move_forward((1, 0)) assert l2 == (0, 0) - - + + def test_add(): d = Direction(Direction.U) l1 = d + "right" From b6f9de464bda1d5bf2ef5c5a16f4da59f9881805 Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:41:23 +0530 Subject: [PATCH 05/15] Update test_text.py added missing whitespace after comma --- tests/test_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_text.py b/tests/test_text.py index d58cd497a..577ad661b 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -54,7 +54,7 @@ def test_viterbi_segmentation(): P = UnigramTextModel(wordseq) text = "itiseasytoreadwordswithoutspaces" - s, p = viterbi_segment(text,P) + s, p = viterbi_segment(text, P) assert s == [ 'it', 'is', 'easy', 'to', 'read', 'words', 'without', 'spaces'] From 7d5dcba1de45cd511e85e44ce197eccf2f717c31 Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:48:32 +0530 Subject: [PATCH 06/15] Update utils.py added space after comma --- utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils.py b/utils.py index cfdc88d37..73dd63d63 100644 --- a/utils.py +++ b/utils.py @@ -194,7 +194,7 @@ def probability(p): return p > random.uniform(0.0, 1.0) -def weighted_sample_with_replacement(n,seq, weights): +def weighted_sample_with_replacement(n, seq, weights): """Pick n samples from seq at random, with replacement, with the probability of each element in proportion to its corresponding weight.""" From 2783b6a981a4cef8c7f653fe16f4a82092393e82 Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:49:12 +0530 Subject: [PATCH 07/15] Update search.py added space after comma --- search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search.py b/search.py index c8885a9ed..94f4949d2 100644 --- a/search.py +++ b/search.py @@ -587,7 +587,7 @@ def genetic_algorithm(population, fitness_fn, ngen=1000, pmut=0.1): new_population = [] for i in range(len(population)): fitnesses = map(fitness_fn, population) - p1, p2 = weighted_sample_with_replacement(2,population, fitnesses) + p1, p2 = weighted_sample_with_replacement(2, population, fitnesses) child = p1.mate(p2) if random.uniform(0, 1) < pmut: child.mutate() From 99cbbaf72c3be44c84ed5e0b4a5958934bbb55e6 Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:49:35 +0530 Subject: [PATCH 08/15] Update probability.py added space after comma --- probability.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/probability.py b/probability.py index fa856c330..1d7992e6d 100644 --- a/probability.py +++ b/probability.py @@ -643,5 +643,5 @@ def particle_filtering(e, N, HMM): w[i] = float("{0:.4f}".format(w[i])) # STEP 2 - s = weighted_sample_with_replacement(N,s,w) + s = weighted_sample_with_replacement(N, s, w) return s From 620cb4182fed8b4fb4af005a69d0e8b6607deb41 Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:50:29 +0530 Subject: [PATCH 09/15] Update learning.py added space after comma --- learning.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/learning.py b/learning.py index 4917a2cf0..2b86ff8e4 100644 --- a/learning.py +++ b/learning.py @@ -754,7 +754,7 @@ def weighted_replicate(seq, weights, n): wholes = [int(w * n) for w in weights] fractions = [(w * n) % 1 for w in weights] return (flatten([x] * nx for x, nx in zip(seq, wholes)) + - weighted_sample_with_replacement(n - sum(wholes),seq, fractions, )) + weighted_sample_with_replacement(n - sum(wholes), seq, fractions)) def flatten(seqs): return sum(seqs, []) From ab26cd8971b4eaa9cd37dc11781be9507ab62972 Mon Sep 17 00:00:00 2001 From: sofmonk Date: Sat, 18 Mar 2017 17:52:40 +0530 Subject: [PATCH 10/15] Update planning.py added double_tennis_problem --- planning.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/planning.py b/planning.py index a17677460..17028e4c6 100644 --- a/planning.py +++ b/planning.py @@ -526,3 +526,34 @@ def spare_tire_graphplan(): graphplan.graph.expand_graph() if len(graphplan.graph.levels)>=2 and graphplan.check_leveloff(): return None + +def double_tennis_problem(): + init = [expr('At(A, LeftBaseLine)'), + expr('At(B, RightNet)'), + expr('Approaching(Ball, RightBaseLine)'), + expr('Partner(A,B)'), + expr('Partner(A,B)')] + + def goal_test(kb): + required = [expr('Goal(Returned(Ball))'), expr('At(a, RightNet)'), expr('At(a, LeftNet)')] + for q in required: + if kb.ask(q) is False: + return False + return True + + ##actions + #hit + precond_pos=[expr("Approaching(Ball,loc)"), expr("At(actor,loc)")] + precond_neg=[] + effect_add=[expr("Returned(Ball)")] + effect_rem = [] + hit = Action(expr("Hit(actor,Ball)"), [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]) + + return PDLL(init, [hit, go], goal_test) From f69e7c1e7ab1689da33766ae856fb7f55b2ee8ad Mon Sep 17 00:00:00 2001 From: Angira Sharma Date: Sun, 19 Mar 2017 12:43:08 +0530 Subject: [PATCH 11/15] Update rl.py In the pseudocode figure 21.8, the first 'if' starts with argument 's', which is the previous state, not s1(i.e, the current state). --- rl.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rl.py b/rl.py index 5241710fe..77a04f98a 100644 --- a/rl.py +++ b/rl.py @@ -154,13 +154,13 @@ def __call__(self, percept): s1, r1 = self.update_state(percept) Q, Nsa, s, a, r = self.Q, self.Nsa, self.s, self.a, self.r alpha, gamma, terminals, actions_in_state = self.alpha, self.gamma, self.terminals, self.actions_in_state - if s1 in terminals: - Q[s1, None] = r1 + if s in terminals: + Q[s, None] = r1 if s is not None: Nsa[s, a] += 1 Q[s, a] += alpha(Nsa[s, a]) * (r + gamma * max(Q[s1, a1] for a1 in actions_in_state(s1)) - Q[s, a]) - if s1 in terminals: + if s in terminals: self.s = self.a = self.r = None else: self.s, self.r = s1, r1 From fce259abe690e0fd7380b1490c3502dfefacf440 Mon Sep 17 00:00:00 2001 From: Angira Sharma Date: Sun, 19 Mar 2017 15:05:45 +0530 Subject: [PATCH 12/15] Update search.py the 'uniform_cost_search' in notebook 'search-4e.ipynb' resembles more to the pseudocode in book. --- search.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/search.py b/search.py index 94f4949d2..416855008 100644 --- a/search.py +++ b/search.py @@ -268,9 +268,22 @@ def best_first_graph_search(problem, f): return None -def uniform_cost_search(problem): - "[Figure 3.14]" - return best_first_graph_search(problem, lambda node: node.path_cost) +def uniform_cost_search(problem, costfn=lambda node: node.path_cost): + """[Figure 3.14]""" + frontier = FrontierPQ(Node(problem.initial), costfn) + explored = set() + while frontier: + node = frontier.pop() + if problem.is_goal(node.state): + return node + explored.add(node.state) + for action in problem.actions(node.state): + child = node.child(problem, action) + if child.state not in explored and child not in frontier: + frontier.add(child) + elif child in frontier and frontier.cost[child] < child.path_cost: + frontier.replace(child) + return None def depth_limited_search(problem, limit=50): From 6b82783aebe359b43e11119f942b1ff3ecb38f79 Mon Sep 17 00:00:00 2001 From: Angira Sharma Date: Sun, 19 Mar 2017 15:17:16 +0530 Subject: [PATCH 13/15] Update search.py --- search.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/search.py b/search.py index 416855008..342daf13a 100644 --- a/search.py +++ b/search.py @@ -268,9 +268,10 @@ def best_first_graph_search(problem, f): return None -def uniform_cost_search(problem, costfn=lambda node: node.path_cost): - """[Figure 3.14]""" - frontier = FrontierPQ(Node(problem.initial), costfn) +def uniform_cost_search(problem, costfn = lambda node: node.path_cost): + "[Figure 3.14]" + node = Node(problem.initial) + frontier = PriorityQueue(node, costfn) explored = set() while frontier: node = frontier.pop() From 5e628be627aaec90265c66b6c95120532033ecf9 Mon Sep 17 00:00:00 2001 From: Angira Sharma Date: Sun, 19 Mar 2017 15:24:37 +0530 Subject: [PATCH 14/15] Update search.py --- search.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/search.py b/search.py index 342daf13a..44d6961be 100644 --- a/search.py +++ b/search.py @@ -268,23 +268,9 @@ def best_first_graph_search(problem, f): return None -def uniform_cost_search(problem, costfn = lambda node: node.path_cost): +def uniform_cost_search(problem): "[Figure 3.14]" - node = Node(problem.initial) - frontier = PriorityQueue(node, costfn) - explored = set() - while frontier: - node = frontier.pop() - if problem.is_goal(node.state): - return node - explored.add(node.state) - for action in problem.actions(node.state): - child = node.child(problem, action) - if child.state not in explored and child not in frontier: - frontier.add(child) - elif child in frontier and frontier.cost[child] < child.path_cost: - frontier.replace(child) - return None +return best_first_graph_search(problem, lambda node: node.path_cost) def depth_limited_search(problem, limit=50): From 86f10d5eefacc6d8261a16fe144927b5fb6d3194 Mon Sep 17 00:00:00 2001 From: Angira Sharma Date: Sun, 19 Mar 2017 15:28:04 +0530 Subject: [PATCH 15/15] Update search.py --- search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search.py b/search.py index 44d6961be..94f4949d2 100644 --- a/search.py +++ b/search.py @@ -270,7 +270,7 @@ def best_first_graph_search(problem, f): def uniform_cost_search(problem): "[Figure 3.14]" -return best_first_graph_search(problem, lambda node: node.path_cost) + return best_first_graph_search(problem, lambda node: node.path_cost) def depth_limited_search(problem, limit=50):