-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkoopman-taxi.py
153 lines (121 loc) · 4.3 KB
/
koopman-taxi.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#%% Imports
import gym
import numpy as np
import estimate_L
import algorithmsv2
env = gym.make("Taxi-v3")
def l2_norm(true_state, predicted_state):
return np.sum( np.power( ( true_state - predicted_state ), 2 ) )
#%% Load data
X = np.load('random-agent/taxi/states.npy').T
Y = np.append(np.roll(X, -1, axis=1)[:,:-1], np.zeros((X.shape[0],1)), axis=1)
U = np.load('random-agent/taxi/actions.npy').reshape(1,-1)
#%% Enumerate all possible states and actions
num_rows = 5
num_cols = 5
num_locations = 5
num_destinations = 4
enumerated_states = []
for i in range(num_rows):
for j in range(num_cols):
for k in range(num_locations):
for l in range(num_destinations):
state = (i,j,k,l)
enumerated_states.append(state)
enumerated_states = np.array(enumerated_states)
num_unique_actions = 6
enumerated_actions = []
for i in range(num_unique_actions):
enumerated_actions.append([i])
enumerated_actions = np.array(enumerated_actions)
#%% Cost function
def reward(x, u):
u = u[0]
if u == 4: # Pickup
# If passenger not at location
if x[2] == 4 or (x[0], x[1]) != env.locs[x[2]]:
return -10
elif u == 5: # Dropoff
# if can drop off passenger
if ((x[0], x[1]) == env.locs[x[3]]) and x[2] == 4:
return 20
else: # Drop off at wrong location
return -10
return -1 # Default reward when there is no pickup/dropoff
def cost(x, u):
return -reward(x, u)
#%% Koopman tensor EDMD setup
def phi(x):
phi_x = np.zeros(enumerated_states.shape[0])
for i,state in enumerate(enumerated_states):
if np.array_equal(x, state):
phi_x[i] = 1
return phi_x
def psi(u):
psi_u = np.zeros(num_unique_actions)
psi_u[u] = 1
return psi_u
def getPhiMatrix(X):
Phi_X = []
for x in X.T:
Phi_X.append(phi(x))
return np.array(Phi_X).T
def getPsiMatrix(U):
Psi_U = []
for u in U.T:
Psi_U.append(psi(u))
return np.array(Psi_U).T
#%% Build Phi and Psi Matrices
Phi_X = getPhiMatrix(X)
Psi_U = getPsiMatrix(U)
print(Phi_X.shape)
print(Psi_U.shape)
num_lifted_state_observations = Phi_X.shape[1]
num_lifted_state_features = Phi_X.shape[0]
num_lifted_action_observations = Psi_U.shape[1]
num_lifted_action_features = Psi_U.shape[0]
# @nb.njit(fastmath=True)
def getPsiPhiMatrix(Psi_U, Phi_X):
psiPhiMatrix = np.empty((num_lifted_action_features * num_lifted_state_features, num_lifted_state_observations))
for i in range(num_lifted_state_observations):
kron = np.kron(Psi_U[:,i], Phi_X[:,i])
psiPhiMatrix[:,i] = kron
return psiPhiMatrix
psiPhiMatrix = getPsiPhiMatrix(Psi_U, Phi_X)
print("PsiPhiMatrix shape:", psiPhiMatrix.shape)
#%% Compute M as in writeup
M = estimate_L.rrr(psiPhiMatrix.T, getPhiMatrix(Y).T).T
print("M shape:", M.shape)
assert M.shape == (num_lifted_state_features, num_lifted_state_features * num_lifted_action_features)
#%% Reshape M into Koopman tensor
K = np.empty((num_lifted_state_features, num_lifted_state_features, num_lifted_action_features))
for i in range(M.shape[0]):
K[i] = M[i].reshape((num_lifted_state_features, num_lifted_action_features))
print("K shape:", K.shape)
def K_u(K, u):
return np.einsum('ijz,z->ij', K, psi(u))
#%% Test prediction error
episodes = 1
norms = []
for episode in range(episodes):
true_state = env.reset()
taxi_y,taxi_x,passenger,destination = env.decode(true_state)
decoded_true_state = np.array([taxi_y, taxi_x, passenger, destination])
done = False
while not done:
action = env.action_space.sample()
print("Action:", action)
print("Decoded state:", decoded_true_state)
phi_x_prime = K_u(K, action) @ phi(decoded_true_state)
predicted_state = enumerated_states[np.argmax(phi_x_prime)]
print("Predicted state:", predicted_state)
true_state, _, done, __ = env.step(action)
taxi_y,taxi_x,passenger,destination = env.decode(true_state)
decoded_true_state = np.array([taxi_y, taxi_x, passenger, destination])
print("Decoded state:", decoded_true_state)
norms.append(l2_norm(decoded_true_state, predicted_state))
env.close()
norms = np.array(norms)
print("Mean norm:", np.mean(norms))
# pi = algorithmsv2.algorithm2(X, U, phi, psi, K, cost)
# print(pi(U[:,0], X[:,0]))