-
Notifications
You must be signed in to change notification settings - Fork 10
/
environments.py
219 lines (177 loc) · 7.56 KB
/
environments.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
import numpy as np
from typing import Tuple
from collections import namedtuple
import gym
from gym import spaces
from gym.utils import seeding
gym.logger.set_level(40) # noqa
from gym_hybrid.agents import BaseAgent
from gym_hybrid.agents import MovingAgent
from gym_hybrid.agents import SlidingAgent
# Action Id
ACCELERATE = 0
TURN = 1
BREAK = 2
Target = namedtuple('Target', ['x', 'y', 'radius'])
class Action:
""""
Action class to store and standardize the action for the environment.
"""
def __init__(self, id_: int, parameters: list):
""""
Initialization of an action.
Args:
id_: The id of the selected action.
parameters: The parameters of an action.
"""
self.id = id_
self.parameters = parameters
@property
def parameter(self) -> float:
""""
Property method to return the parameter related to the action selected.
Returns:
The parameter related to this action_id
"""
if len(self.parameters) == 2:
return self.parameters[self.id]
else:
return self.parameters[0]
class BaseEnv(gym.Env, ):
def __init__(self, seed=None, max_turn=np.pi/2, max_acceleration=0.5, delta_t=0.005, max_step=200, penalty=0.001,
break_value=0.1):
# Agent Parameters
self.max_turn = max_turn
self.max_acceleration = max_acceleration
self.break_value = break_value
# Environment Parameters
self.delta_t = delta_t
self.max_step = max_step
self.field_size = 1.0
self.target_radius = 0.1
self.penalty = penalty
# Initialization
self.seed(seed)
self.target = None
self.viewer = None
self.current_step = None
self.agent = BaseAgent(break_value=break_value, delta_t=delta_t)
parameters_min = np.array([0, -1])
parameters_max = np.array([1, +1])
self.action_space = spaces.Tuple((spaces.Discrete(3),
spaces.Box(parameters_min, parameters_max)))
self.observation_space = spaces.Box(np.ones(10), -np.ones(10))
def seed(self, seed=None) -> list:
self.np_random, seed = seeding.np_random(seed) # noqa
return [seed]
def reset(self) -> list:
self.current_step = 0
limit = self.field_size-self.target_radius
low = [-limit, -limit, self.target_radius]
high = [limit, limit, self.target_radius]
self.target = Target(*self.np_random.uniform(low, high))
low = [-self.field_size, -self.field_size, 0]
high = [self.field_size, self.field_size, 2 * np.pi]
self.agent.reset(*self.np_random.uniform(low, high))
return self.get_state()
def step(self, raw_action: Tuple[int, list]) -> Tuple[list, float, bool, dict]:
action = Action(*raw_action)
last_distance = self.distance
self.current_step += 1
if action.id == TURN:
rotation = self.max_turn * max(min(action.parameter, 1), -1)
self.agent.turn(rotation)
elif action.id == ACCELERATE:
acceleration = self.max_acceleration * max(min(action.parameter, 1), 0)
self.agent.accelerate(acceleration)
elif action.id == BREAK:
self.agent.break_()
if self.distance < self.target_radius and self.agent.speed == 0:
reward = self.get_reward(last_distance, True)
done = True
elif abs(self.agent.x) > self.field_size or abs(self.agent.y) > self.field_size or self.current_step > self.max_step:
reward = -1
done = True
else:
reward = self.get_reward(last_distance)
done = False
return self.get_state(), reward, done, {}
def get_state(self) -> list:
state = [
self.agent.x,
self.agent.y,
self.agent.speed,
np.cos(self.agent.theta),
np.sin(self.agent.theta),
self.target.x,
self.target.y,
self.distance,
0 if self.distance > self.target_radius else 1,
self.current_step / self.max_step
]
return state
def get_reward(self, last_distance: float, goal: bool = False) -> float:
return last_distance - self.distance - self.penalty + (1 if goal else 0)
@property
def distance(self) -> float:
return self.get_distance(self.agent.x, self.agent.y, self.target.x, self.target.y)
@staticmethod
def get_distance(x1: float, y1: float, x2: float, y2: float) -> float:
return np.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
def render(self, mode='human'):
screen_width = 400
screen_height = 400
unit_x = screen_width / 2
unit_y = screen_height / 2
agent_radius = 0.05
if self.viewer is None:
from gym.envs.classic_control import rendering
self.viewer = rendering.Viewer(screen_width, screen_height)
agent = rendering.make_circle(unit_x * agent_radius)
self.agent_trans = rendering.Transform(translation=(unit_x * (1 + self.agent.x), unit_y * (1 + self.agent.y))) # noqa
agent.add_attr(self.agent_trans)
agent.set_color(0.1, 0.3, 0.9)
self.viewer.add_geom(agent)
t, r, m = 0.1 * unit_x, 0.04 * unit_y, 0.06 * unit_x
arrow = rendering.FilledPolygon([(t, 0), (m, r), (m, -r)])
self.arrow_trans = rendering.Transform(rotation=self.agent.theta) # noqa
arrow.add_attr(self.arrow_trans)
arrow.add_attr(self.agent_trans)
arrow.set_color(0, 0, 0)
self.viewer.add_geom(arrow)
target = rendering.make_circle(unit_x * self.target_radius)
target_trans = rendering.Transform(translation=(unit_x * (1 + self.target.x), unit_y * (1 + self.target.y)))
target.add_attr(target_trans)
target.set_color(1, 0.5, 0.5)
self.viewer.add_geom(target)
self.arrow_trans.set_rotation(self.agent.theta)
self.agent_trans.set_translation(unit_x * (1 + self.agent.x), unit_y * (1 + self.agent.y))
return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def close(self):
if self.viewer:
self.viewer.close()
self.viewer = None
class MovingEnv(BaseEnv):
def __init__(self,
seed: int = None,
max_turn: float = np.pi/2,
max_acceleration: float = 0.5,
delta_t: float = 0.005,
max_step: int = 200,
penalty: float = 0.001,
break_value: float = 0.1):
super(MovingEnv, self).__init__(seed=seed, max_turn=max_turn, max_acceleration=max_acceleration,
delta_t=delta_t, max_step=max_step, penalty=penalty, break_value=break_value)
self.agent = MovingAgent(break_value=break_value, delta_t=delta_t)
class SlidingEnv(BaseEnv):
def __init__(self,
seed: int = None,
max_turn: float = np.pi/2,
max_acceleration: float = 0.5,
delta_t: float = 0.005,
max_step: int = 200,
penalty: float = 0.001,
break_value: float = 0.1):
super(SlidingEnv, self).__init__(seed=seed, max_turn=max_turn, max_acceleration=max_acceleration,
delta_t=delta_t, max_step=max_step, penalty=penalty, break_value=break_value)
self.agent = SlidingAgent(break_value=break_value, delta_t=delta_t)