-
Notifications
You must be signed in to change notification settings - Fork 0
/
taxi-v3.py
61 lines (46 loc) · 1.45 KB
/
taxi-v3.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
import numpy as np
import gym
import matplotlib.pyplot as plt
import random
env=gym.make('Taxi-v3').env
#Q table
q_table=np.zeros([env.observation_space.n, env.action_space.n])
#HyperParameters
alpha= 0.1
gamma = 0.95
epsilon = 0.2
#Plotting matrix
reward_list = []
dropout_list = []
episode = 100000
for i in range(1, episode):
#Initialize the environment
state=env.reset()
reward_count = 0
dropout_count = 0
while True:
#exploit and explore
if random.uniform(0,1)<epsilon:
action = env.action_space.sample()
else:
action = np.argmax(q_table[state])
# action process and reward
next_state, reward, done, _ = env.step(action)
old_value=q_table[state,action]
next_max = np.max(q_table[next_state])
#Q LEARNING FUNCTION
next_value = (1-alpha)*old_value + alpha*(reward+gamma*next_max)
#Q table update
q_table[state, action] = next_value
#state update
state= next_state
#find wrong dropouts
if reward == -10:
dropout_count+=1
reward_count+=reward
if done:
break
if i%10==0:
dropout_list.append(dropout_count)
reward_list.append(reward_count)
print("Episode count: {}, reward: {}, wrong dropout: {}".format(i, reward_count, dropout_count))