-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq.py
More file actions
164 lines (123 loc) · 3.75 KB
/
q.py
File metadata and controls
164 lines (123 loc) · 3.75 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
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
154
155
156
157
158
159
160
161
162
163
164
from collections import defaultdict, namedtuple
from sympy.matrices import Matrix, ImmutableMatrix
import gym
import numpy as np
import random
import pandas as pd
from random import random
# CartPole-v1
'''Other simple environments can also perform reasonably but require adjusting parameter values'''
# setting up environment using Gym
env = gym.make('CartPole-v1')
# checking the action space
print('Action Space: ', env.action_space.n)
# Defining storage for tables
# and parameters
# Q-table
Q_table = defaultdict(float)
# Frequency table
N_table = defaultdict(int)
# reward count
reward_util = 0
# steps
steps = 0
# value of future reward
# used to balance immediate and future reward
gamma= 0.99
# epsilon parameters
epsilon = 0.99
# decay rate
epsilon_decay = 0.999
# final epsilon - exploitation
final_epsilon = 0.01
# learning rate
alpha = 0.6
# learning rate over time
alpha_decay = 0.8
# state seve
# using named tuple
# function for creating tuple
# subclasses with named fields
save_state = namedtuple('SavedState', ['state'])
# previous state
prev_s = None
# previous reward
prev_r = 0
# previous action
prev_a = 0
# epsilon greedy function
# adjust exploration/exploitation amount over time
def epsilon_greedy(s, epsilon):
if np.random.rand() < epsilon:
return env.action_space.sample()
# choosing action with highest Q.
qvals = {a: Q_table[s, a] for a in range(env.action_space.n)}
max_q = max(qvals.values())
# in case numerous actions have same max Q value.
actions_with_max_q = [a for a, q in qvals.items() if q == max_q]
return np.random.choice(actions_with_max_q)
# updating the Q-table
def update_q_val(prev_state, action, reward, next_state, done):
# choosing best action next state
max_q_next = max([Q_table[next_state, a] for a in range(env.action_space.n)])
# updating N table
N_table[prev_state, action] = N_table[prev_state, action] + 1
# update Q table
# isf state terminated value on next state is not included
Q_table[prev_state, action] += alpha * (
reward + gamma * max_q_next * (1 - done) - Q_table[prev_state, action])
# main loop
# running agent/game for number of runs
samples = 10000
# psilon drop rate
eps_drop = (epsilon - final_epsilon) / samples * 2
for sample in range(samples):
# initialize the environment
curr_s = env.reset()
reward_util = 0
steps = 0
curr_r = 0
done = False
step = 0
#print(Q_table)
while not done:
# previous action
prev_a = epsilon_greedy(save_state(ImmutableMatrix(curr_s)), epsilon)
# show visualisation
env.render()
# preforming action
# and getting reward, state
next_state, reward, done, info = env.step(prev_a)
# if not None set reward to previous rewerd
if reward is not None:
prev_r = reward
## add reward
reward_util = reward_util + reward
# set states
prev_s = curr_s
curr_s = next_state
# update q values
update_q_val(save_state(ImmutableMatrix(prev_s)),
prev_a,
reward,
save_state(ImmutableMatrix(curr_s)),
done)
# add step
steps = steps + 1
# update epsilon
if(epsilon > final_epsilon):
epsilon -= eps_drop
if(epsilon < final_epsilon):
epsilon = final_epsilon
# Print data
print("epsilon:")
print(epsilon)
print("sample:")
print(sample)
print("Reward value: ")
print(reward_util)
# set data frame
df = pd.DataFrame([[sample, reward_util]], columns=["Steps", "Reward"])
# append data to file
#print(df)
df.to_csv("./q-learning_data.csv", header=None, mode="a")