-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_cluster.py
299 lines (263 loc) · 12 KB
/
agent_cluster.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
from snake import Snake
from constants import *
import pygame
class Agent_cluster(Snake):
def __init__(self,body=[(0,0)] , direction=(1,0), name="Agent1"):
super().__init__(body,direction,name=name)
self.openNodes=[]
self.closedNodes=[]
self.lastPlayerlen=None
self.foodpos=None
self.path=[]
self.first=True
# def pathlen(self,a,b):
# return int( ((a[0]-b[0])**2 + (a[1]-b[1])**2 )**0.5)
def add(self,a,b):
return (a[0]+b[0])%60,(a[1]+b[1])%40
def pathlen(self,a,b):
distX = abs(a[0]-b[0])
distY = abs(a[1]-b[1])
if distX > 60/2:
distX = 60- distX;
if distY > 40/2:
distY = 40 - distY;
return distX + distY
def update(self,points=None, mapsize=None, count=None,agent_time=None):
#self.points=points
self.mapsize=mapsize
self.count=count
self.agent_time=agent_time
def updateDirection(self,maze):
#this is the brain of the snake player
begin_time = pygame.time.get_ticks();
olddir=self.direction
position=self.body[0]
#new direction can't be up if current direction is down...and so on
complement=[(up,down),(down,up),(right,left),(left,right)]
invaliddir=[x for (x,y) in complement if y==olddir]
validdir=[dir for dir in directions if not ( dir in invaliddir )]
#get the list of valid directions for us
validdir=[dir for dir in validdir if not (self.add(position,dir) in maze.obstacles or self.add(position,dir) in maze.playerpos)]
#if we collide then set olddir to first move of validdir (if validdir is empty then leave it to olddir)
olddir= olddir if olddir in validdir or len(validdir)==0 else validdir[0]
#shortest path.....we assume that the direction we are currently going now gives the shortest path
# shortest=self.pathlen(self.add(position,olddir) , maze.foodpos)#length in shortest path
# for dir in validdir:
# newpos=self.add(position,dir)
# newlen=self.pathlen(newpos , maze.foodpos)#length in shortest path
# if newlen < shortest:
# olddir=dir
# shortest=newlen
# clusters=self.abstractMaze((2,2))
# clusters=[print(x) for x in clusters]
# targetPoint=maze.foodpos
# idActualCluster = self.calculateClusterId(position)
# idGoalCluster = self.calculateClusterId(maze.foodpos)
# if(idActualCluster==idGoalCluster)
# self.a*(position,maze.foodpos)
# else:
# bestPoint=self.bestTargetPoint(position,idActualCluster,clusters)
# self.a*(position,bestPoint)
#
#
# if self.openNodes!=[]:
# path = self.aa(position, self.direction, maze, begin_time)
#
# else:
# if path==[]:
#print( self.pathlen(position,maze.foodpos))
if self.openNodes!=[] or self.closedNodes!=[]:
if self.lastPlayerlen!=len(maze.playerpos):
self.lastPlayerlen=len(maze.playerpos)
self.openNodes=[]
self.closedNodes=[]
elif self.pathlen(position,maze.foodpos)<10:
print("teste")
self.openNodes=[]
self.closedNodes=[]
elif self.foodpos==None:
self.foodpos=maze.foodpos
self.openNodes=[]
self.closedNodes=[]
elif self.pathlen(maze.foodpos,self.foodpos)>10:
self.foodpos=maze.foodpos
self.openNodes=[]
self.closedNodes=[]
else:
print("guarda")
if self.path!=[] and self.closedNodes!=[]:
self.path=self.aa(position, self.direction, maze, begin_time)
#print("&")
#print(position, self.path[-1])
ver=False
for x in self.path:
#print(x.get_pos()==position)
if x.get_pos()==position:
dir=x.dir
ver=True
break
if not ver:
print("UPS")
self.openNodes=[]
self.closedNodes=[]
self.path = self.aa(position, self.direction, maze, begin_time)
dir = self.path[-1].dir if self.path else olddir
self.direction = dir # if self.path está por segurança
else:
##isto é o normal sem o if anterior e tirar o self.path para path
self.path = self.aa(position, self.direction, maze, begin_time)
# else:
dir = self.path[-1].dir if self.path else olddir
self.direction = dir # if self.path está por segurança
# def bestTargetPoint(self,position,idActualCluster,clusters):
# getPoint=True
# clusterPoints=cluster[idActualCluster].defineCluster()
#
# while getPoint:
# distTofood=[(x,self.pathlen(x,maze.foodpos)) for x in clusterPoints]
# def calculateClusterId(self,point):
# xC=point[0]//self.horizontalNodesOfCluster
# yC=point[1]//self.verticalNodesOfCluster
#
# return (xC,yC)
def abstractMaze(self,numberofclusters):
clusters=[]
node=[0,0]
cont=0
self.horizontalNodesOfCluster=self.mapsize[0]/numberofclusters[0]
self.verticalNodesOfCluster=self.mapsize[1]/numberofclusters[1]
for y in range(numberofclusters[1]):
for x in range(numberofclusters[0]):
clusters.append(Cluster((x,y),(node[0],node[1]),(node[0]+self.horizontalNodesOfCluster-1,(y+1)*self.verticalNodesOfCluster-1)))
node[0]=node[0]+self.horizontalNodesOfCluster
cont+=1
node[0]=0
node[1]=(y+1)*self.verticalNodesOfCluster
return clusters
######################################################################################
######################################################################################
######################################################################################
def aa(self,startPos, startDir, maze, begin_time):
##############
# clusterId=self.calculateClusterId(startPos)
#############
targetNode=Node(maze.foodpos)
if self.closedNodes==[]:
self.startNode=Node(startPos, dir=startDir)
self.startNode.hCost = self.pathlen((startPos[0],startPos[1]),(targetNode.x,targetNode.y))
self.openNodes.append(self.startNode)
self.first=True
while self.openNodes!=[]:
if self.first:
self.currentNode = self.openNodes[0]
for node in self.openNodes:
if node.fCost() < self.currentNode.fCost():
self.currentNode = node
if self.currentNode in self.openNodes:
self.openNodes.remove(self.currentNode)
self.closedNodes.append(self.currentNode)
if (pygame.time.get_ticks() - begin_time > self.agent_time):
self.first=False
return self.retracePath(self.startNode,self.currentNode)
elif self.currentNode == targetNode: #or clusterId!=self.calculateClusterId(currentNode.get_pos())):
self.openNodes=[]
self.closedNodes=[]
return self.retracePath(self.startNode,self.currentNode)
self.first=True
for n in self.getNeighbours(self.currentNode, targetNode, maze):#otimizar
if n not in self.closedNodes and n not in self.openNodes:
self.openNodes.append(n)
def retracePath(self,startNode, endNode):
path=[]
currentNode = endNode
while currentNode != startNode:
path.append(currentNode)
currentNode = currentNode.parent
print("----")
dd=[print(x) for x in path]
return path #if path else [startNode]
def getNeighbours(self,node,foodNode,maze):
neighbours = []
validdirs = self.getValidDirs(node,maze)
for dir in validdirs:
coord = self.add((node.x,node.y),dir)
newnode = Node(coord, dir=dir,gCost=node.gCost+1,parent=node)
newnode.hCost = self.pathlen((newnode.x,newnode.y),(foodNode.x,foodNode.y))
neighbours.append(newnode)
for diagDir in self.getValidDirsDiag(node,maze):
coord = self.add((node.x,node.y),diagDir)
if node.dir == up or node.dir == down:
if diagDir == (-1,-1) or diagDir == (-1,1):
dir = left
else:
dir = right
else:
if diagDir == (-1,-1) or diagDir == (1,-1):
dir = up
else:
dir = down
if dir in validdirs:
newnode = Node(coord, dir=dir,gCost=node.gCost+2,parent=node)
newnode.hCost = self.pathlen((newnode.x,newnode.y),(foodNode.x,foodNode.y))
neighbours.append(newnode)
return neighbours
def getValidDirs(self,node,maze):
position=node.get_pos()
dirs = []
complement=[(up,down),(down,up),(right,left),(left,right)]
invaliddir=[x for (x,y) in complement if y==node.dir]
validdir = [dir for dir in directions if not ( dir in invaliddir )]
return [dir for dir in validdir if not self.add(position,dir) in maze.obstacles and not self.add(position,dir) in maze.playerpos and not self.add(position,dir) in self.body] #verificar se não vai contra o corpo
def getValidDirsDiag(self,node,maze):
position=node.get_pos()
diagComplement =[(up,(-1,1)), (up, (1,1)) , (down, (1,-1)), (down, (-1,-1)), (left, (1, 1)), (left, (1, -1)), (right, (-1,1)), (right, (-1, -1))]
diagDirections = [(1,1),(1,-1),(-1,1),(-1,-1)]
invaliddir = [y for (x,y) in diagComplement if x == node.dir]
validdir = [dir for dir in diagDirections if not ( dir in invaliddir )]
return [dir for dir in validdir if not self.add(position,dir) in maze.obstacles and not self.add(position,dir) in maze.playerpos and not self.add(position,dir) in self.body]
class Node:
def __init__(self,coord,dir=(0,0), gCost=0, hCost=0, parent=None):
self.x=coord[0]
self.y=coord[1]
self.dir=dir
self.parent=parent
self.gCost=gCost
self.hCost=hCost
def __str__(self):
return str((self.x,self.y))
def __eq__(self,other):
return self.x==other.x and self.y==other.y
def fCost(self):
return self.hCost+self.gCost
def get_pos(self):
return (self.x, self.y)
######################################################################################
######################################################################################
######################################################################################
class Cluster():
def __init__(self,idCluster,point1,point2):
self.id=idCluster #(0,0) first cluster (0,1) # second cluster
self.point1=point1
self.point2=point2
def __str__(self):
return str(((self.point1[0],self.point2[1]),(self.point1[0],self.point2[1])))
def getPoint1(self):
return self.point1
def getPoint2(self):
return self.point2
# def defineCluster(self):
# self.clusterLines=[]
# for x in range(self.horizontalNodesOfCluster):
# self.clusterLines.append((point1[0]+x,point1[1])
# self.clusterLines.append((point1[0]+x,point2[1]))
# for y in range(self.verticalNodesOfCluster):
# self.clusterLines.append((point2[0],point2[1]+y))
# self.clusterLines.append((point1[0],point2[1]+y))
# return self.clusterLines
# def getId(position):
# x,y=self.getClustern(position)
# if x != self.getClustern((x+postion[0],postion[1])[0]:
# x=x+1
# if y != self.getClustern((position[0],postion[1]+y)[1]:
# y=y+1
# return x*y #id of cluster| os ids são de 1 para cima.