MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / play_game

Function play_game

rl/monte_carlo.py:17–55  ·  view source on GitHub ↗
(grid, policy, max_steps=20)

Source from the content-addressed store, hash-verified

15# NOTE: this is only policy evaluation, not optimization
16
17def play_game(grid, policy, max_steps=20):
18 # returns a list of states and corresponding returns
19
20 # reset game to start at a random position
21 # we need to do this, because given our current deterministic policy
22 # we would never end up at certain states, but we still want to measure their value
23 start_states = list(grid.actions.keys())
24 start_idx = np.random.choice(len(start_states))
25 grid.set_state(start_states[start_idx])
26
27 s = grid.current_state()
28
29 # keep track of all states and rewards encountered
30 states = [s]
31 rewards = [0]
32
33 steps = 0
34 while not grid.game_over():
35 a = policy[s]
36 r = grid.move(a)
37 next_s = grid.current_state()
38
39 # update states and rewards lists
40 states.append(next_s)
41 rewards.append(r)
42
43 steps += 1
44 if steps >= max_steps:
45 break
46
47 # update state
48 # note: there is no need to store the final terminal state
49 s = next_s
50
51 # we want to return:
52 # states = [s(0), s(1), ..., S(T)]
53 # rewards = [R(0), R(1), ..., R(T)]
54
55 return states, rewards
56
57
58if __name__ == '__main__':

Callers 1

monte_carlo.pyFile · 0.70

Calls 4

set_stateMethod · 0.45
current_stateMethod · 0.45
game_overMethod · 0.45
moveMethod · 0.45

Tested by

no test coverage detected