def encode_state_range(agent, vision_range): grid = agent.model.grid radius = vision_range #cell_locations = grid.get_neighborhood(agent.pos, moore = True, include_center = True, radius = vision_range) state_w_h = vision_range + vision_range + 1 list_state = np.zeros((state_w_h, state_w_h)) list_x = 0 #stored in list as list_y = 0 #stored in list as pos_x = agent.pos[0] # actual location of agent to start pos_y = agent.pos[1] # actual location of agent to start for dy in range(-radius, radius + 1): for dx in range(-radius, radius + 1): grid_position = grid.torus_adj((pos_x + dx, pos_y + dy)) if not grid.is_cell_empty(grid_position): # get cell content of grid position cell_content = grid[grid_position[0]][grid_position[1]] list_state[list_x][list_y] = encode_cell(cell_content) list_x += 1 list_y += 1 list_x = 0 distance = int( round( movement_control.get_distance(grid, agent.pos, agent.model.goalTarget))) print("distance is ", distance) state_tuple = [tuple(l) for l in list_state] #extra_information = (distance, agent.model.current_cow_count) extra_information = (pos_x, pos_y) #print(tuple([tuple(state_tuple), extra_information])) return tuple([tuple(state_tuple), extra_information])
def flank_cow(self): print("in flank method") # if distance from me to the cow is greater than half the vision range distance = movement_control.get_distance(self.model.grid, self.pos, self.cow_to_follow.pos) if (float(distance) > (float(self.vision_radius) / 2.0)) : print("moving to flank") movement_control.move_towards(self, self.cow_to_follow.pos)
def block(self): print("in block method") distance = movement_control.get_distance(grid, self.pos, self.model.goalTarget) if (float(distance) > (float(self.vision_radius) / 2.0)): print("moving to block") movement_control.move_towards(self, self.cow_to_follow.pos)
def calculate_single_weight(self, cell): """ Calculate the weight of cell based on visibility radius""" cell_weight = 0.0 neighborhood_cells = self.model.grid.get_neighborhood(cell, moore=True, include_center=False, radius=self.cow_visibility) for neighbor in neighborhood_cells: distance = movement_control.get_distance(self.model.grid, cell, neighbor) weight = self.get_weight(neighbor) cell_weight += (weight/distance) return cell_weight
def find_closest_cow_to_goal(self, free_cows): if not free_cows: return None closest_cow = None closest_distance = np.inf for cow in free_cows: current_distance = movement_control.get_distance( self.model.grid, cow.pos, self.model.goalTarget) if (current_distance < closest_distance): closest_distance = current_distance closest_cow = cow return closest_cow