예제 #1
0
    def test_get_state(self):
        string = "23041"
        conv = NQueensConverter(5)

        expected_board = NQueensBoard(5)
        expected_board.add_queen_at(XYLocation(0, 2))
        expected_board.add_queen_at(XYLocation(1, 3))
        expected_board.add_queen_at(XYLocation(2, 0))
        expected_board.add_queen_at(XYLocation(3, 4))
        expected_board.add_queen_at(XYLocation(4, 1))

        self.assertEqual(expected_board, conv.get_state(string))
예제 #2
0
    def test_get_state(self):
        string = "23041"
        conv = NQueensConverter(5)

        expected_board = NQueensBoard(5)
        expected_board.add_queen_at(XYLocation(0, 2))
        expected_board.add_queen_at(XYLocation(1, 3))
        expected_board.add_queen_at(XYLocation(2, 0))
        expected_board.add_queen_at(XYLocation(3, 4))
        expected_board.add_queen_at(XYLocation(4, 1))

        self.assertEqual(expected_board, conv.get_state(string))
예제 #3
0
def main():
    succeeded = 0
    for i in range(NUMBER_OF_TASKS):
        print("Iteration number " + str(i))

        boards = [create_random_board() for i in range(50)]
        print("Created boards:")
        for board in boards:
            print(str(board))
        print("\n")

        p = GeneticProblem(boards, NQueensGoalTest(), NQueensConverter(SIZE))
        ga = GeneticAlgorithm(0.1)
        best_state = ga.search(p, AttackingPairHeuristic(), 100)

        if ga.failed:
            print("Search Failed")
        else:
            print("Search Succeeded")
            succeeded += 1

        print("Final state:")
        print(str(best_state))
        print("\n\n")

    print("Succeeded " + str(succeeded) + "/" + str(NUMBER_OF_TASKS))
예제 #4
0
    def test_get_alphabet(self):
        conv = NQueensConverter(5)
        expected_alphabet = ['0', '1', '2', '3', '4']

        self.assertSameElements(set(expected_alphabet),
                                set(conv.get_alphabet()))
예제 #5
0
    def test_get_length(self):
        length = 5
        conv = NQueensConverter(length)

        self.assertEqual(length, conv.get_individual_length())
예제 #6
0
    def test_get_alphabet(self):
        conv = NQueensConverter(5)
        expected_alphabet = ['0', '1', '2', '3', '4']

        self.assertSameElements(set(expected_alphabet), set(conv.get_alphabet()))
예제 #7
0
    def test_get_length(self):
        length = 5
        conv = NQueensConverter(length)

        self.assertEqual(length, conv.get_individual_length())