-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum_Communication_Policy.py
301 lines (244 loc) · 10.9 KB
/
Minimum_Communication_Policy.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
from typing import List, Tuple, Dict
from Agent import Agent, OnlineAgent
from Execution_Policy import ExecutionPolicy, OnlineExecutionPolicy
from Position import Position
from Schedule_Table import ScheduleTable, OnlineSchedule
from Status import Status
class MCP(ExecutionPolicy):
"""
A class representing the Minimum Communication Policy for a Central
Controller.
Attributes:
-----------
agents : List[Agent]
A list of agents.
schedule_table : ScheduleTable
A schedule table for the agents.
Methods:
--------
get_next_position(agent_id: int) -> Tuple[Position, int]:
Returns the next position and timestep for the given agent.
update(data) -> None:
Updates the agent data.
"""
def __init__(self, plan_file: str, num_agent: int) -> None:
"""
Initializes the MCP class.
Parameters:
-----------
plan_file : str
The file containing the plan for the agents.
num_of_agents : int
The number of agents.
"""
self.agents: List[Agent] = [Agent(plan_file) for _ in range(num_agent)]
if Agent.plans is None:
print("Error: Plans have not been loaded.")
exit(1)
self.schedule_table: ScheduleTable = ScheduleTable(Agent.plans)
def get_next_position(self, agent_id) -> Tuple[List[Position], Tuple[int, int]]:
"""
Returns the next position and timestep for the given agent.
Parameters:
-----------
agent_id : int
The ID of the agent.
Returns:
--------
Tuple[List[Position], Tuple[int, int]]
A tuple containing the next positions and timestep for the given
agent.
"""
agent: Agent = self.agents[agent_id]
if agent.position is None:
start_position = agent.get_initial_position()
end_timestep = 0
return [start_position], (end_timestep, end_timestep)
start_timestep = agent.timestep
end_timestep = agent.timestep
start_position = agent.view_position(end_timestep)
target_positions: List[Position] = [start_position]
# Join up to MAX_STEPS_INTO_FUTURE into a single motion
while end_timestep + 1 < len(agent.get_plan()):
next_timestep = end_timestep + 1
next_position = agent.view_position(next_timestep)
# Check if we are scheduled at the next position
if not self.schedule_table.scheduled(next_position, agent_id):
break
# If we are next scheduled we an go to next position.
end_timestep = next_timestep
target_positions.append(next_position)
# If the next position requires a turn we stay where we are.
if start_position.theta != next_position.theta:
break
return target_positions, (start_timestep ,end_timestep)
def update(self, data) -> None:
"""
Updates the agent data.
Parameters:
-----------
data : dict
A dictionary containing the agent data.
"""
agent_id: int | None = data.get("agent_id")
if agent_id is None:
print("Agent id was not provided, cannot update central controller")
return
agent: Agent = self.agents[agent_id] #
agent.status = Status.from_string(data.get("status"))
if agent.status == Status.SUCCEEDED:
pose: Dict[str, int] = data.get("position")
if pose is None:
print(f"Pose was not provided, by agent {agent_id}, cannot update")
return
if not all(map(lambda val: val in pose.keys(), ["x", "y", "theta"])):
print(f"Pose is missing one of x, y, theta values for agent {agent_id}")
return
agent.timestep = data.get("timestep")
agent.position = Position(pose["x"], pose["y"], pose["theta"])
agent.position = agent.view_position(agent.timestep)
plan = agent.get_plan()
self.schedule_table.remove_path(agent_id, plan, agent.timestep)
print(agent)
def get_status(self) -> List[Tuple[int, Status]]:
return [(agent._id, agent.status) for agent in self.agents]
class OnlineMCP(OnlineExecutionPolicy):
def __init__(self, num_agents: int):
self.agents: List[OnlineAgent] = [OnlineAgent() for _ in range(num_agents)]
for agent in self.agents:
if agent.plans is not None:
agent.plans.setdefault(agent._id, [])
else:
raise ValueError("Plans were not intialised")
self.timestep: int = 0
self.schedule_table = OnlineSchedule(num_agents)
def get_next_position(self, agent_id: int) -> Tuple[List[Position], Tuple[int, int]]:
agent: Agent = self.agents[agent_id]
if agent.position is None:
start_position = agent.get_initial_position()
end_timestep = 0
return [start_position], (end_timestep, end_timestep)
start_timestep = agent.timestep
end_timestep = agent.timestep
start_position = agent.view_position(end_timestep)
target_positions: List[Position] = [start_position]
# Join up to MAX_STEPS_INTO_FUTURE into a single motion
while end_timestep + 1 < len(agent.get_plan()):
next_timestep = end_timestep + 1
next_position = agent.view_position(next_timestep)
# Check if we are scheduled at the next position
if not self.schedule_table.scheduled(next_position, agent_id):
break
# If we are next scheduled we an go to next position.
end_timestep = next_timestep
target_positions.append(next_position)
# If the next position requires a turn we stay where we are.
if start_position.theta != next_position.theta:
break
return target_positions, (start_timestep ,end_timestep)
def update(self, data) -> None:
"""
Updates the agent data.
Parameters:
-----------
data : dict
A dictionary containing the agent data.
"""
agent_id: int | None = data.get("agent_id")
if agent_id is None:
print("Agent id was not provided, cannot update central controller")
return
agent: Agent = self.agents[agent_id] # Mutate Agent Data
agent.status = Status.from_string(data.get("status"))
if agent.status == Status.SUCCEEDED:
pose: Dict[str, int] = data.get("position")
if pose is None:
print(f"Pose was not provided, by agent {agent_id}, cannot update")
return
if not all(map(lambda val: val in pose.keys(), ["x", "y", "theta"])):
print(f"Pose is missing one of x, y, theta values for agent {agent_id}")
return
agent.position = Position(pose["x"], pose["y"], pose["theta"])
prev_timestep = agent.timestep
agent.timestep = data.get("timestep")
agent.position = agent.view_position(agent.timestep)
plan = agent.get_plan()
## Test whether this has off by one errors
print("Checking timesteps for removal:")
print(f"Previous: {prev_timestep} Current: {agent.timestep}")
self.schedule_table.remove_path(
agent_id,
[*enumerate(plan[prev_timestep:agent.timestep],
prev_timestep + 1)]
)
print(agent)
def get_agent_locations(self) -> Tuple[List[Tuple[Position, int]], bool]:
"""
Method to get the final position of agents in the committed plan
Returns:
List[Tuple[Position, int]]: A list containing pairs of Position and the id of the agent there
"""
agent_positions = []
all_started = True
for agent in self.agents:
plan = agent.get_plan()
if plan:
agent_positions.append((plan[-1], agent._id))
else:
all_started = False
return agent_positions, all_started
def extend_plans(self, extensions: List[Tuple[int, List[Position]]], lookahead: int = 10) -> None:
"""
Extend the existing plans for agents
Args:
extensions:
List[Tuple[int, List[Tuple[Position, int]]]]:
A list containing pairs of agent_id and plan extensions,
where plan extensions are tuples of Position and timestep to reach it
"""
for (agent_id, extension) in extensions:
if not (0 <= agent_id < len(self.agents)):
print("Not a valid agent id, ignoring")
continue
agent = self.agents[agent_id]
# Commit up to {lookahead} steps for this agent, ignoring further extensions
(len(agent.get_plan()) - agent.timestep)
extension = extension[:lookahead - (len(agent.get_plan()) - agent.timestep)]
for next_pos in extension:
if agent.plans is None:
raise ValueError("Plans were not initialised")
agent.plans[agent_id].append(next_pos)
self.schedule_table.update_plan([*enumerate(extension, len(agent.get_plan()))], agent_id)
print(f"Agent {agent_id}:", agent.plans[agent_id][-15:]) # type: ignore
# print(agent.plans)
def get_status(self) -> List[Tuple[int, Status]]:
return [(agent._id, agent.status) for agent in self.agents]
if __name__ == "__main__":
mcp = OnlineMCP(2)
plan = [(0, [Position(0,0,0)]), (1, [Position(0,1,0)]), (0, [Position(0,1,180)]), (1, [Position(1,1,180)])] # noqa: E501
mcp.extend_plans(plan)
for q in mcp.schedule_table.path_table.items():
print(q)
print("Agent 0 moves once")
def extract(x: tuple[int, List[Position]]):
return x[1][0]
try:
mcp.schedule_table.remove_path(0,
[*enumerate(map(extract, plan[0:1]), 1)])
except AssertionError as ex:
print(ex) # Expect an error on deleting the 2nd constraint on (0, 1) for agent 1
for b in mcp.schedule_table.path_table.items():
print(b)
print("Agent 1 moves once")
mcp.schedule_table.remove_path(1, [*enumerate(map(extract,plan[1:2]), 1)])
for c in mcp.schedule_table.path_table.items():
print(c)
print("Then agent 0 moves again")
mcp.schedule_table.remove_path(0, [*enumerate(map(extract,plan[2:3]), 2)])
print("Now Agent 1 takes final move")
for d in mcp.schedule_table.path_table.items():
print(d)
mcp.schedule_table.remove_path(1, [*enumerate(map(extract,plan[3:4]), 2)])
print("After removing")
for e in mcp.schedule_table.path_table.items():
print(e)