Exemplo n.º 1
0
def iterated_dfs(*args):
    puzzle = args[0]
    reached = puzzle.is_sorted()
    bottom = 0
    expanded_nodes = 0

    while not reached:
        bottom += 1
        level = 1
        nodes_stack = [(deepcopy(puzzle), level, '')]

        while nodes_stack != [] and not reached:
            visited_states = []
            last_one = nodes_stack.pop()
            node, node_level, previous_move = last_one[0], last_one[
                1], last_one[2]
            reached = node.is_sorted()

            if not reached and node_level < bottom:
                expanded_nodes += 1

                for move in node.possible_moves:

                    if not check_opposites(previous_move, move):
                        node_to_add = deepcopy(node)
                        node_to_add.swap(move)

                        if node_to_add not in visited_states:
                            nodes_stack.append(
                                (node_to_add, node_level + 1, move))
                            visited_states.append(node_to_add)

    return expanded_nodes
Exemplo n.º 2
0
def iterated_dfs(*args):
    puzzle = args[0]
    reached = puzzle.is_sorted()
    bottom = 0
    expanded_nodes = 0
    
    while not reached:
        bottom += 1
        level = 1
        nodes_stack = [(deepcopy(puzzle), level, '')]
        
        while nodes_stack != [] and not reached:
            visited_states = []
            last_one = nodes_stack.pop()
            node, node_level, previous_move = last_one[0], last_one[1], last_one[2]
            reached = node.is_sorted()
            
            if not reached and node_level < bottom:
                expanded_nodes += 1
                
                for move in node.possible_moves:
                    
                    if not check_opposites(previous_move, move):
                        node_to_add = deepcopy(node)
                        node_to_add.swap(move)
                        
                        if node_to_add not in visited_states:
                            nodes_stack.append((node_to_add, node_level+1, move))
                            visited_states.append(node_to_add)
        
    return expanded_nodes
Exemplo n.º 3
0
def a_star(*args):
    puzzle = args[0]
    heuristics = args[1]
    heuristic = args[2]
    copied_puzzle = deepcopy(puzzle)
    open_list = [[copied_puzzle, 0, 0, 0, None,
                  '']]  # state, F, G, H, parent, last move
    closed_list = []
    reached = copied_puzzle.is_sorted()
    expanded_nodes = 0

    while (not reached and open_list != []):
        index_to_find_min = 1
        last_node = find_min_by_pos(index_to_find_min, open_list)
        open_list.remove(last_node)
        closed_list.append(last_node)
        node = last_node[0]
        previous_move = last_node[5]
        actual_g_cost = last_node[2]
        reached = node.is_sorted()

        if not reached:
            expanded_nodes += 1

            for move in node.possible_moves:

                if not check_opposites(previous_move, move):
                    node_to_add = deepcopy(node)
                    node_to_add.swap(move)
                    index_to_is_in = 0
                    closed_index = is_in(node_to_add, closed_list,
                                         index_to_is_in)

                    if closed_index == -1:
                        g = actual_g_cost + 1
                        h = heuristics[heuristic](node_to_add)
                        f = g + h
                        open_index = is_in(node_to_add, open_list,
                                           index_to_is_in)

                        if open_index == -1:
                            open_list.append(
                                [node_to_add, f, g, h, node, move])

                        else:

                            if g < open_list[open_index][2]:
                                open_list[open_index][1] = f
                                open_list[open_index][2] = g
                                open_list[open_index][3] = h
                                open_list[open_index][4] = node
                                open_list[open_index][5] = move

    return expanded_nodes
Exemplo n.º 4
0
def a_star(*args):
    puzzle = args[0]
    heuristics = args[1]
    heuristic = args[2]
    copied_puzzle = deepcopy(puzzle)
    open_list = [[copied_puzzle, 0, 0, 0, None, '']] # state, F, G, H, parent, last move
    closed_list = []
    reached = copied_puzzle.is_sorted()
    expanded_nodes = 0
    
    while (not reached and open_list != []):
        index_to_find_min = 1
        last_node = find_min_by_pos(index_to_find_min, open_list)
        open_list.remove(last_node)
        closed_list.append(last_node)
        node = last_node[0]
        previous_move = last_node[5]
        actual_g_cost = last_node[2]
        reached = node.is_sorted()
        
        if not reached:
            expanded_nodes += 1
            
            for move in node.possible_moves:
                
                if not check_opposites(previous_move, move):
                    node_to_add = deepcopy(node)
                    node_to_add.swap(move)
                    index_to_is_in = 0
                    closed_index = is_in(node_to_add, closed_list, index_to_is_in)
                    
                    if closed_index == -1:
                        g = actual_g_cost+1
                        h = heuristics[heuristic](node_to_add)
                        f = g+h
                        open_index = is_in(node_to_add, open_list, index_to_is_in)
                        
                        if open_index == -1:
                            open_list.append([node_to_add, f, g, h, node, move])
                        
                        else:
                            
                            if g < open_list[open_index][2]:
                                open_list[open_index][1] = f
                                open_list[open_index][2] = g
                                open_list[open_index][3] = h
                                open_list[open_index][4] = node
                                open_list[open_index][5] = move
        
    return expanded_nodes
Exemplo n.º 5
0
 def __init__(self, length):
     """
     Creates an ordered puzzle of dimension lengthXlength with length > 1
     :param length: Int the size of the edge of the puzzle 
     """
     
     if (length <= 1 or length.__class__ != int):
         
         try:
             raise ValueError('length must be an integer greater than 1')
         
         except:
             print('something went wrong!')
             raise
     
     self.__length = length
     self.__possibilities = (True, False, True, False) # up, down, left, right
     self.__blank_box = (length-1, length-1)
     self.__board = [[i*length+j+1 for j in range(length)]
             for i in range(length)]
     self.__board[length-1][length-1] = None
     self.__sorted = deepcopy(self.__board)
     self.__previous_move = ''
from a_star import a_star
import helpers

puzzle_length = 4
number_of_puzzles = 30
puzzle = Puzzle(puzzle_length)
puzzles = {10: [], 15: [], 20: [], 25: []}

#disorders the puzzle
for key in puzzles:

    for i in range(number_of_puzzles):
        puzzles[key].append(helpers.disorder_puzzle(key, puzzle))

# assigns the same disorded puzzles to the lists to be ran for the respective algorithm
idfs_puzzles = helpers.deepcopy(puzzles)
h1_puzzles = helpers.deepcopy(puzzles)
h2_puzzles = helpers.deepcopy(puzzles)

# gets the results
heuristics = {'h1': helpers.count_misplaceds, 'h2': helpers.calc_manhattan}
idfs_puzzles.pop(
    25
)  # since IDFS algorithm is too slow, it won't calculate for 25 moves puzzles
idfs_results = helpers.run_algorithm(idfs_puzzles, algorithm=i_dfs)
h1_results = helpers.run_algorithm(h1_puzzles,
                                   heuristics,
                                   'h1',
                                   algorithm=a_star)
h2_results = helpers.run_algorithm(h1_puzzles,
                                   heuristics,
Exemplo n.º 7
0
 def board(self, ):
     return deepcopy(self.__board)
Exemplo n.º 8
0
import helpers

puzzle_length = 4
number_of_puzzles = 30
puzzle = Puzzle(puzzle_length)
puzzles = {10: [], 15: [], 20: [], 25: []}

#disorders the puzzle
for key in puzzles:
    
    for i in range(number_of_puzzles):
        puzzles[key].append(helpers.disorder_puzzle(key, puzzle))


# assigns the same disorded puzzles to the lists to be ran for the respective algorithm
idfs_puzzles = helpers.deepcopy(puzzles)
h1_puzzles = helpers.deepcopy(puzzles)
h2_puzzles = helpers.deepcopy(puzzles)

# gets the results
heuristics={'h1': helpers.count_misplaceds, 'h2':helpers.calc_manhattan}
idfs_puzzles.pop(25) # since IDFS algorithm is too slow, it won't calculate for 25 moves puzzles
idfs_results = helpers.run_algorithm(idfs_puzzles, algorithm = i_dfs)
h1_results = helpers.run_algorithm(h1_puzzles, heuristics, 'h1', algorithm = a_star)
h2_results = helpers.run_algorithm(h1_puzzles, heuristics, 'h2', algorithm = a_star)

#compute results
idfs_computes = helpers.computes_results(idfs_results)
h1_computes = helpers.computes_results(h1_results)
h2_computes = helpers.computes_results(h2_results)