Exemplo n.º 1
0
def run(file, heuristic=0, display_mode=False):
    astar = None

    # Teardown:
    if astar:
        astar.reset()
        board = None
        node = None
        winner_node = None

    # Reading file:
    input = read_file_to_string(os.path.join(map_folder, file))

    # Instantiating initial board and node:
    board = Board(input)
    agenda = Agenda()
    node = RushHourNode(board, parent=None, heuristic=heuristic)
    node.setF()
    node.weight = 0
    agenda.enqueue(node)

    # Running A*:
    astar = AStarCore(
        agenda,
        displaymode=display_mode,
        gui=RushHourGUI("Running best first search with displaymode."))
    winner_node = astar.best_first_search()

    # Printing stats:
    print_stats(file, heuristics[heuristic], winner_node, astar.time_used(),
                astar.nodes_analyzed(), astar.total_nodes())
    record_best(file, heuristics[heuristic], winner_node.g, astar.time_used(),
                astar.nodes_analyzed(), astar.total_nodes())

    return winner_node
 def setUpClass(cls):
     cls.all_boards = [
         read_file_to_string(os.path.join(file_folder, file))
         for file in files
     ][:3]
     cls.puzzles = [Puzzle(b) for b in cls.all_boards]
Exemplo n.º 3
0
    "nono-cat.txt",
    "nono-chick.txt",
    "nono-clover.txt",
    "nono-elephant.txt",
    "nono-fox.txt",
    "nono-rabbit.txt",
    "nono-sailboat.txt",
    "nono-snail2.txt",
    "nono-telephone.txt",
    "nono-reindeer.txt",
 ]

if __name__ == '__main__':
    for file in files[-6:-5]:
        print("\nCalculating " + file)
        _initial_state = State(read_file_to_string(os.path.join(file_folder, file)))

        use_gui = False
        display_mode = False
        if len(sys.argv) == 3:
            use_gui = len(sys.argv) > 1 and sys.argv[1] == "-gui"
            display_mode = sys.argv[2] == "-displaymode"
        elif len(sys.argv) == 2:
            use_gui = len(sys.argv) > 1 and sys.argv[1] == "-gui"
            if not use_gui: file = sys.argv[1]


        # INITIALIZING A*
        agenda = Agenda()
        astar = AStarCore(agenda, displaymode=display_mode, gui=NonogramGUI("Nonogram: " + file))
        agenda.enqueue( GACNode(None, _initial_state) )
Exemplo n.º 4
0
                    for constraint in state.constraints:
                        if y == constraint.y:
                            neightbours += [(col, constraint,
                                             state.rows[constraint.x])]
            return neightbours

        while queue:
            focal, constraint, other = queue.pop(0)
            if revise(focal, constraint, other):
                if not focal.domain: return False
                queue += create_neighbours(focal)

    queue = initialize(state)

    domain_filter(queue)
    # if sum( [len(v.domain)-1 for v in state.rows + state.cols] ) != 0:
    #     domain_filter(queue)

    return state


if __name__ == '__main__':
    _initial_state = State(
        read_file_to_string(
            os.path.join(
                "/Users/MagnusPoppe/Google Drive/Utvikling/appsPython/AI_project_1/nonogram/boards",
                "nono-reindeer.txt")))

    result = GAC(_initial_state)
    if sum([len(v.domain) - 1 for v in result.rows + result.cols]) == 0:
        print("SOLVED!")
Exemplo n.º 5
0
    "nono-cat.txt",
    "nono-chick.txt",
    "nono-clover.txt",
    "nono-elephant.txt",
    "nono-fox.txt",
    "nono-rabbit.txt",
    "nono-reindeer.txt",
    "nono-sailboat.txt",
    "nono-snail2.txt",
    "nono-telephone.txt"
 ]

if __name__ == '__main__':

    file = files[0]
    input = read_file_to_string(os.path.join(file_folder, file))

    puzzle = Puzzle(input)

    print("ROWS:")
    for row in puzzle.rows:
        text = ""
        for i in range(len(row)):
            text += str(row[i])
        print(str(row)+" = " + text)

    print("\nCOLUMNS:")
    for row in puzzle.columns:
        text = ""
        for i in range(len(row)):
            text += str(row[i]) + "  -  "
Exemplo n.º 6
0
 def setUpClass(cls):
     cls.all_boards = [
         read_file_to_string(os.path.join(file_folder, file))
         for file in files
     ][:3]
     cls.example_puzzle = Datastructure(cls.all_boards[0])