diff --git a/learning.py b/learning.py index 8308fe607..981a557c2 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, []) 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) 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 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 diff --git a/tests/test_agents.py b/tests/test_agents.py index 77421c2c7..0162a78b8 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -1,21 +1,23 @@ from agents import Direction from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment + 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) @@ -37,7 +39,7 @@ 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 def test_ReflexVacuumAgent() : # create an object of the ReflexVacuumAgent @@ -62,3 +64,4 @@ def test_ModelBasedVacuumAgent() : environment.run() # check final status of the environment assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'} + 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'] 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."""