def setup(self, graph, graph_config, main_app, max_turns, button_presses, number=-1, button_ratio=0.2, real_user=True): """ Does a single run of a game - 3 stages: Graph learning - the main game section. Questionnaire - A list of questions about the graph Results - The result screen and summary of the data. :param graph_config: :param real_user: Bool. False if machine player :param number: The game number :param main_app: The main who called this screen :param max_turns: Number of steps for the game :param graph: The graph used in this specific game """ # Init self.game_number = number self.main_app = main_app self.real_user = real_user self.score = 0 self.graph = graph self.max_turns = max_turns main_app.discovered_graph = None self.graph_config = graph_config self.button_presses = button_presses self.button_ratio = button_ratio self.graph_game = GraphTabletDisplay(self)
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))
def run_buttons_on_graph(graph, buttons): #log = logging.getLogger() #log.setLevel(Utils.game_config_data['Default']['log_level']) dummy_screen = DummyScreen(graph) game = GraphTabletDisplay(dummy_screen) #game.run() data_handler = GameDataHandler(GRAPH_CONFIG_FILE, graph.size) data_handler.add_view_to_db(game.get_info_from_screen()) for i in range(int(Utils.game_config_data['Default']['max_turns'])): #log.debug("doing a step {}/{}".format(i, Utils.game_config_data['Default']['max_turns'])) game.press_button(int(buttons[i])) data_handler.add_view_to_db(game.get_info_from_screen()) # print ("known nodes-"+str(data_handler.get_number_of_known_nodes())+"\n") answer = (data_handler.get_number_of_known_nodes() == len(graph.node_list)) return answer, data_handler.get_number_of_known_nodes()
class GraphGameScreen(Screen): real_user = True game_number = -1 main_app = None score = 0 max_turns = -1 graph = None graph_config = None button_presses = None graph_game = None event = None def setup(self, graph, graph_config, main_app, max_turns, button_presses, number=-1, button_ratio=0.2, real_user=True): """ Does a single run of a game - 3 stages: Graph learning - the main game section. Questionnaire - A list of questions about the graph Results - The result screen and summary of the data. :param graph_config: :param real_user: Bool. False if machine player :param number: The game number :param main_app: The main who called this screen :param max_turns: Number of steps for the game :param graph: The graph used in this specific game """ # Init self.game_number = number self.main_app = main_app self.real_user = real_user self.score = 0 self.graph = graph self.max_turns = max_turns main_app.discovered_graph = None self.graph_config = graph_config self.button_presses = button_presses self.button_ratio = button_ratio self.graph_game = GraphTabletDisplay(self) def on_enter(self, *args): log_str = 'start,' log_str += 'turns=' + str(self.graph_game.max_turns) + ',' GLogger.log(logging.INFO, "", action=LogAction.data, obj='game_graph_' + str(self.game_number), comment=log_str) self.main_app.current_graph = self.graph self.button_presses = [] self.graph_game.load() self.explanation() if self.game_number == 0: # first game is also on timer self.event = Clock.schedule_once(self.next_game, FIRST_GAME_DURATION) def explanation(self): if self.game_number == 1: SoundLoader.load('Sounds/6.wav').play() if self.game_number > 8: pass #Goren - add explantion to the last graph #SoundLoader.load('Sounds/6.wav').play() def end_graph(self): KL.log.insert(action=LogAction.data, comment='end graph') self.graph_game.the_end = True if not self.graph_game.is_playing: self.main_app.discovered_graph = self.graph_game.current_data_handler.cleaned_graph( ) self.main_app.current_graph = self.graph self.log_graphs() self.next_game() def log_graphs(self): graph_number = self.main_app.sm.screen_names.index( self.main_app.sm.current) game_number = self.main_app.sm.screens[graph_number].game_number graph_data = save_graph_json(self.main_app.discovered_graph) KL.log.insert(action=LogAction.data, obj="game_%d_graph_%d_discovered" % (game_number, graph_number), comment=json.dumps(graph_data), sync=True) graph_data = save_graph_json(self.main_app.current_graph) KL.log.insert(action=LogAction.data, obj="game_%d_graph_%d_current" % (game_number, graph_number), comment=json.dumps(graph_data), sync=True) def next_game(self, *args): if self.game_number == 0: self.event.cancel() log_str = 'end game' GLogger.log(logging.INFO, "", action=LogAction.data, obj='game_graph_' + str(self.game_number), comment=log_str) try: self.main_app.sm.current = 'game_questionnaire_' + str( self.game_number) except Exception as e: print("except Exception as e\n") GLogger.log(logging.INFO, "", action=LogAction.data, obj='game_graph_', comment='the_end - {}'.format(e), sync=True) self.graph_game.is_playing = True self.end_subject() def end_subject(self): #Goren #added exiting here, the shutting down is problematic - should we do something like exit(1)? KL.log.insert(action=LogAction.data, comment='end game') self.main_app.stop()