def _load(self) -> List[GameStats]: """Load all game statistics from iterated TAC output.""" result = [] # type: List[GameStats] game_dirs = sorted(os.listdir(self.competition_directory)) for game_dir in game_dirs: game_data_json_filepath = os.path.join(self.competition_directory, game_dir, "game.json") game_data = json.load(open(game_data_json_filepath)) game = Game.from_dict(game_data) game_stats = GameStats(game) result.append(game_stats) return result
def from_datadir(datadir: str, env_name: str) -> 'ControllerDashboard': """ Return a ControllerDashboard from a data directory. :param datadir: the data directory :param env_name: the environment name :return: controller dashboard """ game_data_json_filepath = os.path.join(datadir, "game.json") print("Loading data from {}".format(game_data_json_filepath)) game_data = json.load(open(game_data_json_filepath)) game = Game.from_dict(game_data, Crypto()) # any crypto object will do here game_stats = GameStats(game) return ControllerDashboard(game_stats, env_name=env_name)
def collect_data(datadir: str, experiment_names: List[str]) -> List[GameStats]: """ Collect data of every experiment. :param datadir: path to the directory where the data of the experiments are saved. :param experiment_names: the names of the experiments :return: a list of statistics about games """ result = [] for experiment_name in experiment_names: json_experiment_data = json.load( open(os.path.join(datadir, experiment_name, "game.json"))) game_stats = GameStats.from_json(json_experiment_data) result.append(game_stats) return result
def _start_competition(self): """Create a game and send the game setting to every registered agent, and start the inactivity timeout checker.""" # assert that there is no competition running. assert not self.is_game_running() self.current_game = self._create_game() try: self.controller_agent.monitor.set_gamestats(GameStats(self.current_game)) self.controller_agent.monitor.update() except Exception as e: logger.exception(e) self._send_game_data_to_agents() # log messages logger.debug("[{}]: Started competition:\n{}".format(self.controller_agent.name, self.current_game.get_holdings_summary())) logger.debug("[{}]: Computed equilibrium:\n{}".format(self.controller_agent.name, self.current_game.get_equilibrium_summary()))
def main(name: str = "controller", nb_agents: int = 5, nb_goods: int = 5, money_endowment: int = 200, base_good_endowment: int = 2, lower_bound_factor: int = 0, upper_bound_factor: int = 0, tx_fee: float = 1.0, oef_addr: str = "127.0.0.1", oef_port: int = 10000, start_time: Union[str, datetime.datetime] = str(datetime.datetime.now() + datetime.timedelta(0, 10)), registration_timeout: int = 10, inactivity_timeout: int = 60, competition_timeout: int = 240, whitelist_file: Optional[str] = None, verbose: bool = False, dashboard: bool = False, visdom_addr: str = "localhost", visdom_port: int = 8097, data_output_dir: str = "data", experiment_id: Optional[str] = None, seed: int = 42, version: int = 1, **kwargs): """Run the controller script.""" agent = None # type: Optional[ControllerAgent] random.seed(seed) if verbose: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) monitor = VisdomMonitor( visdom_addr=visdom_addr, visdom_port=visdom_port) if dashboard else NullMonitor() try: agent = ControllerAgent(name=name, oef_addr=oef_addr, oef_port=oef_port, monitor=monitor, version=version) whitelist = set( open(whitelist_file).read().splitlines( keepends=False)) if whitelist_file else None tac_parameters = TACParameters( min_nb_agents=nb_agents, money_endowment=money_endowment, nb_goods=nb_goods, tx_fee=tx_fee, base_good_endowment=base_good_endowment, lower_bound_factor=lower_bound_factor, upper_bound_factor=upper_bound_factor, start_time=dateutil.parser.parse(start_time) if type(start_time) == str else start_time, registration_timeout=registration_timeout, competition_timeout=competition_timeout, inactivity_timeout=inactivity_timeout, whitelist=whitelist) agent.connect() agent.register() agent.wait_and_handle_competition(tac_parameters) except Exception as e: logger.exception(e) except KeyboardInterrupt: logger.debug("Controller interrupted...") finally: if agent is not None: agent.terminate() experiment_name = experiment_id if experiment_id is not None else str( datetime.datetime.now()).replace(" ", "_") agent.dump(data_output_dir, experiment_name) if agent.game_handler is not None and agent.game_handler.is_game_running( ): game_stats = GameStats(agent.game_handler.current_game) game_stats.dump(data_output_dir, experiment_name)