Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 581fa6b

Browse files
sofmonknorvig
authored andcommitted
added double_tennis_problem to planning.py; and minor pep8 edits (#373)
* Update test_agents.py pep8 changes, showed flake8 errors * Update test_agents.py * Update test_agents.py * Update test_agents.py * Update test_text.py added missing whitespace after comma * Update utils.py added space after comma * Update search.py added space after comma * Update probability.py added space after comma * Update learning.py added space after comma * Update planning.py added double_tennis_problem * 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). * Update search.py the 'uniform_cost_search' in notebook 'search-4e.ipynb' resembles more to the pseudocode in book. * Update search.py * Update search.py * Update search.py
1 parent efa5628 commit 581fa6b

File tree

7 files changed

+52
-18
lines changed

7 files changed

+52
-18
lines changed

learning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ def weighted_replicate(seq, weights, n):
754754
wholes = [int(w * n) for w in weights]
755755
fractions = [(w * n) % 1 for w in weights]
756756
return (flatten([x] * nx for x, nx in zip(seq, wholes)) +
757-
weighted_sample_with_replacement(n - sum(wholes),seq, fractions, ))
757+
weighted_sample_with_replacement(n - sum(wholes), seq, fractions))
758758

759759

760760
def flatten(seqs): return sum(seqs, [])

planning.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,34 @@ def spare_tire_graphplan():
526526
graphplan.graph.expand_graph()
527527
if len(graphplan.graph.levels)>=2 and graphplan.check_leveloff():
528528
return None
529+
530+
def double_tennis_problem():
531+
init = [expr('At(A, LeftBaseLine)'),
532+
expr('At(B, RightNet)'),
533+
expr('Approaching(Ball, RightBaseLine)'),
534+
expr('Partner(A,B)'),
535+
expr('Partner(A,B)')]
536+
537+
def goal_test(kb):
538+
required = [expr('Goal(Returned(Ball))'), expr('At(a, RightNet)'), expr('At(a, LeftNet)')]
539+
for q in required:
540+
if kb.ask(q) is False:
541+
return False
542+
return True
543+
544+
##actions
545+
#hit
546+
precond_pos=[expr("Approaching(Ball,loc)"), expr("At(actor,loc)")]
547+
precond_neg=[]
548+
effect_add=[expr("Returned(Ball)")]
549+
effect_rem = []
550+
hit = Action(expr("Hit(actor,Ball)"), [precond_pos, precond_neg], [effect_add, effect_rem])
551+
552+
#go
553+
precond_pos = [ expr("At(actor,loc)")]
554+
precond_neg = []
555+
effect_add = [expr("At(actor,to)")]
556+
effect_rem = [expr("At(actor,loc)")]
557+
go = Action(expr("Go(actor,to)"), [precond_pos, precond_neg], [effect_add, effect_rem])
558+
559+
return PDLL(init, [hit, go], goal_test)

probability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,5 +643,5 @@ def particle_filtering(e, N, HMM):
643643
w[i] = float("{0:.4f}".format(w[i]))
644644

645645
# STEP 2
646-
s = weighted_sample_with_replacement(N,s,w)
646+
s = weighted_sample_with_replacement(N, s, w)
647647
return s

rl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ def __call__(self, percept):
154154
s1, r1 = self.update_state(percept)
155155
Q, Nsa, s, a, r = self.Q, self.Nsa, self.s, self.a, self.r
156156
alpha, gamma, terminals, actions_in_state = self.alpha, self.gamma, self.terminals, self.actions_in_state
157-
if s1 in terminals:
158-
Q[s1, None] = r1
157+
if s in terminals:
158+
Q[s, None] = r1
159159
if s is not None:
160160
Nsa[s, a] += 1
161161
Q[s, a] += alpha(Nsa[s, a]) * (r + gamma * max(Q[s1, a1] for a1 in actions_in_state(s1))
162162
- Q[s, a])
163-
if s1 in terminals:
163+
if s in terminals:
164164
self.s = self.a = self.r = None
165165
else:
166166
self.s, self.r = s1, r1

tests/test_agents.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
from agents import Direction
22
from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment
33

4+
45
def test_move_forward():
56
d = Direction("up")
6-
l1 = d.move_forward((0,0))
7-
assert l1 == (0,-1)
7+
l1 = d.move_forward((0, 0))
8+
assert l1 == (0, -1)
89
d = Direction(Direction.R)
9-
l1 = d.move_forward((0,0))
10-
assert l1 == (1,0)
10+
l1 = d.move_forward((0, 0))
11+
assert l1 == (1, 0)
1112
d = Direction(Direction.D)
12-
l1 = d.move_forward((0,0))
13-
assert l1 == (0,1)
13+
l1 = d.move_forward((0, 0))
14+
assert l1 == (0, 1)
1415
d = Direction("left")
15-
l1 = d.move_forward((0,0))
16-
assert l1 == (-1,0)
17-
l2 = d.move_forward((1,0))
18-
assert l2 == (0,0)
16+
l1 = d.move_forward((0, 0))
17+
assert l1 == (-1, 0)
18+
l2 = d.move_forward((1, 0))
19+
assert l2 == (0, 0)
20+
1921

2022
def test_add():
2123
d = Direction(Direction.U)
@@ -37,7 +39,7 @@ def test_add():
3739
l1 = d + Direction.R
3840
l2 = d + Direction.L
3941
assert l1.direction == Direction.U
40-
assert l2.direction == Direction.D #fixed
42+
assert l2.direction == Direction.D
4143

4244
def test_ReflexVacuumAgent() :
4345
# create an object of the ReflexVacuumAgent
@@ -62,3 +64,4 @@ def test_ModelBasedVacuumAgent() :
6264
environment.run()
6365
# check final status of the environment
6466
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
67+

tests/test_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_viterbi_segmentation():
5454
P = UnigramTextModel(wordseq)
5555
text = "itiseasytoreadwordswithoutspaces"
5656

57-
s, p = viterbi_segment(text,P)
57+
s, p = viterbi_segment(text, P)
5858
assert s == [
5959
'it', 'is', 'easy', 'to', 'read', 'words', 'without', 'spaces']
6060

utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def probability(p):
194194
return p > random.uniform(0.0, 1.0)
195195

196196

197-
def weighted_sample_with_replacement(n,seq, weights):
197+
def weighted_sample_with_replacement(n, seq, weights):
198198
"""Pick n samples from seq at random, with replacement, with the
199199
probability of each element in proportion to its corresponding
200200
weight."""

0 commit comments

Comments
 (0)