-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
367 lines (293 loc) · 12.9 KB
/
main.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import sys
sys.path.insert(0, '../')
import argparse
import yaml
from math import fabs
from itertools import combinations
from copy import deepcopy
import numpy as np
from q_learning import QLearningTable
from a_star import AStar
class Location(object):
def __init__(self, x=-1, y=-1):
self.x = x
self.y = y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __str__(self):
return str((self.x, self.y))
class State(object):
def __init__(self, time, location):
self.time = time
self.location = location
def __eq__(self, other):
return self.time == other.time and self.location == other.location
def __hash__(self):
return hash(str(self.time)+str(self.location.x) + str(self.location.y))
def is_equal_except_time(self, state):
return self.location == state.location
def __str__(self):
return str((self.time, self.location.x, self.location.y))
class Conflict(object):
VERTEX = 1
EDGE = 2
def __init__(self):
self.time = -1
self.type = -1
self.agent_1 = ''
self.agent_2 = ''
self.location_1 = Location()
self.location_2 = Location()
def __str__(self):
return '(' + str(self.time) + ', ' + self.agent_1 + ', ' + self.agent_2 + \
', '+ str(self.location_1) + ', ' + str(self.location_2) + ')'
class VertexConstraint(object):
def __init__(self, time, location):
self.time = time
self.location = location
def __eq__(self, other):
return self.time == other.time and self.location == other.location
def __hash__(self):
return hash(str(self.time)+str(self.location))
def __str__(self):
return '(' + str(self.time) + ', '+ str(self.location) + ')'
class EdgeConstraint(object):
def __init__(self, time, location_1, location_2):
self.time = time
self.location_1 = location_1
self.location_2 = location_2
def __eq__(self, other):
return self.time == other.time and self.location_1 == other.location_1 \
and self.location_2 == other.location_2
def __hash__(self):
return hash(str(self.time) + str(self.location_1) + str(self.location_2))
def __str__(self):
return '(' + str(self.time) + ', '+ str(self.location_1) +', '+ str(self.location_2) + ')'
class Constraints(object):
def __init__(self):
self.vertex_constraints = set()
self.edge_constraints = set()
def add_constraint(self, other):
self.vertex_constraints |= other.vertex_constraints
self.edge_constraints |= other.edge_constraints
def __str__(self):
return "VC: " + str([str(vc) for vc in self.vertex_constraints]) + \
"EC: " + str([str(ec) for ec in self.edge_constraints])
class Environment(object):
def __init__(self, dimension, agents, obstacles):
self.dimension = dimension
self.obstacles = obstacles
self.agents = agents
self.agent_dict = {}
self.make_agent_dict()
self.constraints = Constraints()
self.constraint_dict = {}
self.q_learning = QLearningTable(self, State, Location)
def step(self, agent_name, current_state, action):
#n = State(current_state.time, current_state.location)
#if self.state_valid(n):
# neighbors.append(n)
current_state = State(0, Location(current_state[0], current_state[1]))
next_state = State(0, Location(0,0))
# Up action
n = State(current_state.time, Location(current_state.location.x, current_state.location.y+1))
if self.state_valid(n) and self.transition_valid(current_state, n) and action == 0:
next_state= n
# Down action
n = State(current_state.time, Location(current_state.location.x, current_state.location.y-1))
if self.state_valid(n) and self.transition_valid(current_state, n) and action == 1:
next_state = n
# Left action
n = State(current_state.time, Location(current_state.location.x-1, current_state.location.y))
if self.state_valid(n) and self.transition_valid(current_state, n) and action == 2:
next_state = n
# Right action
n = State(current_state.time, Location(current_state.location.x+1, current_state.location.y))
if self.state_valid(n) and self.transition_valid(current_state, n) and action == 3:
next_state = n
#Evaluate the reward
if self.is_at_goal(next_state,agent_name):
reward = 100
done = 1
end_route = 1
#Invalid action
elif next_state == State(0, Location(0,0)):
reward = -100000
done = 1
end_route = 0
#Normal action
else:
reward = self.reward_system(next_state,agent_name)
done = 0
end_route = 0
next_state = [next_state.location.x,next_state.location.y]
return next_state, reward, done, end_route
def get_first_conflict(self, solution):
max_t = max([len(plan) for plan in solution.values()])
result = Conflict()
for t in range(max_t):
for agent_1, agent_2 in combinations(solution.keys(), 2):
state_1 = self.get_state(agent_1, solution, t)
state_2 = self.get_state(agent_2, solution, t)
if state_1.is_equal_except_time(state_2):
result.time = t
result.type = Conflict.VERTEX
result.location_1 = state_1.location
result.agent_1 = agent_1
result.agent_2 = agent_2
return result
for agent_1, agent_2 in combinations(solution.keys(), 2):
state_1a = self.get_state(agent_1, solution, t)
state_1b = self.get_state(agent_1, solution, t+1)
state_2a = self.get_state(agent_2, solution, t)
state_2b = self.get_state(agent_2, solution, t+1)
if state_1a.is_equal_except_time(state_2b) and state_1b.is_equal_except_time(state_2a):
result.time = t
result.type = Conflict.EDGE
result.agent_1 = agent_1
result.agent_2 = agent_2
result.location_1 = state_1a.location
result.location_2 = state_1b.location
return result
return False
def create_constraints_from_conflict(self, conflict):
constraint_dict = {}
if conflict.type == Conflict.VERTEX:
v_constraint = VertexConstraint(conflict.time, conflict.location_1)
constraint = Constraints()
constraint.vertex_constraints |= {v_constraint}
constraint_dict[conflict.agent_1] = constraint
constraint_dict[conflict.agent_2] = constraint
elif conflict.type == Conflict.EDGE:
constraint1 = Constraints()
constraint2 = Constraints()
e_constraint1 = EdgeConstraint(conflict.time, conflict.location_1, conflict.location_2)
e_constraint2 = EdgeConstraint(conflict.time, conflict.location_2, conflict.location_1)
constraint1.edge_constraints |= {e_constraint1}
constraint2.edge_constraints |= {e_constraint2}
constraint_dict[conflict.agent_1] = constraint1
constraint_dict[conflict.agent_2] = constraint2
return constraint_dict
def get_state(self, agent_name, solution, t):
if t < len(solution[agent_name]):
return solution[agent_name][t]
else:
return solution[agent_name][-1]
def state_valid(self, state):
return state.location.x >= 0 and state.location.x < self.dimension[0] \
and state.location.y >= 0 and state.location.y < self.dimension[1] \
and VertexConstraint(state.time, state.location) not in self.constraints.vertex_constraints \
and (state.location.x, state.location.y) not in self.obstacles
def transition_valid(self, state_1, state_2):
return EdgeConstraint(state_1.time, state_1.location, state_2.location) not in self.constraints.edge_constraints
def is_solution(self, agent_name):
pass
def admissible_heuristic(self, state, agent_name):
goal = self.agent_dict[agent_name]["goal"]
return fabs(state.location.x - goal.location.x) + fabs(state.location.y - goal.location.y)
def is_at_goal(self, state, agent_name):
goal_state = self.agent_dict[agent_name]["goal"]
return state.is_equal_except_time(goal_state)
def make_agent_dict(self):
for agent in self.agents:
start_state = State(0, Location(agent['start'][0], agent['start'][1]))
goal_state = State(0, Location(agent['goal'][0], agent['goal'][1]))
self.agent_dict.update({agent['name']:{'start':start_state, 'goal':goal_state}})
def reward_system(self, next_state, agent_name):
goal = self.agent_dict[agent_name]["goal"]
return -np.sqrt((next_state.location.x - goal.location.x)**2+(next_state.location.y - goal.location.y)**2)
def compute_solution(self, method):
solution = {}
for agent in self.agent_dict.keys():
self.constraints = self.constraint_dict.setdefault(agent, Constraints())
if method == "q_learning":
local_solution = self.q_learning.search(agent)
if not local_solution:
return False
solution.update({agent:local_solution})
return solution
def compute_solution_cost(self, solution):
return sum([len(path) for path in solution.values()])
class HighLevelNode(object):
def __init__(self):
self.solution = {}
self.constraint_dict = {}
self.cost = 0
def __eq__(self, other):
if not isinstance(other, type(self)): return NotImplemented
return self.solution == other.solution and self.cost == other.cost
def __hash__(self):
return hash((self.cost))
def __lt__(self, other):
return self.cost < other.cost
class CBS(object):
def __init__(self, environment, method):
self.env = environment
self.open_set = set()
self.closed_set = set()
self.method = method
def search(self):
start = HighLevelNode()
# TODO: Initialize it in a better way
start.constraint_dict = {}
for agent in self.env.agent_dict.keys():
start.constraint_dict[agent] = Constraints()
start.solution = self.env.compute_solution(self.method)
if not start.solution:
return {}
start.cost = self.env.compute_solution_cost(start.solution)
self.open_set |= {start}
while self.open_set:
P = min(self.open_set)
self.open_set -= {P}
self.closed_set |= {P}
self.env.constraint_dict = P.constraint_dict
conflict_dict = self.env.get_first_conflict(P.solution)
if not conflict_dict:
print("solution found")
return self.generate_plan(P.solution)
constraint_dict = self.env.create_constraints_from_conflict(conflict_dict)
for agent in constraint_dict.keys():
new_node = deepcopy(P)
new_node.constraint_dict[agent].add_constraint(constraint_dict[agent])
self.env.constraint_dict = new_node.constraint_dict
new_node.solution = self.env.compute_solution(self.method)
if not new_node.solution:
continue
new_node.cost = self.env.compute_solution_cost(new_node.solution)
# TODO: ending condition
if new_node not in self.closed_set:
self.open_set |= {new_node}
return {}
def generate_plan(self, solution):
plan = {}
for agent, path in solution.items():
path_dict_list = [{'t':state.time, 'x':state.location.x, 'y':state.location.y} for state in path]
plan[agent] = path_dict_list
return plan
def main():
with open("input.yaml", 'r') as param_file:
try:
param = yaml.load(param_file, Loader=yaml.FullLoader)
except yaml.YAMLError as exc:
print(exc)
dimension = param["map"]["dimensions"]
obstacles = param["map"]["obstacles"]
agents = param['agents']
env = Environment(dimension, agents, obstacles)
# Searching
cbs = CBS(env, method = "q_learning")
solution = cbs.search()
print(solution)
if not solution:
print(" Solution not found" )
return
#Qlearning
# Write to output file
output = dict()
output["schedule"] = solution
output["cost"] = env.compute_solution_cost(solution)
with open("output.yaml", 'w') as output_yaml:
yaml.safe_dump(output, output_yaml)
if __name__ == "__main__":
main()