Esempio n. 1
0
	def __init__(self, ants, logger):
		# Setup share variables
		self.ants = ants
		self.logger = logger
		self.turn = 0
		
		self.hill_time = HILL_TIME_LIMIT * self.ants.turntime
		self.attack_time = ATTACK_TIME_LIMIT * self.ants.turntime
		self.food_time = FOOD_TIME_LIMIT * self.ants.turntime
		self.explore_time = EXPLORE_TIME_LIMIT * self.ants.turntime
		
		# Setup my ant variables
		self.my_ants = set()
		self.movable = set()
		self.num_my_ants = 0		
		self.remove_ant = set()
		self.my_hills = set()
		
		# Setup enemy ant variables
		self.enemy_ants = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
		self.enemy_hills = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
		self.num_enemy_ants = 0
		
		# Setup path variables
		self.paths = []
		
		# Setup food variables
		self.food = set()
		self.targets = set()
		
		# Setup bucket map 
		self.size = BUCKET_SIZE
		self.bucket_rows = int(self.ants.rows / self.size)
		self.bucket_cols = int(self.ants.cols / self.size)

		self.bucket_map = [ [ bucket(self.size) ] * (self.bucket_rows + 1) for i in range(self.bucket_cols + 1)]
		
		# Empty the buckets
		for col in range(self.bucket_cols):
			for row in range(self.bucket_rows):
				self.bucket_map[col][row] = bucket(self.size)
Esempio n. 2
0
	def __init__(self, ants, logger):
		# Setup share variables
		self.ants = ants
		self.logger = logger
		self.turn = 0	
		
		# Setup my ant variables
		self.my_ants = set()
		self.movable = set()
		self.num_my_ants = 0		
		self.remove_ant = set()
		self.my_hills = set()
		
		# Setup enemy ant variables
		self.enemy_ants = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
		self.enemy_hills = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
		self.num_enemy_ants = 0
		
		# Setup path variables
		self.paths = []
		
		# Setup food variables
		self.food = set()
		self.targets = set()
		
		# Setup bucket map 
		self.size = 8
		self.bucket_rows = int(self.ants.rows / self.size)
		self.bucket_cols = int(self.ants.cols / self.size)

		self.bucket_map = [ [ bucket(self.size) ] * (self.bucket_rows + 1) for i in range(self.bucket_cols + 1)]
		
		# Empty the buckets
		for col in range(self.bucket_cols):
			for row in range(self.bucket_rows):
				self.bucket_map[col][row] = bucket(self.size)
Esempio n. 3
0
	def __init__(self, ants, logger):
		try:		
			# Setup shared variables
			self.ants = ants
			self.logger = logger
			self.render = render(ants, self)
			self.turn = 0
			self.diffuse_rate = DIFFUSE_RATE
			self.coefficient = COEFFICIENT
			
			# Time out setup
			total_time = self.ants.turntime
			safe_time = total_time * TIME_OUT_BUFFER
			self.time_out_vision = total_time - (TIME_OUT_VISION * safe_time)
			self.time_out_analysis = self.time_out_vision - (TIME_OUT_ANALYSIS * safe_time)
			self.time_out_diffuse = self.time_out_analysis - (TIME_OUT_DIFFUSE * safe_time)
			self.logger.debug("Time out total: %s , safe: %s", total_time, safe_time)
			self.logger.debug("Time out vision: %s", self.time_out_vision)
			self.logger.debug("Time out analysis: %s", self.time_out_analysis)
			self.logger.debug("Time out diffuse: %s", self.time_out_diffuse)
			
			self.kill_time = self.ants.turns * KILL_TIME
			self.logger.debug("Kill time: %s", self.kill_time)
			
			# Setup my ant variables
			self.my_hills = set()
			self.num_my_hills = 0
			self.prev_my_hills = 0
			
			self.my_dead_ants = []
			self.my_ants = set()
			self.movable = set()
			self.num_my_ants = 0		
			
			# Setup enemy ant variables
			self.enemy_dead_ants = []
			self.enemy_ants = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
			self.enemy_hills = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
			self.num_enemy_ants = 0

			# Setup food variables
			self.food = set()
			
			self.desired = set()
			self.weakest = 0
			self.target = 0
			self.confidence = 0
			self.attack_ant = None
			self.attack_target = None
			self.food_target = None
			self.path_max = PATH_MAX
			self.paths = []
			
			self.map_scan = map_scan(ants, logger, self)				
					
			# Setup bucket map 
			self.size = BUCKET_SIZE
			self.bucket_rows = int(self.ants.rows / self.size)
			self.bucket_cols = int(self.ants.cols / self.size)
			
			self.find_bucket = [[(0,0)]*self.ants.cols for row in range(self.ants.rows)]	
			for row in range(self.ants.rows):
				for col in range(self.ants.cols):
					b_col = int(col / self.size)
					b_row = int(row / self.size)
					self.find_bucket[row][col] = (b_row, b_col)
					
			self.bucket_map = []
			for row in range(self.bucket_rows + 1):
				self.bucket_map.append([])
				for col in range(self.bucket_cols + 1):
					self.bucket_map[row].append(bucket(self.size))
				
			for row in range(self.bucket_rows):
				for col in range(self.bucket_cols):
					self.bucket_map[row][col].loc = (row * self.size, col * self.size)
							
			# Other maps
			self.occupied = [[False]*self.ants.cols for row in range(self.ants.rows)]
			self.mobile_map = [[False]*self.ants.cols for row in range(self.ants.rows)]
			self.food_map = [[False]*self.ants.cols for row in range(self.ants.rows)]
			self.seen = [[False]*self.ants.cols for row in range(self.ants.rows)]
			self.passable = [[True]*self.ants.cols for row in range(self.ants.rows)]
			self.vision = [[False]*self.ants.cols for row in range(self.ants.rows)]
			self.diffusion = [[0]*self.ants.cols for row in range(self.ants.rows)]
			self.new_diffusion = [[0]*self.ants.cols for row in range(self.ants.rows)]
			self.my_attack_map = [[0]*self.ants.cols for row in range(self.ants.rows)]
			self.enemy_attack_map = [[0]*self.ants.cols for row in range(self.ants.rows)]
			self.fighting_map = [[-999999]*self.ants.cols for row in range(self.ants.rows)]
			self.g_map = [[0]*self.ants.cols for row in range(self.ants.rows)]
			self.g_map = [[0]*self.ants.cols for row in range(self.ants.rows)]
			self.g_highest = 0
			self.nodes = []
						
			# Setup vision offset
			self.vision_offsets_2 = []
			mx = int(sqrt(self.ants.viewradius2))
			for d_row in range(-mx,mx+1):
				for d_col in range(-mx,mx+1):
					d = d_row**2 + d_col**2
					if d <= self.ants.viewradius2:
						self.vision_offsets_2.append((
							d_row%self.ants.rows-self.ants.rows,
							d_col%self.ants.cols-self.ants.cols
						))
			
			# Setup attack offset
			radius = self.ants.attackradius2 * ATTACK_BUFFER
			self.enemy_attack_offsets_2 = []
			mx = int(sqrt(radius))
			for d_row in range(-mx,mx+1):
				for d_col in range(-mx,mx+1):
					d = d_row**2 + d_col**2
					if d <= radius:
						self.enemy_attack_offsets_2.append((
							d_row%self.ants.rows-self.ants.rows,
							d_col%self.ants.cols-self.ants.cols
						))
						
			# Setup attack offset
			radius = self.ants.attackradius2 * DEFEND_BUFFER
			self.my_attack_offsets_2 = []
			mx = int(sqrt(radius))
			for d_row in range(-mx,mx+1):
				for d_col in range(-mx,mx+1):
					d = d_row**2 + d_col**2
					if d <= radius:
						self.my_attack_offsets_2.append((
							d_row%self.ants.rows-self.ants.rows,
							d_col%self.ants.cols-self.ants.cols
						))
			
			# Full sized maps
			self.west_map = []
			self.east_map = []
			self.north_map = []
			self.south_map = []
					
			for row in range(self.ants.rows):
				self.west_map.append((row - 1) % self.ants.rows)
				self.east_map.append((row + 1) % self.ants.rows)
				
			for col in range(self.ants.cols):
				self.north_map.append((col - 1) % self.ants.cols)
				self.south_map.append((col + 1) % self.ants.cols)
				
			# Bucket sized maps			
			self.west_bucket_map = []
			self.east_bucket_map = []
			self.north_bucket_map = []
			self.south_bucket_map = []			
			
			for row in range(self.bucket_rows):
				self.west_bucket_map.append((row - 1) % self.bucket_rows)
				self.east_bucket_map.append((row + 1) % self.bucket_rows)
				
			for col in range(self.bucket_cols):
				self.north_bucket_map.append((col - 1) % self.bucket_cols)
				self.south_bucket_map.append((col + 1) % self.bucket_cols)
			
			self.diffuse_sections = []
			self.bucket_sections = []
			self.total_sections = int(1 / (SECTION * SECTION))
			self.next_diffuse_section = 0
			self.next_vision_section = 1
			sections = int(1 / SECTION)
			for row in range(sections):
				for col in range(sections):
					left = int((row / sections) * self.ants.rows)
					top = int((col / sections) * self.ants.cols)
					right = int(((row + 1) / sections) * self.ants.rows)
					bottom = int(((col + 1) / sections) * self.ants.cols)
					section_bounds = bounds(left, top, right, bottom)
					self.diffuse_sections.append(section_bounds)

					left = int((row / sections) * self.bucket_rows)
					top = int((col / sections) * self.bucket_cols)
					right = int(((row + 1) / sections) * self.bucket_rows)
					bottom = int(((col + 1) / sections) * self.bucket_cols)
					section_bounds = bounds(left, top, right, bottom)
					self.bucket_sections.append(section_bounds)
					
			self.diffuse_sections.append(bounds(0, 0, 0, 0))
			self.bucket_sections.append(bounds(0, 0, 0, 0))
	
			#self.logger.debug("Diffuse Map size %s, %s", self.ants.rows, self.ants.cols)
			#for section in self.diffuse_sections:
			#	self.logger.debug("Diffuse Section left %s, top %s, right %s, bottom %s", section.left, section.top, section.right, section.bottom)
			
			#self.logger.debug("Bucket Map size %s, %s", self.bucket_rows, self.bucket_cols)
			#for section in self.bucket_sections:
			#	self.logger.debug("Bucket Section left %s, top %s, right %s, bottom %s", section.left, section.top, section.right, section.bottom)
				
		except:
			self.logger.error("Error game_state init: print_exc(): %s", traceback.format_exc())
Esempio n. 4
0
class node:
    bucket = bucket()
    isLeaf = bool()
    unbalanced = bool()
    spilled = bool()
    key = bytes()
    pgid = int()
    parent = node()
    children = []
    inodes = []
    def __init__(self):
        pass

    def root(self):
        if self.parent is None:
            return self
        return self.parent.root()

    def minKeys(self):
        if self.isLeaf:
            return 1
        return 2
    def size(self):
        sz , elsz = page.pageHeaderSize, self.pageElementSize()
        for i in range(self.inodes):
            item = self.inodes[i]
            sz += elsz + len(item.key) + len(item.value)
        return sz
    def sizeLessThan(self):
        sz, elsz = page.pageHeaderSize, self.pageElementSize()
        for i in range(self.inodes):
            item = self.inodes[i]
            sz += elsz + len(item.key) + len(item.value)
            if sz >= v:
                return False
        return True
    def pageElementSize(self):
        if self.isLeaf:
            return page.leafPageElementSize
        return page.branchPageElementSize
    def childAt(self,index):
        if self.isLeaf:
            raise Exception("invalid childAt(%d) on a leaf node" % index)
        return self.bucket.node(self.inodes[index].pgid, self)
    def childIndex(self, child):
        for i in self.inodes:
            if self.inodes[i].key == child.key:
                return i
        return -1
    def numChildren(self):
        return len(self.inodes)
    def nextSiblint(self):
        if self.parent is None:
            return None
        index = self.parent.childIndex(self)
        if index == -1:
            return None
        return self.parent.childAt(index + 1)
    def prevSibling(self):
        if self.parent is None:
            return None
        index = self.parent.childIndex(self)
        if index == -1:
            return None
        return self.parent.childAt(index -1 )
    def put(self,oldKey,newKey,value,pgid,flags):
        if pgid >= self.bucket.tx.meta.pgid:
            raise Exception("pgid (%d) above high water mark (%d)" % pgid, self.bucket.tx.meta.pgid)
        elif len(oldKey) <= 0:
            raise Exception("put: zero-length old key")
        elif len(newKey) <=0:
            raise Exception("put: zero-length new key")
        index = -1
        for i in self.inodes:
            if self.inodes[i].key == oldKey:
                index = i
        exact = (len(self.inodes) >0 and index < len(self.inodes) and self.inodes[i].key == oldKey )
        if exact is not False:
            self.inodes = self.inodes.append(inode())
            self.inodes[index+1:] = self.inodes[index:]
        inode = self.inodes[index]
        inode.flags = flags
        inode.key = newKey
        inode.value = value
        inode.pgid = pgid
        if len(inode.key) <= 0:
            raise Exception("put: zero-length inode key")

    def delete(self,key):
        index = -1
        for i in self.inodes:
            if self.inodes[i].key == key:
                index = i
        if index >= len(self.inodes) or not cmp(self.inodes[index].key,key):
            return
        self.inodes.append(self.inodes[:index],self.inodes[index+1:])
        self.unbalanced = True
    def read(self,p):
Esempio n. 5
0
	def __init__(self, ants, logger):
		# Setup share variables
		self.ants = ants
		self.logger = logger
		self.render = render(ants, self)
		self.turn = 0
						
		# Setup my ant variables
		self.my_ants = set()
		self.movable = set()
		self.num_my_ants = 0		
		self.my_hills = set()
		
		# Setup enemy ant variables
		self.enemy_ants = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
		self.enemy_hills = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
		self.num_enemy_ants = 0

		# Setup food variables
		self.food = set()
		self.eaten = 0
		self.unseen = set()
		self.fog = set()
		self.blocked = set()
		
		self.reset_list = [[False]*self.ants.cols for row in range(self.ants.rows)]
		self.empty = [[0]*self.ants.cols for row in range(self.ants.rows)]
		
		# Setup a quick set of all the blocks
		self.all_blocks = set()
		for col in range(self.ants.cols):
			for row in range(self.ants.rows):
				loc = row, col
				self.all_blocks.add(loc)
				
		
		for row in range(self.ants.rows):
			for col in range(self.ants.cols):
				loc = (row, col)
				self.unseen.add(loc)
				
		# Setup bucket map 
		self.size = BUCKET_SIZE
		self.bucket_rows = int(self.ants.rows / self.size)
		self.bucket_cols = int(self.ants.cols / self.size)
		
		self.find_bucket = [[(0,0)]*self.ants.cols for row in range(self.ants.rows)]	
		for row in range(self.ants.rows):
			for col in range(self.ants.cols):
				b_col = int(col / self.size)
				b_row = int(row / self.size)
				self.find_bucket[row][col] = (b_row, b_col)
				
		self.bucket_map = []
		for row in range(self.bucket_rows + 1):
			self.bucket_map.append([])
			for col in range(self.bucket_cols + 1):
				self.bucket_map[row].append(bucket(self.size))
			
		for row in range(self.bucket_rows):
			for col in range(self.bucket_cols):
				self.bucket_map[row][col].loc = (row * self.size, col * self.size)
						
		# Other maps
		self.occupied = [[False]*self.ants.cols for row in range(self.ants.rows)]
		self.mobile_map = [[False]*self.ants.cols for row in range(self.ants.rows)]
		self.food_map = [[False]*self.ants.cols for row in range(self.ants.rows)]
		self.seen = [[False]*self.ants.cols for row in range(self.ants.rows)]
		self.passable = [[True]*self.ants.cols for row in range(self.ants.rows)]
		self.vision = [[False]*self.ants.cols for row in range(self.ants.rows)]
		self.diffusion = [[0]*self.ants.cols for row in range(self.ants.rows)]
		self.new_diffusion = [[0]*self.ants.cols for row in range(self.ants.rows)]
		
		# Setup vision offset
		self.vision_offsets_2 = []
		mx = int(sqrt(self.ants.viewradius2))
		for d_row in range(-mx,mx+1):
			for d_col in range(-mx,mx+1):
				d = d_row**2 + d_col**2
				if d <= self.ants.viewradius2:
					self.vision_offsets_2.append((
						d_row%self.ants.rows-self.ants.rows,
						d_col%self.ants.cols-self.ants.cols
					))
		
		# Setup attack offset
		radius = self.ants.attackradius2 * ATTACK_BUFFER
		self.attack_offsets_2 = []
		mx = int(sqrt(radius))
		for d_row in range(-mx,mx+1):
			for d_col in range(-mx,mx+1):
				d = d_row**2 + d_col**2
				if d <= radius:
					self.attack_offsets_2.append((
						d_row%self.ants.rows-self.ants.rows,
						d_col%self.ants.cols-self.ants.cols
					))