def sample_strategy_from_mixed(env, str_set, mix_str, identity): if not isinstance(mix_str,np.ndarray): raise ValueError("mix_str in sample func is not a numpy array.") if not len(str_set) == len(mix_str): raise ValueError("Length of mixed strategies does not match number of strategies.") picked_str = np.random.choice(str_set,p=mix_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 = DIR + 'defender_strategies/' elif identity == 1: path = DIR + 'attacker_strategies/' else: raise ValueError("identity is neither 0 or 1!") if not fp.isExist(path + picked_str): raise ValueError('The strategy picked does not exist!') #TODO: assign nn info from game act = deepq.learn( env, network=models.mlp(num_hidden=256, num_layers=1), total_timesteps=0, load_path= path + picked_str ) return act
def gambit_analysis(timeout): if not fp.isExist(gambit_DIR): raise ValueError(".nfg file does not exist!") command_str = "gambit-lcp -q " + os.getcwd( ) + "/gambit_data/payoffmatrix.nfg > " + os.getcwd( ) + "/gambit_data/nash.txt" subproc.call_and_wait_with_timeout(command_str, timeout)
def load_json_data(json_file): ''' Loads the data from the file as Json into a new object. ''' if not fp.isExist(DIR_json + json_file): raise ValueError(DIR_json + json_file + " does not exist.") with open(DIR_json + json_file) as data_file: result = json.load(data_file) return result
def add_def_str(self, str_name): if not fp.isExist(self.dir_def + str_name): raise ValueError("This strategy does not exist.") if '_def' not in str_name: raise ValueError("This may not be a defender's strategy due to no def sign") if not isinstance(str_name,str): raise ValueError("The name to be added is not a str." ) self.def_str.append(str_name) print(str_name + " has been added to defender's strategy set")
def sample_both_strategies(env, att_str_set, att_mix_str, def_str_set, def_mix_str): if not len(att_str_set) == len(att_mix_str): raise ValueError("Length of mixed strategies does not match number of strategies for the attacker.") if not len(def_str_set) == len(def_mix_str): raise ValueError("Length of mixed strategies does not match number of strategies for the defender.") att_picked_str = np.random.choice(att_str_set, p=att_mix_str) def_picked_str = np.random.choice(def_str_set, p=def_mix_str) if not fp.isInName('.pkl', name=def_picked_str): raise ValueError('The strategy picked is not a pickle file for the defender.') if not fp.isInName('.pkl', name=att_picked_str): raise ValueError('The strategy picked is not a pickle file for the attacker.') path_def = DIR + 'defender_strategies/' path_att = DIR + 'attacker_strategies/' if not fp.isExist(path_def + def_picked_str): raise ValueError('The strategy picked does not exist for the defender!') if not fp.isExist(path_att + att_picked_str): raise ValueError('The strategy picked does not exist for the attacker!') act_att = deepq.learn( env, network=models.mlp(num_hidden=256, num_layers=1), total_timesteps=0, load_path=path_att + att_picked_str ) act_def = deepq.learn( env, network=models.mlp(num_hidden=256, num_layers=1), total_timesteps=0, load_path= path_def + def_picked_str ) return act_att, act_def
def decode_gambit_file(): nash_DIR = os.getcwd() + '/gambit_data/nash.txt' if not fp.isExist(nash_DIR): raise ValueError("nash.txt file does not exist!") with open(nash_DIR, '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=2) nash_def = new_nash[:int(len(new_nash) / 2)] nash_att = new_nash[int(len(new_nash) / 2):] return nash_att, nash_def