-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeach_cleanup.py
206 lines (190 loc) · 7.55 KB
/
beach_cleanup.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
import random
import copy
import math
from heapq import heappush,heappop
class node:
def __init__(self,pos,g,l,h,parent=None):
self.pos=pos
self.g=g
self.h=h
self.l=l
self.f=g+h
self.parent=parent
def set_heuristic(self,task_pos,coll_pos):
dist=0
if self.l==1:
dist=abs(self.pos[0]-task_pos[0])+abs(self.pos[1]-task_pos[1])
dist+=abs(task_pos[0]-coll_pos[0])+abs(task_pos[1]-coll_pos[1])
if self.l==2:
dist=abs(self.pos[0]-coll_pos[0])+abs(self.pos[1]-coll_pos[1])
self.h=dist
self.f=self.g+self.h
def __lt__(self,other):
return self.f<other.f
class Beach :
def __init__(self):
self.size=7
self.robot_count=5
self.collection_point=(0,0)
self.debri_count=5
population=[]
for x in range(self.size):
for y in range(self.size):
population.append((x,y))
self.debris=random.sample(population,self.debri_count)
self.robot_positions=random.sample(population,self.robot_count)
def show(self):
print("@Time : ",self.time_step)
print("@Collection Point,")
print("Debri collected :",end=' ')
for i,x in enumerate(self.debris):
if(x==self.collection_point):
print(f"D{i} ",end="")
print()
print("Resting robots :",end=" ")
for i,x in enumerate(self.robot_positions):
if(x==self.collection_point):
print(f"R{i} ",end="")
print()
for i in range(0,self.size):
print((self.size*9+1)*"-")
for j in range(0,self.size):
pos=(i,j)
a=0
print("|",end="")
if pos==self.collection_point:
print(" C0 ",end="")
a=1
else:
for k in range(0,len(self.robot_positions)):
if pos==self.robot_positions[k]:
print(f" R{k} ",end="")
a=1
break
for k in range(0,len(self.debris)):
if pos==self.debris[k]:
print(f" D{k} ",end="")
a+=2
break
if a==0:
print(" ",end="")
if a==1 or a==2:
print(" ",end="")
print("|")
print((self.size*9+1)*"-")
def get_adjacent(self,pos):
new_pos=[(pos[0]+dx, pos[1]+dy) for dx,dy in [(0,1),(0,-1),(1,0),(-1,0)]]
new=list(new_pos)
for i,j in new:
if i<0 or j<0:
new_pos.remove((i,j))
elif i>=self.size or j>=self.size:
new_pos.remove((i,j))
return new_pos
def mla_star(self,start,task_pos,t_max):
collection_pos=self.collection_point
open=[]
start_node=node(start,0,1,0,None)
heappush(open,start_node)
while len(open)>0:
current=heappop(open)
if current.l==1 and current.g>t_max:
continue
if current.l==1 and current.pos==task_pos:
t_max=current.g
n_1=node(current.pos,current.g+1,2,0,current)
n_1.set_heuristic(task_pos,collection_pos)
heappush(open,n_1)
if current.l==2 and current.pos==collection_pos:
path=[]
while current:
path.append(current.pos)
current=current.parent
path.reverse()
return path
adjacent_pos=self.get_adjacent(current.pos)
adjacent=list(adjacent_pos)
for neighbour in adjacent:
if(neighbour==self.collection_point):
new_node=node(neighbour,current.g+1,current.l,0,current)
new_node.set_heuristic(task_pos,collection_pos)
heappush(open,new_node)
continue
for i in self.ongoing_tasks:
t=current.g
if t<len(i['path'])-1:
if neighbour==i['path'][t+1]:
adjacent_pos.remove(neighbour)
break
else:
new_node=node(neighbour,current.g+1,current.l,0,current)
new_node.set_heuristic(task_pos,collection_pos)
heappush(open,new_node)
return None
def create_pairs(self,task_list,agent_list):
pairs=[]
for t in task_list:
for a in agent_list:
heuristic=abs(t["pos"][0]-a["pos"][0])+abs(t["pos"][1]-a["pos"][1])#add distance to collection
pair={"debri":t,"agent":a,"h":heuristic}
pairs.append(pair)
pairs.sort(key=lambda x:x["h"])
return pairs
def move_idle_agent(self,agent_list,a):
pass
def task_allocation(self,task_list,agent_list):
while len(task_list)!=0:
expired=[]
for i in self.ongoing_tasks:
position=i["path"].pop(0)
if(self.debris[i["debri"]["index"]]==self.robot_positions[i["agent"]["index"]]):
self.debris[i["debri"]["index"]]=position
self.robot_positions[i["agent"]["index"]]=position
if(len(i["path"])==0):
expired.append(i)
for i in expired:
self.ongoing_tasks.remove(i)
i["agent"]["pos"]=self.robot_positions[i["agent"]["index"]]
agent_list.append(i["agent"])
self.show()
pairs=self.create_pairs(task_list,agent_list)
for pair in pairs:
if(pair["agent"] in agent_list and pair["debri"] in task_list):
path_time=self.mla_star(pair["agent"]["pos"],pair["debri"]["pos"],math.inf)
if(path_time==None):
continue
task={"agent":pair["agent"],"debri":pair["debri"],"path":path_time}
self.ongoing_tasks.append(task)
agent_list.remove(pair["agent"])
task_list.remove(pair["debri"])
self.time_step=self.time_step+1
while(len(self.ongoing_tasks)!=0):
expired=[]
for i in self.ongoing_tasks:
position=i["path"].pop(0)
if(self.debris[i["debri"]["index"]]==self.robot_positions[i["agent"]["index"]]):
self.debris[i["debri"]["index"]]=position
self.robot_positions[i["agent"]["index"]]=position
if(len(i["path"])==0):
expired.append(i)
for i in expired:
self.ongoing_tasks.remove(i)
i["agent"]["pos"]=self.robot_positions[i["agent"]["index"]]
agent_list.append(i["agent"])
self.show()
self.time_step=self.time_step+1
def simulate(self):
self.time_step=0
task_list=[{"index":i,"pos":x} for i,x in enumerate(self.debris)]
agent_list=[{"index":i,"pos":x} for i,x in enumerate(self.robot_positions)]
self.ongoing_tasks=[]
print("Tasks:")
for i in task_list:
print(i)
print("Robots:")
for i in agent_list:
print(i)
self.task_allocation(task_list,agent_list)
if __name__== "__main__":
arena=Beach()
arena.simulate()