Beispiel #1
0
def load_json_data(path):
    '''
    Loads the data from the file as Json into a new object.
    '''
    if not fp.isExist(path):
        raise ValueError(path + " does not exist.")
    with open(path) as data_file:
        result = json.load(data_file)
    return result
 def add_att_str(self, str_name):
     if not fp.isExist(osp.join(self.dir_att, str_name)):
         raise ValueError("This strategy does not exist.")
     if 'att_' not in str_name:
         raise ValueError("This may not be an attacker's strategy due to no att_ sign")
     if not isinstance(str_name, str):
         raise ValueError("The name to be added is not a str.")
     self.att_str.append(str_name)
     logger.info(str_name + " has been added to attacker's strategy set")
def gambit_analysis(timeout, in_path: str = None, out_path: str = None):
    """ Run gambit analysis on the current empirical game.

    :param timeout:
    :type timeout: int
    """
    in_path = in_path or _get_gambit_matrix_path()
    out_path = out_path or _get_gambit_nash_path()

    if not fp.isExist(in_path):
        raise ValueError(".nfg file does not exist!")
    command_str = "gambit-lcp "
    command_str += "-q {} ".format(in_path)
    command_str += "-d 8 "
    command_str += "> {}".format(out_path)
    subproc.call_and_wait_with_timeout(command_str, timeout)
def sample_strategy_from_mixed(env, str_set, mix_str, identity, str_dict=None):
    """ Sample a pure strategy from a mixed strategy.

    Note: str in str_set should include .pkl.

    :param env:
    :param str_set:
    :param mix_str:
    :param identity:
    :param str_dict:
    """
    assert env.training_flag != identity
    if not len(str_set) == len(mix_str):
        raise ValueError(
            "Length of mixed strategies does not match number of strategies.")

    mix_str = np.array(mix_str)

    if np.sum(mix_str) != 1.0:
        mix_str = mix_str / np.sum(mix_str)

    picked_str = np.random.choice(str_set, p=mix_str)
    # TODO: modification for fast sampling.
    if str_dict is not None:
        return str_dict[picked_str], picked_str

    if not fp.isInName('.pkl', name=picked_str):
        raise ValueError('The strategy picked is not a pickle file.')

    if identity == 0:  # pick a defender's strategy
        path = settings.get_defender_strategy_dir()
    elif identity == 1:
        path = settings.get_attacker_strategy_dir()
    else:
        raise ValueError("identity is neither 0 or 1!")

    if not fp.isExist(osp.join(path, picked_str)):
        raise ValueError('The strategy picked does not exist!')

    if "epoch1.pkl" in picked_str:
        act = fp.load_pkl(osp.join(path, picked_str))
        return act, picked_str

    act = torch.load(osp.join(path, picked_str))
    return act, picked_str
def decode_gambit_file(path: str = None):
    path = path or _get_gambit_nash_path()

    if not fp.isExist(path):
        raise ValueError("nash.txt file does not exist!")
    with open(path, 'r') as f:
        nash = f.readline()
        if len(nash.strip()) == 0:
            return 0, 0

    nash = nash[3:]
    nash = nash.split(',')
    new_nash = []
    for i in range(len(nash)):
        new_nash.append(convert(nash[i]))

    new_nash = np.array(new_nash)
    new_nash = np.round(new_nash, decimals=8)
    nash_def = new_nash[:int(len(new_nash) / 2)]
    nash_att = new_nash[int(len(new_nash) / 2):]

    return nash_att, nash_def
Beispiel #6
0
def initialize(load_env=None, env_name=None, n_processes: int = 1):
    logger.info("=======================================================")
    logger.info("=======Begin Initialization and first epoch============")
    logger.info("=======================================================")

    # Create Environment
    if isinstance(load_env, str):
        path = osp.join(settings.get_env_data_dir(), "{}.pkl".format(load_env))
        if not fp.isExist(path):
            raise ValueError("The env being loaded does not exist.")
        env = fp.load_pkl(path)
    else:
        # env is created and saved.
        env = dag.env_rand_gen_and_save(env_name)

    # save graph copy
    env.save_graph_copy()
    env.save_mask_copy()  # TODO: change transfer

    # create players and point to their env
    env.create_players()
    env.create_action_space()

    # print root node
    roots = env.get_Roots()
    logger.info(f"Root Nodes: {roots}")
    ed = env.get_ORedges()
    logger.info(f"Or edges: {ed}")

    # initialize game data
    game = empirical_game.EmpiricalGame(env)
    game.env.defender.set_env_belong_to(game.env)
    game.env.attacker.set_env_belong_to(game.env)

    # make no sense
    env.defender.set_env_belong_to(env)
    env.attacker.set_env_belong_to(env)

    # uniform strategy has been produced ahead of time
    logger.info("Epoch 1")
    epoch = 1
    epoch_dir = osp.join(settings.get_results_dir(), f"epoch_{epoch}")
    writer = SummaryWriter(logdir=epoch_dir)

    act_att = 'att_str_epoch1.pkl'
    act_def = 'def_str_epoch1.pkl'

    game.add_att_str(act_att)
    game.add_def_str(act_def)

    logger.info('Begin simulation for uniform strategy.')
    aReward, dReward = simulation.simulate_profile(
        env=game.env,
        game=game,
        nn_att=act_att,
        nn_def=act_def,
        n_episodes=game.num_episodes,
        n_processes=n_processes,
        save_dir=epoch_dir,
        summary_writer=writer)
    logger.info('Done simulation for uniform strategy.')

    game.init_payoffmatrix(dReward, aReward)
    ne = {}
    ne[0] = np.array([1], dtype=np.float32)
    ne[1] = np.array([1], dtype=np.float32)
    game.add_nasheq(epoch, ne)

    # save a copy of game data
    game_path = osp.join(settings.get_run_dir(), "game.pkl")
    fp.save_pkl(game, game_path)

    sys.stdout.flush()
    return game