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

Function play_game

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

Source from the content-addressed store, hash-verified

19
20
21def play_game(grid, policy, max_steps=20):
22 # reset game to start at a random position
23 # we need to do this if we have a deterministic policy
24 # we would never end up at certain states, but we still want to measure their value
25 # this is called the "exploring starts" method
26 start_states = list(grid.actions.keys())
27 start_idx = np.random.choice(len(start_states))
28 grid.set_state(start_states[start_idx])
29
30 s = grid.current_state()
31 a = np.random.choice(ALL_POSSIBLE_ACTIONS) # first action is uniformly random
32
33 states = [s]
34 actions = [a]
35 rewards = [0]
36
37 for _ in range(max_steps):
38 r = grid.move(a)
39 s = grid.current_state()
40
41 rewards.append(r)
42 states.append(s)
43
44 if grid.game_over():
45 break
46 else:
47 a = policy[s]
48 actions.append(a)
49
50 # we want to return:
51 # states = [s(0), s(1), ..., s(T-1), s(T)]
52 # actions = [a(0), a(1), ..., a(T-1), ]
53 # rewards = [ 0, R(1), ..., R(T-1), R(T)]
54
55 return states, actions, rewards
56
57
58def max_dict(d):

Callers 1

monte_carlo_es.pyFile · 0.70

Calls 4

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

Tested by

no test coverage detected