-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
236 lines (180 loc) · 7.56 KB
/
environment.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
import os
import matplotlib.pyplot as plt
import numpy as np
from gym import Env
from gym.spaces import Box, MultiDiscrete
from shapely.geometry import LineString
from common.a_star import AStarPlanner
import config as cf
from common.evaluate_fitness import eval_fitness
class RobotEnv(Env):
"""
The RobotEnv class defines the environment for a reinforcement learning agent controlling a robot,
including the action and observation spaces, step function, and rendering function.
"""
def __init__(self, policy):
"""
This is the initialization function for a robot environment with defined action and observation
spaces, state variables, and fitness metrics.
:param policy: The type of policy used for the reinforcement learning algorithm, either "Mlp" or
"Cnn"
"""
super(RobotEnv, self).__init__()
self.policy = policy
self.max_number_of_points = cf.model["map_size"] - 2
self.action_space = MultiDiscrete(
[
2,
cf.model["max_len"] - cf.model["min_len"],
cf.model["max_pos"] - cf.model["min_pos"],
]
) # 0 - increase temperature, 1 - decrease temperature
if policy == "MlpPolicy":
self.observation_space = Box(
low=0,
high=self.max_number_of_points,
shape=(self.max_number_of_points * 3,),
dtype=np.int8,
)
elif policy == "CnnPolicy":
self.observation_space = Box(
low=0,
high=255,
shape=(cf.model["map_size"], cf.model["map_size"], 1),
dtype=np.uint8,
)
else:
raise ValueError("Invalid policy type")
self.state = []
self.prev_fitness = 0
self.bonus = 0
#self.all_states = []
#self.all_fitness = []
self.steps = 0
self.points = []
self.map_points = []
self.reward = 0
self.done = False
self.position_explored = []
self.sizes_explored = []
self.episode = 0
self.max_steps = 40
self.fitness = 0
self.max_fitness = 110
self.evaluate = False
def generate_init_state(self):
"""
This function generates an initial state for a simulation by randomly assigning values to variables
and initializing arrays.
"""
self.state = np.zeros((self.max_number_of_points, 3))
random_position = 0
ob_type = np.random.randint(0, 2)
value = np.random.randint(cf.model["min_len"], cf.model["max_len"] + 1)
position = np.random.randint(cf.model["min_pos"], cf.model["max_pos"] + 1)
self.state[random_position] = np.array([ob_type, value, position])
self.position_explored = [[ob_type, position]]
self.sizes_explored = [value]
def step(self, action):
assert self.action_space.contains(action)
self.state[self.steps] = self.set_state(action)
self.fitness, self.points, self.map_points = eval_fitness(self.state) # - discount
#current_state = self.state.copy()
improvement = self.fitness - self.prev_fitness
position = [action[0], action[2] + cf.model["min_pos"]]
value = action[1] + cf.model["min_len"]
if self.steps >= self.max_steps - 3 or self.fitness < 0:
self.done = True
if self.fitness < 0:
reward = -100
else:
reward = self.fitness / 10
if improvement > 0:
reward += improvement * 10 # *10
if not (value in self.sizes_explored):
reward += 1
self.sizes_explored.append(value)
if not (position in self.position_explored):
reward += 1
self.position_explored.append(position)
if self.fitness > self.max_fitness:
reward += self.fitness
#self.render()
self.prev_fitness = self.fitness
self.reward = reward
#self.all_fitness.append(self.fitness)
#self.all_states.append(current_state)
self.steps += 1
info = {}
if self.policy == "MlpPolicy":
observations = [coordinate for tuple in self.state for coordinate in tuple]
elif self.policy == "CnnPolicy":
map_points = self.map_points.astype('uint8')*255
observations = np.reshape(map_points, (cf.model["map_size"], cf.model["map_size"], 1))
else:
raise ValueError("Invalid policy type")
return np.array(observations, dtype=np.int8), reward, self.done, info
def reset(self):
self.generate_init_state()
self.prev_fitness, points, map_points = eval_fitness(self.state)
#self.all_states = []
#self.all_fitness = []
self.fitness = 0
self.steps = 1
self.done = False
if self.policy == "MlpPolicy":
observations = [coordinate for tuple in self.state for coordinate in tuple]
elif self.policy == "CnnPolicy":
map_points = map_points.astype('uint8')*255
observations = np.reshape(map_points, (cf.model["map_size"], cf.model["map_size"], 1))
else:
raise ValueError("Invalid policy type")
return np.array(observations, dtype=np.int8)
def render(self, mode="human"):
fig, ax = plt.subplots(figsize=(12, 12))
road_x = []
road_y = []
for p in self.points:
road_x.append(p[0])
road_y.append(p[1])
a_star = AStarPlanner(road_x, road_y, cf.model["grid_size"], cf.model["robot_radius"])
r_x, r_y, _ = a_star.planning(
cf.model["start"], cf.model["start"], cf.model["goal"], cf.model["goal"]
)
path = list(zip(r_x, r_y))
robot_path = LineString(path)
fit = robot_path.length
ax.plot(r_x, r_y, "-r", label="Robot path")
title = f"Scenario reward: {self.reward}, scenario fitness: {self.fitness}"
ax.set_title(title)
ax.scatter(road_x, road_y, s=150, marker="s", color="k", label="Walls")
map_size = cf.model["map_size"]
ax.tick_params(axis="both", which="major", labelsize=18)
ax.set_xlim(0, map_size)
ax.set_ylim(0, map_size)
ax.legend(fontsize=22)
img_path = cf.files["img_path"]
os.makedirs(img_path, exist_ok=True)
if self.evaluate:
fig.savefig(
f"{img_path}{self.episode}_{fit:.2f}.png",
bbox_inches="tight"
)
else:
os.makedirs("debug", exist_ok=True)
fig.savefig("debug\\debug_step_" + str(self.steps) + ".png")
plt.close(fig)
def set_state(self, action):
"""
This function takes an action and returns a list with modified values for the second and third
elements.
:param action: The input action to be performed in the environment. It is a list containing three
elements:
:return: a list with three elements: the first element is the same as the first element of the input
`action` list, the second element is the sum of the second element of the input `action` list and a
constant value `cf.model["min_len"]`, and the third element is the sum of the third element of the
input `action` list and a constant value `
"""
obs_size = action[1] + cf.model["min_len"]
position = action[2] + cf.model["min_pos"]
return [action[0], obs_size, position]