-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_tp1_ex1.py
More file actions
127 lines (92 loc) · 2.44 KB
/
main_tp1_ex1.py
File metadata and controls
127 lines (92 loc) · 2.44 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from mdp import MDP
import numpy as np
import matplotlib.pyplot as plt
import time
P_x_a = np.array([[[0.45, 0.55, 0.00],
[0.60, 0.40, 0.00],
[0.00, 1.00, 0.00]],
[[0.00, 1.00, 0.00],
[0.00, 0.10, 0.90],
[0.50, 0.10, 0.40]]])
R_x_a = np.array([[-0.4, -1.0, 2.0],
[ 0.0, -0.5, 0.0]])
env = MDP(P_x_a, R_x_a)
X = range(env.n_states)
A = range(env.n_actions)
gamma = 0.95
# ========= Policy evaluation ========= #
pi_opt = [1,1,0] # Optimal policy
V_opt = np.array([0.0] * env.n_states)
T = 10000
for t in range(T):
for x in X:
next_state, reward = env.step(x, pi_opt[x])
V_opt[x] = reward + gamma*sum(P_x_a[pi_opt[x], x, :] * V_opt[:]) # WARNING
# ========== Value iteration ========== #
t = time.time()
Delta_V_Value = []
V = np.array([0.0] * env.n_states)
term = False
while not term:
TV = np.array([0.0] * env.n_states)
for x in X:
TV[x] = -float('inf')
for a in A:
next_state, reward = env.step(x, a)
TVa = reward + gamma*sum(P_x_a[a, x, :] * V[:])
if TVa > TV[x]:
TV[x] = TVa
#print(max(abs(TV - V)))
if max(abs(TV - V)) < 0.01:
term = True
Delta_V_Value.append(max(abs(V_opt - V)))
V = TV
pi = [A[0]] * env.n_states
for x in X:
best_a = A[0]
best_Va = -float('inf')
for a in A:
next_state, reward = env.step(x, a)
Va = reward + gamma*sum(P_x_a[a, x, :] * V[:])
if Va > best_Va:
best_a = a
best_Va = Va
pi[x] = best_a
elapsed = time.time() - t
plt.plot(Delta_V_Value)
print('elapsed Value Iteration', elapsed)
# ========= Policy iteration ========== #
t = time.time()
Delta_V_Policy = []
pi = [A[0]] * env.n_states
V = np.array([0.0] * env.n_states)
term = False
while (not term):
V_pi = np.array([0.0] * env.n_states)
for x in X:
next_state, reward = env.step(x, pi[x])
V_pi[x] = reward + gamma*sum(P_x_a[pi[x], x, :] * V[:])
for x in X:
best_a = A[0]
best_Va = -float('inf')
for a in A:
next_state, reward = env.step(x, a)
Va = reward + gamma*sum(P_x_a[a, x, :] * V[:])
if Va > best_Va:
best_a = a
best_Va = Va
pi[x] = best_a
Delta_V_Policy.append(max(abs(V_opt - V)))
if max(abs(V_pi - V)) < 0.01:
term = True
V = V_pi
elapsed = time.time() - t
plt.plot(Delta_V_Policy)
print(len(Delta_V_Policy),len(Delta_V_Value))
print('elapsed Policy Iteration', elapsed)
# ====================================== #
plt.show()
print(2*0.01 * gamma / (1.0-gamma))
print(pi)
print(V)
print(V_opt)