Beispiel #1
0
 def test_integration_test1(self):
     chess = Chess(path="../res/test_case_1.txt", configuration=None)
     result = chess.translate(
         minimax_decision(game=chess,
                          state=chess.initial_state,
                          depth_limit=chess.config.depth_limit))
     self.assertEqual("F4-H2\n160\n160\n5\n", result)
     chess.write_to_file(result)
Beispiel #2
0
    def test_tie_break_circle(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Circle
MINIMAX
7
0,0,0,0,0,0,S1,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,C1,0,0,C1,0,0
0,0,0,0,0,0,0,0
10,20,30,40,52,70,90,1000
            """)
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = minimax_decision(chess1.initial_state, chess1, 1024)
        self.assertEqual((((6, 2), (7, 1))), utility1[0])
        self.assertEqual(1000 + 90 - 1000, utility1[1])  # myopic
        self.assertEqual(1000, utility1[2])
        self.assertEqual(utility1[3], 33)

        utility2 = alphabeta_cutoff_search(chess1.initial_state, chess1,
                                           configuration1.depth_limit)
        self.assertEqual(utility1[0], utility2[0])
        self.assertEqual(utility1[1], utility2[1])
        self.assertEqual(utility1[2], utility2[2])
        self.assertGreaterEqual(utility1[3], 33)
Beispiel #3
0
 def test_read_file1(self):
     configuration = Configuration("../res/test_case_1.txt")
     self.assertEqual(configuration.player, "S")
     self.assertEqual(configuration.algorithm, "MINIMAX")
     self.assertEqual(configuration.depth_limit, 2)
     self.assertEqual(configuration.row_values,
                      [10, 20, 30, 40, 50, 60, 70, 80])
     # self.assertEqual(configuration.initial_state,[['0','S1','0','0','0','0','0','0'],
     #                                               ['0','0','C1','0','0','0','0','0'],
     #                                               ['0','0','0','S1','0','0','0','0'],
     #                                               ['0','0','0','0','0','0','0','0'],
     #                                               ['0','0','0','0','0','0','0','0'],
     #                                               ['0','0','0','0','0','0','0','0'],
     #                                               ['0','0','0','0','0','0','0','0'],
     #                                               ['0','0','0','0','0','0','0','0']])
     board = configuration.initial_map
     self.assertEqual(board, [['0', 'S1', '0', '0', '0', '0', '0', '0'],
                              ['0', '0', 'C1', '0', '0', '0', '0', '0'],
                              ['0', '0', '0', 'S1', '0', '0', '0', '0'],
                              ['0', '0', '0', '0', '0', '0', '0', '0'],
                              ['0', '0', '0', '0', '0', '0', '0', '0'],
                              ['0', '0', '0', '0', '0', '0', '0', '0'],
                              ['0', '0', '0', '0', '0', '0', '0', '0'],
                              ['0', '0', '0', '0', '0', '0', '0', '0']])
     chess = Chess(path=None, configuration=configuration)
     self.assertEqual(
         len(chess.initial_state.s_pieces + chess.initial_state.c_pieces),
         3)
     piece1 = Piece("S", (0, 1))
     piece2 = Piece("S", (2, 3))
     piece3 = Piece("C", (1, 2))
     self.assertTrue(piece1 in chess.initial_state.s_pieces)
     self.assertTrue(piece2 in chess.initial_state.s_pieces)
     self.assertTrue(piece3 in chess.initial_state.c_pieces)
Beispiel #4
0
    def test_action_go_to_boarder(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Circle
MINIMAX
2
0,S2,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,C1
0,0,0,0,0,0,S1,0
0,0,0,0,0,S1,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80
        """)
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = minimax_decision(chess1.initial_state, chess1)
        self.assertEqual(2 * 80 + 2 * 60, -utility1[2])
        chess1.initial_state.to_move = configuration1.player
        utility2 = alphabeta_cutoff_search(chess1.initial_state,
                                           chess1,
                                           d=1024)
        self.assertEqual(utility1[0], utility2[0])
        self.assertEqual(utility1[1], utility2[1])
        self.assertEqual(utility1[2], utility2[2])
        self.assertGreaterEqual(utility1[3], utility2[3])
Beispiel #5
0
    def test_depth_limit4(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Star
MINIMAX
7
0,C1,0,C1,0,C1,0,C1
C1,0,C1,0,C1,0,C1,0
0,S1,0,S1,0,S1,0,S1
S1,0,S1,0,S1,0,S1,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,52,70,90,1000
            """)
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = minimax_decision(chess1.initial_state, chess1,
                                    configuration1.depth_limit)
        self.assertEqual((('Noop')), utility1[0])
        self.assertEqual(368, utility1[1])  # myopic
        self.assertEqual(368, utility1[2])
        self.assertGreaterEqual(utility1[3], 3)

        utility2 = alphabeta_cutoff_search(chess1.initial_state, chess1,
                                           configuration1.depth_limit)
        self.assertEqual(utility1[0], utility2[0])
        self.assertEqual(utility1[1], utility2[1])
        self.assertEqual(utility1[2], utility2[2])
        self.assertGreaterEqual(utility1[3], utility2[3])
Beispiel #6
0
    def test_depth_limit3(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Circle
MINIMAX
2
0,S2,0,0,0,0,0,0
S1,0,0,0,0,0,0,0
0,0,0,0,0,0,0,C1
0,0,0,0,0,0,S1,0
0,0,0,0,0,S1,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80""")
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = minimax_decision(chess1.initial_state, chess1,
                                    configuration1.depth_limit)
        self.assertEqual('Noop', utility1[0])
        self.assertEqual(-290, utility1[1])  # myopic
        self.assertEqual(-300, utility1[2])
        self.assertEqual(5, utility1[3])
        chess1.initial_state.to_move = configuration1.player
        utility2 = alphabeta_cutoff_search(chess1.initial_state, chess1,
                                           configuration1.depth_limit)
        self.assertEqual(utility1[0], utility2[0])
        self.assertEqual(utility1[1], utility2[1])
        self.assertEqual(utility1[2], utility2[2])
        self.assertGreaterEqual(utility1[3], utility2[3])
Beispiel #7
0
    def test_action_no_action(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Star
MINIMAX
2
0,S1,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,S1,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80
        """)
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = minimax_decision(chess1.initial_state, chess1)
        self.assertEqual(('Noop'), utility1[0])  # min action
        self.assertEqual(140, utility1[1])  # myopic
        self.assertEqual(140, utility1[2])  # farsighted
        self.assertEqual(1, utility1[3])  # total number

        chess1.initial_state.to_move = configuration1.player
        utility2 = alphabeta_cutoff_search(chess1.initial_state,
                                           chess1,
                                           d=1024)
        self.assertEqual(utility1[0], utility2[0])
        self.assertEqual(utility1[1], utility2[1])
        self.assertEqual(utility1[2], utility2[2])
        self.assertGreaterEqual(utility1[3], utility2[3])
Beispiel #8
0
    def test_alphabeta_3(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Circle
ALPHABETA
10
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,C1,0,0,0
0,0,0,0,0,0,0,0
S1,0,S1,0,S1,0,S1,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,200,250,300
            """)
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = alphabeta_cutoff_search(chess1.initial_state, chess1,
                                           configuration1.depth_limit)
        print(chess1.translate(utility1))
Beispiel #9
0
    def test_translate_no_action(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Star
MINIMAX
7
0,C1,0,C1,0,C1,0,C1
C1,0,C1,0,C1,0,C1,0
0,S1,0,S1,0,S1,0,S1
S1,0,S1,0,S1,0,S1,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,52,70,90,1000""")
        chess1 = Chess(path=None, configuration=configuration1)
        utility = (('Noop'), 130, 90, 26)
        self.assertEqual("""pass
130
90
26
""", chess1.translate(utility))
Beispiel #10
0
    def test_evaluation(self):
        configuration2 = Configuration(path=None)
        configuration2.generate_configuration_from_string("""Circle
ALPHABETA
2
0,S3,0,0,0,0,0,S1
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,C1,0
10,20,30,40,50,60,70,80
            """)
        chess2 = Chess(path=None, configuration=configuration2)
        self.assertEqual(
            80 - 4 * 80,
            chess2.evaluation(s_pieces=chess2.initial_state.s_pieces,
                              c_pieces=chess2.initial_state.c_pieces,
                              player="C",
                              row_values=configuration2.row_values))
Beispiel #11
0
    def test_pressure_test(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Circle
MINIMAX
10
0,C1,0,C1,0,C1,0,C1
C1,0,C1,0,C1,0,C1,0
0,C1,0,C1,0,C1,0,C1
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
S1,0,S1,0,S1,0,S1,0
0,S1,0,S1,0,S1,0,S1
S1,0,S1,0,S1,0,S1,0
10,20,30,40,50,60,70,80
        """)
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = minimax_decision(chess1.initial_state,
                                    chess1,
                                    depth_limit=10)
Beispiel #12
0
 def test_read_file2(self):
     configuration = Configuration("../res/test_case_2.txt")
     self.assertEqual(configuration.player, "C")
     self.assertEqual(configuration.algorithm, "ALPHABETA")
     self.assertEqual(configuration.depth_limit, 2)
     self.assertEqual(configuration.row_values,
                      [10, 20, 30, 40, 50, 60, 70, 80])
     chess = Chess(path=None, configuration=configuration)
     self.assertEqual(
         len(chess.initial_state.c_pieces + chess.initial_state.s_pieces),
         6)
     piece1 = Piece("S", (0, 1))
     piece2 = Piece("S", (1, 0))
     piece3 = Piece("C", (2, 7))
     piece4 = Piece("S", (3, 6))
     piece5 = Piece("S", (4, 5))
     self.assertEqual(chess.initial_state.s_pieces.count(piece1), 2)
     self.assertTrue(piece2 in chess.initial_state.s_pieces)
     self.assertTrue(piece3 in chess.initial_state.c_pieces)
     self.assertTrue(piece4 in chess.initial_state.s_pieces)
     self.assertTrue(piece5 in chess.initial_state.s_pieces)
Beispiel #13
0
    def test_alphabeta_2(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Circle
ALPHABETA
2
0,S2,0,0,0,0,0,0
S1,0,0,0,0,0,0,0
0,0,0,0,0,0,0,C1
0,0,0,0,0,0,S1,0
0,0,0,0,0,S1,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80""")
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = alphabeta_cutoff_search(chess1.initial_state,
                                           chess1,
                                           d=configuration1.depth_limit)
        self.assertEqual(('Noop'), utility1[0])  # min action
        self.assertEqual(-290, utility1[1])  # myopic
        self.assertEqual(-300, utility1[2])  # farsighted
        self.assertEqual(5, utility1[3])  # total number
Beispiel #14
0
    def test_action_no_action(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Star
ALPHABETA
2
0,S1,0,0,0,0,0,0
0,0,C1,0,0,0,0,0
0,0,0,S1,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80
        """)
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = alphabeta_cutoff_search(chess1.initial_state,
                                           chess1,
                                           d=configuration1.depth_limit)
        self.assertEqual(((2, 3), (0, 1)), utility1[0])  # min action
        self.assertEqual(160, utility1[1])  # myopic
        self.assertEqual(160, utility1[2])  # farsighted
        self.assertEqual(4, utility1[3])  # total number
Beispiel #15
0
    def test_tie_break(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Star
MINIMAX
2
0,0,0,0,0,0,0,0
0,S1,0,0,0,0,0,0
0,0,0,0,S1,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,C1,0
10,20,30,40,50,60,70,80
        """)
        chess1 = Chess(path=None, configuration=configuration1)
        utility1 = minimax_decision(chess1.initial_state, chess1)
        self.assertEqual(((1, 1), (0, 0)), utility1[0])
        self.assertEqual(80, utility1[2])
        chess1.initial_state.to_move = configuration1.player
        utility2 = alphabeta_cutoff_search(chess1.initial_state,
                                           chess1,
                                           d=1024)
        self.assertEqual(utility1[0], utility2[0])
Beispiel #16
0
    def test_action_go_to_boarder4(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Star
MINIMAX
2
0,C1,0,0,0,0,0,S1
C1,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80
    """)
        chess1 = Chess(path=None, configuration=configuration1)
        # utility1 = minimax_decision(chess1.initial_state, chess1)
        # self.assertEqual(80, -utility1[2])

        chess1.initial_state.to_move = configuration1.player
        utility2 = alphabeta_cutoff_search(chess1.initial_state,
                                           chess1,
                                           d=1024)
        self.assertEqual(-80, utility2[2])
Beispiel #17
0
    def test_actions(self):
        configuration1 = Configuration(path=None)
        configuration1.generate_configuration_from_string("""Star
MINIMAX
2
0,S1,0,0,0,0,0,0
0,0,C1,0,0,0,0,0
0,0,0,S1,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80
        """)

        configuration2 = Configuration(path=None)
        configuration2.generate_configuration_from_string("""Circle
ALPHABETA
2
0,S2,0,0,0,0,0,0
S1,0,0,0,0,0,0,0
0,0,0,0,0,0,0,C1
0,0,0,0,0,0,S1,0
0,0,0,0,0,S1,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80
            """)

        configuration3 = Configuration(path=None)
        configuration3.generate_configuration_from_string("""Circle
ALPHABETA
2
0,S100,0,0,0,0,0,0
0,0,C1,0,0,0,0,0
0,0,0,S1,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80
            """)
        configuration4 = Configuration(path=None)
        configuration4.generate_configuration_from_string("""Star
ALPHABETA
2
0,C1,0,0,0,0,0,0
0,0,C1,0,0,0,0,0
0,0,0,S1,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
10,20,30,40,50,60,70,80
            """)
        chess1 = Chess(path=None, configuration=configuration1)
        chess2 = Chess(path=None, configuration=configuration2)
        chess3 = Chess(path=None, configuration=configuration3)
        chess4 = Chess(path=None, configuration=configuration4)
        actions1 = chess1.actions(chess1.initial_state)
        self.assertEqual(2, len(actions1))
        actions2 = chess2.actions(chess2.initial_state)
        self.assertEqual(1, len(actions2))
        actions3 = chess3.actions(chess3.initial_state)
        self.assertEqual(2, len(actions3))
        actions4 = chess4.actions(chess4.initial_state)
        self.assertEqual(1, len(actions4))

        self.assertEqual(
            80 + 60 - 20,
            Chess.evaluation(c_pieces=chess1.initial_state.c_pieces,
                             s_pieces=chess1.initial_state.s_pieces,
                             player="S",
                             row_values=configuration1.row_values))
        self.assertEqual(
            80 * 2 + 70 + 50 + 40 - 30,
            Chess.evaluation(c_pieces=chess2.initial_state.c_pieces,
                             s_pieces=chess2.initial_state.s_pieces,
                             player="S",
                             row_values=configuration2.row_values))
        self.assertEqual(
            80 * 100 + 60 - 20,
            Chess.evaluation(c_pieces=chess3.initial_state.c_pieces,
                             s_pieces=chess3.initial_state.s_pieces,
                             player="S",
                             row_values=configuration3.row_values))
        self.assertEqual(
            60 - 10 - 20,
            Chess.evaluation(c_pieces=chess4.initial_state.c_pieces,
                             s_pieces=chess4.initial_state.s_pieces,
                             player="S",
                             row_values=configuration4.row_values))

        state = chess1.result(chess1.initial_state, actions1[0])
        self.assertEqual('C', state.to_move)
        self.assertEqual(
            len(chess1.initial_state.c_pieces + chess1.initial_state.s_pieces),
            len(state.c_pieces + state.s_pieces))

        state = chess1.result(chess1.initial_state, actions1[1])
        self.assertEqual('C', state.to_move)
        self.assertEqual(
            len(chess1.initial_state.c_pieces + chess1.initial_state.s_pieces),
            len(state.c_pieces + state.s_pieces) + 1)

        for a in actions2:
            state = chess2.result(chess2.initial_state, a)
            self.assertEqual('S', state.to_move)
            self.assertEqual(
                len(chess2.initial_state.c_pieces +
                    chess2.initial_state.s_pieces),
                len(state.c_pieces + state.s_pieces))

        state = chess3.result(chess3.initial_state, actions3[0])
        self.assertEqual('S', state.to_move)
        self.assertEqual(
            len(chess3.initial_state.c_pieces + chess3.initial_state.s_pieces),
            len(state.c_pieces + state.s_pieces))

        state = chess3.result(chess3.initial_state, actions3[1])
        self.assertEqual('S', state.to_move)
        self.assertEqual(
            len(chess3.initial_state.c_pieces + chess3.initial_state.s_pieces),
            len(state.c_pieces + state.s_pieces) + 1)

        for a in actions4:
            state = chess4.result(chess4.initial_state, a)
            self.assertEqual('C', state.to_move)
            self.assertEqual(
                len(chess4.initial_state.c_pieces +
                    chess4.initial_state.s_pieces),
                len(state.c_pieces + state.s_pieces))

        utility1 = minimax_decision(chess1.initial_state, chess1)
        self.assertEqual(80 * 2, utility1[2])
        chess2.initial_state.to_move = 'Circle'
        utility2 = minimax_decision(chess2.initial_state, chess2)
        self.assertEqual(-80 * 3 - 60 * 2, utility2[2])
        utility3 = minimax_decision(chess3.initial_state, chess3)
        self.assertEqual(80 - 80 * 100, utility3[2])