-
Notifications
You must be signed in to change notification settings - Fork 3
/
gambler_problem.py
68 lines (53 loc) · 1.59 KB
/
gambler_problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import matplotlib.pyplot as plt
PH = 0.25
MAX_MONEY = 100
def r(state):
return 0
def v(value, state, action):
# No win
new_state = state - action
val = (1 - PH) * (r(new_state) + value[new_state])
# Win
new_state = min(MAX_MONEY, state + action)
val += PH * (r(new_state) + value[new_state])
return val
def policy_evaluation(value):
next_value = value.copy()
for state in range(1, len(value) - 1):
vals = []
for bet in range(min(state, MAX_MONEY - state) + 1):
vals.append(v(value, state, bet))
next_value[state] = np.amax(vals)
return next_value
def get_policy(value):
policy = np.zeros(value.shape)
for state in range(1, len(value) - 1):
best_bet = 0
best_val = 0
for bet in range(min(state, MAX_MONEY - state) + 1):
val = v(value, state, bet)
if best_val <= val:
best_val = val
best_bet = bet
policy[state] = best_bet
return policy
if __name__ == '__main__':
value = np.zeros((MAX_MONEY + 1,))
value[MAX_MONEY] = 1
change = float('inf')
fig = plt.figure()
sub = fig.add_subplot(121)
x = np.arange(value.shape[0])[1:-1]
while change > 1e-10:
prev = value
value = policy_evaluation(value)
sub.plot(x, value[1:-1])
change = np.abs(value - prev).sum()
print('Value change:', change)
policy = get_policy(value)
sub.plot(x, value[1:-1])
sub = fig.add_subplot(122)
sub.plot(x, policy[1:-1], 'ro')
plt.tight_layout()
plt.show()