def test_level_up_5_increase_vertex_count_with_5_times_5(self): matrix = AcocMatrix(self.data) old_vertex_count = len(matrix.vertices) for _ in range(5): matrix.level_up() new_vertex_count = len(matrix.vertices) self.assertEqual(new_vertex_count - old_vertex_count, 5 * 5)
def test_level_up_two_times_increases_vertex_count_with_10(self): matrix = AcocMatrix(self.data) old_vertex_count = len(matrix.vertices) matrix.level_up() matrix.level_up() new_vertex_count = len(matrix.vertices) self.assertEqual(new_vertex_count - old_vertex_count, 10)
def test_level_up_5_increase_vertex_count_with_5_times_5(self): matrix = AcocMatrix(self.data) old_vertex_count = len(matrix.vertices) for _ in range(5): matrix.level_up() new_vertex_count = len(matrix.vertices) self.assertEqual(new_vertex_count - old_vertex_count, 5*5)
def test_all_edges_move_in_positive_direction_from_a_to_b_after_level_up( self): matrix = AcocMatrix(np.array([[0, 0, 0], [1, 1, 1]])) matrix.level_up() is_positive = map(lambda e: e.b.x >= e.a.x and e.b.y >= e.a.y, matrix.edges) self.assertTrue(all(is_positive))
def test_level_up_nested_increase_number_of_edges(self): matrix = AcocMatrix(self.data) old_edge_count = len(matrix.edges) if self.show: plotter.plot_matrix_and_data(matrix, matrix.data, show=True) matrix.level_up() if self.show: plotter.plot_matrix_and_data(matrix, matrix.data, show=True) new_edge_count = len(matrix.edges) self.assertGreater(new_edge_count, old_edge_count)
def test_level_up_nested_increase_number_of_vertices_by_10_in_other_data_set(self): self.data = np.array([[0, 0, 0], [0, 0, 1], [3, 3, 0], [3, 3, 1]]) matrix = AcocMatrix(self.data) old_vertex_count = len(matrix.vertices) if self.show: plotter.plot_matrix_and_data(matrix, matrix.data, show=True) matrix.level_up() if self.show: plotter.plot_matrix_and_data(matrix, matrix.data, show=True) new_vertex_count = len(matrix.vertices) self.assertEqual(new_vertex_count - old_vertex_count, 10)
def test_level_up_nested_increase_number_of_vertices_by_10_in_other_data_set( self): self.data = np.array([[0, 0, 0], [0, 0, 1], [3, 3, 0], [3, 3, 1]]) matrix = AcocMatrix(self.data) old_vertex_count = len(matrix.vertices) if self.show: plotter.plot_matrix_and_data(matrix, matrix.data, show=True) matrix.level_up() if self.show: plotter.plot_matrix_and_data(matrix, matrix.data, show=True) new_vertex_count = len(matrix.vertices) self.assertEqual(new_vertex_count - old_vertex_count, 10)
def test_increase_section_granularity_preserves_pheromones_on_new_edges(self): matrix = AcocMatrix(self.data) start_vertex = matrix.vertices[0] up_edge = start_vertex.connected_edges[DIRECTION['UP']] strength = 10 up_edge.pheromone_strength = strength matrix.level_up(best_polygon=[up_edge]) new_edge = start_vertex.connected_edges[DIRECTION['UP']] if self.show: plotter.plot_matrix_and_data(matrix, matrix.data, show=True) self.assertNotEqual(up_edge, new_edge, 'New matrix edge is equal to the old matrix edge. Should be shorter.') self.assertEqual(new_edge.pheromone_strength, strength) next_new_edge = new_edge.b.connected_edges[DIRECTION['UP']] self.assertEqual(next_new_edge.pheromone_strength, strength)
def _construct_polygon(self, data, plane_string, start_time, print_string): cuda.to_device(data) ant_scores = [] current_best_ant = [] last_level_up_or_best_ant = 0 matrix = AcocMatrix(data, tau_initial=self.config.tau_init) current_best_score = 0 while matrix.level <= self.config.max_level: start_vertex = get_random_weighted(matrix.edges) if self.config.multi_level: if (len(ant_scores) - last_level_up_or_best_ant ) > self.config.level_convergence_rate: matrix.level_up(current_best_ant) last_level_up_or_best_ant = len(ant_scores) _ant = Ant(start_vertex) _ant.move_ant() while not _ant.at_target and not _ant.is_stuck: _ant.move_ant() if _ant.at_target: ant_score = self._score(_ant.edges_travelled, data) if ant_score > current_best_score: current_best_ant = _ant.edges_travelled current_best_score = ant_score last_level_up_or_best_ant = len(ant_scores) if self.config.plot: plotter.plot_path_with_data( current_best_ant, data, matrix, save=True, save_folder=osp.join( self.save_folder, 'best_paths/{}/'.format(plane_string)), file_name='ant' + str(len(ant_scores))) self._put_pheromones(current_best_ant, current_best_score) self._reset_at_random(matrix) ant_scores.append(ant_score) t_elapsed = utils.seconds_to_hms(time.time() - start_time) utils.print_on_current_line( "Level {}/{}, {}, Time elapsed: {}".format( matrix.level, self.config.max_level, print_string, t_elapsed)) return current_best_ant
def test_first_vertex_is_smallest_vertex(self): matrix = AcocMatrix(np.array([[0, 3, 5], [0, 3, 4], [0, 0, 0]])) first_v = matrix.vertices[0] smallest_x = min(matrix.vertices, key=lambda v: v.x).x smallest_y = min(matrix.vertices, key=lambda v: v.y).y self.assertEqual(first_v.x, smallest_x) self.assertEqual(first_v.y, smallest_y)
def test_increase_section_granularity_preserves_pheromones_on_new_edges( self): matrix = AcocMatrix(self.data) start_vertex = matrix.vertices[0] up_edge = start_vertex.connected_edges[DIRECTION['UP']] strength = 10 up_edge.pheromone_strength = strength matrix.level_up(best_polygon=[up_edge]) new_edge = start_vertex.connected_edges[DIRECTION['UP']] if self.show: plotter.plot_matrix_and_data(matrix, matrix.data, show=True) self.assertNotEqual( up_edge, new_edge, 'New matrix edge is equal to the old matrix edge. Should be shorter.' ) self.assertEqual(new_edge.pheromone_strength, strength) next_new_edge = new_edge.b.connected_edges[DIRECTION['UP']] self.assertEqual(next_new_edge.pheromone_strength, strength)
def _construct_polygon(self, data, plane_string, start_time, print_string): cuda.to_device(data) ant_scores = [] current_best_ant = [] last_level_up_or_best_ant = 0 matrix = AcocMatrix(data, tau_initial=self.config.tau_init) current_best_score = 0 while matrix.level <= self.config.max_level: start_vertex = get_random_weighted(matrix.edges) if self.config.multi_level: if (len(ant_scores) - last_level_up_or_best_ant) > self.config.level_convergence_rate: matrix.level_up(current_best_ant) last_level_up_or_best_ant = len(ant_scores) _ant = Ant(start_vertex) _ant.move_ant() while not _ant.at_target and not _ant.is_stuck: _ant.move_ant() if _ant.at_target: ant_score = self._score(_ant.edges_travelled, data) if ant_score > current_best_score: current_best_ant = _ant.edges_travelled current_best_score = ant_score last_level_up_or_best_ant = len(ant_scores) if self.config.plot: plotter.plot_path_with_data(current_best_ant, data, matrix, save=True, save_folder=osp.join(self.save_folder, 'best_paths/{}/'.format(plane_string)), file_name='ant' + str(len(ant_scores))) self._put_pheromones(current_best_ant, current_best_score) self._reset_at_random(matrix) ant_scores.append(ant_score) t_elapsed = utils.seconds_to_hms(time.time() - start_time) utils.print_on_current_line("Level {}/{}, {}, Time elapsed: {}".format(matrix.level, self.config.max_level, print_string, t_elapsed)) return current_best_ant
def test_level_up_one_section_increases_vertex_count_with_5(self): matrix = AcocMatrix(self.data) old_vertex_count = len(matrix.vertices) matrix.level_up() new_vertex_count = len(matrix.vertices) self.assertEqual(new_vertex_count - old_vertex_count, 5)
def test_level_up_nested_increase_number_of_edges_with_8(self): matrix = AcocMatrix(self.data) old_edge_count = len(matrix.edges) matrix.level_up() new_edge_count = len(matrix.edges) self.assertEqual(new_edge_count - old_edge_count, 8)
def test_all_edges_move_in_positive_direction_from_a_to_b_after_level_up(self): matrix = AcocMatrix(np.array([[0, 0, 0], [1, 1, 1]])) matrix.level_up() is_positive = map(lambda e: e.b.x >= e.a.x and e.b.y >= e.a.y, matrix.edges) self.assertTrue(all(is_positive))