Skip to content
Merged
2 changes: 1 addition & 1 deletion learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
Expand Down
31 changes: 31 additions & 0 deletions planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions rl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions tests/test_agents.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -62,3 +64,4 @@ def test_ModelBasedVacuumAgent() :
environment.run()
# check final status of the environment
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}

2 changes: 1 addition & 1 deletion tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down
2 changes: 1 addition & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down