Example #1
0
    def find(state, goals, heuristic):
        start_node = Node(state, find_element(state, 0), 0, 0, 0, 0, 0, 0,
                          None)
        open = OpenList([start_node])
        closed = ClosedList()

        current_node = open.pop()
        while not is_goal(current_node.state, goals):
            for successor in successors(current_node.state,
                                        current_node.position):
                heuristic_score = heuristic.evaluate(successor[0], goals)
                sorting_key = current_node.total_cost + successor[
                    2] + heuristic_score
                new_node = Node(successor[0], successor[1], heuristic_score,
                                successor[2],
                                current_node.total_cost + successor[2],
                                sorting_key, successor[3], sorting_key,
                                current_node)

                if (new_node not in closed
                        and not open.replace_if_smaller(new_node)):
                    open.push(new_node)
            closed += current_node
            current_node = open.pop()

        return current_node, closed
Example #2
0
def test_generate_start_state_4x2():
    state = ([[1, 2, 3, 4], [5, 6, 7, 0]], Position(3, 1))

    for i in range(10000):
        choice = random.choice(list(successors(state[0], state[1])))
        state = (choice[0], choice[1])

    print(state)
    assert True
Example #3
0
def test_successor_4x4_vertical_edge_right():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 7, 0],
        [9, 10, 11, 12],
        [13, 14, 15, 13]
    ]
    current_position = Position(3, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 3, 4],
                [5, 6, 0, 7],
                [9, 10, 11, 12],
                [13, 14, 15, 13]
            ],
            Position(2, 1),
            1,
            7
        ),
        (
            [
                [1, 2, 3, 0],
                [5, 6, 7, 4],
                [9, 10, 11, 12],
                [13, 14, 15, 13]
            ],
            Position(3, 0),
            1,
            4
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 12],
                [9, 10, 11, 0],
                [13, 14, 15, 13]
            ],
            Position(3, 2),
            1,
            12
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Example #4
0
def test_successor_4x4_horizontal_edge_down():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [13, 14, 0, 15]
    ]
    current_position = Position(2, 3)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 0]
            ],
            Position(3, 3),
            1,
            15
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 0, 14, 15]
            ],
            Position(1, 3),
            1,
            14
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 0, 12],
                [13, 14, 11, 15]
            ],
            Position(2, 2),
            1,
            11
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Example #5
0
def test_successor_4x4_vertical_edge_left():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [0, 10, 11, 12],
        [13, 14, 15, 9]
    ]
    current_position = Position(0, 2)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 3, 4],
                [0, 6, 7, 8],
                [5, 10, 11, 12],
                [13, 14, 15, 9]
            ],
            Position(0, 1),
            1,
            5
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [13, 10, 11, 12],
                [0, 14, 15, 9]
            ],
            Position(0, 3),
            1,
            13
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [10, 0, 11, 12],
                [13, 14, 15, 9]
            ],
            Position(1, 2),
            1,
            10
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Example #6
0
def test_successor_4x2_horizontal_edge_down():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 0, 7]
    ]
    current_position = Position(2, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 0, 4],
                [5, 6, 3, 7]
            ],
            Position(2, 0),
            1,
            3
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 0]
            ],
            Position(3, 1),
            1,
            7
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 0, 6, 7]
            ],
            Position(1, 1),
            1,
            6
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Example #7
0
def test_generate_start_state_10x10():
    state = (
        [
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
            [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
            [31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
            [41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
            [51, 52, 53, 54, 55, 56, 57, 58, 59, 60],
            [61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
            [71, 72, 73, 74, 75, 76, 77, 78, 79, 80],
            [81, 82, 83, 84, 85, 86, 87, 88, 89, 90],
            [91, 92, 93, 94, 95, 96, 97, 98, 99,
             0]  # this is the most unsatisfying thing i've seen all week
        ],
        Position(9, 9))

    for i in range(10000):
        choice = random.choice(list(successors(state[0], state[1])))
        state = (choice[0], choice[1])

    print(state)
    assert True
Example #8
0
    def find(state, goals, heuristic):
        start_node = Node(state, find_element(state, 0), 0, 0, 0, 0, 0, 0,
                          None)
        open = OpenList([start_node])
        closed = ClosedList()

        current_node = open.pop()
        while not is_goal(
                current_node.state,
                goals):  # TODO do we need to check if the open list is empty?
            for successor in successors(current_node.state,
                                        current_node.position):
                new_node = Node(successor[0], successor[1], 0, successor[2],
                                current_node.total_cost + successor[2], 0,
                                successor[3],
                                current_node.sorting_key + successor[2],
                                current_node)
                if (new_node not in closed
                        and not open.replace_if_smaller(new_node)):
                    open.push(new_node)
            closed += current_node
            current_node = open.pop()

        return current_node, closed
Example #9
0
def test_successors_4x2_minor_corner():
    # given
    current_state = [
        [1, 2, 3, 4],
        [0, 6, 7, 5]
    ]
    current_position = Position(0, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [0, 2, 3, 4],
                [1, 6, 7, 5]
            ],
            Position(0, 0),
            1,
            1
        ),
        (
            [
                [1, 2, 3, 4],
                [6, 0, 7, 5]
            ],
            Position(1, 1),
            1,
            6
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 0]
            ],
            Position(3, 1),
            2,
            5
        ),
        (
            [
                [1, 0, 3, 4],
                [2, 6, 7, 5]
            ],
            Position(1, 0),
            3,
            2
        ),
        (
            [
                [1, 2, 3, 0],
                [4, 6, 7, 5]
            ],
            Position(3, 0),
            3,
            4
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Example #10
0
def test_successor_4x4_minor_corner():
    # given
    current_state = [
        [1, 2, 3, 0],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [13, 14, 15, 4]
    ]
    current_position = Position(3, 0)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 0, 3],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 4]
            ],
            Position(2, 0),
            1,
            3
        ),
        (
            [
                [1, 2, 3, 8],
                [5, 6, 7, 0],
                [9, 10, 11, 12],
                [13, 14, 15, 4]
            ],
            Position(3, 1),
            1,
            8
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 0]
            ],
            Position(3, 3),
            2,
            4
        ),
        (
            [
                [0, 2, 3, 1],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 4]
            ],
            Position(0, 0),
            2,
            1
        ),
        (
            [
                [1, 2, 3, 7],
                [5, 6, 0, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 4]
            ],
            Position(2, 1),
            3,
            7
        ),
        (
            [
                [1, 2, 3, 13],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [0, 14, 15, 4]
            ],
            Position(0, 3),
            3,
            13
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Example #11
0
def test_successors_4x2_major_corner():
    # given
    current_state = [
        [4, 2, 3, 1],
        [5, 6, 7, 0]
    ]
    current_position = Position(3, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [4, 2, 3, 0],
                [5, 6, 7, 1],
            ],
            Position(3, 0),
            1,
            1
        ),
        (
            [
                [4, 2, 3, 1],
                [5, 6, 0, 7],
            ],
            Position(2, 1),
            1,
            7
        ),
        (
            [
                [4, 2, 3, 1],
                [0, 6, 7, 5],
            ],
            Position(0, 1),
            2,
            5
        ),
        (
            [
                [4, 2, 0, 1],
                [5, 6, 7, 3],
            ],
            Position(2, 0),
            3,
            3
        ),
        (
            [
                [0, 2, 3, 1],
                [5, 6, 7, 4],
            ],
            Position(0, 0),
            3,
            4
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Example #12
0
def test_successor_4x4_middle():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 0, 8],
        [9, 10, 11, 12],
        [13, 14, 15, 7]
    ]
    current_position = Position(2, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 0, 4],
                [5, 6, 3, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 7]
            ],
            Position(2, 0),
            1,
            3
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 11, 8],
                [9, 10, 0, 12],
                [13, 14, 15, 7]
            ],
            Position(2, 2),
            1,
            11
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 0, 6, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 7]
            ],
            Position(1, 1),
            1,
            6
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 8, 0],
                [9, 10, 11, 12],
                [13, 14, 15, 7]
            ],
            Position(3, 1),
            1,
            8
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors