forked from ronekko/basic_reinforcement_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_q_learning.py
192 lines (156 loc) · 6.2 KB
/
main_q_learning.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 31 18:14:03 2015
@author: sakurai
"""
import time
import numpy as np
import matplotlib.pyplot as plt
import vrep
import contexttimer
from environment import Robot
class Agent(object):
def __init__(self, robot, alpha=0.1, gamma=0.9, epsilon=0.05, q_init=1):
self.robot = robot
self.num_actions_0 = 3
self.num_actions_1 = 3
self.num_actions = self.num_actions_0 * self.num_actions_1
self.angles_0 = np.linspace(0, np.pi/2, self.num_actions_0)
self.angles_1 = np.linspace(0, np.pi/2, self.num_actions_1)
# look-up table from action to angles
self.angles_lut = np.array(np.meshgrid(self.angles_1, self.angles_0,
indexing='ij')).reshape(2, -1).T
self.num_states_0 = 5 # angle of joint 0
self.num_states_1 = 5 # angle of joint 1
self.num_states = self.num_states_0 * self.num_states_1
self.state_bins = [
np.linspace(0, np.pi/2, self.num_states_0, endpoint=False)[1:],
np.linspace(0, np.pi/2, self.num_states_1, endpoint=False)[1:]]
self.q_table = np.full((self.num_states, self.num_actions), q_init)
self.alpha = alpha # learning rate
self.gamma = gamma # discount factor
self.epsilon = epsilon # epsilon-greedy rate
def choose_action(self, state):
if np.random.uniform() < self.epsilon:
action = np.random.choice(self.num_actions)
else:
action = np.argmax(self.q_table[state])
return action
def do_action(self, action):
angles = self.angles_lut[action]
self.robot.set_joint_angles(angles)
self.robot.proceed_simulation()
def observe_state(self):
angles = self.robot.get_joint_angles()
return self.calc_state(angles)
def calc_state(self, angles):
state_0 = np.digitize([angles[0]], self.state_bins[0])[0]
state_1 = np.digitize([angles[1]], self.state_bins[1])[0]
state = state_0 * self.num_states_1 + state_1
return state
def play(self):
action = self.choose_action(self.state)
self.do_action(action)
state_new = self.observe_state()
position_new = self.robot.get_body_position()
x_forward = position_new[0] - self.position[0]
reward = x_forward - 0.001
# update Q-table
self.update_q(self.state, action, reward, state_new)
self.state = state_new
self.position = position_new
def update_q(self, state, action, reward, state_new):
q_sa = self.q_table[state, action]
td_error = reward + self.gamma * np.max(self.q_table[state_new]) - q_sa
self.q_table[state, action] = q_sa + self.alpha * td_error
def initialize_episode(self):
self.robot.restart_simulation()
self.robot.initialize_pose()
self.position = self.robot.get_body_position()
angles = self.robot.get_joint_angles()
self.state = self.calc_state(angles)
def plot(body_trajectory, joints_trajectory, return_history, q_table):
fig = plt.figure(figsize=(9, 4))
T = len(body_trajectory)
# plot an xyz trajectory of the body
ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(222)
ax4 = plt.subplot(224)
ax1.grid()
ax1.set_color_cycle('rgb')
ax1.plot(np.arange(T) * 0.05, np.array(body_trajectory))
ax1.set_title('Position of the body')
ax1.set_ylabel('position [m]')
ax1.legend(['x', 'y', 'z'], loc='best')
# plot a trajectory of angles of the joints
ax2.grid()
ax2.set_color_cycle('rg')
ax2.plot(np.arange(T) * 0.05, np.array(joints_trajectory))
ax2.set_title('Angle of the joints')
ax2.set_xlabel('time in simulation [s]')
ax2.set_ylabel('angle [rad]')
ax2.legend(['joint_0', 'joint_1'], loc='best')
# plot a history of returns of each episode
ax3.grid()
ax3.plot(return_history)
ax3.set_title('Returns (total rewards) of each episode')
ax3.set_xlabel('episode')
ax3.set_ylabel('position [m]')
# show Q-table
ax4.matshow(q_table.T, cmap=plt.cm.gray)
ax4.set_title('Q-table')
ax4.xaxis.set_ticks_position('bottom')
ax4.set_xlabel('state')
ax4.set_ylabel('action')
plt.tight_layout()
plt.show()
plt.draw()
if __name__ == '__main__':
try:
client_id
except NameError:
client_id = -1
e = vrep.simxStopSimulation(client_id, vrep.simx_opmode_oneshot_wait)
vrep.simxFinish(-1)
client_id = vrep.simxStart('127.0.0.1', 19998, True, True, 5000, 5)
assert client_id != -1, 'Failed connecting to remote API server'
# print ping time
sec, msec = vrep.simxGetPingTime(client_id)
print "Ping time: %f" % (sec + msec / 1000.0)
robot = Robot(client_id)
agent = Agent(robot, alpha=0.1, gamma=0.9, epsilon=0.05, q_init=0.3)
num_episodes = 500
len_episode = 100
return_history = []
try:
for episode in range(num_episodes):
print "start simulation # %d" % episode
with contexttimer.Timer() as timer:
agent.initialize_episode()
body_trajectory = []
joints_trajectory = []
body_trajectory.append(robot.get_body_position())
joints_trajectory.append(robot.get_joint_angles())
for t in range(len_episode):
agent.play()
print agent.state,
body_trajectory.append(robot.get_body_position())
joints_trajectory.append(robot.get_joint_angles())
position = body_trajectory[-1]
return_history.append(position[0])
q_table = agent.q_table
plot(body_trajectory, joints_trajectory, return_history, q_table)
print
print "Body position: ", position
print "Elapsed time (wall-clock): ", timer.elapsed
print
except KeyboardInterrupt:
print "Terminated by `Ctrl+c` !!!!!!!!!!"
plt.grid()
plt.plot(return_history)
plt.title('Return (total reward in a episode)')
plt.xlabel('episode')
plt.ylabel('position [m]')
plt.show()
e = vrep.simxStopSimulation(client_id, vrep.simx_opmode_oneshot_wait)