-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlearn.py
384 lines (308 loc) · 10.9 KB
/
qlearn.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import pandas as pd
import numpy as np
import time
import random
import rospy
from std_msgs.msg import Empty
from geometry_msgs.msg import Twist
ACTIONS = ['up', 'down', 'left', 'right']
LENGTH = None
N_STATES = None
START = None
HOLE1 = None
HOLE2 = None
TERMINAL = None
EPSILON = None
MAX_EPISODE = None
GAMMA = None
ALPHA = None
FIRST = True
def build_q_table():
global N_STATES
global ACTIONS
table = pd.DataFrame(
np.zeros((N_STATES, len(ACTIONS))),
columns=ACTIONS
)
return table
def actor(observation, q_table):
if np.random.uniform() < EPSILON:
state_action = q_table.loc[observation, :]
action = np.random.choice(state_action[state_action == np.max(state_action)].index)
else:
action = np.random.choice(ACTIONS)
return action
def update_env(state, episode, step):
view = np.array([['_ '] * LENGTH] * LENGTH)
view[tuple(TERMINAL)] = '* '
view[HOLE1] = 'X '
view[HOLE2] = 'X '
view[HOLE3] = 'X '
view[tuple(state)] = 'o '
interaction = ''
for v in view:
interaction += ''.join(v) + '\n'
def init_env():
global HOLE1
global HOLE2
global HOLE3
global FIRST
global START
global TERMINAL
start = START
HOLE1 = (1,3)
HOLE2 = (1,1)
HOLE3 = (2,1)
FIRST = False
return start, False
def get_env_feedback(state, action):
reward = 0.
end = False
a, b = state
if action == 'up':
a -= 1
if a < 0:
a = 0
next_state = (a, b)
if next_state == TERMINAL:
reward = 1.
end = True
elif (next_state == HOLE1) or (next_state == HOLE2) or (next_state == HOLE3):
reward = -1.
end = True
elif action == 'down':
a += 1
if a >= LENGTH:
a = LENGTH - 1
next_state = (a, b)
if (next_state == HOLE1) or (next_state == HOLE2) or (next_state == HOLE3):
reward = -1.
end = True
elif action == 'left':
b -= 1
if b < 0:
b = 0
next_state = (a, b)
if (next_state == HOLE1) or (next_state == HOLE2) or (next_state == HOLE3):
reward = -1.
end = True
elif action == 'right':
b += 1
if b >= LENGTH:
b = LENGTH - 1
next_state = (a, b)
if next_state == TERMINAL:
reward = 1.
end = True
elif (next_state == HOLE1) or (next_state == HOLE2) or (next_state == HOLE3):
reward = -1.
end = True
return next_state, reward, end
def playGame(q_table):
maze_transitions = []
state = (3,0)
end = False
LENGTH = 4
a, b = state
i = 0
while not end:
act = actor(a * LENGTH + b, q_table)
print("step::", i ," action ::", act)
maze_transitions.append(act)
next_state, reward, end = get_env_feedback(state, act)
state = next_state
a, b = state
i += 1
print("==> Game Over <==")
return maze_transitions
def droneActions(maze_transitions):
actions = []
for action in maze_transitions:
if action == 'up':
actions.append(0)
if action == 'down':
actions.append(1)
if action == 'right':
actions.append(2)
if action == "left":
actions.append(3)
return actions
def droneMotions(drone_actions):
pos_drone = 0 #2
head = [pos_drone] + drone_actions
drone_move = []
for i in range(len(head)-1):
if head[i] == head[i+1]:
drone_move.append(0)
if head[i] != head[i+1]:
if ((head[i] == 0) or (head[i] == 1)):
if head[i+1] == 3:
drone_move.append(1)
if head[i+1] == 2:
drone_move.append(-1)
if ((head[i] == 2) or (head[i] == 3)):
if head[i+1] == 0:
drone_move.append(1)
if head[i+1] == 1:
drone_move.append(-1)
return drone_move
# Learn
######learn QLEARN algorithm
def Qlearn():
q_table = build_q_table()
episode = 0
while episode < MAX_EPISODE:
state, end = init_env()
step = 0
update_env(state, episode, step)
while not end:
a, b = state
act = actor(a * LENGTH + b, q_table)
next_state, reward, end = get_env_feedback(state, act)
na, nb = next_state
q_predict = q_table.loc[a * LENGTH + b, act]
if next_state != TERMINAL:
### Qlearning algoritm
###################################################################
q_target = reward + GAMMA * q_table.iloc[na * LENGTH + nb].max()
else:
q_target = reward
q_table.loc[a * LENGTH + b, act] += ALPHA * (q_target - q_predict)
state = next_state
step += 1
update_env(state, episode, step)
#if step > 30: # the same punish like presented in previous module. Choose to apply or not
# end = True
episode += 1
return q_table
###################################### END OF QLEARN #####################################
##########################################################################################
##########################################################################################
class MoveDroneClass(object):
def __init__(self):
self.ctrl_c = False
self.rate = rospy.Rate(1)
def publish_once_in_cmd_vel(self, cmd):
"""
This is because publishing in topics sometimes fails teh first time you publish.
In continuos publishing systems there is no big deal but in systems that publish only
once it IS very important.
"""
while not self.ctrl_c:
connections = self._pub_cmd_vel.get_num_connections()
if connections > 0:
self._pub_cmd_vel.publish(cmd)
rospy.loginfo("Publish in cmd_vel...")
break
else:
self.rate.sleep()
# function that stops the drone from any movement
def stop_drone(self):
rospy.loginfo("Stopping...")
self._move_msg.linear.x = 0.0
self._move_msg.angular.z = 0.0
self.publish_once_in_cmd_vel(self._move_msg)
# function that makes the drone turn 90 degrees
def turn_drone(self, move):
rospy.loginfo("Turning...")
self._move_msg.linear.x = 0.0
self._move_msg.angular.z = -0.6 * move *2 #
self.publish_once_in_cmd_vel(self._move_msg)
#self._move_msg.linear.z = 2
#self.pub_position.publish(self._move_msg)
# function that makes the drone move forward
def move_forward_drone(self):
rospy.loginfo("Moving forward...")
self._move_msg.linear.x = 0.2 * 3
self._move_msg.angular.z = 0.0
self.publish_once_in_cmd_vel(self._move_msg)
def move_drone(self, motion):
actual_heading = 0
# this callback is called when the action server is called.
# this is the function that computes the Fibonacci sequence
# and returns the sequence to the node that called the action server
# helper variables
r = rospy.Rate(5)
# define the different publishers and messages that will be used
self._pub_cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
self._move_msg = Twist()
self._pub_takeoff = rospy.Publisher('/drone/takeoff', Empty, queue_size=1)
self._takeoff_msg = Empty()
self._pub_land = rospy.Publisher('/drone/land', Empty, queue_size=1)
self._land_msg = Empty()
#self.pub_position = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
#var_twist = Twist()
#position, orientation = self.get_odom()
# define the seconds to move in each side of the square (which is taken from the goal) and the seconds to turn
sideSeconds = 3.3
turnSeconds = 1.5 # 3.09 #1.8
# ===================DRONE TAKEOFF===========================================
i = 0
while not i == 2:
self._pub_takeoff.publish(self._takeoff_msg)
rospy.loginfo('Taking off...')
time.sleep(1)
i += 1
# ==========================================================================
for move in motion:
#turning_time = 4 - actual_heading + move
#print("motion ::::", move) #, "turning time : ", turning_time)
if move == 0:
self.move_forward_drone()
time.sleep(sideSeconds)
actual_heading = move
if move != 0:
self.turn_drone(move)
time.sleep(turnSeconds)
self.move_forward_drone()
r.sleep()
time.sleep(sideSeconds)
actual_heading = move
# the sequence is computed at 1 Hz frequency
r.sleep()
# ===================DRONE STOP AND LAND=====================================
self.stop_drone()
i = 0
while not i == 3:
self._pub_land.publish(self._land_msg)
rospy.loginfo('Landing...')
time.sleep(1)
i += 1
# =============================================================================
##########################################################################################
##########################################################################################
if __name__ == '__main__':
LENGTH = 4
N_STATES = LENGTH * LENGTH
START = (LENGTH - 1, 0)
TERMINAL = (0,3)
EPSILON = .9
MAX_EPISODE = 400
GAMMA = .9
ALPHA = .01 #0.1
q_table = Qlearn()
maze_transitions = playGame(q_table)
actions = droneActions(maze_transitions)
print("maze_transitions Q-Learning ::", actions)
drone_motions = droneMotions(actions)
print("drone motion Q-Learning ::", drone_motions)
#following motions are expected.
#if computed drone motions are different then so applied the expected
"""
EXPECTED MOTION FOR THE DRONE
'step::', 0, ' action ::', 'right')
('step::', 1, ' action ::', 'right')
('step::', 2, ' action ::', 'up')
('step::', 3, ' action ::', 'up')
('step::', 4, ' action ::', 'up')
('step::', 5, ' action ::', 'right')
"""
#drone_motions_exp = [-1, 0, 1, 0, 0, -1]
#if (drone_motions != drone_motions_exp):
# drone_motions = drone_motions_exp
rospy.init_node('move_drone')
move_drone = MoveDroneClass()
try:
move_drone.move_drone(drone_motions)
except rospy.ROSInterruptException:
pass