Esempio n. 1
0
    def test_edge_in_polygon_different_vertex_instances_returns_true(self):
        vertex_a = Vertex(1, 1)
        vertex_b = Vertex(2, 1)

        mtx_edge = Edge(vertex_a, vertex_b)
        ply_edge = Edge(Vertex(1, 1), Vertex(2, 1))
        ply_list = [ply_edge]
        self.assertTrue(mtx_edge in ply_list)
Esempio n. 2
0
    def test_matrix_edge_in_polygon_edge_array_returns_false(self):
        vertex_a = Vertex(1, 1)
        vertex_b = Vertex(2, 1)

        mtx_edge = Edge(vertex_a, vertex_a)
        ply_edge = Edge(vertex_a, vertex_b)
        ply_list = [ply_edge]
        self.assertFalse(mtx_edge in ply_list)
Esempio n. 3
0
 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])
     ])
Esempio n. 4
0
    def test_connect_edges_to_vertex_should_be_1(self):
        v1 = Vertex(0, 0)
        v2 = Vertex(1, 2)
        e = Edge(v1, v2)
        am.connect_edges_to_vertices([e])

        self.assertEqual(1,
                         len([v for v in v1.connected_edges if v is not None]))
Esempio n. 5
0
 def test_connect_edges_to_vertex_should_be_2(self):
     v1 = Vertex(0, 0)
     v2 = Vertex(1, 0)
     v3 = Vertex(-1, 0)
     e1 = Edge(v1, v2)
     e2 = Edge(v3, v1)
     am.connect_edges_to_vertices([e1, e2])
     self.assertEqual(2,
                      len([v for v in v1.connected_edges if v is not None]))
Esempio n. 6
0
    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
Esempio n. 7
0
def load_simple_polygon(start_vertex, edge_length=1):
    vs = [
        start_vertex,
        Vertex(start_vertex.x + edge_length, start_vertex.y),
        Vertex(start_vertex.x, start_vertex.y + edge_length),
        Vertex(start_vertex.x + edge_length, start_vertex.y + edge_length)
    ]
    return [
        Edge(vs[0], vs[1]),
        Edge(vs[0], vs[2]),
        Edge(vs[1], vs[3]),
        Edge(vs[2], vs[3])
    ]
Esempio n. 8
0
    def test_connect_edges_to_vertex_should_be_sorted_right_left_up_down(self):
        v = Vertex(0, 0)
        e_left = Edge(Vertex(-1, 0), v)
        e_right = Edge(v, Vertex(1, 0))
        e_up = Edge(v, Vertex(0, 1))
        e_down = Edge(Vertex(0, -1), v)

        edges = [e_left, e_right, e_down, e_up]
        am.connect_edges_to_vertices(edges)
        ce = v.connected_edges
        self.assertEqual(ce[DIRECTION['RIGHT']], e_right)
        self.assertEqual(ce[DIRECTION['LEFT']], e_left)
        self.assertEqual(ce[DIRECTION['UP']], e_up)
        self.assertEqual(ce[DIRECTION['DOWN']], e_down)
Esempio n. 9
0
    def test_polygon_length_four_edges_returns_4(self):
        e1 = Edge(Vertex(0, 0), Vertex(1, 0))
        e2 = Edge(Vertex(1, 0), Vertex(1, 1))
        e3 = Edge(Vertex(0, 1), Vertex(1, 1))
        e4 = Edge(Vertex(0, 0), Vertex(0, 1))

        polygon = [e1, e2, e3, e4]
        self.assertEqual(polygon_length(polygon), 4)
Esempio n. 10
0
    def __init__(self, data, tau_initial=1.0):
        self.data = data
        self.x_min_max = np.amin(data[:, 0]) - .1, np.amax(data[:, 0]) + .1
        self.y_min_max = np.amin(data[:, 1]) - .1, np.amax(data[:, 1]) + .1
        self.init_edge_length_x = (self.x_min_max[1] - self.x_min_max[0])
        self.init_edge_length_y = (self.y_min_max[1] - self.y_min_max[0])
        self.tau_initial = tau_initial
        self.sections = []
        self.level = 0

        x_coord = np.linspace(self.x_min_max[0],
                              self.x_min_max[1],
                              num=2,
                              endpoint=True)
        y_coord = np.linspace(self.y_min_max[0],
                              self.y_min_max[1],
                              num=2,
                              endpoint=True)

        coordinates = list(product(x_coord, y_coord))
        self.vertices = [Vertex(x, y) for x, y in coordinates]

        # The number of edges |E| depends on number of vertices with the following formula |E| = y(x-1) + x(y-1)
        # Example: A 4x4 matrix will generate 4(4-1) + 4(4-1) = 24
        self.edges = create_edges(self.vertices, self.tau_initial)
        connect_edges_to_vertices(self.edges)
        self.level_up()
Esempio n. 11
0
 def test_evaluate_3_classes_2_dimensions(self):
     data = np.array([[0.1, 0.5], [1.1, 0.5], [1.1, 1.3], [1.1, 1.4]])
     clf = acoc.PolyACO(data.shape[1], [0, 1, 2])
     polygon3 = load_simple_polygon(Vertex(1, 0))
     clf.model = [[self.polygon1, self.polygon2, polygon3]]
     result = list(clf.evaluate(data))
     self.assertEqual(result.count(0), 1)
     self.assertEqual(result.count(1), 2)
     self.assertEqual(result.count(2), 1)
Esempio n. 12
0
 def test_evaluate_3_classes_3_dimensions_with_overlap(self):
     data = np.array([[0.5, 0.5, 0.5], [0.5, 1.5, 1.5], [2.5, 2.5, 2.5]])
     clf = acoc.PolyACO(data.shape[1], [0, 1, 2])
     clf.model = [[
         self.polygon1, self.polygon2,
         load_simple_polygon(Vertex(2, 2))
     ]] * len(clf.planes)
     result = list(clf.evaluate(data))
     self.assertEqual(result.count(0), 1)
     self.assertEqual(result.count(1), 1)
     self.assertEqual(result.count(2), 1)
Esempio n. 13
0
 def setUp(self):
     self.data = np.array([[0.1, 0.1, 0.1], [1.3, 1.2, 1.2],
                           [1.1, 1.1, 1.1], [1.1, 1.1, 1.1], [1.5, 2, 1.2]])
     self.polygon1 = load_simple_polygon(Vertex(0, 0))
     self.polygon2 = load_simple_polygon(Vertex(1, 1))
     self.class_indices = [0, 1]