예제 #1
0
    def test_snapping(self):
        decomp = Decomposition(self.chi)

        polygon1 = Polygon([(0, 0), (1, 0), (1, 1)])
        robotPosition1 = Point((0, 0))
        polyId1 = decomp.add_polygon(polygon=polygon1,
                                     robotPosition=robotPosition1)

        polygon2 = Polygon([(1, 1), (0.5, 1), (0.5, 0.500000001)])
        robotPosition2 = Point((20, 0))
        polyId2 = decomp.add_polygon(polygon=polygon2,
                                     robotPosition=robotPosition2)

        polygon3 = Polygon([(0, 1), (0, 0), (0.5, 0.499999999), (0.5, 1)])
        robotPosition3 = Point((20, 0))
        polyId3 = decomp.add_polygon(polygon=polygon3,
                                     robotPosition=robotPosition3)

        self.assertEqual(len(decomp.id2Polygon.keys()), 3)
        self.assertEqual(decomp.id2Polygon[polyId1], polygon1)
        #self.assertEqual(decomp.id2Polygon[polyId2], polygon2)
        self.assertEqual(len(decomp.id2Adjacent.keys()), 3)
        self.assertEqual(decomp.id2Adjacent[polyId1], [polyId2, polyId3])
        self.assertEqual(decomp.id2Adjacent[polyId2], [polyId1, polyId3])
        self.assertEqual(decomp.id2Adjacent[polyId3], [polyId1, polyId2])
예제 #2
0
    def test_addTwoTouchingPolygon(self):
        decomp = Decomposition(self.chi)

        polygon1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
        robotPosition1 = Point((0, 0))
        polyId1 = decomp.add_polygon(polygon=polygon1,
                                     robotPosition=robotPosition1)

        polygon2 = Polygon([(1, 1), (2, 1), (2, 2), (1, 2)])
        robotPosition2 = Point((1, 1))
        polyId2 = decomp.add_polygon(polygon=polygon2,
                                     robotPosition=robotPosition2)

        self.assertEqual(len(decomp.id2Polygon.keys()), 2)
        self.assertEqual(decomp.id2Polygon[polyId1], polygon1)
        self.assertEqual(decomp.id2Polygon[polyId2], polygon2)

        self.assertEqual(decomp.id2Position[polyId1], robotPosition1)
        self.assertEqual(decomp.id2Position[polyId2], robotPosition2)

        self.assertEqual(
            decomp.id2Cost[polyId1],
            self.chi.compute(polygon=polygon1, initialPosition=robotPosition1))
        self.assertEqual(
            decomp.id2Cost[polyId2],
            self.chi.compute(polygon=polygon2, initialPosition=robotPosition2))

        self.assertEqual(len(decomp.id2Adjacent.keys()), 2)
        self.assertEqual(decomp.id2Adjacent[polyId1], [])
        self.assertEqual(decomp.id2Adjacent[polyId2], [])

        self.assertEqual(len(decomp.sortedCosts), 2)
        self.assertEqual(decomp.sortedCosts[0], polyId1)
        self.assertEqual(decomp.sortedCosts[1], polyId2)
예제 #3
0
    def test_addAfterRemove(self):
        decomp = Decomposition(self.chi)

        polygon1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
        robotPosition1 = Point((0, 0))
        polyId1 = decomp.add_polygon(polygon=polygon1,
                                     robotPosition=robotPosition1)

        polygon2 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
        robotPosition2 = Point((20, 0))
        polyId2 = decomp.add_polygon(polygon=polygon2,
                                     robotPosition=robotPosition2)

        polygon3 = Polygon([(1, 0), (2, 0), (2, 1), (1, 1)])
        robotPosition3 = Point((1, 0))
        polyId3 = decomp.add_polygon(polygon=polygon3,
                                     robotPosition=robotPosition3)

        decomp.remove_polygon(polyId1)

        polygon1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
        robotPosition1 = Point((0, 0))
        polyId1 = decomp.add_polygon(polygon=polygon1,
                                     robotPosition=robotPosition1)

        self.assertEqual(len(decomp.id2Polygon.keys()), 3)
        self.assertEqual(decomp.id2Polygon[polyId1], polygon1)
        self.assertEqual(decomp.id2Polygon[polyId2], polygon2)
    def train(self, x_train, y_train, x_test, y_test, decomposition=False, visualize=False, multiclass=False, once=False):
        # Update multiclass flag and setup results
        self.multiclass = multiclass
        self.setupResults()

        if decomposition is True:
            # Create decomposition object
            dec = Decomposition()
            # Plot dimensionality reduction to 2D
            if visualize is True:
                dec.test_visualize(x_train, y_train)
            if once is True:
                x_train = dec.fit(x_train, y_train, kernel='rbf', gamma=0.1, n_components=10)
                x_test = dec.transform(x_test)
                self.dim_red = { 'kernel': 'rbf', 'gamma': 0.1, 'n_component': 10 }
                # Test for all available values for neighbors
                self.nearestNeighbor(x_train, y_train, x_test, y_test, once=once)
                # Test with Nearest Centroid
                self.nearestCentroid(x_train, y_train, x_test, y_test)
            else:
                # Test with all available variables
                for n_component in dec.components:
                    for kernel in dec.kernel:
                        if kernel != 'linear':
                            for gamma in dec.Gamma:
                                x_train = dec.fit(x_train, y_train, kernel=kernel, gamma=gamma, n_components=n_component)
                                x_test = dec.transform(x_test)
                                self.dim_red = { 'kernel': kernel, 'gamma': gamma, 'n_component': n_component }
                                # Test for all available values for neighbors
                                self.nearestNeighbor(x_train, y_train, x_test, y_test)
                                # Test with Nearest Centroid
                                self.nearestCentroid(x_train, y_train, x_test, y_test)
                        else:
                            x_train = dec.fit(x_train, y_train, n_components=n_component)
                            x_test = dec.transform(x_test)
                            self.dim_red = { 'kernel': kernel, 'gamma': '-', 'n_component': n_component }
                            # Test for all available values for neighbors
                            self.nearestNeighbor(x_train, y_train, x_test, y_test)
                            # Test with Nearest Centroid
                            self.nearestCentroid(x_train, y_train, x_test, y_test)
        else:
            # Run without dimesionality reduction
            self.dim_red = { 'kernel': '-', 'gamma': '-', 'n_component': '-' }
            # Test for all available values for neighbors
            self.nearestNeighbor(x_train, y_train, x_test, y_test, once=once)
            # Test with Nearest Centroid
            self.nearestCentroid(x_train, y_train, x_test, y_test)
예제 #5
0
    def test_addThreePolygons(self):
        decomp = Decomposition(self.chi)

        polygon1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
        robotPosition1 = Point((0, 0))
        polyId1 = decomp.add_polygon(polygon=polygon1,
                                     robotPosition=robotPosition1)

        polygon2 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
        robotPosition2 = Point((20, 0))
        polyId2 = decomp.add_polygon(polygon=polygon2,
                                     robotPosition=robotPosition2)

        polygon3 = Polygon([(1, 0), (2, 0), (2, 1), (1, 1)])
        robotPosition3 = Point((1, 0))
        polyId3 = decomp.add_polygon(polygon=polygon3,
                                     robotPosition=robotPosition3)

        self.assertEqual(len(decomp.id2Polygon.keys()), 3)
        self.assertEqual(decomp.id2Polygon[polyId1], polygon1)
        self.assertEqual(decomp.id2Polygon[polyId2], polygon2)
        self.assertEqual(decomp.id2Polygon[polyId3], polygon3)

        self.assertEqual(decomp.id2Position[polyId1], robotPosition1)
        self.assertEqual(decomp.id2Position[polyId2], robotPosition2)
        self.assertEqual(decomp.id2Position[polyId3], robotPosition3)

        self.assertEqual(
            decomp.id2Cost[polyId1],
            self.chi.compute(polygon=polygon1, initialPosition=robotPosition1))
        self.assertEqual(
            decomp.id2Cost[polyId2],
            self.chi.compute(polygon=polygon2, initialPosition=robotPosition2))
        self.assertEqual(
            decomp.id2Cost[polyId3],
            self.chi.compute(polygon=polygon3, initialPosition=robotPosition3))

        self.assertEqual(len(decomp.id2Adjacent.keys()), 3)
        self.assertEqual(decomp.id2Adjacent[polyId1], [polyId3])
        self.assertEqual(decomp.id2Adjacent[polyId2], [polyId3])
        self.assertEqual(decomp.id2Adjacent[polyId3], [polyId1, polyId2])

        self.assertEqual(len(decomp.sortedCosts), 3)
        self.assertEqual(decomp.sortedCosts[0], polyId1)
        self.assertEqual(decomp.sortedCosts[1], polyId3)
        self.assertEqual(decomp.sortedCosts[2], polyId2)
예제 #6
0
    def test_addOneInvalidPolygon(self):
        decomp = Decomposition(self.chi)

        polygon = Polygon([(0, 0), (1, 0), (1, -1), (1, 1), (0, 1)])
        robotPosition = Point((0, 0))
        polyId = decomp.add_polygon(polygon=polygon,
                                    robotPosition=robotPosition)

        self.assertLess(polyId, 0)

        self.assertEqual(len(decomp.id2Polygon.keys()), 0)

        self.assertEqual(len(decomp.id2Position), 0)

        self.assertEqual(len(decomp.id2Cost), 0)

        self.assertEqual(len(decomp.id2Adjacent), 0)

        self.assertEqual(len(decomp.sortedCosts), 0)
예제 #7
0
    def test_snapping3(self):
        decomp = Decomposition(self.chi)

        polygon1 = Polygon([(0, 0), (0.99999, 0), (0.99999, 1)])
        robotPosition1 = Point((0, 0))
        polyId1 = decomp.add_polygon(polygon=polygon1,
                                     robotPosition=robotPosition1)

        polygon2 = Polygon([(1, 0.5), (2, 0.5), (2, 1.5), (1, 1.5)])
        robotPosition2 = Point((20, 0))
        polyId2 = decomp.add_polygon(polygon=polygon2,
                                     robotPosition=robotPosition2)

        self.assertEqual(len(decomp.id2Polygon.keys()), 2)
        self.assertNotEqual(decomp.id2Polygon[polyId1], polygon1)
        self.assertNotEqual(decomp.id2Polygon[polyId2], polygon2)
        self.assertEqual(len(decomp.id2Adjacent.keys()), 2)
        self.assertEqual(decomp.id2Adjacent[polyId1], [polyId2])
        self.assertEqual(decomp.id2Adjacent[polyId2], [polyId1])
예제 #8
0
    def test_addTouching(self):
        decomp = Decomposition(self.chi)

        polygon1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
        robotPosition1 = Point((0, 0))
        polyId1 = decomp.add_polygon(polygon=polygon1,
                                     robotPosition=robotPosition1)

        polygon2 = Polygon([(1, 1), (2, 1), (2, 2), (1, 2)])
        robotPosition2 = Point((20, 0))
        polyId2 = decomp.add_polygon(polygon=polygon2,
                                     robotPosition=robotPosition2)

        self.assertEqual(len(decomp.id2Polygon.keys()), 2)
        self.assertEqual(decomp.id2Polygon[polyId1], polygon1)
        self.assertEqual(decomp.id2Polygon[polyId2], polygon2)
        self.assertEqual(len(decomp.id2Adjacent.keys()), 2)
        self.assertEqual(decomp.id2Adjacent[polyId1], [])
        self.assertEqual(decomp.id2Adjacent[polyId2], [])
예제 #9
0
def make_traindatas():
    # 画像収集処理
    # girls = GirlsScraping()
    # girls.scraping()

    # リサイズしての顔抽出処理

    # GRAY_SCALE画像をndarray配列化
    features = np.array([
        data.imread('../images/gray_faces/' + str(path))
        for path in os.listdir('../images/gray_faces')
    ])
    # 3次元配列の画像データを2次元配列のデータに変換
    reduced_features = features.reshape(len(features), -1).astype(np.float64)

    # 次元削減処理 主成分分析(PCA)による次元削減処理
    decomposition_obj = Decomposition(n=2)
    reduced_features = decomposition_obj.transform(reduced_features)

    # クラスタリング処理
    cluster = 4
    k_mean = KMeansClustering(cluster)
    pred = k_mean.clustering(reduced_features)

    print(pred)

    # ラベル付けを行っていく
    girls_csv = "../girls.csv"
    girls_info = pd.read_csv(girls_csv)
    # print(girls_info)
    # print(type(girls_info))
    # for girl_info in girls_info.iteritems():
    for index, row in girls_info.iterrows():

        # data = data[data.column1 == 'hoge']
        # print(row.name == '新垣結衣')
        # print(row['name'] == '新垣結衣')
        print(index)
        print(row['name'])
        print("---------------------------")
    def get_sorted_costs(
            self, decomposition: Decomposition) -> List[Tuple[int, float]]:
        """Helper function for calculating and sorting costs of all cells in decoms.

        Args:
            decomposition (Decomposition): Decomposition object.

        Returns:
            sorted list of costs
        """
        costs = [(cell_id, self.cost_func(poly, site))
                 for cell_id, poly, site in decomposition.items()]
        return sorted(costs, key=lambda v: v[1], reverse=True)
예제 #11
0
    def test_addOnePolygon(self):
        decomp = Decomposition(self.chi)

        polygon = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
        robotPosition = Point((0, 0))
        polyId = decomp.add_polygon(polygon=polygon,
                                    robotPosition=robotPosition)

        self.assertEqual(len(decomp.id2Polygon.keys()), 1)
        self.assertEqual(decomp.id2Polygon.keys()[0], polyId)
        self.assertEqual(decomp.id2Polygon[polyId], polygon)

        self.assertEqual(decomp.id2Position[polyId], robotPosition)

        self.assertEqual(
            decomp.id2Cost[polyId],
            self.chi.compute(polygon=polygon, initialPosition=robotPosition))

        self.assertEqual(decomp.id2Adjacent[polyId], [])
        self.assertEqual(len(decomp.id2Adjacent.keys()), 1)

        self.assertEqual(len(decomp.sortedCosts), 1)
        self.assertEqual(decomp.sortedCosts[0], polyId)
def decomposition_generator(poly_id: int = 0):
    """Hard coded polygons and decompositions

    Args:
        poly_id (int): Id of the decomposition to generate.

    Returns:
        P (List): Polygon.
        cell_to_site_map (Dict): Map from cell to starting point of robots.
        decomposition (List): Decomposition of polygon P.
    """
    # pylint: disable=invalid-name
    if poly_id == 0:
        P: List[List] = [[(0.0, 0.0), (10.0, 0.0), (10.0, 1.0), (0.0, 1.0)],
                         []]

        dec = Decomposition(P)
        dec.add_cell([[(0.0, 0.0), (10.0, 0.0), (10.0, 0.5)], []])
        dec.add_cell([[(0.0, 0.0), (10.0, 0.5), (10.0, 1.0), (5.0, 0.5)], []])
        dec.add_cell([[(5.0, 0.5), (10.0, 1.0), (0.0, 1.0)], []])
        dec.add_cell([[(0.0, 0.0), (5.0, 0.5), (0.0, 1.0)], []])
        dec.add_robot_site(0, (0., 0.))
        dec.add_robot_site(1, (0., 0.))
        dec.add_robot_site(2, (0., 0.))
        dec.add_robot_site(3, (0., 0.))

    elif poly_id == 1:
        P = [[(0.0, 0.0), (10.0, 0.0), (10.0, 1.0), (0.0, 1.0)], []]

        dec = Decomposition(P)

        dec.add_cell([[(0.0, 0.0), (2.5, 0.0), (2.5, 1.0), (0.0, 1.0)], []])
        dec.add_cell([[(2.5, 0.0), (5.0, 0.0), (5.0, 1.0), (2.5, 1.0)], []])
        dec.add_cell([[(5.0, 0.0), (7.5, 0.0), (7.5, 1.0), (5.0, 1.0)], []])
        dec.add_cell([[(7.5, 0.0), (10.0, 0.0), (10.0, 1.0), (7.5, 1.0)], []])

        dec.add_robot_site(0, (0., 0.))
        dec.add_robot_site(1, (0., 1.))
        dec.add_robot_site(2, (10., 1.))
        dec.add_robot_site(3, (10., 0.))

    elif poly_id == 2:
        P = [[(1.0, 0.0), (2.0, 0.0), (3.0, 1.0), (3.0, 2.0), (2.0, 3.0),
              (1.0, 3.0), (0.0, 2.0), (0.0, 1.0)], []]

        dec = Decomposition(P)

        dec.add_cell([[(0.8333333333333334, 0.16666666666666666),
                       (0.9285714285714286, 1.7857142857142854), (1.0, 3.0),
                       (0.0, 2.0), (0.0, 1.0)], []])
        dec.add_cell([[(0.9285714285714286, 1.7857142857142854),
                       (1.6818181818181817, 1.8636363636363633), (3.0, 2.0),
                       (2.0, 3.0), (1.0, 3.0)], []])
        dec.add_cell([[(1.6818181818181817, 1.8636363636363633),
                       (0.9285714285714286, 1.7857142857142854),
                       (0.8333333333333334, 0.16666666666666666), (1.0, 0.0),
                       (2.0, 0.0)], []])
        dec.add_cell([[(1.6818181818181817, 1.8636363636363633), (2.0, 0.0),
                       (3.0, 1.0), (3.0, 2.0)], []])

        dec.add_robot_site(0, (0., -1.))
        dec.add_robot_site(1, (0., 3.))
        dec.add_robot_site(2, (3., -1.))
        dec.add_robot_site(3, (2., 3.))

    elif poly_id == 3:
        P = [[(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (6.0, 4.0), (6.0, 0.0),
              (10.0, 0.0), (10.0, 6.0), (8.0, 7.0), (7.5, 8.0), (10.0, 7.5),
              (10.0, 10.0), (0.0, 10.0), (0.0, 5.0), (5.0, 6.0), (5.0, 5.0),
              (0.0, 4.0)], []]

        dec = Decomposition(P)

        dec.add_cell([[(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (6.0, 4.0),
                       (5.0, 5.0), (0.0, 4.0)], []])
        dec.add_cell([[(6.0, 4.0), (6.0, 0.0), (10.0, 0.0), (10.0, 6.0),
                       (8.0, 7.0), (5.0, 5.0)], []])
        dec.add_cell([[(7.5, 8.0), (10.0, 7.5), (10.0, 10.0), (0.0, 10.0),
                       (0.0, 5.0), (5.0, 6.0)], []])
        dec.add_cell([[(5.0, 5.0), (8.0, 7.0), (7.5, 8.0), (5.0, 6.0)], []])

        dec.add_robot_site(0, (10, 0))
        dec.add_robot_site(1, (10, 10))
        dec.add_robot_site(2, (0, 10))
        dec.add_robot_site(3, (0, 0))

    elif poly_id == 4:
        # A more complex shape with one hole
        P = [[(0.0, 0.0), (6.0, 0.0), (6.0, 5.0), (4.0, 5.0), (4.0, 3.0),
              (5.0, 3.0), (5.0, 2.0), (3.0, 2.0), (3.0, 6.0), (7.0, 6.0),
              (7.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)],
             [[(4.0, 7.0), (3.5, 8.0), (4.5, 9.0), (6.0, 8.0)]]]

        dec = Decomposition(P)

        dec.add_cell([[(6.0, 0.0), (6.0, 5.0), (4.0, 5.0), (4.0, 3.0),
                       (5.0, 3.0), (5.0, 2.0)], []])
        dec.add_cell([[(7.0, 6.0), (7.0, 0.0), (10.0, 0.0), (10.0, 10.0),
                       (6.0, 8.0), (4.0, 7.0)], []])
        dec.add_cell([[(6.0, 8.0), (10.0, 10.0), (0.0, 10.0), (3.5, 8.0),
                       (4.5, 9.0)], []])
        dec.add_cell([[(0.0, 0.0), (6.0, 0.0), (5.0, 2.0), (3.0, 2.0),
                       (3.0, 6.0), (7.0, 6.0), (4.0, 7.0), (3.5, 8.0),
                       (0.0, 10.0)], []])

        dec.add_robot_site(0, (10, 0))
        dec.add_robot_site(1, (10, 10))
        dec.add_robot_site(2, (0, 10))
        dec.add_robot_site(3, (0, 0))

    elif poly_id == 5:
        # A rectangle
        P = [[(0.0, 0.0), (9.0, 0.0), (9.0, 1.0), (0.0, 1.0)], []]

        dec = Decomposition(P)

        dec.add_cell([[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)], []])
        dec.add_cell([[(1.0, 0.0), (6.0, 0.0), (6.0, 1.0), (1.0, 1.0)], []])
        dec.add_cell([[(6.0, 0.0), (9.0, 0.0), (9.0, 1.0), (6.0, 1.0)], []])

        dec.add_robot_site(0, (0, 0))
        dec.add_robot_site(1, (0, 0))
        dec.add_robot_site(2, (0, 0))

    elif poly_id == 6:
        P = [[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)],
             [[(7.0, 1.0), (7.0, 3.0), (8.0, 3.0), (8.0, 1.0)],
              [(1.0, 4.0), (1.5, 4.5), (3.0, 5.0), (3.5, 4.5), (3.0, 3.0),
               (2.0, 3.0)],
              [(4.0, 7.0), (6.0, 9.0), (7.0, 8.0), (6.0, 7.0), (8.0, 6.0),
               (8.5, 6.5), (9.0, 6.0), (8.0, 5.0)]]]

        dec = Decomposition(P)

        dec.add_cell([[(0.0, 0.0), (1.0, 4.0), (1.5, 4.5), (3.0, 5.0),
                       (4.0, 7.0), (6.0, 9.0), (10.0, 10.0), (0.0, 10.0)], []])
        dec.add_cell([[(0.0, 0.0), (7.0, 1.0), (7.0, 3.0), (8.0, 5.0),
                       (4.0, 7.0), (3.0, 5.0), (3.5, 4.5), (3.0, 3.0),
                       (2.0, 3.0), (1.0, 4.0)], []])
        dec.add_cell([[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (9.0, 6.0),
                       (8.0, 5.0), (7.0, 3.0), (8.0, 3.0), (8.0, 1.0),
                       (7.0, 1.0)], []])
        dec.add_cell([[(10.0, 10.0), (6.0, 9.0), (7.0, 8.0), (6.0, 7.0),
                       (8.0, 6.0), (8.5, 6.5), (9.0, 6.0)], []])

        dec.add_robot_site(0, (10, 0))
        dec.add_robot_site(1, (10, 10))
        dec.add_robot_site(2, (0, 10))
        dec.add_robot_site(3, (0, 0))

    elif poly_id == 7:
        P = [[(0.0, 0.0), (4.0, 0.0), (4.0, 5.0), (2.0, 5.0), (2.0, 6.0),
              (5.0, 6.0), (5.0, 0.0), (10.0, 0.0), (10.0, 4.0), (7.0, 4.0),
              (7.0, 5.0), (10.0, 5.0), (10.0, 10.0), (0.0, 10.0)],
             [[(7.0, 7.0), (7.0, 9.0), (8.0, 9.0), (8.0, 7.0)],
              [(3.0, 7.0), (2.0, 8.0), (3.0, 9.0), (6.0, 8.0)]]]

        dec = Decomposition(P)

        dec.add_cell([[(0.0, 0.0), (4.0, 0.0), (4.0, 5.0), (2.0, 5.0),
                       (0.0, 5.0)], []])
        dec.add_cell([[(0.0, 10.0), (0.0, 5.0), (2.0, 5.0), (2.0, 6.0),
                       (3.0, 6.0), (3.0, 7.0), (2.0, 8.0), (3.0, 9.0),
                       (3.0, 10.0)], []])
        dec.add_cell([[
            (10.0, 7.0),
            (10.0, 10.0),
            (3.0, 10.0),
            (3.0, 9.0),
            (6.0, 8.0),
            (7.0, 9.0),
            (8.0, 9.0),
            (8.0, 7.0),
        ], []])
        dec.add_cell([[(10.0, 0.0), (10.0, 4.0), (7.0, 4.0), (7.0, 5.0),
                       (10.0, 5.0), (10.0, 7.0), (8.0, 7.0), (7.0, 7.0),
                       (7.0, 9.0), (6.0, 8.0), (3.0, 7.0), (3.0, 6.0),
                       (5.0, 6.0), (5.0, 0.0)], []])

        dec.add_robot_site(0, (10, 0))
        dec.add_robot_site(1, (10, 10))
        dec.add_robot_site(2, (0, 10))
        dec.add_robot_site(3, (0, 0))

    # New combined holes
    elif poly_id == 8:
        # A more complex shape with one hole
        P = [[(0.0, 0.0), (6.0, 0.0), (6.0, 5.0), (4.0, 5.0), (4.0, 3.0),
              (5.0, 3.0), (5.0, 2.0), (3.0, 2.0), (3.0, 6.0), (3.9, 6.0),
              (3.9, 7.0), (3.5, 8.0), (4.5, 9.0), (6.0, 8.0), (4.1, 7.0),
              (4.1, 6.0), (7.0, 6.0), (7.0, 0.0), (10.0, 0.0), (10.0, 10.0),
              (0.0, 10.0)], []]

        dec = Decomposition(P)

        dec.add_cell([[(6.0, 0.0), (6.0, 5.0), (4.0, 5.0), (4.0, 3.0),
                       (5.0, 3.0), (5.0, 2.0)], []])
        dec.add_cell([[(7.0, 6.0), (7.0, 0.0), (10.0, 0.0), (10.0, 10.0),
                       (6.0, 8.0), (4.1, 7.0), (4.1, 6.0)], []])
        dec.add_cell([[(6.0, 8.0), (10.0, 10.0), (0.0, 10.0), (3.5, 8.0),
                       (4.5, 9.0)], []])
        dec.add_cell([[(0.0, 0.0), (6.0, 0.0), (5.0, 2.0), (3.0, 2.0),
                       (3.0, 6.0), (3.9, 6.0), (3.9, 7.0), (3.5, 8.0),
                       (0.0, 10.0)], []])

        dec.add_robot_site(0, (10, 0))
        dec.add_robot_site(1, (10, 10))
        dec.add_robot_site(2, (0, 10))
        dec.add_robot_site(3, (0, 0))

    elif poly_id == 9:
        P = [[(0.0, 0.0), (7.0, 0.0), (7.0, 3.0), (7.4, 3.0), (7.9, 5.0),
              (4.1, 7.0), (3.1, 5.0), (3.5, 4.5), (3.0, 3.0), (2.0, 3.0),
              (1.0, 4.0), (1.5, 4.5), (2.9, 5.0), (3.9, 7.0), (6.0, 9.0),
              (7.0, 8.0), (6.0, 7.0), (8.0, 6.0), (8.5, 6.5), (9.0, 6.0),
              (8.1, 5.0), (7.6, 3.0), (8.0, 3.0), (8.0, 0.0), (10.0, 0.0),
              (10.0, 10.0), (0.0, 10.0)], []]

        dec = Decomposition(P)

        dec.add_cell([[(0.0, 0.0), (1.0, 4.0), (1.5, 4.5), (2.9, 5.0),
                       (3.9, 7.0), (6.0, 9.0), (10.0, 10.0), (0.0, 10.0)], []])
        dec.add_cell([[(0.0, 0.0), (7.0, 0.0), (7.0, 3.0), (7.4, 3.0),
                       (7.9, 5.0), (4.1, 7.0), (3.1, 5.0), (3.5, 4.5),
                       (3.0, 3.0), (2.0, 3.0), (1.0, 4.0)], []])
        dec.add_cell([[(8.0, 0.0), (10.0, 0.0), (10.0, 6.0), (9.0, 6.0),
                       (8.1, 5.0), (7.6, 3.0), (8.0, 3.0)], []])
        dec.add_cell([[(10.0, 10.0), (6.0, 9.0), (7.0, 8.0), (6.0, 7.0),
                       (8.0, 6.0), (8.5, 6.5), (9.0, 6.0), (10.0, 6.0)], []])

        dec.add_robot_site(0, (10, 0))
        dec.add_robot_site(1, (10, 10))
        dec.add_robot_site(2, (0, 10))
        dec.add_robot_site(3, (0, 0))

    elif poly_id == 10:
        P = [[(0.0, 0.0), (4.0, 0.0), (4.0, 5.0), (2.0, 5.0), (2.0, 6.0),
              (5.0, 6.0), (5.0, 0.0), (10.0, 0.0), (10.0, 4.0), (7.0, 4.0),
              (7.0, 9.0), (8.0, 9.0), (8.0, 5.0), (10.0, 5.0), (10.0, 10.0),
              (3.1, 10.0), (3.1, 9.0), (6.0, 8.0), (3.0, 7.0), (2.0, 8.0),
              (2.9, 9.0), (2.9, 10.0), (0.0, 10.0)], []]

        dec = Decomposition(P)

        dec.add_cell([[(0.0, 0.0), (4.0, 0.0), (4.0, 5.0), (2.0, 5.0),
                       (0.0, 5.0)], []])
        dec.add_cell([[(0.0, 10.0), (0.0, 5.0), (2.0, 5.0), (2.0, 6.0),
                       (3.0, 6.0), (3.0, 7.0), (2.0, 8.0), (2.9, 9.0),
                       (2.9, 10.0)], []])
        dec.add_cell([[(10.0, 5.0), (10.0, 10.0), (3.1, 10.0), (3.1, 9.0),
                       (6.0, 8.0), (7.0, 9.0), (8.0, 9.0), (8.0, 5.0)], []])
        dec.add_cell([[(10.0, 0.0), (10.0, 4.0), (7.0, 4.0), (7.0, 9.0),
                       (6.0, 8.0), (3.0, 7.0), (3.0, 6.0), (5.0, 6.0),
                       (5.0, 0.0)], []])

        dec.add_robot_site(0, (10, 0))
        dec.add_robot_site(1, (10, 10))
        dec.add_robot_site(2, (0, 10))
        dec.add_robot_site(3, (0, 0))

    return dec
예제 #13
0
 def test_createDecomp(self):
     decomp = Decomposition(self.chi)
예제 #14
0
    def train(self,
              x_train,
              y_train,
              x_test,
              y_test,
              binary=True,
              iterate=False,
              linear=True,
              decomposition=False,
              once=False,
              fileprefix=''):
        self.setupResults()

        if decomposition is True:
            if once is True:
                # Create decomposition object if it does not exists otherwise use the stored object
                if self.dec is None:
                    self.dec = Decomposition()
                    x_train = self.dec.fit(x_train,
                                           y_train,
                                           kernel='rbf',
                                           gamma=0.1,
                                           n_components=10)
                else:
                    x_train = self.dec.transform(x_train)
                if self.starcraft is True:
                    self.dec.visualize(x_train, y_train, fileprefix=fileprefix)
                x_test = self.dec.transform(x_test)
                self.dim_red = {
                    'kernel': 'rbf',
                    'gamma': 0.1,
                    'n_component': 15
                }
                self.svm(x_train, y_train, x_test, y_test, binary, iterate,
                         linear)
            else:
                # Test with all available variables
                dec = Decomposition()
                for n_component in dec.components:
                    for kernel in dec.kernel:
                        if kernel == 'rbf':
                            for gamma in dec.Gamma:
                                x_train = dec.fit(x_train,
                                                  y_train,
                                                  kernel=kernel,
                                                  gamma=gamma,
                                                  n_components=n_component)
                                x_test = dec.transform(x_test)
                                self.dim_red = {
                                    'kernel': kernel,
                                    'gamma': gamma,
                                    'n_component': n_component
                                }
                                self.svm(x_train, y_train, x_test, y_test,
                                         binary, iterate, linear)
                        else:
                            x_train = dec.fit(x_train,
                                              y_train,
                                              n_components=n_component)
                            x_test = dec.transform(x_test)
                            self.dim_red = {
                                'kernel': kernel,
                                'gamma': '-',
                                'n_component': n_component
                            }
                            self.svm(x_train, y_train, x_test, y_test, binary,
                                     iterate, linear)
        else:
            # Run without dimesionality reduction
            self.dim_red = {'kernel': '-', 'gamma': '-', 'n_component': '-'}
            self.svm(x_train, y_train, x_test, y_test, binary, iterate, linear)
예제 #15
0
class SVM:
    """
    Constructor
    Initialize arrays for C, gamma, class weights and decision function variables
    """
    def __init__(self):
        # Iteration variables
        self.C = [1, 10, 100, 1000]
        self.gammas = [0.001, 0.01, 0.1, 1]
        self.class_weights = [None, 'balanced']
        # Create results file
        self.results = None
        # Data for KPCA-LDA iterations
        self.dim_red = {'kernel': '-', 'gamma': '-', 'n_component': '-'}
        # Decomposition object
        self.dec = None
        # Flag for starctaft dataset or mnist
        self.starcraft = True

    """
    Train SVM model
    If decomposition is true it will also apply KPCA-LDA to the training data with
    variables
    """

    def train(self,
              x_train,
              y_train,
              x_test,
              y_test,
              binary=True,
              iterate=False,
              linear=True,
              decomposition=False,
              once=False,
              fileprefix=''):
        self.setupResults()

        if decomposition is True:
            if once is True:
                # Create decomposition object if it does not exists otherwise use the stored object
                if self.dec is None:
                    self.dec = Decomposition()
                    x_train = self.dec.fit(x_train,
                                           y_train,
                                           kernel='rbf',
                                           gamma=0.1,
                                           n_components=10)
                else:
                    x_train = self.dec.transform(x_train)
                if self.starcraft is True:
                    self.dec.visualize(x_train, y_train, fileprefix=fileprefix)
                x_test = self.dec.transform(x_test)
                self.dim_red = {
                    'kernel': 'rbf',
                    'gamma': 0.1,
                    'n_component': 15
                }
                self.svm(x_train, y_train, x_test, y_test, binary, iterate,
                         linear)
            else:
                # Test with all available variables
                dec = Decomposition()
                for n_component in dec.components:
                    for kernel in dec.kernel:
                        if kernel == 'rbf':
                            for gamma in dec.Gamma:
                                x_train = dec.fit(x_train,
                                                  y_train,
                                                  kernel=kernel,
                                                  gamma=gamma,
                                                  n_components=n_component)
                                x_test = dec.transform(x_test)
                                self.dim_red = {
                                    'kernel': kernel,
                                    'gamma': gamma,
                                    'n_component': n_component
                                }
                                self.svm(x_train, y_train, x_test, y_test,
                                         binary, iterate, linear)
                        else:
                            x_train = dec.fit(x_train,
                                              y_train,
                                              n_components=n_component)
                            x_test = dec.transform(x_test)
                            self.dim_red = {
                                'kernel': kernel,
                                'gamma': '-',
                                'n_component': n_component
                            }
                            self.svm(x_train, y_train, x_test, y_test, binary,
                                     iterate, linear)
        else:
            # Run without dimesionality reduction
            self.dim_red = {'kernel': '-', 'gamma': '-', 'n_component': '-'}
            self.svm(x_train, y_train, x_test, y_test, binary, iterate, linear)

    """
    Run SVM model with x_train = data and y_train = labels and test with x_test and y_test accordingly
    By default it uses the binary svm classification
    If iterate is True it will test with all the available variables for this type of classification
    """

    def svm(self, x_train, y_train, x_test, y_test, binary, iterate, linear):
        # Single iteration
        if iterate is False:
            if binary is True:
                # Train SVM with rbf and default values for the given data
                # By default C=1.0, gamma=0.01 and class weight = None
                clf = None
                clf = svm.SVC(kernel='rbf', C=1.0, gamma=0.01)
                # Train created model
                clf.fit(x_train, y_train)
                # Predict on test data
                prediction = None
                prediction = clf.predict(x_test)
                # Log results
                self.logBinaryResults(y_test,
                                      prediction,
                                      n_support=clf.n_support_,
                                      C=1.0,
                                      gamma=0.01)
            else:
                if (linear is True):
                    # Train svm for multiclass prediction using the liblinear library implementation
                    # LinearSVC with the following parameters
                    # multiclass=ovr (one v rest) creates (n_labels) number of classifiers, another argument could be crammer_singer
                    # class_weight = balanced by default in multi class
                    clf = None
                    clf = LinearSVC(multi_class='ovr',
                                    random_state=0,
                                    class_weight='balanced')
                    # Train created model
                    clf.fit(x_train, y_train)
                    # Predict on test data
                    prediction = None
                    prediction = clf.predict(x_test)
                    # Log results
                    self.logMultiClassResults(y_test,
                                              prediction,
                                              plotConfMat=True,
                                              decision_function='ovr',
                                              cnfTitle='Liblinear SVM',
                                              cnfFilename='_liblinear',
                                              kernel='linear')
                else:
                    # Train svm for multiclass prediction using the libsvm library implementation
                    # Do one model for balanced weights and one without
                    # libsvm SVC with the following parameters
                    # multiclass=ovr (one v rest) creates (n_labels) number of classifiers, another argument could be ovo
                    # class_weight = balanced by default in multi class
                    clf = None
                    clf = svm.SVC(kernel='rbf',
                                  gamma='auto',
                                  decision_function_shape='ovo',
                                  class_weight='balanced')
                    # Train created model
                    clf.fit(x_train, y_train)
                    # Predict on test data
                    prediction = None
                    prediction = clf.predict(x_test)
                    # Log results
                    self.logMultiClassResults(
                        y_test,
                        prediction,
                        plotConfMat=True,
                        n_support=clf.n_support_,
                        cnfTitle='Libsvm SVM with Balanced weights',
                        C=1,
                        gamma='auto',
                        cnfFilename='_libsvm_balanced')

                    clf = None
                    clf = svm.SVC(kernel='rbf',
                                  gamma='auto',
                                  decision_function_shape='ovo')
                    # Train created model
                    clf.fit(x_train, y_train)
                    # Predict on test data
                    prediction = None
                    prediction = clf.predict(x_test)
                    # Log results
                    self.logMultiClassResults(
                        y_test,
                        prediction,
                        n_support=clf.n_support_,
                        weights='unbalanced',
                        C=1,
                        gamma='auto',
                        plotConfMat=True,
                        cnfTitle='Libsvm SVM without weights',
                        cnfFilename='_libsvm')
        # Multiple iterations
        else:
            if binary is True:
                for c in self.C:
                    # Test binary svm with all available c values
                    for gamma in self.gammas:
                        # Test with all available gamma values
                        for weight in self.class_weights:
                            # Test with balanced and equal weights
                            clf = None
                            clf = svm.SVC(kernel='rbf',
                                          C=c,
                                          gamma=gamma,
                                          class_weight=weight)
                            # Train created model
                            clf.fit(x_train, y_train)
                            # Predict on test data
                            prediction = None
                            prediction = clf.predict(x_test)
                            # Calculate and save results
                            results = metrics.precision_recall_fscore_support(
                                y_test, prediction, average='macro')
                            # Log results of current run
                            self.logBinaryResults(y_test,
                                                  prediction,
                                                  n_support=clf.n_support_,
                                                  C=c,
                                                  gamma=gamma,
                                                  weight=weight)
            else:
                for c in self.C:
                    # Test binary svm with all available c values
                    for gamma in self.gammas:
                        # Test with all available gamma values
                        clf = None
                        clf = svm.SVC(kernel='rbf',
                                      C=c,
                                      gamma=gamma,
                                      class_weight='balanced')
                        # Train created model
                        clf.fit(x_train, y_train)
                        # Predict on test data
                        prediction = None
                        prediction = clf.predict(x_test)
                        # Calculate and save results
                        results = metrics.precision_recall_fscore_support(
                            y_test, prediction, average='macro')
                        # Log results of current run
                        self.logMultiClassResults(y_test,
                                                  prediction,
                                                  C=c,
                                                  gamma=gamma,
                                                  decision_function='ovo')

    """
    Log binary SVM results
    """

    def logBinaryResults(self,
                         y_test,
                         prediction,
                         kernel='rbf',
                         C=1.0,
                         gamma=1.0,
                         n_support='-',
                         weight='-'):
        confusionMatrix = metrics.confusion_matrix(y_test, prediction)
        result = metrics.precision_recall_fscore_support(y_test,
                                                         prediction,
                                                         average='macro')
        if self.starcraft is True:
            self.results = self.results.append(
                {
                    'Kernel': kernel,
                    'C': str(C),
                    'Gamma': str(gamma),
                    'Recall': float("%0.3f" % result[1]),
                    'Precision': float("%0.3f" % result[0]),
                    'F1': float("%0.3f" % result[2]),
                    'Support vectors': n_support,
                    'Class weights': weight,
                    'Casual': confusionMatrix[0],
                    'Hardcore': confusionMatrix[1]
                },
                ignore_index=True)
        else:
            self.results = self.results.append(
                {
                    'Kernel': kernel,
                    'C': str(C),
                    'Gamma': str(gamma),
                    'Recall': float("%0.3f" % result[1]),
                    'Precision': float("%0.3f" % result[0]),
                    'F1': float("%0.3f" % result[2]),
                    'Support vectors': n_support,
                    'Class weights': weight,
                    'Even': confusionMatrix[0],
                    'Odd': confusionMatrix[1]
                },
                ignore_index=True)

    """
    Log multi-class SVM results
    """

    def logMultiClassResults(self,
                             y_test,
                             prediction,
                             kernel='rbf',
                             C='-',
                             gamma='-',
                             weights='balanced',
                             decision_function='ovo',
                             n_support='-',
                             plotConfMat=False,
                             cnfTitle='',
                             cnfFilename=''):
        # Calculate and save in dataframe
        confusionMatrix = metrics.confusion_matrix(y_test, prediction)
        if plotConfMat is True:
            self.plotHeatMap(confusionMatrix,
                             'Multi Class Heatmap ' + cnfTitle,
                             'multi-class' + cnfFilename)
        result = metrics.precision_recall_fscore_support(y_test,
                                                         prediction,
                                                         average='macro')
        if self.starcraft is True:
            self.results = self.results.append(
                {
                    'Kernel': kernel,
                    'C': str(C),
                    'Gamma': str(gamma),
                    'Recall': float("%0.3f" % result[1]),
                    'Precision': float("%0.3f" % result[0]),
                    'F1': float("%0.3f" % result[2]),
                    'Support vectors': n_support,
                    'Class weights': weights,
                    'Decision Function': decision_function,
                    'KPCA-LDA Components': self.dim_red['n_component'],
                    'KPCA-LDA Kernel': self.dim_red['kernel'],
                    'KPCA-LDA Gamma': self.dim_red['gamma'],
                    'Bronze': confusionMatrix[0][0],
                    'Silver': confusionMatrix[1][1],
                    'Gold': confusionMatrix[2][2],
                    'Platinum': confusionMatrix[3][3],
                    'Diamond': confusionMatrix[4][4],
                    'Master': confusionMatrix[5][5],
                    'GrandMaster': confusionMatrix[6][6],
                    'Professional': confusionMatrix[7][7]
                },
                ignore_index=True)
        else:
            self.results = self.results.append(
                {
                    'Kernel': kernel,
                    'C': str(C),
                    'Gamma': str(gamma),
                    'Recall': float("%0.3f" % result[1]),
                    'Precision': float("%0.3f" % result[0]),
                    'F1': float("%0.3f" % result[2]),
                    'Support vectors': n_support,
                    'Class weights': weights,
                    'Decision Function': decision_function,
                    'KPCA-LDA Components': self.dim_red['n_component'],
                    'KPCA-LDA Kernel': self.dim_red['kernel'],
                    'KPCA-LDA Gamma': self.dim_red['gamma'],
                    'Zero': confusionMatrix[0][0],
                    'One': confusionMatrix[1][1],
                    'Two': confusionMatrix[2][2],
                    'Three': confusionMatrix[3][3],
                    'Four': confusionMatrix[4][4],
                    'Five': confusionMatrix[5][5],
                    'Six': confusionMatrix[6][6],
                    'Seven': confusionMatrix[7][7],
                    'Eight': confusionMatrix[8][8],
                    'Nine': confusionMatrix[9][9]
                },
                ignore_index=True)

    """
    Plot Confusion Matrix
    """

    def plotHeatMap(self, confusionMatrix, title, filename):
        pyplot.figure(figsize=[13, 6])
        # Calculate confusion matrix
        ax = sns.heatmap(confusionMatrix, annot=True, fmt="d", cmap="OrRd")
        # Plot confusion matrix
        pyplot.title(title)
        pyplot.xlabel('True output')
        pyplot.ylabel('Predicted output')
        pyplot.savefig('logs/heatmap_' + filename + '.png')
        pyplot.clf()

    """
    Setup results dataframe object
    """

    def setupResults(self):
        if self.starcraft is True:
            self.results = pd.DataFrame(columns=[
                'Kernel', 'C', 'Gamma', 'Precision', 'Recall', 'F1',
                'Support vectors', 'Class weights', 'Decision Function',
                'Casual', 'Hardcore', 'Bronze', 'Silver', 'Gold', 'Platinum',
                'Diamond', 'Master', 'GrandMaster', 'Professional'
            ])
        else:
            self.results = pd.DataFrame(columns=[
                'Kernel', 'C', 'Gamma', 'Precision', 'Recall', 'F1',
                'Support vectors', 'Class weights', 'Decision Function',
                'Even', 'Odd', 'Zero', 'One', 'Two', 'Three', 'Four', 'Five',
                'Six', 'Seven', 'Eight', 'Nine'
            ])

    def isMnist(self):
        self.starcraft = False