def get(force=False): if not os.path.exists("data/fit.p") or force: fit_b = compute.get_fit(force) else: fit_b = backup.load("data/fit.p") r_values = np.sort(np.unique(fit_b.r)) s_values = (False, True) exp_conditions = list(it.product(r_values, s_values)) data = [] scores_to_plot = score.Score.names # fit.Score.names n_dim = len(scores_to_plot) for r_value, s_value in exp_conditions: cond0 = fit_b.r == r_value cond1 = fit_b.display_opponent_score == int(s_value) cond = cond0 * cond1 n = np.sum(cond) d = np.zeros((n_dim, n)) for i, sc in enumerate(scores_to_plot): d[i] = fit_b.fit_scores[sc][cond] data.append(d) return data
def compute_means(self): """ computes means for all simulations """ for s in self.strategies.values(): file_name = "data/simulation_{}_vs_{}.p".format(*s) backups = backup.load(file_name) mean_score = np.zeros(len(backups)) mean_distance = np.zeros(len(backups)) mean_price = np.zeros(len(backups)) radius = np.zeros(len(backups)) for i, b in enumerate(backups): mean_score[i] = np.mean(b.profits[:, :]) d = np.absolute(b.positions[:, 0] - b.positions[:, 1]) / self.n_positions mean_distance[i] = np.mean(d) mean_price[i] = np.mean(b.prices[:, :]) radius[i] = b.r for r in (0.25, 0.5): self.means[s][r]["price"] = np.mean(mean_price[radius == r]) self.means[s][r]["distance"] = np.mean( mean_distance[radius == r]) self.means[s][r]["score"] = np.mean(mean_score[radius == r])
def get_fit_b_all(args): # get all strategies s = list(run_simulation.treat_args("all")) # store strategies strategies = {k: v for k, v in zip(range(len(s)), s)} reversed_strategies = {k: v for v, k in strategies.items()} file_name = "data/fit_simulation_all.p" if not os.path.exists(file_name) or args.do_it_again: backups = [] for strategy in strategies.values(): args.p0_strategy, args.p1_strategy = strategy backups.append( get_fit( force=args.force, p0_strategy=args.p0_strategy, p1_strategy=args.p1_strategy, softmax=args.softmax, reversed_strategies=reversed_strategies, ) ) backup.save(obj=backups, file_name=file_name) else: backups = backup.load(file_name=file_name) return backups, strategies
def get_fit_b_ind(args): file_name = "data/fit_simulation_{}_vs_{}.p".format(args.p0_strategy, args.p1_strategy) if not os.path.exists(file_name) or args.force: fit_b = get_fit(args.force, args.p0_strategy, args.p1_strategy, args.softmax) else: fit_b = backup.load(file_name) return fit_b
def _get_bot_mean_profits(strategy0, strategy1): backups = backup.load( file_name="data/simulation_{}_strategy_vs_{}_strategy.p".format(strategy0, strategy1) ) means = [] for r in (0.25, 0.5): profits = [] for b in backups: if b.r == r: profits.append(np.mean(b.profits[:, 0])) means.append(np.mean(profits)) return means
def get_profile_all(args): strategies = { 0: ("profit", "profit"), 1: ("competition", "competition"), } file_name = "data/round_profiler_all.p" if not os.path.exists(file_name) or args.do_it_again: backups = get_all_backup_round_profiler(args.force, strategies) backup.save(obj=backups, file_name=file_name) else: backups = backup.load(file_name=file_name) return backups, strategies
def main(args): if not args.all: run_simulation.treat_args(args.p0_strategy, args.p1_strategy) file_name = "data/simulation_{}_vs_{}.p".format(args.p0_strategy, args.p1_strategy) backups = run_simulation.backup_simulation(file_name, args) individual_plot(backups, args.p0_strategy, args.p1_strategy) else: # get all strategies s = list(run_simulation.treat_args("all")) # store strategies strategies = {k: v for k, v in zip(range(len(s)), s)} reversed_strategies = {k: v for v, k in strategies.items()} file_name = "data/simulation_all.p" if not os.path.exists(file_name) or args.force: backups = [] for strategy in strategies.values(): args.p0_strategy, args.p1_strategy = strategy file_name = "data/simulation_{}_vs_{}.p".format(args.p0_strategy, args.p1_strategy) backups += run_simulation.backup_simulation(file_name=file_name, args=args) backup.save(obj=backups, file_name=file_name) else: backups = backup.load(file_name=file_name) # Finally plot all the data all_plot(backups, strategies, reversed_strategies)
def backup_simulation(file_name, args): if not os.path.exists(file_name) or args.force: run(p0_strategy=args.p0_strategy, p1_strategy=args.p1_strategy) return backup.load(file_name=file_name)
def get_fit(force, p0_strategy, p1_strategy, softmax, reversed_strategies=None): file_name = "data/simulation_{}_vs_{}.p".format(p0_strategy, p1_strategy) if not os.path.exists(file_name) or force: run_simulation.main( p0_strategy=p0_strategy, p1_strategy=p1_strategy ) backups = backup.load(file_name) m = { 0.25: fit.Model(r=0.25), 0.50: fit.Model(r=0.5) } temp_c = [] prediction_accuracy_c = [] temp_p = [] prediction_accuracy_p = [] r = [] score = [] strategy = [] idx_strategy = [] tqdm.write("Creating fit backup file...") for b in tqdm(backups): for player in (0, 1): kwargs = { "dm_model": m[b.r], "str_method": "p_profit", "firm_id": player, "active_player_t0": b.active_player_t0, "positions": b.positions, "prices": b.prices, "t_max": b.t_max } if not softmax: # --- Profit based ---- # rm = RunModel(**kwargs) p = rm.run(temp=None) * -1 temp_p.append(-1) prediction_accuracy_p.append(p) # --- Competition based --- # kwargs["str_method"] = "p_competition" rm = RunModel(**kwargs) c = rm.run(temp=None) * -1 temp_c.append(-1) prediction_accuracy_c.append(c) else: # --- Profit based ---- # best_temp = optimize_model(**kwargs) rm = RunModel(**kwargs) p = rm.run(temp=best_temp) * -1 temp_p.append(best_temp) prediction_accuracy_p.append(p) # --- Competition based --- # kwargs["str_method"] = "p_competition" best_temp = optimize_model(**kwargs) rm = RunModel(**kwargs) c = rm.run(temp=best_temp) * -1 temp_c.append(best_temp) prediction_accuracy_c.append(c) r.append(b.r) score.append(np.sum(b.profits[:, player])) strategy.append(p0_strategy if not player else p1_strategy) if reversed_strategies: idx_strategy.append(reversed_strategies[(p0_strategy, p1_strategy)]) fit_b = BackupFit( temp_c=np.array(temp_c), temp_p=np.array(temp_p), prediction_accuracy_p=np.array(prediction_accuracy_p), prediction_accuracy_c=np.array(prediction_accuracy_c), r=np.array(r), score=np.array(score), strategy=np.array(strategy), idx_strategy=np.array(idx_strategy) ) file_name = "data/fit_simulation_{}_vs_{}.p".format(p0_strategy, p1_strategy) backup.save(fit_b, file_name) return fit_b