Skip to content
Snippets Groups Projects
Commit 62809ce7 authored by tuhe's avatar tuhe
Browse files

Solutions to exercises

parent de8ed7a8
No related branches found
No related tags found
No related merge requests found
Showing
with 49 additions and 0 deletions
return a+b
\ No newline at end of file
return ["mr " + a for a in animals]
\ No newline at end of file
return sum([x * p for x, p in p_dict.items()])
\ No newline at end of file
return self.prices[fruit]
\ No newline at end of file
self.s = self.x0
\ No newline at end of file
terminated = True
if a == 0:
s_next = self.s * 1.1
else:
if np.random.rand() < 1/4:
s_next = 0
else:
s_next = self.s + 12
reward = s_next - self.s
\ No newline at end of file
return 0
\ No newline at end of file
return 1
\ No newline at end of file
self.s = []
\ No newline at end of file
if np.random.rand() < self.p_draw:
game_outcome = 0
else:
if np.random.rand() < self.p_win:
game_outcome = 1
else:
game_outcome = -1
\ No newline at end of file
done = len(self.s) >= 2 and self.s[-1] == self.s[-2] and self.s[-1] != 0
\ No newline at end of file
r = self.s[-1] == 1 if done else 0
\ No newline at end of file
stats, _ = train(env, Agent(env), num_episodes=T)
\ No newline at end of file
s_next = max(0, min(2, self.s-w+a)) # next state; x_{k+1} = f_k(x_k, u_k, w_k)
reward = -(a + (self.s + a - w)**2) # reward = -cost = -g_k(x_k, u_k, w_k)
terminated = self.k == self.N-1 # Have we terminated? (i.e. is k==N-1)
self.s = s_next # update environment state
self.k += 1 # update current time step
\ No newline at end of file
return np.random.choice(3) # Return a random action
\ No newline at end of file
a = agent.pi(s, k)
sp, r, terminated, truncated, metadata = env.step(a)
agent.train(s, a, sp, r, terminated)
s = sp
J += r
if terminated or truncated:
break
\ No newline at end of file
if k < 7:
return 'South'
elif k < 14:
return 'East'
elif k < 21:
return 'North'
elif k < 28:
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment