def iteration_stats_csv(res_dir, defender_budget, attacker_budgets, main_game_iteration_portion, min_iterations, max_iterations, jump, num_assets, step_order_size, max_order_num, num_exp=10): iterations_num = min_iterations regrets = [] iteration_regrets = {} for i in range(0, num_exp): while iterations_num < max_iterations: if iterations_num not in iteration_regrets: iteration_regrets[iterations_num] = {'split': 0, 'vanilla': 0} dirname, network = gen_new_network(num_assets) split_cfr_exp = get_split_cfr_exploitabilty( defender_budget, attacker_budgets, network, step_order_size, max_order_num, iterations_num, main_game_iteration_portion) vanilla_cfr_exp = get_vanilla_cfr_exp(defender_budget, attacker_budgets, network, step_order_size, max_order_num, iterations_num) iteration_regrets[iterations_num]['split'] += split_cfr_exp iteration_regrets[iterations_num]['vanilla'] += vanilla_cfr_exp iterations_num += jump for it_num in iteration_regrets.keys(): regrets.append({ 'num_iterations': it_num, 'split_cfr_exp': iteration_regrets[it_num]['split'] / num_exp, 'vanilla_cfr_exp': iteration_regrets[it_num]['vanilla'] / num_exp }) with open(res_dir + 'exploitability_' + str(num_assets) + '.csv', 'w', newline='') as csvfile: writer = csv.DictWriter( csvfile, fieldnames=['num_iterations', 'vanilla_cfr_exp', 'split_cfr_exp']) writer.writeheader() for row in regrets: writer.writerow(row)
def iteration_portion_exp_csv(res_dir, defender_budget, attacker_budgets, min_portion, max_portion, jump, num_assets, iterations_num, step_order_size, max_order_num, num_exp=10): portion = min_portion regrets = [] iteration_regrets = {} for i in range(0, num_exp): while portion < max_portion: if portion not in regrets: iteration_regrets[portion] = 0 dirname, network = gen_new_network(num_assets) split_cfr_regret = get_split_cfr_regret(defender_budget, attacker_budgets, network, step_order_size, max_order_num, iterations_num, portion) iterations_num += jump iteration_regrets[portion] += split_cfr_regret for it_portion in iteration_regrets.keys(): regrets.append({ 'iteration_portion': it_portion, 'regret': iteration_regrets[it_portion] / num_exp }) with open(res_dir + 'iteration_portion.csv', 'w') as csvfile: writer = csv.DictWriter( csvfile, fieldnames=['num_iterations', 'vanilla_cfr', 'split_cfr']) writer.writeheader() for row in regrets: writer.writerow(row)
def count_game_nodes_csv(res_dir, defender_budget, attacker_budgets, max_num_assets, num_exp=10): results = [] for i in range(1, max_num_assets + 1): print('num assets = ' + str(i)) dirname, network = gen_new_network(i) network.limit_trade_step = True step_order_size = SysConfig.get("STEP_ORDER_SIZE") max_order_num = 1 print(str(datetime.now()) + ': split') split = count_portfolios_nodes(defender_budget=defender_budget, attacker_budgets=attacker_budgets, network=network, step_order_size=step_order_size, max_order_num=max_order_num) print(str(datetime.now()) + ': vanilla') vanilla = count_ppa_nods(defender_budget=defender_budget, attacker_budgets=attacker_budgets, network=network, step_order_size=step_order_size, max_order_num=max_order_num) results.append({ 'num_assets': str(i), 'vanilla_cfr': vanilla, 'split_cfr': split }) with open(res_dir + 'count_game_nodes_ppa.csv', 'w', newline='') as csvfile: writer = csv.DictWriter( csvfile, fieldnames=['num_assets', 'vanilla_cfr', 'split_cfr']) writer.writeheader() for row in results: writer.writerow(row)
def iteration_stats_assets_csv(res_dir, defender_budget, attacker_budgets, main_game_iteration_portion, min_iterations, max_iterations, jump, num_assets_range, step_order_size, max_order_num, num_exp=10): regrets = [] iteration_regrets = {} iterations_num = min_iterations while iterations_num < max_iterations: iteration_regrets[iterations_num] = {} for num_assets in num_assets_range: iteration_regrets[iterations_num].update({ 'split_' + str(num_assets): 0, 'vanilla_' + str(num_assets): 0 }) iterations_num += jump for num_assets in num_assets_range: iterations_num = min_iterations while iterations_num <= max_iterations: for i in range(0, num_exp): dirname, network = gen_new_network(num_assets) split_cfr_exp = get_split_cfr_exploitabilty( defender_budget, attacker_budgets, network, step_order_size, max_order_num, iterations_num, main_game_iteration_portion) vanilla_cfr_exp = get_vanilla_cfr_exp(defender_budget, attacker_budgets, network, step_order_size, max_order_num, iterations_num) iteration_regrets[iterations_num][ 'split_' + str(num_assets)] += split_cfr_exp iteration_regrets[iterations_num][ 'vanilla_' + str(num_assets)] += vanilla_cfr_exp iterations_num += jump for it_num in iteration_regrets.keys(): row = {'num_iterations': it_num} for num_assets in num_assets_range: row.update({ 'split_cfr_exp_' + str(num_assets): iteration_regrets[it_num]['split_' + str(num_assets)] / num_exp, 'vanilla_cfr_exp_' + str(num_assets): iteration_regrets[it_num]['vanilla_' + str(num_assets)] / num_exp }) regrets.append(row) fieldnames = ['num_iterations'] for num_assets in num_assets_range: fieldnames.append('vanilla_cfr_exp_' + str(num_assets)) fieldnames.append('split_cfr_exp_' + str(num_assets)) with open(res_dir + 'exploitability.csv', 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for row in regrets: writer.writerow(row)