Files
PathPlanning/Model-free Control/Q-learning.py
T

167 lines
4.6 KiB
Python
Raw Normal View History

2020-06-19 17:25:37 -07:00
import env
2020-06-20 19:41:17 -07:00
import plotting
2020-06-19 17:25:37 -07:00
import motion_model
import numpy as np
2020-06-21 21:57:29 -07:00
2020-06-19 17:25:37 -07:00
class QLEARNING:
def __init__(self, x_start, x_goal):
self.xI, self.xG = x_start, x_goal
2020-06-21 21:57:29 -07:00
self.M = 500 # iteration numbers
self.gamma = 0.9 # discount factor
2020-06-19 17:25:37 -07:00
self.alpha = 0.5
2020-06-20 19:41:17 -07:00
self.epsilon = 0.1
2020-06-19 17:25:37 -07:00
2020-06-20 19:41:17 -07:00
self.env = env.Env(self.xI, self.xG)
self.motion = motion_model.Motion_model(self.xI, self.xG)
self.plotting = plotting.Plotting(self.xI, self.xG)
2020-06-19 17:25:37 -07:00
2020-06-21 21:57:29 -07:00
self.u_set = self.env.motions # feasible input set
self.stateSpace = self.env.stateSpace # state space
self.obs = self.env.obs_map() # position of obstacles
self.lose = self.env.lose_map() # position of lose states
2020-06-20 19:41:17 -07:00
self.name1 = "SARSA, M=" + str(self.M)
[self.value, self.policy] = self.Monte_Carlo(self.xI, self.xG)
self.path = self.extract_path(self.xI, self.xG, self.policy)
self.plotting.animation(self.path, self.name1)
def Monte_Carlo(self, xI, xG):
2020-06-19 17:25:37 -07:00
"""
Monte_Carlo experiments
:return: Q_table, policy
"""
2020-06-21 21:57:29 -07:00
Q_table = self.table_init() # Q_table initialization
policy = {} # policy table
2020-06-19 17:36:24 -07:00
2020-06-21 21:57:29 -07:00
for k in range(self.M): # iterations
x = self.state_init() # initial state
while x != xG: # stop condition
u = self.epsilon_greedy(int(np.argmax(Q_table[x])), self.epsilon) # epsilon_greedy policy
x_next = self.move_next(x, self.u_set[u]) # next state
reward = self.env.get_reward(x_next) # reward observed
2020-06-19 17:25:37 -07:00
Q_table[x][u] = (1 - self.alpha) * Q_table[x][u] + \
self.alpha * (reward + self.gamma * max(Q_table[x_next]))
x = x_next
for x in Q_table:
2020-06-21 21:57:29 -07:00
policy[x] = int(np.argmax(Q_table[x])) # extract policy
2020-06-19 17:25:37 -07:00
return Q_table, policy
def table_init(self):
"""
Initialize Q_table: Q(s, a)
:return: Q_table
"""
Q_table = {}
2020-06-20 19:41:17 -07:00
for x in self.stateSpace:
u = []
if x not in self.obs:
for k in range(len(self.u_set)):
if x == self.xG:
u.append(0)
else:
u.append(np.random.random_sample())
Q_table[x] = u
2020-06-19 17:25:37 -07:00
return Q_table
def state_init(self):
"""
initialize a starting state
:return: starting state
"""
while True:
2020-06-20 19:41:17 -07:00
i = np.random.randint(0, self.env.x_range - 1)
j = np.random.randint(0, self.env.y_range - 1)
2020-06-19 17:25:37 -07:00
if (i, j) not in self.obs:
return (i, j)
def epsilon_greedy(self, u, error):
"""
generate a policy using epsilon_greedy algorithm
:param u: original input
:param error: epsilon value
:return: epsilon policy
"""
if np.random.random_sample() < 3 / 4 * error:
u_e = u
while u_e == u:
p = np.random.random_sample()
2020-06-21 21:57:29 -07:00
if p < 0.25:
u_e = 0
elif p < 0.5:
u_e = 1
elif p < 0.75:
u_e = 2
else:
u_e = 3
2020-06-19 17:25:37 -07:00
return u_e
return u
def move_next(self, x, u):
"""
get next state.
:param x: current state
:param u: input
:return: next state
"""
x_next = (x[0] + u[0], x[1] + u[1])
if x_next in self.obs:
return x
return x_next
2020-06-20 19:41:17 -07:00
def extract_path(self, xI, xG, policy):
2020-06-19 17:25:37 -07:00
"""
2020-06-20 19:41:17 -07:00
extract path from converged policy.
2020-06-19 17:25:37 -07:00
:param xI: starting state
2020-06-20 19:41:17 -07:00
:param xG: goal states
2020-06-19 17:25:37 -07:00
:param policy: converged policy
2020-06-20 19:41:17 -07:00
:return: path
2020-06-19 17:25:37 -07:00
"""
2020-06-20 19:41:17 -07:00
x, path = xI, [xI]
2020-06-20 19:52:56 -07:00
while x != xG:
2020-06-19 17:25:37 -07:00
u = self.u_set[policy[x]]
x_next = (x[0] + u[0], x[1] + u[1])
if x_next in self.obs:
2020-06-20 19:41:17 -07:00
print("Collision! Please run again!")
break
2020-06-19 17:25:37 -07:00
else:
2020-06-20 19:41:17 -07:00
path.append(x_next)
2020-06-19 17:25:37 -07:00
x = x_next
return path
2020-06-19 17:36:24 -07:00
def message(self):
2020-06-20 19:41:17 -07:00
"""
print important message.
:param count: iteration numbers
:return: print
"""
2020-06-19 17:36:24 -07:00
print("starting state: ", self.xI)
print("goal state: ", self.xG)
print("iteration numbers: ", self.M)
print("discount factor: ", self.gamma)
print("epsilon error: ", self.epsilon)
print("alpha: ", self.alpha)
2020-06-19 17:25:37 -07:00
if __name__ == '__main__':
x_Start = (1, 1)
x_Goal = (12, 1)
Q_CALL = QLEARNING(x_Start, x_Goal)