ThatSnail

DiffusionBot

Nov 30th, 2011
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from ants import *
  3.  
  4. # define a class with a do_turn method
  5. # the Ants.run method will parse and update bot input
  6. # it will also run the do_turn method for us
  7. class MyBot:
  8. def __init__(self):
  9. # define class level variables, will be remembered between turns
  10. pass
  11.  
  12. # do_setup is run once at the start of the game
  13. # after the bot has received the game settings
  14. # the ants class is created and setup by the Ants.run method
  15. def do_setup(self, ants):
  16. # initialize data structures after learning the game settings
  17. self.priority=[[0.00 for j in range(ants.cols)] for i in range(ants.rows)]
  18. self.enemy_hills=[]
  19. self.my_ants=[] #stores ants
  20. self.paths=[] #paths[ants.my_ants().index(ant)]
  21.  
  22. # priority constants
  23. self.PRIOR_VISITED=-90
  24.  
  25. self.PRIOR_VISIBLE=0
  26. self.PRIOR_WATER=-20
  27. self.PRIOR_FOOD=50
  28. self.PRIOR_ENEMY_HILL=10
  29. self.PRIOR_MY_HILL=-2
  30. self.PRIOR_ENEMY_ANT=-4 # for one lone ant
  31. self.PRIOR_MY_ANT=1 # with no enemies
  32.  
  33. self.RADIUS_VISIBLE=0 #2
  34. self.RADIUS_WATER=2
  35. self.RADIUS_FOOD=10
  36. self.RADIUS_ENEMY_HILL=15
  37. self.RADIUS_MY_HILL=4
  38. self.RADIUS_ENEMY_ANT=3 # for one lone ant
  39. self.RADIUS_MY_ANT=2
  40.  
  41. # do turn is run once per turn
  42. # the ants class has the game state and is updated by the Ants.run method
  43. # it also has several helper methods to use
  44. def do_turn(self, ants):
  45. def add_priority(_x,_y,radius,amount):
  46. xx=_x
  47. yy=_y-radius
  48. while yy<=_y+radius:
  49. while xx<=_x+radius-abs(yy-_y):
  50. #sys.stderr.write(str(self.priority[xx%ants.rows][yy%ants.cols]))
  51. #self.priority[xx%ants.rows][yy%ants.cols]+=amount*(float(radius)+1-(abs(xx-_x)+abs(yy-_y)))/(radius+1)
  52. if ants.passable((xx%ants.rows,yy%ants.cols)) and (xx%ants.rows,yy%ants.cols) not in ants.my_ants():
  53. self.priority[xx%ants.rows][yy%ants.cols]+=amount*((radius+1-(float(abs(xx-_x)+abs(yy-_y))))/(radius+1))**2
  54. #if(amount>0 and _x==28 and _y==17 and xx==28 and yy==18):
  55. #print(amount*(radius+1-(abs(xx-_x)+abs(yy-_y)))/(radius+1))
  56. xx+=1
  57. yy+=1
  58. xx=_x+abs(yy-_y)-radius
  59. # end def add_priority
  60.  
  61. def check_timeout():
  62. #if ants.time_remaining() < ants.turntime/30:
  63. if ants.time_remaining() < ants.turntime/10:
  64. print("OHNO")
  65. return True
  66. else:
  67. return False
  68.  
  69. def nearby_ants(loc, max_dist, include=None, exclude=None):
  70. """ Returns ants where 0 < dist to loc <= sqrt(max_dist)
  71.  
  72. If exclude is not None, ants with owner == exclude
  73. will be ignored.
  74. """
  75. antslist = []
  76. row, col = loc
  77. for d_row, d_col in ants.neighbourhood_offsets(loc,max_dist):
  78. if ANTS <= ants.map[row+d_row][col+d_col] != exclude:
  79. if include==None or (include!=None and ANTS <= ants.map[row+d_row][col+d_col] == include):
  80. n_loc = self.destination(loc, (d_row, d_col))
  81. antslist.append(self.current_ants[n_loc])
  82. return antslist
  83.  
  84. ordered=[]
  85. #print((ants.rows,ants.cols))
  86. self.priority=[[0.00 for j in range(ants.cols)] for i in range(ants.rows)]
  87. #newpriority=[[0.00 for j in range(ants.cols)] for i in range(ants.rows)]
  88. for x in range(ants.rows):
  89. for y in range(ants.cols):
  90. if ants.time_remaining() < 50:
  91. #print("OHNO")
  92. return
  93. #if ants.visible((x,y)):
  94. #add_priority(x,y,self.RADIUS_VISIBLE,self.PRIOR_VISIBLE)
  95. #continue
  96. if not ants.visible((x,y)):
  97. #continue
  98. pass
  99. elif not ants.passable((x,y)):
  100. add_priority(x,y,self.RADIUS_WATER,self.PRIOR_WATER)
  101. #continue
  102. elif (x,y) in ants.food():
  103. add_priority(x,y,self.RADIUS_FOOD,self.PRIOR_FOOD)
  104. #continue
  105. elif (x,y) in self.enemy_hills:
  106. add_priority(x,y,self.RADIUS_ENEMY_HILL,self.PRIOR_ENEMY_HILL)
  107. #continue
  108. elif (x,y) in ants.my_hills():
  109. add_priority(x,y,self.RADIUS_MY_HILL,self.PRIOR_MY_HILL)
  110. #continue
  111. elif (x,y) in ants.enemy_ants():
  112. # 0 = 1*PRIOR_ENEMY_ANT
  113. # 1 = 0
  114. # 2 = -1*PRIOR_ENEMY_ANT
  115. #add_priority(x,y,self.RADIUS_ENEMY_ANT,(1-len(nearby_ants((x,y),1,ants.map[x][y],None)))*self.PRIOR_ENEMY_ANT)
  116. add_priority(x,y,self.RADIUS_ENEMY_ANT,self.PRIOR_ENEMY_ANT)
  117. #continue
  118. elif (x,y) in ordered or (x,y) in ants.my_ants():
  119. # 0 = PRIOR_MY_ANT
  120. # 1 = 0
  121. # 2 = -1*PRIOR_MY_ANT
  122. #add_priority(x,y,self.RADIUS_MY_ANT,(1-len(nearby_ants((x,y),1,None,ants.map[x][y])))*self.PRIOR_MY_ANT)
  123. add_priority(x,y,self.RADIUS_MY_ANT,self.PRIOR_MY_ANT)
  124. #continue
  125. #self.priority[x][y]=(self.priority[x][y]+newpriority[x][y])/2
  126. #self.priority[x][y]=newpriority[x][y]
  127.  
  128. if check_timeout():
  129. return
  130. if check_timeout():
  131. return
  132. print(ants.time_remaining())
  133. # general loop
  134. # record new enemy hills
  135. for enemy_hill in ants.enemy_hills():
  136. if enemy_hill not in self.enemy_hills:
  137. self.enemy_hills.append(enemy_hill[0])
  138. if check_timeout():
  139. return
  140. # record new my ants
  141. for ant in ants.my_ants():
  142. if ant not in self.my_ants:
  143. self.my_ants.append(ant)
  144. self.paths.append([])
  145. if check_timeout():
  146. return
  147. # delete dead ants
  148. for ant in self.my_ants[:]:
  149. if ant not in ants.my_ants():
  150. index=self.my_ants.index(ant)
  151. self.my_ants.pop(index)
  152. self.paths.pop(index)
  153. if check_timeout():
  154. return
  155. # end general loop
  156.  
  157. if check_timeout():
  158. return
  159.  
  160. # prepare orders
  161. #preorders=[] # (loc,direction)
  162. ordered=[] # loc
  163. for ant in ants.my_ants():
  164. ant_id=self.my_ants.index(ant)
  165. directions=['n','w','s','e','']
  166. _prior=[0.00,0.00,0.00,0.00,0.00] # (int priority,char direction)
  167. for d in range(4):
  168. if directions[d]=='':
  169. dest=ant
  170. else:
  171. dest=ants.destination(ant,directions[d])
  172. priority=self.priority[dest[0]][dest[1]]
  173. # if previously visited, penalize
  174. if dest in self.paths[ant_id]:
  175. priority+=self.PRIOR_VISITED
  176. # if impassable or has ant, penalize horribly
  177. if not ants.passable(dest) or dest in ordered:
  178. priority-=100000
  179. _prior[d]=(priority,directions[d])
  180. _prior.sort()
  181. _prior.reverse()
  182. #print(_prior)
  183. if(_prior[0][1]==''):
  184. bestdest=ant
  185. else:
  186. bestdest=ants.destination(ant,_prior[0][1])
  187. if ants.passable(bestdest) and bestdest not in ordered:
  188. index=self.my_ants.index(ant)
  189. self.my_ants[index]=bestdest
  190. self.paths[index].insert(0,ant)
  191. ordered.append(bestdest)
  192. if(_prior[0][1]!=''):
  193. ants.issue_order((ant,_prior[0][1]))
  194. if check_timeout():
  195. return
  196.  
  197. # precompute weaknesses
  198. #my_weakness=[] # my_weakness[order_id]=[enemy ants nearby]
  199. #enemy_weakness=[] # enemy_weakness[loc]=[enemy ants nearby]
  200. #for order_id in range(len(preorders)):
  201. #order=preorders[order_id] #(loc,dir)
  202. #dest=ants.destination(order[0],order[1])
  203. #my_weakness[order_id]=ants.nearby_ants(dest,ants.attackradius,ant.owner)
  204. #for enemy_ant in ants.enemy_ants():
  205. #enemy_weakness[enemy_ant]=ants.nearby_ants(enemy_ant,ants.attackradius,enemy_ant.owner)
  206.  
  207. #order_fixed=[]
  208. # adjust orders
  209. #for order_id in range(len(preorders)): #order_id = int, preorders[order_id]=(loc,dir)
  210. #order=preorders[order_id]
  211. #if(order in order_fixed):
  212. #continue
  213. #if(my_weakness[order_id]==[]):
  214. #order_fixed.append(order_id)
  215. #continue
  216. ## contested orders (enemy ant nearby)
  217. #min_enemy_weakness=min(len(enemy_weakness[enemy]) for enemy in my_weakness[order_id])
  218. #if(len(my_weakness[order_id])>=min_enemy_weakness):
  219. ## avoid
  220. #_weakdir=[] #(int weakness,char direction)
  221. #for d in ['n','w','s','e']:
  222. #_dest=ants.destination(order[0],d)
  223. #_weakdir.append(my_weakness[_dest],d)
  224. #
  225. ## issue orders
  226. #for ant,d in preorders:
  227. #dest=ants.destination(ant,d)
  228. #ordered.append(dest)
  229. #ants.issue_order((ant,d))
  230.  
  231. # start post general loop
  232. # filter out closed
  233. #for n in range(len(self.paths[:])):
  234. # self.paths[n].insert(0,None)
  235. # self.paths[n]=self.paths[n][len(self.paths[n])-10:]
  236. # end post general loop
  237.  
  238. if __name__ == '__main__':
  239. # psyco will speed up python a little, but is not needed
  240. try:
  241. import psyco
  242. psyco.full()
  243. except ImportError:
  244. pass
  245.  
  246. try:
  247. # if run is passed a class with a do_turn method, it will do the work
  248. # this is not needed, in which case you will need to write your own
  249. # parsing function and your own game state class
  250. Ants.run(MyBot())
  251. except KeyboardInterrupt:
  252. print('ctrl-c, leaving ...')
  253.  
  254.  
Advertisement
Add Comment
Please, Sign In to add comment