Example #1
0
def main():
    print("sdsdsds")
    GLogger('file', 'graph_validator_logger.txt', 'ERROR')
    Window.size = (1920, 1090)

    Utils.read_game_config_file(MAIN_CONFIG_FILE_PATH)
    Utils.read_graph_config_file(GRAPH_CONFIG_FILE)
    Utils.image_folder = path.join("..", Utils.image_folder)

    max_turns = int(Utils.game_config_data['Default']['max_turns'])
    it = itertools.product('1234', repeat=max_turns)
    number_of_successful_runs = 0
    # use line 30 to test all the graphs in SAVED_GRAPH_PATH; use line 31 to only test the graphs specified in 'graphs_names'
    # for current_graph in [item for item in listdir(SAVED_GRAPH_PATH) if item.endswith(".xml")]:
    for current_graph in graphs_names:
        curr_path = path.join(SAVED_GRAPH_PATH, current_graph)
        graph = load_graph_from_json(curr_path)
        with open("{}_saved_steps.txt".format(curr_path[:-5]), 'w') as f:
            num_of_graph_nodes = len(graph.node_list)
            f.write("The graph contains {} nodes\n".format(
                str(num_of_graph_nodes)))
            while True:
                try:
                    buttons = it.next()
                except StopIteration:
                    break
                answer, number_of_nodes_seen = run_buttons_on_graph(
                    graph, buttons)
                number_of_successful_runs += answer
                f.write("steps: {}, seen nodes: {} \n".format(
                    str(buttons), str(number_of_nodes_seen)))

            f.write("number of successful runs = {0}\n".format(
                number_of_successful_runs))
Example #2
0
    def run_q_player(self, graph_file_path, log_file_path):
        Utils.read_game_config_file(CONFIG_FILE_PATH)
        Utils.read_graph_config_file(GRAPH_CONFIG_PATH)
        Utils.image_folder = path.join("..", Utils.image_folder)

        log.setLevel(Utils.game_config_data['Default']['log_level'])
        session_length = 1000

        graph = load_py_graph(graph_file_path)
        q_matrix = QMatrix(action_space=4, max_steps=int(Utils.game_config_data['Default']['max_turns']), nodes_in_graph=len(graph.node_list))

        with open(log_file_path,'w') as f:
            f.write("episode, score\n")
            for i in range(session_length):
                dummy_screen = DummyScreen(graph)
                game = GraphTabletDisplay(dummy_screen)
                data_handler = GameDataHandler(GRAPH_CONFIG_PATH, graph.size)
                data_handler.add_view_to_db(game.get_info_from_screen())

                rw = 0
                game.press_button(self.auto_first_press + 1)
                data_handler.add_view_to_db(game.get_info_from_screen())
                q_matrix.reinit(known_nodes=len(data_handler.get_real_nodes()))
                for j in range(1, int(Utils.game_config_data['Default']['max_turns'])):
                    log.debug("doing a step {}/{}".format(j, Utils.game_config_data['Default']['max_turns']))
                    btn = q_matrix.choose_action_epsilon_greedy()
                    game.press_button(btn + 1)
                    data_handler.add_view_to_db(game.get_info_from_screen())
                    rw = q_matrix.update_matrix(num_nodes=len(data_handler.get_real_nodes()), current_step=btn)
                log.info("Q session {}:{} - reword:{}".format(i, session_length, rw))
                f.write("{},{}\n".format(i + 1, rw))
Example #3
0
    def test_trim_data(self, mock_utils):
        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        Utils.read_game_config_file(path.join("..", CONFIG_FILE_PATH))
        Utils.read_graph_config_file(path.join("..", GRAPH_CONFIG_PATH))
        data_handler = GameDataHandler(path.join("../", "graph_config.txt"),
                                       None)
        data_handler.graph.add_node(self.node_1_real.x,
                                    self.node_1_real.y,
                                    serial=self.node_1_real.serial_num)
        data_handler.graph.add_node(self.node_2_unreal.x,
                                    self.node_2_unreal.y,
                                    serial=self.node_2_unreal.serial_num)
        data_handler.graph.add_node(self.node_2_unreal_moved.x,
                                    self.node_2_unreal_moved.y,
                                    serial=self.node_2_unreal_moved.serial_num)
        data_handler.graph.add_node(self.node_3_unreal.x,
                                    self.node_3_unreal.y,
                                    serial=self.node_3_unreal.serial_num)
        data_handler.graph.add_node(self.node_3_unreal_moved.x,
                                    self.node_3_unreal_moved.y,
                                    serial=self.node_3_unreal_moved.serial_num)
        data_handler.graph.add_node(self.node_4_real.x,
                                    self.node_4_real.y,
                                    serial=self.node_4_real.serial_num)
        edge_1 = (self.node_1_real, self.node_2_unreal_moved, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_1_real,
                               edge2=self.node_2_unreal_moved))
        edge_2 = (self.node_2_unreal, self.node_3_unreal_moved, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_2_unreal,
                               edge2=self.node_3_unreal_moved))
        edge_3 = (self.node_3_unreal, self.node_4_real, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_3_unreal,
                               edge2=self.node_4_real))
        edge_4 = (self.node_1_real, self.node_4_real, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_1_real,
                               edge2=self.node_4_real))
        data_handler.extra_edges = [edge_1, edge_2, edge_3]

        data_handler.trim_data()
        self.assertEquals(len(data_handler.edges_to_add), 1)
        self.assertEquals(data_handler.edges_to_add[0][0].serial_num,
                          edge_4[0].serial_num)
        self.assertEquals(data_handler.edges_to_add[0][1].serial_num,
                          edge_4[1].serial_num)
Example #4
0
def create_rand_graph(config_file):
    """

    :param config_file:
    :return:
    """
    new_graph = GraphObject(config_file)
    config = Utils.read_game_config_file(config_file)

    for i in range(config.getint("GeneralParams", "NodeCount")):
        while True:
            xRandom = random.randint(
                config.getint("NodeData", "NodeSize"),
                config.getint("GeneralParams", "GraphSizeX") -
                config.getint("NodeData", "NodeSize"))
            yRandom = random.randint(
                config.getint("NodeData", "NodeSize"),
                config.getint("GeneralParams", "GraphSizeY") -
                config.getint("NodeData", "NodeSize"))
            if not check_collisions(xRandom, yRandom, new_graph,
                                    config.getint("NodeData", "NodeSize"),
                                    config.getint("NodeData",
                                                  "ExtraDistance")):
                break
        randColor = random.choice(Colours.values())
        new_graph.add_node(xRandom, yRandom, randColor, Shapes['circle'],
                           config.getint("NodeData", "NodeSize"))
    connect_graph(new_graph, config.getint("NodeData", "MaxNeighbors"),
                  config.getint("NodeData", "MinNeighbors"))
    return new_graph
Example #5
0

class GameType(Enum):
    VIEW_ONLY = 1
    ALLOW_PLAY = 2


def main(game_type, graph_data):
    GLogger('file', 'graph_runner_logger.txt', 'ERROR')
    if game_type == GameType.VIEW_ONLY:
        game = DisplayApp(graph_data)
    elif game_type == GameType.ALLOW_PLAY:
        button_presses = []
        # This needs to be more versatile
        test_screen = TestScreen(graph_data, button_presses, 0.2)
        test_screen.graph_config = "../GraphsData/graph_config.txt"
        game = GraphGameApp(test_screen)
    game.run()


if __name__ == "__main__":
    Utils.read_game_config_file(CONFIG_FILE_PATH)
    Utils.read_graph_config_file(GRAPH_CONFIG_PATH)
    Utils.image_folder = path.join("..", Utils.image_folder)

    # ALLOW_PLAY, VIEW_ONLY
    game_type = GameType.ALLOW_PLAY

    graph = load_graph_from_json(graph_file_path)
    main(game_type, graph)
Example #6
0
    def build(self):
        self.init_communication()

        self.config = Utils.read_game_config_file(CONFIG_FILE_PATH)
        Utils.read_graph_config_file(GRAPH_CONFIG_PATH)
        self.logger = GLogger(
            self.config['Default']['logger_output_type'],
            self.config['Default']['logger_writing_location'],
            self.config['Default']['log_level'], self.user_data_dir)
        #self.init_communication(self.config['Cloud']['server_ip'])
        graph_config_path = self.config['Default']['graph_config_path']
        self.sm = ScreenManager()

        screen = ZeroScreen()
        screen.start()
        screen.ids['subject_id'].bind(
            text=screen.ids['subject_id'].on_text_change)
        self.sm.add_widget(screen)

        screen = FinalScreen()
        self.sm.add_widget(screen)

        # # Setting up the login screen separately
        # login_screen = LoginScreen(name='LoginScreen')
        # login_screen.setup(main_app=self)
        # login_screen.add_widget(login_screen.display.layout)
        # self.sm.add_widget(login_screen)

        graph_list = self.load_graphs_from_folder()

        self.current_graph = None
        self.discovered_graph = None
        self.user_answers = []
        self.question_list = []
        self.button_presses = []
        # Enumerate over all the graphs in the folder
        for i_net, graph_data in enumerate(graph_list):
            # Step 1 - Graph Game
            self.question_list = graph_data.question_object_list
            self.game_screen.append(
                GraphGameScreen(name='game_graph_' + str(i_net)))
            self.game_screen[-1].setup(
                number=i_net,
                main_app=self,
                max_turns=int(self.config['Default']['max_turns']),
                real_user=True,
                graph=graph_data,
                graph_config=graph_config_path,
                button_presses=self.button_presses)
            self.game_screen[-1].add_widget(
                self.game_screen[-1].graph_game.layout)
            # Step 2 - Questionnaire
            #Goren - run nine graphs with question and then one without
            if i_net < number_of_graphs:
                self.game_screen.append(
                    QuestionnaireScreen(name='game_questionnaire_' +
                                        str(i_net)))
                self.game_screen[-1].setup(number=i_net,
                                           main_app=self,
                                           real_user=self.real_user)
                self.game_screen[-1].add_widget(
                    self.game_screen[-1].questionnaire.the_widget)

                # Step 3 - Results
                self.game_screen.append(
                    ResultScreen(name='game_results_' + str(i_net)))
                self.game_screen[-1].setup(number=i_net,
                                           main_app=self,
                                           real_user=True)
                self.game_screen[-1].add_widget(
                    self.game_screen[-1].result_app.the_widget)

        for gs in self.game_screen:
            self.sm.add_widget(gs)

        self.sm.current = 'zero_screen'
        return self.sm