Example #1
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
Example #2
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])
Example #3
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])
Example #4
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])