def setUp(self): vs = [Vertex(0, 0), Vertex(1, 0), Vertex(1, 1), Vertex(0, 1)] self.polygon = polygon_to_array([ Edge(vs[0], vs[1]), Edge(vs[1], vs[2]), Edge(vs[3], vs[2]), Edge(vs[0], vs[3]) ])
def _score(self, polygon, data): edges = polygon_to_array(polygon) if self.config.gpu: cost = cost_function_gpu(data, edges) else: cost = cost_function(data, edges) # try: length_factor = 1 / polygon_length(polygon) return (cost**self.config.alpha) * (length_factor**self.config.beta)
def _score(self, polygon, data): edges = polygon_to_array(polygon) if self.config.gpu: cost = cost_function_gpu(data, edges) else: cost = cost_function(data, edges) # try: length_factor = 1 / polygon_length(polygon) return (cost ** self.config.alpha) * (length_factor ** self.config.beta)
def increase_section_granularity(self, section, best_polygon=None): p_inside = points_inside(self.data, polygon_to_array(section)) if not all_points_have_same_position( p_inside) and have_points_of_both_classes(p_inside): new_edges = [] remove_edges = [] # Cut edges in two for edge in section: v_mid = Vertex(((edge.a.x + edge.b.x) / 2), ((edge.a.y + edge.b.y) / 2)) if v_mid not in self.vertices: self.vertices.append(v_mid) else: v_mid = next(v for v in self.vertices if v == v_mid) new_edges.extend([ Edge(edge.a, v_mid, edge.pheromone_strength), Edge(v_mid, edge.b, edge.pheromone_strength) ]) if best_polygon: if edge in best_polygon: best_polygon.remove(edge) best_polygon.extend(new_edges[-2:]) remove_edges.extend(section) connect_edges_to_vertices(new_edges) v_lower_left = section[0].a v_down = v_lower_left.connected_edges[DIRECTION['RIGHT']].b v_right = v_down.connected_edges[ DIRECTION['RIGHT']].b.connected_edges[DIRECTION['UP']].b v_left = v_lower_left.connected_edges[DIRECTION['UP']].b v_up = v_left.connected_edges[DIRECTION['UP']].b.connected_edges[ DIRECTION['RIGHT']].b v_center = Vertex(v_down.x, v_left.y) new_edges.extend([ Edge(v_down, v_center, pheromone_strength=self.tau_initial), Edge(v_left, v_center, pheromone_strength=self.tau_initial), Edge(v_center, v_right, pheromone_strength=self.tau_initial), Edge(v_center, v_up, pheromone_strength=self.tau_initial) ]) self.vertices.append(v_center) connect_edges_to_vertices(new_edges) corners = [v_lower_left, v_down, v_left, v_center] subsections = [] for c in corners: subsections.append([ c.connected_edges[DIRECTION['RIGHT']], c.connected_edges[ DIRECTION['RIGHT']].b.connected_edges[DIRECTION['UP']], c.connected_edges[DIRECTION['UP']].b.connected_edges[ DIRECTION['RIGHT']], c.connected_edges[DIRECTION['UP']] ]) return subsections, new_edges, remove_edges
def increase_section_granularity(self, section, best_polygon=None): p_inside = points_inside(self.data, polygon_to_array(section)) if not all_points_have_same_position(p_inside) and have_points_of_both_classes(p_inside): new_edges = [] remove_edges = [] # Cut edges in two for edge in section: v_mid = Vertex(((edge.a.x + edge.b.x) / 2), ((edge.a.y + edge.b.y) / 2)) if v_mid not in self.vertices: self.vertices.append(v_mid) else: v_mid = next(v for v in self.vertices if v == v_mid) new_edges.extend([Edge(edge.a, v_mid, edge.pheromone_strength), Edge(v_mid, edge.b, edge.pheromone_strength)]) if best_polygon: if edge in best_polygon: best_polygon.remove(edge) best_polygon.extend(new_edges[-2:]) remove_edges.extend(section) connect_edges_to_vertices(new_edges) v_lower_left = section[0].a v_down = v_lower_left.connected_edges[DIRECTION['RIGHT']].b v_right = v_down.connected_edges[DIRECTION['RIGHT']].b.connected_edges[DIRECTION['UP']].b v_left = v_lower_left.connected_edges[DIRECTION['UP']].b v_up = v_left.connected_edges[DIRECTION['UP']].b.connected_edges[DIRECTION['RIGHT']].b v_center = Vertex(v_down.x, v_left.y) new_edges.extend([Edge(v_down, v_center, pheromone_strength=self.tau_initial), Edge(v_left, v_center, pheromone_strength=self.tau_initial), Edge(v_center, v_right, pheromone_strength=self.tau_initial), Edge(v_center, v_up, pheromone_strength=self.tau_initial)]) self.vertices.append(v_center) connect_edges_to_vertices(new_edges) corners = [v_lower_left, v_down, v_left, v_center] subsections = [] for c in corners: subsections.append([ c.connected_edges[DIRECTION['RIGHT']], c.connected_edges[DIRECTION['RIGHT']].b.connected_edges[DIRECTION['UP']], c.connected_edges[DIRECTION['UP']].b.connected_edges[DIRECTION['RIGHT']], c.connected_edges[DIRECTION['UP']] ]) return subsections, new_edges, remove_edges
def evaluate(self, test_data): if len(self.model) == 0: raise RuntimeError('PolyACO must be trained before evaluation') inside = np.empty((len(self.planes), len(self.class_indices), test_data.shape[0]), dtype=bool) for i, plane in enumerate(self.model): plane_data = np.take(test_data, list(self.planes[i]), axis=1) for j, poly in enumerate(plane): inside[i, j, :] = is_points_inside_cuda(plane_data, polygon_to_array(poly)) aggregated_scores = np.mean(inside, axis=0) predictions = np.empty((test_data.shape[0]), dtype=int) max_elements = np.argmax(aggregated_scores, axis=0) for i in range(test_data.shape[0]): predictions[i] = self.class_indices[max_elements[i]] return predictions
def evaluate(self, test_data): if len(self.model) == 0: raise RuntimeError('PolyACO must be trained before evaluation') inside = np.empty( (len(self.planes), len(self.class_indices), test_data.shape[0]), dtype=bool) for i, plane in enumerate(self.model): plane_data = np.take(test_data, list(self.planes[i]), axis=1) for j, poly in enumerate(plane): inside[i, j, :] = is_points_inside_cuda(plane_data, polygon_to_array(poly)) aggregated_scores = np.mean(inside, axis=0) predictions = np.empty((test_data.shape[0]), dtype=int) max_elements = np.argmax(aggregated_scores, axis=0) for i in range(test_data.shape[0]): predictions[i] = self.class_indices[max_elements[i]] return predictions