Exemple #1
0
def create_train_file():
    path_puzzles = './boards'
    train_set = [[
        'w', 'h', 'number_of_colors', 'percent_of_filled_cells',
        'path_start_x', 'path_start_y', 'path_end_x', 'path_end_y',
        'normalize_path', 'label'
    ]]

    puzzles = [f for f in os.listdir(path_puzzles)]

    with open('learner/train_set.csv', 'w', newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerows(train_set)

        for i, puzzle in enumerate(puzzles[:-2]):
            print(f'{i} out of {len(puzzles[:-2])}')
            xml_dict = ag.get_xml_from_path(path_puzzles + '/' + puzzle)
            train_set = convert_xml_dict_to_rows(xml_dict)

            writer.writerows(train_set)

    print('Train set is written to disk')
Exemple #2
0
    while not game.is_goal_state():
        game.do_move_csp()
        if time() - start > TIME_OUT:
            print(f'Takes more than {TIME_OUT} seconds. Abort.')
            return -1

    # End clock and print results
    end = time()
    print('Done.')

    return end - start


if __name__ == '__main__':
    for puzzle in puzzles:
        game = Game(get_xml_from_path(path_puzzles + '/' + puzzle))

        with open(path_results + '/' + puzzle[:-4] + '_ml_time.csv',
                  'w') as report_time:
            with open(path_results + '/' + puzzle[:-4] + '_ml_turns.csv',
                      'w') as report_turns:
                report_time.write(os.path.basename(puzzle))
                report_turns.write(os.path.basename(puzzle))

                for heuristic in heuristics_dict:
                    report_time.write(f',{heuristic}')
                    report_turns.write(f',{heuristic}')

                report_time.write('\n')
                report_turns.write('\n')
Exemple #3
0
def runGUI(layout):
    """
    Runs the GUI.
    :param layout:
    """
    # Create the Window
    window = sg.Window(APP_NAME, layout)
    game = None
    graph = None

    # Event Loop to process 'events' and get the 'values' of the inputs
    while True:
        event, values = window.read()

        # If user closes window, close the program
        if event == sg.WIN_CLOSED:
            break

        # If player selects file, update GUI to show file name
        if event == 'file_path':
            print('File selected:', values['file_path'])

            # Show file in GUI
            window['text_puzzle_name'](os.path.basename(values['file_path']))

            # Create game object
            xml_dict = ag.get_xml_from_path(values['file_path'])
            game = gm.Game(xml_dict)

            # Create board in GUI
            window['graph_board'].erase()
            graph = BoardGraph(window['graph_board'], game)

        # Start run the game with given parameters
        if event == 'button_run':
            start = time()
            print('button_run')

            if values['file_path'] != '':
                # Disable buttons on GUI while running the search
                toggle_gui(window, False)

                # Set game to run
                game.set_boards_generator(values['combo_search'], values['combo_variable'], values['combo_heuristic'])

                # Select search, we scarified here generality for performance, since this is the core to the search
                # and we wanted to avoid unnecessary 'if' statements each iteration
                if values['checkbox_show_animation']:
                    if values['combo_search'] == 'CSP':
                        run_paths_based_search_with_animation(window, graph, game)
                    else:
                        run_board_based_search_with_animation(window, graph, game)
                else:
                    if values['combo_search'] == 'CSP':
                        run_paths_based_search_without_animation(window, graph, game)
                    else:
                        run_board_based_search_without_animation(window, graph, game)
                end = time()
                print("Running Time ", end - start)
                window.finalize()
                window['button_reset'].update(disabled=False)

            else:
                print(FILE_NOT_SELECTED_MESSAGE)

        if event == 'button_reset':
            game.reset_game()
            graph.clear_board()

            window.finalize()
            toggle_gui(window, True)
            window['button_reset'].update(disabled=True)

        if event == 'graph_board':
            x, y = values["graph_board"]
            print(f'Clicked on x: {x}\t y: {y}')
            print(f'Figures in location {window["graph_board"].get_figures_at_location((x, y))}')

    window.close()