-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ_learning_robot_test.py
More file actions
67 lines (50 loc) · 1.38 KB
/
Q_learning_robot_test.py
File metadata and controls
67 lines (50 loc) · 1.38 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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
from shapely import Polygon, Point
plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["figure.autolayout"] = True
plt.figure()
plt.title("The Environment")
# Draw the environment
rows, columns = 11, 11
env_points = [(0,0), (0,10), (10,10), (10,0)]
env_points.append(env_points[0])
x, y = zip(*env_points)
plt.plot(x, y)
# Draw the obstacles
obs_1 = [(1,8), (2,1), (3,1), (2, 9)]
obs_1.append(obs_1[0])
obs_2 = [(4, 7), (4,6), (7,6), (7,2), (9, 2), (9, 7)]
obs_2.append(obs_2[0])
x, y = zip(*obs_1)
plt.plot(x, y)
x, y = zip(*obs_2)
plt.plot(x, y)
# Start and goal location
start, goal = (1,3), (9, 8)
x, y = start
plt.plot(x, y, "-xb")
x, y = goal
plt.plot(x, y, "-pg")
plt.show(block=False)
cmap = colors.ListedColormap(['red', 'blue'])
bounds = [0,10,20]
norm = colors.BoundaryNorm(bounds, cmap.N)
# Initialize all the points in the environment having reward -1
rewards = np.full((rows, columns), -1)
# Set the reward for the goal
rewards[goal[0], goal[1]] = 100
# print(rewards)
#Create obstacle polygons
obs_poly_1 = Polygon(obs_1)
obs_poly_2 = Polygon(obs_2)
for i in range(rows):
for j in range(columns):
p = Point(i,j)
if obs_poly_1.intersects(p):
rewards[i,j] = -100
if obs_poly_2.intersects(p):
rewards[i,j] = -100
print(rewards)
plt.show()