Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from ants import *
- # define a class with a do_turn method
- # the Ants.run method will parse and update bot input
- # it will also run the do_turn method for us
- class MyBot:
- def __init__(self):
- # define class level variables, will be remembered between turns
- pass
- # do_setup is run once at the start of the game
- # after the bot has received the game settings
- # the ants class is created and setup by the Ants.run method
- def do_setup(self, ants):
- # initialize data structures after learning the game settings
- self.priority=[[0.00 for j in range(ants.cols)] for i in range(ants.rows)]
- self.enemy_hills=[]
- self.my_ants=[] #stores ants
- self.paths=[] #paths[ants.my_ants().index(ant)]
- # priority constants
- self.PRIOR_VISITED=-90
- self.PRIOR_VISIBLE=0
- self.PRIOR_WATER=-20
- self.PRIOR_FOOD=50
- self.PRIOR_ENEMY_HILL=10
- self.PRIOR_MY_HILL=-2
- self.PRIOR_ENEMY_ANT=-4 # for one lone ant
- self.PRIOR_MY_ANT=1 # with no enemies
- self.RADIUS_VISIBLE=0 #2
- self.RADIUS_WATER=2
- self.RADIUS_FOOD=10
- self.RADIUS_ENEMY_HILL=15
- self.RADIUS_MY_HILL=4
- self.RADIUS_ENEMY_ANT=3 # for one lone ant
- self.RADIUS_MY_ANT=2
- # do turn is run once per turn
- # the ants class has the game state and is updated by the Ants.run method
- # it also has several helper methods to use
- def do_turn(self, ants):
- def add_priority(_x,_y,radius,amount):
- xx=_x
- yy=_y-radius
- while yy<=_y+radius:
- while xx<=_x+radius-abs(yy-_y):
- #sys.stderr.write(str(self.priority[xx%ants.rows][yy%ants.cols]))
- #self.priority[xx%ants.rows][yy%ants.cols]+=amount*(float(radius)+1-(abs(xx-_x)+abs(yy-_y)))/(radius+1)
- if ants.passable((xx%ants.rows,yy%ants.cols)) and (xx%ants.rows,yy%ants.cols) not in ants.my_ants():
- self.priority[xx%ants.rows][yy%ants.cols]+=amount*((radius+1-(float(abs(xx-_x)+abs(yy-_y))))/(radius+1))**2
- #if(amount>0 and _x==28 and _y==17 and xx==28 and yy==18):
- #print(amount*(radius+1-(abs(xx-_x)+abs(yy-_y)))/(radius+1))
- xx+=1
- yy+=1
- xx=_x+abs(yy-_y)-radius
- # end def add_priority
- def check_timeout():
- #if ants.time_remaining() < ants.turntime/30:
- if ants.time_remaining() < ants.turntime/10:
- print("OHNO")
- return True
- else:
- return False
- def nearby_ants(loc, max_dist, include=None, exclude=None):
- """ Returns ants where 0 < dist to loc <= sqrt(max_dist)
- If exclude is not None, ants with owner == exclude
- will be ignored.
- """
- antslist = []
- row, col = loc
- for d_row, d_col in ants.neighbourhood_offsets(loc,max_dist):
- if ANTS <= ants.map[row+d_row][col+d_col] != exclude:
- if include==None or (include!=None and ANTS <= ants.map[row+d_row][col+d_col] == include):
- n_loc = self.destination(loc, (d_row, d_col))
- antslist.append(self.current_ants[n_loc])
- return antslist
- ordered=[]
- #print((ants.rows,ants.cols))
- self.priority=[[0.00 for j in range(ants.cols)] for i in range(ants.rows)]
- #newpriority=[[0.00 for j in range(ants.cols)] for i in range(ants.rows)]
- for x in range(ants.rows):
- for y in range(ants.cols):
- if ants.time_remaining() < 50:
- #print("OHNO")
- return
- #if ants.visible((x,y)):
- #add_priority(x,y,self.RADIUS_VISIBLE,self.PRIOR_VISIBLE)
- #continue
- if not ants.visible((x,y)):
- #continue
- pass
- elif not ants.passable((x,y)):
- add_priority(x,y,self.RADIUS_WATER,self.PRIOR_WATER)
- #continue
- elif (x,y) in ants.food():
- add_priority(x,y,self.RADIUS_FOOD,self.PRIOR_FOOD)
- #continue
- elif (x,y) in self.enemy_hills:
- add_priority(x,y,self.RADIUS_ENEMY_HILL,self.PRIOR_ENEMY_HILL)
- #continue
- elif (x,y) in ants.my_hills():
- add_priority(x,y,self.RADIUS_MY_HILL,self.PRIOR_MY_HILL)
- #continue
- elif (x,y) in ants.enemy_ants():
- # 0 = 1*PRIOR_ENEMY_ANT
- # 1 = 0
- # 2 = -1*PRIOR_ENEMY_ANT
- #add_priority(x,y,self.RADIUS_ENEMY_ANT,(1-len(nearby_ants((x,y),1,ants.map[x][y],None)))*self.PRIOR_ENEMY_ANT)
- add_priority(x,y,self.RADIUS_ENEMY_ANT,self.PRIOR_ENEMY_ANT)
- #continue
- elif (x,y) in ordered or (x,y) in ants.my_ants():
- # 0 = PRIOR_MY_ANT
- # 1 = 0
- # 2 = -1*PRIOR_MY_ANT
- #add_priority(x,y,self.RADIUS_MY_ANT,(1-len(nearby_ants((x,y),1,None,ants.map[x][y])))*self.PRIOR_MY_ANT)
- add_priority(x,y,self.RADIUS_MY_ANT,self.PRIOR_MY_ANT)
- #continue
- #self.priority[x][y]=(self.priority[x][y]+newpriority[x][y])/2
- #self.priority[x][y]=newpriority[x][y]
- if check_timeout():
- return
- if check_timeout():
- return
- print(ants.time_remaining())
- # general loop
- # record new enemy hills
- for enemy_hill in ants.enemy_hills():
- if enemy_hill not in self.enemy_hills:
- self.enemy_hills.append(enemy_hill[0])
- if check_timeout():
- return
- # record new my ants
- for ant in ants.my_ants():
- if ant not in self.my_ants:
- self.my_ants.append(ant)
- self.paths.append([])
- if check_timeout():
- return
- # delete dead ants
- for ant in self.my_ants[:]:
- if ant not in ants.my_ants():
- index=self.my_ants.index(ant)
- self.my_ants.pop(index)
- self.paths.pop(index)
- if check_timeout():
- return
- # end general loop
- if check_timeout():
- return
- # prepare orders
- #preorders=[] # (loc,direction)
- ordered=[] # loc
- for ant in ants.my_ants():
- ant_id=self.my_ants.index(ant)
- directions=['n','w','s','e','']
- _prior=[0.00,0.00,0.00,0.00,0.00] # (int priority,char direction)
- for d in range(4):
- if directions[d]=='':
- dest=ant
- else:
- dest=ants.destination(ant,directions[d])
- priority=self.priority[dest[0]][dest[1]]
- # if previously visited, penalize
- if dest in self.paths[ant_id]:
- priority+=self.PRIOR_VISITED
- # if impassable or has ant, penalize horribly
- if not ants.passable(dest) or dest in ordered:
- priority-=100000
- _prior[d]=(priority,directions[d])
- _prior.sort()
- _prior.reverse()
- #print(_prior)
- if(_prior[0][1]==''):
- bestdest=ant
- else:
- bestdest=ants.destination(ant,_prior[0][1])
- if ants.passable(bestdest) and bestdest not in ordered:
- index=self.my_ants.index(ant)
- self.my_ants[index]=bestdest
- self.paths[index].insert(0,ant)
- ordered.append(bestdest)
- if(_prior[0][1]!=''):
- ants.issue_order((ant,_prior[0][1]))
- if check_timeout():
- return
- # precompute weaknesses
- #my_weakness=[] # my_weakness[order_id]=[enemy ants nearby]
- #enemy_weakness=[] # enemy_weakness[loc]=[enemy ants nearby]
- #for order_id in range(len(preorders)):
- #order=preorders[order_id] #(loc,dir)
- #dest=ants.destination(order[0],order[1])
- #my_weakness[order_id]=ants.nearby_ants(dest,ants.attackradius,ant.owner)
- #for enemy_ant in ants.enemy_ants():
- #enemy_weakness[enemy_ant]=ants.nearby_ants(enemy_ant,ants.attackradius,enemy_ant.owner)
- #order_fixed=[]
- # adjust orders
- #for order_id in range(len(preorders)): #order_id = int, preorders[order_id]=(loc,dir)
- #order=preorders[order_id]
- #if(order in order_fixed):
- #continue
- #if(my_weakness[order_id]==[]):
- #order_fixed.append(order_id)
- #continue
- ## contested orders (enemy ant nearby)
- #min_enemy_weakness=min(len(enemy_weakness[enemy]) for enemy in my_weakness[order_id])
- #if(len(my_weakness[order_id])>=min_enemy_weakness):
- ## avoid
- #_weakdir=[] #(int weakness,char direction)
- #for d in ['n','w','s','e']:
- #_dest=ants.destination(order[0],d)
- #_weakdir.append(my_weakness[_dest],d)
- #
- ## issue orders
- #for ant,d in preorders:
- #dest=ants.destination(ant,d)
- #ordered.append(dest)
- #ants.issue_order((ant,d))
- # start post general loop
- # filter out closed
- #for n in range(len(self.paths[:])):
- # self.paths[n].insert(0,None)
- # self.paths[n]=self.paths[n][len(self.paths[n])-10:]
- # end post general loop
- if __name__ == '__main__':
- # psyco will speed up python a little, but is not needed
- try:
- import psyco
- psyco.full()
- except ImportError:
- pass
- try:
- # if run is passed a class with a do_turn method, it will do the work
- # this is not needed, in which case you will need to write your own
- # parsing function and your own game state class
- Ants.run(MyBot())
- except KeyboardInterrupt:
- print('ctrl-c, leaving ...')
Advertisement
Add Comment
Please, Sign In to add comment