-
Notifications
You must be signed in to change notification settings - Fork 2
/
taxi_lake_dyna_q.py
159 lines (116 loc) · 4.4 KB
/
taxi_lake_dyna_q.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
import gym
import numpy as np
import time, pickle, os
import matplotlib.pyplot as plt
class Model():
def __init__(self, n_states, n_actions):
self.transitions = np.zeros((n_states, n_actions), dtype=np.uint8)
self.rewards = np.zeros((n_states, n_actions))
def add(self, state, action, state2, reward):
self.transitions[state, action] = state2
self.rewards[state, action] = reward
def sample(self, env):
state, action = 0, 0
# Random visited state
if all(np.sum(self.transitions, axis=1)) <= 0:
state = np.random.randint(env.observation_space.n)
else:
state = np.random.choice(np.where(np.sum(self.transitions, axis=1) > 0)[0])
# Random action in that state
if all(self.transitions[state]) <= 0:
action = np.random.randint(env.action_space.n)
else:
action = np.random.choice(np.where(self.transitions[state] > 0)[0])
return state, action
def step(self, state, action):
state2 = self.transitions[state, action]
reward = self.rewards[state, action]
return state2, reward
class Agent:
def __init__(self, env):
self.env = env
self.Q = np.zeros((env.observation_space.n, env.action_space.n))
self.model = Model(env.observation_space.n, env.action_space.n)
def choose_action(self, state):
if np.random.uniform(0, 1) < epsilon:
return self.env.action_space.sample()
else:
return np.argmax(self.Q[state, :])
def learn(self, state, state2, reward, action):
# predict = Q[state, action]
# Q[state, action] = Q[state, action] + lr_rate * (target - predict)
target = reward + gamma * np.max(self.Q[state2, :])
self.Q[state, action] = (1 - lr_rate) * self.Q[state, action] + lr_rate * target
def planning(self, n_steps):
# if len(self.transitions)>planning_steps:
for i in range(n_steps):
state, action = self.model.sample(self.env)
state2, reward = self.model.step(state, action)
self.learn(state, state2, reward, action)
def train():
total_rewards = []
for episode in range(total_episodes):
state = agent.env.reset()
t = 0
ep_rewards = 0
while t < max_steps:
# env.render()
action = agent.choose_action(state)
state2, reward, done, info = agent.env.step(action)
agent.learn(state, state2, reward, action)
agent.model.add(state, action, state2, reward)
agent.planning(planning_steps)
state = state2
ep_rewards+= reward
t += 1
if done:
break
total_rewards.append(ep_rewards)
# epsilon = min_epsilon + (max_epsilon - min_epsilon) * np.exp(-decay_rate * episode)
# time.sleep(0.1)
# if done:
# break
return total_rewards
def test():
total_rewards = []
for episode in range(total_episodes):
ep_rewards = 0
state = agent.env.reset()
for t in range(max_steps):
agent.env.render()
time.sleep(0.5)
act = np.argmax(agent.Q[state,:])
state2, reward, done, info = agent.env.step(act)
if done:
ep_rewards += reward
break
else:
state = state2
total_rewards.append(ep_rewards)
return total_rewards
def train_details(total_rewards):
print("Q table:\n", agent.Q)
print("Model Transitions:\n", agent.model.transitions)
# Perfect actions: [1 2 1 0 1 0 1 0 2 1 1 0 0 2 2 0]
print("Total Rewards in training: {0} in {1} episodes".format(sum(total_rewards), total_episodes))
def test_details(total_rewards):
print("\n\nTotal Rewards in testing: {0} in {1} episodes".format(sum(total_rewards), total_episodes))
# Setup
env = gym.make('Taxi-v3')
epsilon = 0.9
lr_rate = 0.1
gamma = 0.99
planning_steps = 0
total_episodes = 10000
max_steps = 100
agent = Agent(env)
print("Total States:", env.observation_space.n)
print("Total Actions:", env.action_space.n)
print("####### Training ########")
total_rewards = train()
train_details(total_rewards)
print("####### Testing ########")
total_test_rewards = test()
test_details(total_test_rewards)
# with open("frozenLake_qTable.pkl", 'wb') as f:
# pickle.dump(Q, f)