def test_network_input(self): rect = Rectangle(0, 0, 10, 10) point = Point(5, 5) terrain_map = [[Cell((j,i)) for i in range(10)] for j in range(10)] expected_input_list = [1] * 18 + [ 1, 0, 1, 0, 1, 0,1,0] for i in range(4, 7): for j in range(4, 7): terrain_map[i][j].set_state(CellState.BURNABLE) terrain_map[i][j].add_one_agent() f = Firefighter(rect, 0.3, point, 0) result_input = f.get_network_input(terrain_map) self.assertEqual(result_input, expected_input_list) f._current_position = Point(0, 0) for i in range(2): for j in range(2): terrain_map[i][j].set_state(CellState.ON_FIRE) terrain_map[i][j].add_one_agent() expected_list_corner = [0,0, 2,1, 2,1, 0,0, 2,1, 2,1, 0,0, 0,0, 0,0,0,0,1,0,1,0,0,0] self.assertEqual( f.get_network_input(terrain_map), expected_list_corner)
def __init__(self, x, y, radius, a, b): self.__pos = Point(x, y) self.__r = radius # is either 0 or 1, with 0 meaning it is not detected in a given frame and 1 meaning that it is. self.__detected_in_current_frame = a # the summation of this over subsequent frames, # so if the agent is active and continues to be it will be incremented by 0. # If the agent is lost and becomes deactive, then this will become increasingly negative over time. self.__detections_last_frames = b
class Detection: def __init__(self, x, y, radius, a, b): self.__pos = Point(x, y) self.__r = radius # is either 0 or 1, with 0 meaning it is not detected in a given frame and 1 meaning that it is. self.__detected_in_current_frame = a # the summation of this over subsequent frames, # so if the agent is active and continues to be it will be incremented by 0. # If the agent is lost and becomes deactive, then this will become increasingly negative over time. self.__detections_last_frames = b def position(self): return self.__pos def radius(self): return self.__r def to_list(self): return [ self.__pos.x(), self.__pos.y(), self.__r, self.__detected_in_current_frame, self.__detections_last_frames ] def update_deactivity(self): self.__detections_last_frames = self.__detections_last_frames + ( self.__detected_in_current_frame - 1) def reset_deactivity(self): self.__detected_in_current_frame = 0 def is_active(self): return self.__detections_last_frames > -3 def distance(self, other_detection): return self.position().distance(other_detection.position()) def __eq__(self, other): return self.__pos == other.position() and self.__r == other.radius( ) and self.__detected_in_current_frame == other.__detected_in_current_frame and self.__detections_last_frames == other.__detections_last_frames def a(self): return self.__detected_in_current_frame
def index_in_grid(self, pos: Point): # position range from -width/2 to +width/2, -height/2 to +height/2 # numpy range from 0 to width and from 0 to height width = self._arena_rect.width() height = self._arena_rect.height() transformed_position = pos + \ Point(width / 2, height / 2) x = max(0, min(int(transformed_position.x()), width - 1)) y = max(0, min(int(transformed_position.y()), height - 1)) return (x, y)
def agent_pos_to_light_index(self, position: Point): # position range from -width/2 to +width/2, -height/2 to +height/2 # numpy range from 0 to width and from 0 to height transformed_position = position + \ Point(self.__width / 2, self.__height / 2) x = min(max(0, int(transformed_position.x())), self.__width - 1) y = self.__height - max( 1, min(int(transformed_position.y()), self.__height)) return (y, x) # change (x,y) to (row,column)
def test_add_square(self): # Middle square array = np.zeros((10, 10)) array[2:6, 2:6] = 1 other_array = np.zeros((10, 10)) light.add_square(other_array, Point(4, 4), 4) np.testing.assert_array_equal(array, other_array) # Bottom right square array = np.zeros((10, 10)) array[7:, 7:] = 1 other_array = np.zeros((10, 10)) light.add_square(other_array, Point(9, 9), 4) np.testing.assert_array_equal(array, other_array) # Top square array = np.zeros((10, 10)) array[:3, :3] = 1 other_array = np.zeros((10, 10)) light.add_square(other_array, Point(0, 0), 6) np.testing.assert_array_equal(array, other_array)
def __init__(self, arena: Rectangle, speed: float, theta: float, pos: Point, can_be_on_fire: bool, max_capacity: int, encoding: int): self._base_speed = speed self._current_speed = speed self._current_position = pos self._direction_theta = theta self._arena_rect = arena self.__can_be_on_fire = can_be_on_fire self._encoding = encoding self._max_capacity = max_capacity self.__water_tank_location = Point(400, 400) # Liters/m^2 (https://bedtimemath.org/fun-math-firefighting/) self._drop_rate = 17
def do_action(self, dir: Direction, action, pattern): direction_value = direction_list[int(dir) - 1] new_pos = self._current_position + \ Point(direction_value[0], direction_value[1]) if self._arena_rect.contains(new_pos): if action == Action.EXTINGUISH: if pattern[new_pos.x()][ new_pos.y()].get_state() == CellState.ON_FIRE: pattern[new_pos.x()][new_pos.y()].set_state( CellState.BURNABLE) elif action == Action.MOVE: self.move(new_pos, pattern) elif action == Action.TRENCH: pattern[new_pos.x()][new_pos.y()].set_state(CellState.TRENCH) elif action == Action.BURN: pattern[new_pos.x()][new_pos.y()].set_state(CellState.ON_FIRE) else: pass
def get_network_input(self, pattern): directions = np.array( [[-1, 1], [0, 1], [1, 1], [-1, 0], [0, 0], [1, 0], [-1, -1], [0, -1], [1, -1], [-4, 0], [4, 0], [0, 4], [0, -4]], dtype=np.int8) positions = [ int(self._current_position.x()), int(self._current_position.y()) ] + directions inputs = [] for [posx, posy] in positions: if self._arena_rect.contains(Point(posx, posy)): inputs.append(pattern[posx][posy].get_state().value) inputs.append(pattern[posx][posy].get_num_agents()) else: inputs.append(0) inputs.append(0) return inputs
def new_pattern(size, agent_positions): pattern = np.zeros(size) for (posx, posy) in agent_positions: point = Point(int(posx), int(posy)) add_square(pattern, point, 10) return pattern
def test_init(self): rect = Rectangle(-10, -10, 20, 20) point = Point(8, 4) a = Firefighter(rect, 0.3, point, 0) self.assertEqual(point, a.position())