Signal8 3.5__tar.gz → 3.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of Signal8 might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: Signal8
3
- Version: 3.5
3
+ Version: 3.7
4
4
  Summary: A multi-agent environment inspired by the Lewis Signaling Game, featuring eight unique problem configurations with both static and dynamic obstacles.
5
5
  Home-page: https://github.com/ethanmclark1/signal8
6
6
  Author: Ethan Clark
@@ -40,8 +40,8 @@ pip install -e .
40
40
  ```
41
41
  import Signal8
42
42
 
43
- env = Signal8.env(problem_type='disaster_response')
44
- env.reset(options={"instance_num": 0}))
43
+ env = Signal8.env()
44
+ env.reset(options={'problem_instance': 'corners'}))
45
45
  observation, _, terminations, truncations, _ = env.last()
46
46
  env.step(action)
47
47
  env.close()
@@ -49,16 +49,16 @@ env.close()
49
49
 
50
50
  ## List of Problem Instances
51
51
 
52
- | Problem Type | Instance Name | Visualization |
53
- | :---------------: | :------------: | :--------------------------------------------: |
54
- | Disaster Response | ``instance 0`` | ![1686023513741](image/README/1686023513741.png) |
55
- | Disaster Response | ``instance 1`` | ![1686023519237](image/README/1686023519237.png) |
56
- | Disaster Response | ``instance 2`` | ![1686023523656](image/README/1686023523656.png) |
57
- | Disaster Response | ``instance 3`` | ![1686023529431](image/README/1686023529431.png) |
58
- | Precision Farming | ``instance 0`` | ![1686023534970](image/README/1686023534970.png) |
59
- | Precision Farming | ``instance 1`` | ![1686023540501](image/README/1686023540501.png) |
60
- | Precision Farming | ``instance 2`` | ![1686023545379](image/README/1686023545379.png) |
61
- | Precision Farming | ``instance 3`` | ![1686023549246](image/README/1686023549246.png) |
52
+ | Problem Instance | Visualization |
53
+ | :--------------: | :--------------------------------------------: |
54
+ | ``corners``  | ![1686604788813](image/README/1686604788813.png) |
55
+ | ``quarters`` | ![1686604799335](image/README/1686604799335.png) |
56
+ | ``cross`` | ![1686604808540](image/README/1686604808540.png) |
57
+ | ``cluster`` | ![1686604839072](image/README/1686604839072.png) |
58
+ | ``left`` | ![1686604845732](image/README/1686604845732.png) |
59
+ | ``right`` | ![1686604851758](image/README/1686604851758.png) |
60
+ | ``up`` | ![1686604859851](image/README/1686604859851.png) |
61
+ | ``down`` | ![1686604868138](image/README/1686604868138.png) |
62
62
 
63
63
  Each of the colored regions represents an area where the respective entity can be instantiated. Blue regions are starting regions, yellow regions represent goal regions and in the case of precision farming, if a goal region is not generated in the yellow region then changes to a static obstacle region but this is not the case for disaster response, instead the region where the goal was not instantiated does not impact the episode at all, the light red regions represent static obstacles, and dark red regions represent dynamic obstacles.
64
64
 
@@ -28,8 +28,8 @@ pip install -e .
28
28
  ```
29
29
  import Signal8
30
30
 
31
- env = Signal8.env(problem_type='disaster_response')
32
- env.reset(options={"instance_num": 0}))
31
+ env = Signal8.env()
32
+ env.reset(options={'problem_instance': 'corners'}))
33
33
  observation, _, terminations, truncations, _ = env.last()
34
34
  env.step(action)
35
35
  env.close()
@@ -37,16 +37,16 @@ env.close()
37
37
 
38
38
  ## List of Problem Instances
39
39
 
40
- | Problem Type | Instance Name | Visualization |
41
- | :---------------: | :------------: | :--------------------------------------------: |
42
- | Disaster Response | ``instance 0`` | ![1686023513741](image/README/1686023513741.png) |
43
- | Disaster Response | ``instance 1`` | ![1686023519237](image/README/1686023519237.png) |
44
- | Disaster Response | ``instance 2`` | ![1686023523656](image/README/1686023523656.png) |
45
- | Disaster Response | ``instance 3`` | ![1686023529431](image/README/1686023529431.png) |
46
- | Precision Farming | ``instance 0`` | ![1686023534970](image/README/1686023534970.png) |
47
- | Precision Farming | ``instance 1`` | ![1686023540501](image/README/1686023540501.png) |
48
- | Precision Farming | ``instance 2`` | ![1686023545379](image/README/1686023545379.png) |
49
- | Precision Farming | ``instance 3`` | ![1686023549246](image/README/1686023549246.png) |
40
+ | Problem Instance | Visualization |
41
+ | :--------------: | :--------------------------------------------: |
42
+ | ``corners``  | ![1686604788813](image/README/1686604788813.png) |
43
+ | ``quarters`` | ![1686604799335](image/README/1686604799335.png) |
44
+ | ``cross`` | ![1686604808540](image/README/1686604808540.png) |
45
+ | ``cluster`` | ![1686604839072](image/README/1686604839072.png) |
46
+ | ``left`` | ![1686604845732](image/README/1686604845732.png) |
47
+ | ``right`` | ![1686604851758](image/README/1686604851758.png) |
48
+ | ``up`` | ![1686604859851](image/README/1686604859851.png) |
49
+ | ``down`` | ![1686604868138](image/README/1686604868138.png) |
50
50
 
51
51
  Each of the colored regions represents an area where the respective entity can be instantiated. Blue regions are starting regions, yellow regions represent goal regions and in the case of precision farming, if a goal region is not generated in the yellow region then changes to a static obstacle region but this is not the case for disaster response, instead the region where the goal was not instantiated does not impact the episode at all, the light red regions represent static obstacles, and dark red regions represent dynamic obstacles.
52
52
 
@@ -0,0 +1,245 @@
1
+ import copy
2
+ import numpy as np
3
+ import matplotlib.path as mpath
4
+
5
+ from functools import partial
6
+ from .utils.scenario import BaseScenario
7
+ from .utils.simple_env import SimpleEnv, make_env
8
+ from .utils.core import Agent, Goal, Obstacle, World
9
+ from .utils.problems import get_problem_list, get_problem_instance
10
+
11
+ from gymnasium.utils import EzPickle
12
+
13
+
14
+ class raw_env(SimpleEnv, EzPickle):
15
+ def __init__(
16
+ self,
17
+ num_agents=1,
18
+ num_large_obstacles=4,
19
+ num_small_obstacles=10,
20
+ render_mode=None
21
+ ):
22
+
23
+ if num_agents > 2:
24
+ raise ValueError("Signal8 currently can only support up to 2 agents.")
25
+
26
+ if num_large_obstacles > 4:
27
+ raise ValueError("Signal8 can only support up to 4 obstacles.")
28
+
29
+ scenario = Scenario()
30
+ world = scenario.make_world(num_agents, num_large_obstacles, num_small_obstacles)
31
+
32
+ super().__init__(
33
+ scenario=scenario,
34
+ world=world,
35
+ render_mode=render_mode,
36
+ max_cycles=500,
37
+ )
38
+
39
+ env = make_env(raw_env)
40
+
41
+ class Scenario(BaseScenario):
42
+ def make_world(self, num_agents, num_large_obstacles, num_small_obstacles):
43
+ world = World()
44
+ world.problem_list = get_problem_list()
45
+
46
+ for i in range(num_agents):
47
+ agent = Agent()
48
+ agent.name = f"agent_{i}"
49
+ agent.collide = True
50
+ agent.color = np.array([1, 0.95, 0.8])
51
+ world.agents.append(agent)
52
+
53
+ # Agents has two goals (i.e., actual goal and start position)
54
+ # This is to ensure agent returns safely to start position after reaching goal
55
+ for i in range(num_agents*2):
56
+ goal = Goal()
57
+ goal.name = f"goal_{i}"
58
+ goal.collide = False
59
+ # goal_a is the actual goal
60
+ if i < num_agents:
61
+ goal.color = np.array([0.835, 0.90, 0.831])
62
+ # goal_b is the start position to return to after reaching goal
63
+ else:
64
+ goal.color = np.array([0.85, 0.90, 0.99])
65
+ world.goals.append(goal)
66
+
67
+ # Large obstacles can only be observed by aerial agent
68
+ for i in range(num_large_obstacles):
69
+ obstacle = Obstacle(size=0.1)
70
+ obstacle.name = f"obs_{i}"
71
+ obstacle.color = np.array([0.97, 0.801, 0.8])
72
+ world.large_obstacles.append(obstacle)
73
+
74
+ # Small obstacles can only be observed by ground agent(s)
75
+ for i in range(num_small_obstacles):
76
+ obstacle = Obstacle(size=0.01)
77
+ obstacle.name = f"obs_{i}"
78
+ obstacle.color = np.array([0.97, 0.801, 0.8])
79
+ world.small_obstacles.append(obstacle)
80
+
81
+ world.buffer_dist = world.agents[0].size + world.large_obstacles[0].size
82
+ return world
83
+
84
+ # Get constraints on entities given problem instance name
85
+ def _set_problem_instance(self, world, instance_name):
86
+ instance_constr = get_problem_instance(instance_name)
87
+ world.problem_instance = instance_name
88
+ world.instance_constr = instance_constr
89
+
90
+ # Generate valid points according to some condition
91
+ def _generate_position(self, np_random, condition):
92
+ while True:
93
+ point = np_random.uniform(-1, +1, 2)
94
+ if condition(point):
95
+ break
96
+ return point
97
+
98
+ # Check if point is outside of rectangular obstacle regions
99
+ def _safe_position(self, point, epsilon, x_constraints, y_constraints):
100
+ return all(not (low - epsilon <= point[0] <= high + epsilon) for low, high in x_constraints) \
101
+ and all(not (low - epsilon <= point[1] <= high + epsilon) for low, high in y_constraints)
102
+
103
+ # Check if point is outside of triangular obstacle regions
104
+ def _outside_triangle(self, point, paths, epsilon):
105
+ enlarged_paths = []
106
+ for path in paths:
107
+ centroid = np.mean(path.vertices[:-1], axis=0)
108
+ enlarged_vertices = path.vertices + epsilon * (path.vertices - centroid)
109
+ enlarged_paths.append(mpath.Path(enlarged_vertices))
110
+ return not any(path.contains_points(point[None, :]) for path in enlarged_paths)
111
+
112
+ # Reset agents and goals to their initial positions
113
+ def _reset_agents_and_goals(self, world, np_random, paths):
114
+ epsilon = world.buffer_dist
115
+
116
+ if paths is None:
117
+ x_constraints = [constr[0] for constr in world.instance_constr]
118
+ y_constraints = [constr[1] for constr in world.instance_constr]
119
+
120
+ for i, agent in enumerate(world.agents):
121
+ agent.goal_a = world.goals[i]
122
+ agent.goal_b = world.goals[len(world.goals) - 1 - i]
123
+
124
+ agent.state.p_vel = np.zeros(world.dim_p)
125
+ agent.goal_a.state.p_vel = np.zeros(world.dim_p)
126
+ agent.goal_b.state.p_vel = np.zeros(world.dim_p)
127
+
128
+ if world.problem_instance == 'corners':
129
+ condition = partial(
130
+ self._outside_triangle,
131
+ paths=paths,
132
+ epsilon=epsilon
133
+ )
134
+ else:
135
+ condition = partial(
136
+ self._safe_position,
137
+ epsilon=epsilon,
138
+ x_constraints=x_constraints,
139
+ y_constraints=y_constraints
140
+ )
141
+
142
+ agent.state.p_pos = self._generate_position(np_random, condition)
143
+ agent.goal_a.state.p_pos = self._generate_position(np_random, condition)
144
+ agent.goal_b.state.p_pos = copy.copy(agent.state.p_pos)
145
+
146
+ # Reset all large obstacles to a position that does not intersect with the agents
147
+ def _reset_large_obstacles(self, world, np_random, paths):
148
+ def inside_triangle_condition(point):
149
+ return any(path.contains_points(point[None, :]) for path in paths)
150
+
151
+ occupied_triangles = set()
152
+ for i, large_obstacle in enumerate(world.large_obstacles):
153
+ large_obstacle.state.p_vel = np.zeros(world.dim_p)
154
+ idx = i % len(world.instance_constr)
155
+
156
+ # Each corner may only have one large_obstacle in it
157
+ if world.problem_instance == 'corners':
158
+ while True:
159
+ pos = self._generate_position(np_random, inside_triangle_condition)
160
+ triangle_idx = next((i for i, path in enumerate(paths) if path.contains_points(pos[None, :])), None)
161
+ if triangle_idx not in occupied_triangles:
162
+ large_obstacle.state.p_pos = pos
163
+ occupied_triangles.add(triangle_idx)
164
+ break
165
+ else:
166
+ large_obstacle.state.p_pos = np_random.uniform(*zip(*world.instance_constr[idx]))
167
+
168
+ # Reset all small obstacles to a position that does not intersect with the agents or the large obstacles
169
+ def _reset_small_obstacles(self, world, np_random, paths):
170
+ epsilon = world.small_obstacles[0].size + world.large_obstacles[0].size
171
+
172
+ x_constraints = [constr[0] for constr in world.instance_constr]
173
+ y_constraints = [constr[1] for constr in world.instance_constr]
174
+ agent_positions = [agent.state.p_pos for agent in world.agents]
175
+ goal_positions = [goal.state.p_pos for goal in world.goals]
176
+ large_obstacle_positions = [obstacle.state.p_pos for obstacle in world.large_obstacles]
177
+
178
+ def safe_position(point):
179
+ if world.problem_instance == 'corners':
180
+ within_obstacle_constraints = any(path.contains_points(point[None, :]) for path in paths)
181
+ else:
182
+ within_obstacle_constraints = any(low - epsilon <= point[0] <= high + epsilon for low, high in x_constraints) \
183
+ or any(low - epsilon <= point[1] <= high + epsilon for low, high in y_constraints)
184
+
185
+ within_other_positions = any(np.linalg.norm(point - pos) <= epsilon for pos in agent_positions + goal_positions + large_obstacle_positions)
186
+
187
+ return not (within_obstacle_constraints or within_other_positions)
188
+
189
+ for small_obstacle in world.small_obstacles:
190
+ small_obstacle.state.p_vel = np.zeros(world.dim_p)
191
+ small_obstacle.state.p_pos = self._generate_position(np_random, safe_position)
192
+
193
+ def reset_world(self, world, np_random, problem_instance):
194
+ self._set_problem_instance(world, problem_instance)
195
+
196
+ paths = [mpath.Path(np.array(triangle)) for triangle in world.instance_constr] \
197
+ if world.problem_instance == 'corners' else None
198
+
199
+ self._reset_agents_and_goals(world, np_random, paths)
200
+ self._reset_large_obstacles(world, np_random, paths)
201
+ self._reset_small_obstacles(world, np_random, paths)
202
+
203
+ # Reward given by agents to agents for reaching their respective goals
204
+ def reward(self, agent, world):
205
+ return 0
206
+
207
+ # Ground agents can only observe the positions of other agents, goals, and small obstacles
208
+ def observation(self, agent, world):
209
+ agent_pos = agent.state.p_pos
210
+ agent_vel = agent.state.p_vel
211
+
212
+ num_agents = len(world.agents)
213
+ num_small_obstacles = len(world.small_obstacles)
214
+ max_observable_dist = agent.max_observable_dist
215
+
216
+ observed_agents = [np.full_like(agent_pos, max_observable_dist) for _ in range(num_agents - 1)]
217
+ observed_goal = np.full_like(agent_pos, max_observable_dist)
218
+ observed_obstacles = [np.full_like(agent_pos, max_observable_dist) for _ in range(num_small_obstacles)]
219
+
220
+ idx = 0
221
+ for other_agent in world.agents:
222
+ if agent.name == other_agent.name:
223
+ continue
224
+ else:
225
+ other_agent_pos = other_agent.state.p_pos
226
+ relative_pos = other_agent_pos - agent_pos
227
+ dist = np.linalg.norm(relative_pos)
228
+ if dist <= max_observable_dist:
229
+ observed_agents[idx] = relative_pos
230
+ idx += 1
231
+
232
+ for i, small_obstacle in enumerate(world.small_obstacles):
233
+ obs_pos = small_obstacle.state.p_pos
234
+ relative_pos = obs_pos - agent_pos
235
+ dist = np.linalg.norm(relative_pos)
236
+ if dist <= max_observable_dist:
237
+ observed_obstacles[i] = relative_pos
238
+
239
+ goal_pos = agent.goal_b.state.p_pos if agent.reached_goal else agent.goal_a.state.p_pos
240
+ relative_goal_pos = goal_pos - agent_pos
241
+ goal_dist = np.linalg.norm(relative_goal_pos)
242
+ if goal_dist <= max_observable_dist:
243
+ observed_goal = relative_goal_pos
244
+
245
+ return np.concatenate((agent_pos, agent_vel, np.concatenate(observed_obstacles, axis=0), observed_goal))
@@ -0,0 +1,9 @@
1
+ import Signal8
2
+
3
+ env = Signal8.env()
4
+ env.reset(options={'problem_instance': 'cross'})
5
+ start_state = env.state()
6
+ observation, _, terminations, truncations, _ = env.last()
7
+ env.step(1)
8
+ env.step(1)
9
+ env.close()
@@ -19,7 +19,7 @@ class Entity: # properties and state of physical world entity
19
19
  # name
20
20
  self.name = ""
21
21
  # properties:
22
- self.size = 0.1
22
+ self.size = 0.050
23
23
  # entity can move / be pushed
24
24
  self.movable = False
25
25
  # entity collides with others
@@ -42,25 +42,12 @@ class Goal(Entity): # properties of goal entities
42
42
 
43
43
 
44
44
  class Obstacle(Entity): # properties of obstacles entities
45
- def __init__(self):
45
+ def __init__(self, size):
46
46
  super().__init__()
47
47
  # entity can be moved / pushed
48
48
  self.movable = False
49
- # script behavior to execute
50
- self.action_callback = None
51
- self.lock = None
52
-
53
- # updates the state of the obstacle
54
- def update(self, action=None, size=None, dt=0.1, damping=0.25):
55
- with self.lock:
56
- if action is not None:
57
- self.state.p_vel = self.state.p_vel * (1 - damping)
58
- self.state.p_vel += (action / self.mass) * dt
59
- self.state.p_pos += self.state.p_vel * dt
60
- elif size is not None:
61
- self.size += size
62
- else:
63
- raise ValueError("Either action or size must be specified")
49
+ # entity size
50
+ self.size = size
64
51
 
65
52
 
66
53
  class Agent(Entity): # properties of agent entities
@@ -73,12 +60,11 @@ class Agent(Entity): # properties of agent entities
73
60
  self.reached_goal = False
74
61
  # reached start state after reaching goal
75
62
  self.goal_b = None
76
- self.returned_to_safety = False
77
63
  # state
78
64
  self.state = AgentState()
79
65
  # action
80
66
  self.action = None
81
- # how far the agent can see
67
+ # how far the agent can sense around itself
82
68
  self.max_observable_dist = 0.30
83
69
 
84
70
  class World: # multi-agent world
@@ -86,7 +72,8 @@ class World: # multi-agent world
86
72
  # list of agents and entities (can change at execution-time!)
87
73
  self.agents = []
88
74
  self.goals = []
89
- self.obstacles = []
75
+ self.small_obstacles = []
76
+ self.large_obstacles = []
90
77
  # position dimensionality
91
78
  self.dim_p = 2
92
79
  # color dimensionality
@@ -98,17 +85,16 @@ class World: # multi-agent world
98
85
  # contact response parameters
99
86
  self.contact_force = 1e2
100
87
  self.contact_margin = 1e-3
101
- self.problem_type = None
88
+ # minimum distance required between entities (used for resetting positions)
89
+ self.buffer_dist = 0
90
+ # problem instance
102
91
  self.problem_instance = None
103
- self.start_constr = None
104
- self.goal_constr = None
105
- self.static_obstacle_constr = None
106
- self.dynamic_obstacle_constr = None
92
+ self.instance_constr = None
107
93
 
108
94
  # return all entities in the world
109
95
  @property
110
96
  def entities(self):
111
- return self.agents + self.goals + self.obstacles
97
+ return self.agents + self.goals + self.large_obstacles
112
98
 
113
99
  # update state of the world
114
100
  def step(self):
@@ -0,0 +1,43 @@
1
+ # Constraints on obstacle positions
2
+ problems = {
3
+ 'corners': [
4
+ ((1, 1), (1, 0.75), (0.75, 1)),
5
+ ((1, -1), (1, -0.75), (0.75, -1)),
6
+ ((-1, -1), (-1, -0.75), (-0.75, -1)),
7
+ ((-1, 1), (-1, 0.75), (-0.75, 1)),
8
+ ],
9
+ 'quarters': [
10
+ ((-1, -0.375), (-0.375, 0.375)),
11
+ ((-0.375, 0.375), (0.375, 1)),
12
+ ((0.375, 1), (-0.375, 0.375)),
13
+ ((-0.375, 0.375), (-1, -0.375)),
14
+ ],
15
+ 'cross': [
16
+ ((-0.1875, 0.1875), (0, 0.7)),
17
+ ((-0.1875, 0.1875), (-0.7, 0)),
18
+ ((-0.7, 0), (-0.1875, 0.1875)),
19
+ ((0, 0.7), (-0.1875, 0.1875)),
20
+ ],
21
+ 'cluster': [
22
+ ((-0.50, 0.50), (-0.50, 0.50)),
23
+ ],
24
+ 'left': [
25
+ ((-1, 0), (-1, 1))
26
+ ],
27
+ 'right': [
28
+ ((0, 1), (-1, 1))
29
+ ],
30
+ 'up': [
31
+ ((-1, 1), (0, 1))
32
+ ],
33
+ 'down': [
34
+ ((-1, 1), (-1, 0))
35
+ ],
36
+ }
37
+
38
+ def get_problem_list():
39
+ return list(problems.keys())
40
+
41
+ def get_problem_instance(instance_name):
42
+ instance_constr = problems[instance_name]
43
+ return instance_constr
@@ -44,14 +44,15 @@ class SimpleEnv(AECEnv):
44
44
  self.game_font = pygame.freetype.Font(None, 20)
45
45
 
46
46
  self.renderOn = False
47
- self._reset_called = False
48
47
  self.seed()
48
+ self._reset_called = False
49
49
 
50
50
  self.max_cycles = max_cycles
51
51
  self.scenario = scenario
52
52
  self.world = world
53
53
  self.local_ratio = local_ratio
54
54
 
55
+ self.scenario.reset_world(self.world, self.np_random, 'corners')
55
56
  self.agents = [agent.name for agent in self.world.agents]
56
57
  self.possible_agents = self.agents[:]
57
58
  self._index_map = {agent.name: idx for idx, agent in enumerate(self.world.agents)}
@@ -61,17 +62,26 @@ class SimpleEnv(AECEnv):
61
62
  # set spaces
62
63
  self.action_spaces = dict()
63
64
  self.observation_spaces = dict()
64
- obs_dim = 14 if world.problem_type == 'disaster_response' else 24
65
+
65
66
  for agent in self.world.agents:
66
67
  space_dim = self.world.dim_p * 2 + 1
67
68
  self.action_spaces[agent.name] = spaces.Discrete(space_dim)
69
+ obs_dim = len(self.scenario.observation(agent, self.world))
68
70
  self.observation_spaces[agent.name] = spaces.Box(
69
71
  low=-np.float32(1),
70
72
  high=+np.float32(1),
71
73
  shape=(obs_dim,),
72
74
  dtype=np.float32,
73
75
  )
74
-
76
+
77
+ # state space is used by aerial agent; it encodes the positions of all agents, obstacles, and goals
78
+ state_dim = len(world.agents) * 2 + len(world.large_obstacles) * 2 + len(world.agents) * 2
79
+ self.state_space = spaces.Box(
80
+ low=-np.float32(1),
81
+ high=+np.float32(1),
82
+ shape=(state_dim,),
83
+ dtype=np.float32,
84
+ )
75
85
  self.steps = 0
76
86
  self.current_actions = [None] * self.num_agents
77
87
 
@@ -90,30 +100,39 @@ class SimpleEnv(AECEnv):
90
100
  ).astype(np.float32)
91
101
 
92
102
  def state(self):
93
- states = tuple(
94
- self.scenario.observation(
95
- self.world.agents[self._index_map[agent]], self.world
96
- ).astype(np.float32)
97
- for agent in self.possible_agents
98
- )
99
- return np.concatenate(states, axis=None)
103
+ if self.steps > 0:
104
+ raise Exception('The state of the system can only be retrieved at the start of an episode before any steps have been taken.')
105
+
106
+ state = np.zeros(self.state_space.shape)
107
+
108
+ for i, agent in enumerate(self.world.agents):
109
+ state[2*i : 2*i+2] = agent.state.p_pos
110
+
111
+ start_idx = 2 * len(self.world.agents)
112
+ for i, obstacle in enumerate(self.world.large_obstacles):
113
+ state[start_idx + 2*i : start_idx + 2*i+2] = obstacle.state.p_pos
114
+
115
+ start_idx = 2 * len(self.world.agents) + 2 * len(self.world.large_obstacles)
116
+ for i, agent in enumerate(self.world.agents):
117
+ state[start_idx + 2*i : start_idx + 2*i+2] = agent.goal_a.state.p_pos
118
+
119
+ return np.concatenate(state, axis=None)
100
120
 
101
121
  def reset(self, seed=None, return_info=False, options=None):
102
122
  if seed is not None:
103
123
  self.seed(seed=seed)
104
124
 
105
- if 'instance_num' not in options:
106
- raise ValueError("Must provide an instance_num to reset the environment with.")
125
+ if 'problem_instance' not in options:
126
+ raise ValueError("Must provide a problem_instance to reset the environment with.")
107
127
 
108
- instance_num = options['instance_num']
109
- if not 0 <= instance_num <= 3:
110
- raise ValueError("instance_num must be between 0 and 3.")
128
+ problem_instance = options['problem_instance']
129
+ if problem_instance not in self.world.problem_list:
130
+ raise ValueError("problem_instance must be in the problem_list.")
111
131
 
112
- self.scenario.reset_world(self.world, self.np_random, instance_num)
132
+ self.scenario.reset_world(self.world, self.np_random, problem_instance)
113
133
 
114
134
  self.agents = self.possible_agents[:]
115
- # PettingZoo Gymansium requires rewards to be set
116
- # even if they are not used
135
+ # PettingZoo Gymansium requires rewards to be set even if not used
117
136
  self.rewards = {name: 0.0 for name in self.agents}
118
137
  self._cumulative_rewards = {name: 0.0 for name in self.agents}
119
138
  self.terminations = {name: False for name in self.agents}
@@ -125,18 +144,14 @@ class SimpleEnv(AECEnv):
125
144
  self.steps = 0
126
145
 
127
146
  self.current_actions = [None] * len(self.world.agents)
128
-
147
+
129
148
  def get_start_state(self):
130
149
  if not self._reset_called:
131
150
  raise Exception("Cannot get start state without calling reset() first")
132
151
 
133
152
  agent_pos = np.array([agent.state.p_pos for agent in self.world.agents])
134
153
  goal_pos = np.array([(agent.goal_a.state.p_pos, agent.goal_b.state.p_pos) for agent in self.world.agents])
135
- try:
136
- [obs.lock.acquire() for obs in self.world.obstacles if obs.lock is not None]
137
- obs_pos = np.array([obs.state.p_pos for obs in self.world.obstacles])
138
- finally:
139
- [obs.lock.release() for obs in self.world.obstacles if obs.lock is not None]
154
+ obs_pos = np.array([obs.state.p_pos for obs in self.world.large_obstacles])
140
155
 
141
156
  entities = {'agents': agent_pos, 'goals': goal_pos, 'obstacles': obs_pos}
142
157
  return entities
@@ -192,44 +207,36 @@ class SimpleEnv(AECEnv):
192
207
  assert len(action) == 0
193
208
 
194
209
  # Check if episode is terminated or truncated
195
- def _episode_status(self):
196
- dynamic_obs = [obs for obs in self.world.obstacles if obs.movable]
197
- static_obs = [obs for obs in self.world.obstacles if not obs.movable]
198
-
210
+ def _episode_status(self):
199
211
  goal_dist_threshold = self.world.agents[0].size + self.world.agents[0].goal_a.size
200
- static_obs_threshold = self.world.agents[0].size + static_obs[0].size
212
+ small_obs_threshold = self.world.agents[0].size + self.world.small_obstacles[0].size
213
+ large_obs_threshold = self.world.agents[0].size + self.world.large_obstacles[0].size
214
+
215
+ small_obstacles = [obs for obs in self.world.small_obstacles]
216
+ large_obstacles = [obs for obs in self.world.large_obstacles]
201
217
 
202
218
  goal_a_dist = [np.linalg.norm(agent.state.p_pos - agent.goal_a.state.p_pos) for agent in self.world.agents]
203
219
  goal_b_dist = [np.linalg.norm(agent.state.p_pos - agent.goal_b.state.p_pos) for agent in self.world.agents]
204
- static_obs_dist = [min(np.linalg.norm(agent.state.p_pos - obs.state.p_pos) for obs in static_obs)
205
- for agent in self.world.agents]
206
-
207
- crossed_threshold_static = [dist <= static_obs_threshold for dist in static_obs_dist]
208
-
209
- [obs.lock.acquire() for obs in dynamic_obs]
220
+ small_obs_dist = [min(np.linalg.norm(agent.state.p_pos - obs.state.p_pos) for obs in small_obstacles)
221
+ for agent in self.world.agents]
222
+ large_obs_dist = [min(np.linalg.norm(agent.state.p_pos - obs.state.p_pos) for obs in large_obstacles)
223
+ for agent in self.world.agents]
210
224
 
211
- try:
212
- dynamic_obs_dist = [min(np.linalg.norm(agent.state.p_pos - obs.state.p_pos) for obs in dynamic_obs)
213
- for agent in self.world.agents]
214
- dynamic_obs_threshold = [agent.size + obs.size for agent, obs in zip(self.world.agents, dynamic_obs)]
215
-
216
- crossed_threshold_dynamic = [dist <= threshold for dist, threshold in zip(dynamic_obs_dist, dynamic_obs_threshold)]
225
+ crossed_threshold_small = [dist <= small_obs_threshold for dist in small_obs_dist]
226
+ crossed_threshold_large = [dist <= large_obs_threshold for dist in large_obs_dist]
227
+
228
+ truncations = [(crossed_small or crossed_large) for crossed_small, crossed_large in zip(crossed_threshold_small, crossed_threshold_large)]
229
+ truncations = [True] * self.num_agents if self.steps >= self.max_cycles else truncations
217
230
 
218
- truncations = [crossed_stat or crossed_dyn for crossed_stat, crossed_dyn in zip(crossed_threshold_static, crossed_threshold_dynamic)]
219
- truncations = [True] * self.num_agents if self.steps >= self.max_cycles else truncations
231
+ terminations = [False] * self.num_agents
232
+ for i, dist in enumerate(goal_a_dist):
233
+ if dist <= goal_dist_threshold:
234
+ self.world.agents[i].reached_goal = True
220
235
 
221
- terminations = [False] * self.num_agents
222
- for i, dist in enumerate(goal_a_dist):
236
+ for i, dist in enumerate(goal_b_dist):
237
+ if self.world.agents[i].reached_goal:
223
238
  if dist <= goal_dist_threshold:
224
- self.world.agents[i].reached_goal = True
225
-
226
- for i, dist in enumerate(goal_b_dist):
227
- if self.world.agents[i].reached_goal:
228
- if dist <= goal_dist_threshold:
229
- self.agents[i].reached_safety = True
230
- terminations[i] = True
231
- finally:
232
- [obs.lock.release() for obs in dynamic_obs]
239
+ terminations[i] = True
233
240
 
234
241
  return {'terminations': terminations, 'truncations': truncations}
235
242
 
@@ -317,5 +324,4 @@ class SimpleEnv(AECEnv):
317
324
  if self.renderOn:
318
325
  pygame.event.pump()
319
326
  pygame.display.quit()
320
- self.renderOn = False
321
- self.unwrapped.scenario.stop_scripted_obstacles()
327
+ self.renderOn = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: Signal8
3
- Version: 3.5
3
+ Version: 3.7
4
4
  Summary: A multi-agent environment inspired by the Lewis Signaling Game, featuring eight unique problem configurations with both static and dynamic obstacles.
5
5
  Home-page: https://github.com/ethanmclark1/signal8
6
6
  Author: Ethan Clark
@@ -40,8 +40,8 @@ pip install -e .
40
40
  ```
41
41
  import Signal8
42
42
 
43
- env = Signal8.env(problem_type='disaster_response')
44
- env.reset(options={"instance_num": 0}))
43
+ env = Signal8.env()
44
+ env.reset(options={'problem_instance': 'corners'}))
45
45
  observation, _, terminations, truncations, _ = env.last()
46
46
  env.step(action)
47
47
  env.close()
@@ -49,16 +49,16 @@ env.close()
49
49
 
50
50
  ## List of Problem Instances
51
51
 
52
- | Problem Type | Instance Name | Visualization |
53
- | :---------------: | :------------: | :--------------------------------------------: |
54
- | Disaster Response | ``instance 0`` | ![1686023513741](image/README/1686023513741.png) |
55
- | Disaster Response | ``instance 1`` | ![1686023519237](image/README/1686023519237.png) |
56
- | Disaster Response | ``instance 2`` | ![1686023523656](image/README/1686023523656.png) |
57
- | Disaster Response | ``instance 3`` | ![1686023529431](image/README/1686023529431.png) |
58
- | Precision Farming | ``instance 0`` | ![1686023534970](image/README/1686023534970.png) |
59
- | Precision Farming | ``instance 1`` | ![1686023540501](image/README/1686023540501.png) |
60
- | Precision Farming | ``instance 2`` | ![1686023545379](image/README/1686023545379.png) |
61
- | Precision Farming | ``instance 3`` | ![1686023549246](image/README/1686023549246.png) |
52
+ | Problem Instance | Visualization |
53
+ | :--------------: | :--------------------------------------------: |
54
+ | ``corners``  | ![1686604788813](image/README/1686604788813.png) |
55
+ | ``quarters`` | ![1686604799335](image/README/1686604799335.png) |
56
+ | ``cross`` | ![1686604808540](image/README/1686604808540.png) |
57
+ | ``cluster`` | ![1686604839072](image/README/1686604839072.png) |
58
+ | ``left`` | ![1686604845732](image/README/1686604845732.png) |
59
+ | ``right`` | ![1686604851758](image/README/1686604851758.png) |
60
+ | ``up`` | ![1686604859851](image/README/1686604859851.png) |
61
+ | ``down`` | ![1686604868138](image/README/1686604868138.png) |
62
62
 
63
63
  Each of the colored regions represents an area where the respective entity can be instantiated. Blue regions are starting regions, yellow regions represent goal regions and in the case of precision farming, if a goal region is not generated in the yellow region then changes to a static obstacle region but this is not the case for disaster response, instead the region where the goal was not instantiated does not impact the episode at all, the light red regions represent static obstacles, and dark red regions represent dynamic obstacles.
64
64
 
@@ -4,13 +4,13 @@ setup.py
4
4
  Signal8/Signal8.py
5
5
  Signal8/__init__.py
6
6
  Signal8/main.py
7
+ Signal8/signal8.py
7
8
  Signal8.egg-info/PKG-INFO
8
9
  Signal8.egg-info/SOURCES.txt
9
10
  Signal8.egg-info/dependency_links.txt
10
11
  Signal8.egg-info/top_level.txt
11
12
  Signal8/utils/__init__.py
12
13
  Signal8/utils/core.py
13
- Signal8/utils/npc.py
14
14
  Signal8/utils/problems.py
15
15
  Signal8/utils/scenario.py
16
16
  Signal8/utils/simple_env.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="Signal8",
5
- version="3.5",
5
+ version="3.7",
6
6
  packages=find_packages(),
7
7
  author="Ethan Clark",
8
8
  author_email="eclark715@gmail.com.com",
@@ -1,249 +0,0 @@
1
- import time
2
- import copy
3
- import random
4
- import logging
5
- import threading
6
- import numpy as np
7
-
8
- from .utils.npc import NPC
9
- from .utils.scenario import BaseScenario
10
- from .utils.simple_env import SimpleEnv, make_env
11
- from .utils.core import Agent, Goal, Obstacle, World
12
- from .utils.problems import get_problem_instance
13
-
14
- from gymnasium.utils import EzPickle
15
-
16
-
17
- class raw_env(SimpleEnv, EzPickle):
18
- def __init__(self, problem_type, num_agents=1, render_mode=None):
19
-
20
- if problem_type not in {'disaster_response', 'precision_farming'}:
21
- raise ValueError("Signal8 only supports 'disaster_response' and 'precision_farming' problem types.")
22
-
23
- if num_agents > 2:
24
- raise ValueError("Signal8 currently can only support up to 2 agents.")
25
-
26
- scenario = Scenario()
27
- world = scenario.make_world(problem_type, num_agents)
28
-
29
- super().__init__(
30
- scenario=scenario,
31
- world=world,
32
- render_mode=render_mode,
33
- max_cycles=500,
34
- )
35
-
36
- env = make_env(raw_env)
37
-
38
- class Scenario(BaseScenario):
39
- def make_world(self, problem_type, num_agents):
40
- world = World()
41
- self._add_logger()
42
- world.problem_type = problem_type
43
-
44
- self.npc = []
45
- self.scripted_obstacle_threads = []
46
- self.scripted_obstacle_running = False
47
-
48
- world.agents = [Agent() for _ in range(num_agents)]
49
- for i, agent in enumerate(world.agents):
50
- agent.name = f"agent_{i}"
51
- agent.collide = True
52
-
53
- # Agents has two goals (i.e., actual goal and start position)
54
- # This is to ensure agent returns safely to start position
55
- world.goals = [Goal() for _ in range(len(world.agents*2))]
56
- for i, goal in enumerate(world.goals):
57
- goal.name = f"goal_{i}"
58
- goal.collide = False
59
-
60
- # Minimum number of obstacles in a problem instance
61
- world.obstacles = [Obstacle() for _ in range(4)]
62
- for i, obstacle in enumerate(world.obstacles):
63
- obstacle.name = f"obs_{i}"
64
-
65
- return world
66
-
67
- # Get constraints on entities given the problem instance name
68
- def _set_problem_instance(self, world, np_random, instance_num):
69
- instance_name, instance = get_problem_instance(world.problem_type, instance_num, np_random)
70
- world.problem_instance = instance_name
71
- world.start_constr = instance['start']
72
- world.goal_constr = instance['goal']
73
- world.static_obstacle_constr = instance['static_obs']
74
- world.dynamic_obstacle_constr = instance['dynamic_obs']
75
-
76
- """
77
- Returns goal constraints that haven't been selected to be used as an obstacle
78
- for the precision farming case (i.e., crop that wasn't selected as goal becomes an obstacle)
79
- """
80
- def _reset_agents_and_goals(self, world, np_random):
81
- temp_goal_constr = copy.copy(world.goal_constr)
82
- for i, agent in enumerate(world.agents):
83
- agent.color = np.array([1, 0.95, 0.8])
84
- agent.state.p_vel = np.zeros(world.dim_p)
85
- agent_constr = random.choice(world.start_constr)
86
- agent.state.p_pos = np_random.uniform(*zip(*agent_constr))
87
-
88
- agent.goal_a = world.goals[i]
89
- agent.goal_a.color = np.array([0.835, 0.90, 0.831])
90
- agent.goal_a.state.p_vel = np.zeros(world.dim_p)
91
- goal_constr = random.choice(temp_goal_constr)
92
- agent.goal_a.state.p_pos = np_random.uniform(*zip(*goal_constr))
93
- temp_goal_constr.remove(goal_constr)
94
-
95
- agent.goal_b = world.goals[len(world.goals) - 1 - i]
96
- agent.goal_b.color = np.array([0.85, 0.90, 0.99])
97
- agent.goal_b.state.p_vel = np.zeros(world.dim_p)
98
- agent.goal_b.state.p_pos = copy.copy(agent.state.p_pos)
99
-
100
- return temp_goal_constr
101
-
102
- # Reset position of dynamic obstacles
103
- def _reset_dynamic_obstacle(self, world, obstacle, np_random, temp_dynamic_obs_constr):
104
- obstacle.size = 0.025
105
- obstacle.movable = True
106
- obstacle.lock = threading.Lock()
107
- obstacle.color = np.array([0.26, 0.32, 0.36])
108
- obstacle.state.p_vel = np.zeros(world.dim_p)
109
- dynamic_obs_constr = random.choice(temp_dynamic_obs_constr)
110
- obstacle.state.p_pos = np_random.uniform(*zip(*dynamic_obs_constr))
111
- temp_dynamic_obs_constr.remove(dynamic_obs_constr)
112
-
113
- # Reset position of static obstacles, taking leftover entities from goal constraints
114
- def _reset_static_obstacle(self, world, obstacle, np_random, temp_static_obs_constr):
115
- obstacle.color = np.array([0.97, 0.801, 0.8])
116
- obstacle.state.p_vel = np.zeros(world.dim_p)
117
- static_obs_constr = random.choice(temp_static_obs_constr)
118
- obstacle.state.p_pos = np_random.uniform(*zip(*static_obs_constr))
119
- temp_static_obs_constr.remove(static_obs_constr)
120
-
121
- # Add or remove obstacles to match the number of obstacles in problem instance
122
- def _match_obstacles_to_instance(self, world, num_static):
123
- num_total_obstacles = num_static + len(world.dynamic_obstacle_constr)
124
- if len(world.obstacles) > num_total_obstacles:
125
- world.obstacles = world.obstacles[:num_total_obstacles]
126
- elif len(world.obstacles) < num_total_obstacles:
127
- additional_obstacles = [Obstacle() for _ in range(len(world.obstacles), num_total_obstacles)]
128
- [setattr(obstacle, 'name', f"obs_{i+len(world.obstacles)}") for i, obstacle in enumerate(additional_obstacles)]
129
- world.obstacles.extend(additional_obstacles)
130
-
131
- # Reset position of obstacles
132
- def _reset_obstacles(self, world, np_random, leftover_entities):
133
- temp_static_obs_constr = copy.copy(world.static_obstacle_constr)
134
- if world.problem_type.startswith('precision_farming'):
135
- temp_static_obs_constr += leftover_entities
136
- temp_dynamic_obs_constr = copy.copy(world.dynamic_obstacle_constr)
137
-
138
- num_dynamic_obs = len(temp_dynamic_obs_constr)
139
- self._match_obstacles_to_instance(world, len(temp_static_obs_constr))
140
-
141
- for i, obstacle in enumerate(world.obstacles):
142
- if i < num_dynamic_obs:
143
- self._reset_dynamic_obstacle(world, obstacle, np_random, temp_dynamic_obs_constr)
144
- self.npc += [NPC(obstacle.state.p_pos)]
145
- else:
146
- self._reset_static_obstacle(world, obstacle, np_random, temp_static_obs_constr)
147
-
148
- # Start a thread for each dynamic obstacle
149
- def _start_scripted_obstacles(self, world):
150
- self.scripted_obstacle_running = True
151
- for obstacle in world.obstacles:
152
- if obstacle.movable:
153
- t = threading.Thread(target=self.run_scripted_obstacle, args=(world, obstacle))
154
- t.start()
155
- self.scripted_obstacle_threads.append(t)
156
-
157
- def reset_world(self, world, np_random, instance_name=None):
158
- self.npc.clear()
159
- self.stop_scripted_obstacles()
160
- self._set_problem_instance(world, np_random, instance_name)
161
- leftover_entities = self._reset_agents_and_goals(world, np_random)
162
- self._reset_obstacles(world, np_random, leftover_entities)
163
- self._start_scripted_obstacles(world)
164
-
165
- # Reward given by agents to agents for reaching their respective goals
166
- def reward(self, agent, world):
167
- return 0
168
-
169
- def observation(self, agent, world):
170
- agent_pos = agent.state.p_pos
171
- agent_vel = agent.state.p_vel
172
-
173
- num_obstacles = len(world.obstacles)
174
- max_observable_dist = agent.max_observable_dist
175
-
176
- observed_obstacles = [np.full_like(agent_pos, max_observable_dist) for _ in range(num_obstacles)]
177
- observed_goal = np.full_like(agent_pos, max_observable_dist)
178
-
179
- for i, obstacle in enumerate(world.obstacles):
180
- if obstacle.movable:
181
- with obstacle.lock:
182
- obs_pos = obstacle.state.p_pos
183
- else:
184
- obs_pos = obstacle.state.p_pos
185
- relative_pos = obs_pos - agent_pos
186
- dist = np.linalg.norm(relative_pos)
187
- if dist <= max_observable_dist:
188
- observed_obstacles[i] = relative_pos
189
-
190
- goal_pos = agent.goal_b.state.p_pos if agent.reached_goal else agent.goal_a.state.p_pos
191
- relative_goal_pos = goal_pos - agent_pos
192
- goal_dist = np.linalg.norm(relative_goal_pos)
193
- if goal_dist <= max_observable_dist:
194
- observed_goal = relative_goal_pos
195
-
196
- return np.concatenate((agent_pos, agent_vel, np.concatenate(observed_obstacles, axis=0), observed_goal))
197
-
198
- # Run a thread for each scripted obstacle
199
- def run_scripted_obstacle(self, world, obstacle):
200
- while self.scripted_obstacle_running:
201
- # self.logger.debug(f'{obstacle.name} size: {obstacle.size:}, position: {obstacle.state.p_pos}')
202
- action, size = self._action_callback(obstacle, world)
203
- obstacle.update(action, size)
204
- time.sleep(0.5)
205
-
206
- # disaster response: increase obstacle size to resemble increasing size of fire
207
- # precision farming: move obstacle in a zig-zag pattern to resemble a tractor
208
- def _action_callback(self, obstacle, world):
209
- size = None
210
- action = None
211
- instance = world.problem_instance
212
- if world.problem_type == 'disaster_response':
213
- if instance == 'instance_0':
214
- size = 0.005
215
- elif instance == 'instance_1':
216
- size = 0.0075
217
- elif instance == 'instance_2':
218
- size = 0.01
219
- elif instance == 'instance_3':
220
- size = 0.0125
221
- else:
222
- action = np.zeros(world.dim_p)
223
- obstacle_num = int(obstacle.name.split('_')[-1])
224
- instance_num = int(instance.split('_')[-1])
225
- action = self.npc[obstacle_num].get_scripted_action(obstacle, instance_num)
226
- sensitivity = 2.0
227
- action *= sensitivity
228
-
229
- return action, size
230
-
231
- # Stop all threads for scripted obstacles
232
- def stop_scripted_obstacles(self):
233
- self.scripted_obstacle_running = False
234
- for t in self.scripted_obstacle_threads:
235
- t.join()
236
- self.scripted_obstacle_threads.clear()
237
-
238
- # Create a logger to log information from threads
239
- def _add_logger(self):
240
- self.logger = logging.getLogger('Dynamic Obstacles')
241
- self.logger.setLevel(logging.DEBUG)
242
-
243
- ch = logging.StreamHandler()
244
- ch.setLevel(logging.DEBUG)
245
-
246
- formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
247
- ch.setFormatter(formatter)
248
-
249
- self.logger.addHandler(ch)
@@ -1,11 +0,0 @@
1
- import Signal8
2
- import time
3
-
4
- env = Signal8.env('disaster_response')
5
- env.reset(options={"instance_num": 0})
6
- observation, _, terminations, truncations, _ = env.last()
7
- entities = env.unwrapped.get_start_state()
8
- time.sleep(5)
9
- time.sleep(5)
10
- env.step(1)
11
- env.close()
@@ -1,64 +0,0 @@
1
- import numpy as np
2
-
3
- class NPC:
4
- def __init__(self, start_pos):
5
- self.start = start_pos
6
- self.direction = [None, None]
7
- self.status = 'moving_to_destination'
8
- self.farming_instances = {
9
- 0: {'destination': (-0.9, -0.9), 'direction': [-1, 1], 'bounds': [(-1, -0.4), (-1, 1)]},
10
- 1: {'destination': (0.9, 0.9), 'direction': [-1, -1], 'bounds': [(-1, 1), (0.4, 1)]},
11
- 2: {'destination': (0, 1), 'direction': [1, -1], 'bounds': [(-0.25, 0.25), (-1, 1)]},
12
- 3: {'destination': (-0.75, -0.90), 'direction': [1, 1], 'bounds': [(-1, 1), (-1, -0.4)]},
13
- }
14
-
15
- def get_scripted_action(self, obstacle, instance_num):
16
- destination = self.farming_instances[instance_num]['destination']
17
- direction = self.farming_instances[instance_num]['direction']
18
- bounds = self.farming_instances[instance_num]['bounds']
19
-
20
- action = np.array([0, 0])
21
- with obstacle.lock:
22
- x, y, = obstacle.state.p_pos
23
-
24
- if self.status == 'moving_to_destination':
25
- if np.allclose([x, y], destination, atol=0.05):
26
- self.status = 'zigzagging'
27
- self.direction = direction
28
- else:
29
- action = self._move_towards_point(x, y, destination)
30
- elif self.status == 'zigzagging':
31
- if self.direction[1] == 1 and y > bounds[1][1] or self.direction[1] == -1 and y < bounds[1][0]:
32
- self.status = 'moving_to_start'
33
- else:
34
- action = self._zigzag(x, y, bounds)
35
- elif self.status == 'moving_to_start':
36
- if np.allclose([x, y], self.start, atol=0.05):
37
- self.status = 'moving_to_destination'
38
- else:
39
- action = self._move_towards_point(x, y, self.start)
40
-
41
- return action
42
-
43
- def _move_towards_point(self, x, y, point):
44
- x_direction = np.array(point) - np.array([x, y])
45
- direction_norm = x_direction / np.linalg.norm(x_direction)
46
- return np.round(direction_norm).astype(int)
47
-
48
- def _within_bounds(self, x, y, bounds):
49
- x_bounds, y_bounds = bounds
50
- return x_bounds[0] <= x <= x_bounds[1] and y_bounds[0] <= y <= y_bounds[1]
51
-
52
- def _zigzag(self, x, y, bounds):
53
- if self.direction[0] == 1:
54
- if x < bounds[0][1]:
55
- return np.array([1, 0])
56
- else:
57
- self.direction[0] = -1
58
- return np.array([0, self.direction[1]])
59
- elif self.direction[0] == -1:
60
- if x > bounds[0][0]:
61
- return np.array([-1, 0])
62
- else:
63
- self.direction[0] = 1
64
- return np.array([0, self.direction[1]])
@@ -1,133 +0,0 @@
1
- problems = {
2
- 'disaster_response': {
3
- 'instance_0': {
4
- 'start': [((-0.05, 0.05), (-1, -0.90))],
5
- 'goal': [
6
- ((-0.85, -0.75), (0.90, 1)),
7
- ((-1, -0.9), (-0.65, -0.55))
8
- ],
9
- 'static_obs': [
10
- ((-1, -0.3), (0.30, 0.50)),
11
- ((0.15, 0.40), (0.60, 1)),
12
- ((-0.85, -0.75), (0.05, 0.15))
13
- ],
14
- 'dynamic_obs': [((0.95, 1), (0.95, 1))]
15
- },
16
- 'instance_1': {
17
- 'start': [((-0.05, 0.05), (-1, -0.90))],
18
- 'goal': [
19
- ((0.90, 1), (-0.05, 0.05)),
20
- ((-1, -0.90), (-0.05, 0.05))
21
- ],
22
- 'static_obs': [
23
- ((-0.25, 0), (-0.3, 0.65)),
24
- ((0.25, 0.45), (0.5, 1)),
25
- ((0.75, 0.85), (-0.75, -0.30))
26
- ],
27
- 'dynamic_obs': [((-0.6, -0.45), (0.95, 1))]
28
- },
29
- 'instance_2': {
30
- 'start': [((-0.05, 0.05), (-1, -0.90))],
31
- 'goal': [
32
- ((-0.05, 0.05), (0, 0.10)),
33
- ((-1, -0.9), (0.05, 0.15))
34
- ],
35
- 'static_obs': [
36
- ((-1, -0.25), (-0.75, -0.55)),
37
- ((0.25, 0.65), (-0.85, -0.15)),
38
- ((0, 0.20), (0.75, 1))
39
- ],
40
- 'dynamic_obs': [((-0.15, 0), (0.90, 1))]
41
- },
42
- 'instance_3': {
43
- 'start': [((-0.05, 0.05), (-1, -0.90))],
44
- 'goal': [
45
- ((0.90, 1), (0.90, 1)),
46
- ((0.90, 1), (-0.05, 0.05))
47
- ],
48
- 'static_obs': [
49
- ((-0.025, 0.125), (0.5, 1)),
50
- ((-1, -0.35), (0.5, 0.75)),
51
- ((0.1, 1), (-0.70, -0.35))
52
- ],
53
- 'dynamic_obs': [((-1, -0.70), (0.80, 0.875))]
54
- }
55
- },
56
- 'precision_farming': {
57
- 'instance_0': {
58
- 'start': [((0.80, 1), (0.80, 1))],
59
- 'goal': [
60
- ((-0.95, -0.70), (0.525, 0.90)),
61
- ((-0.625, -0.475), (0.525, 0.90)),
62
- ((-0.95, -0.70), (-0.15, 0.225)),
63
- ((-0.625, -0.475), (-0.15, 0.225)),
64
- ((-0.95, -0.70), (-0.825, -0.45)),
65
- ((-0.625, -0.475), (-0.825, -0.45))
66
- ],
67
- 'static_obs': [
68
- ((-0.85, -0.5), (0.275, 0.475)),
69
- ((-0.85, -0.5), (-0.40, -0.20)),
70
- ((0.30, 0.50), (-0.90, -0.65))
71
- ],
72
- 'dynamic_obs': [((0.80, 1), (-1, -0.80))]
73
- },
74
- 'instance_1': {
75
- 'start': [((-1, -0.80), (-1, -0.80))],
76
- 'goal': [
77
- ((-0.95, -0.575), (0.65, 0.90)),
78
- ((-0.95, -0.575), (0.20, 0.45)),
79
- ((-0.275, 0.10), (0.65, 0.90)),
80
- ((-0.275, 0.10), (0.20, 0.45)),
81
- ((0.40, 0.775), (0.65, 0.90)),
82
- ((0.40, 0.775), (0.20, 0.45)),
83
- ],
84
- 'static_obs': [
85
- ((-0.525, -0.325), (0.20, 0.90)),
86
- ((0.15, 0.35), (0.20, 0.90)),
87
- ((-1, -0.825), (-0.15, 0.15)),
88
- ],
89
- 'dynamic_obs': [((0.80, 1), (-1, -0.80))]
90
- },
91
- 'instance_2': {
92
- 'start': [((-1, -0.80), (-0.20, 0))],
93
- 'goal': [
94
- ((-0.1875, 0.1875), (0.65, 0.90)),
95
- ((-0.1875, 0.1875), (0.325, 0.575)),
96
- ((-0.1875, 0.1875), (0, 0.25)),
97
- ((-0.1875, 0.1875), (-0.325, -0.075)),
98
- ((-0.1875, 0.1875), (-0.65, -0.40)),
99
- ((-0.1875, 0.1875), (-0.975, -0.725)),
100
- ],
101
- 'static_obs': [
102
- ((-0.45, -0.25), (-0.67, 0.67)),
103
- ((0.25, 0.45), (-0.67, 0.67)),
104
- ((-0.56875, -0.21875), (0.875, 1)),
105
- ],
106
- 'dynamic_obs': [((-1, -0.80), (0, 0.20))]
107
- },
108
- 'instance_3': {
109
- 'start': [((-0.10, 0.10), (0.80, 1))],
110
- 'goal': [
111
- ((-0.80, -0.425), (-0.20, 0)),
112
- ((-0.1875, 0.1875), (-0.20, 0)),
113
- ((0.425, 0.80), (-0.20, 0)),
114
- ((-0.80, -0.425), (-0.925, -0.725)),
115
- ((-0.1875, 0.1875), (-0.925, -0.725)),
116
- ((0.425, 0.80), (-0.925, -0.725)),
117
- ],
118
- 'static_obs': [
119
- ((-0.67, -0.25), (-0.60, -0.40)),
120
- ((0.25, 0.67), (-0.60, -0.40)),
121
- ((0.10, 0.30), (0.80, 1)),
122
- ],
123
- 'dynamic_obs': [((-0.30, -0.10), (0.80, 1))]
124
- },
125
- },
126
- }
127
-
128
- def get_problem_instance(problem_type, instance_num, np_random):
129
- if instance_num is None:
130
- instance_num = np_random.randint(len(problems[problem_type]))
131
- instance_name = f'instance_{instance_num}'
132
- instance = problems[problem_type][instance_name]
133
- return instance_name, instance
File without changes
File without changes
File without changes
File without changes
File without changes