Beispiel #1
0
def run(p0_strategy, p1_strategy):

    n_simulation = 30

    data = []

    tqdm.write("\n********* {} VS {} *************".format(
        p0_strategy, p1_strategy))

    for r in (0.25, 0.5):

        tqdm.write("Computing {} radius simulations.".format(r))

        for _ in tqdm(range(n_simulation)):

            m = __old__.model.simulation.Model(r=r,
                                               p0_strategy=p0_strategy,
                                               p1_strategy=p1_strategy)
            results = m.run()
            b = BackupSimulation(*results, r)
            data.append(b)

    tqdm.write("*********************************")

    file_name = "data/simulation_{}_vs_{}.p".format(p0_strategy, p1_strategy)

    backup.save(obj=data, file_name=file_name)
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_all_backup_round_profiler(force, strategies):

    backups = backup.get_data(force)

    backups = [b for b in backups if b.pvp]

    profiler = RoundProfiler(strategies=strategies, const=200, force=force)

    profiler_backup = BackupRoundProfiler(size_player=len(backups * 2),
                                          size_round=len(backups),
                                          strategies=strategies)

    tqdm.write("Profiling rounds...")

    with tqdm(total=len(backups * 2)) as pbar:

        i = 0

        for rd_idx, b in enumerate(backups):

            for player in (0, 1):

                # Register information
                profiler_backup.score[i] = np.sum(b.profits[:, player])
                profiler_backup.user_id[i] = b.user_id[player]
                profiler_backup.room_id[i] = b.room_id
                profiler_backup.firm_id[i] = player

                i += 1

                pbar.update()

            # Save round's radius
            profiler_backup.round_id[rd_idx] = b.round_id
            profiler_backup.r[rd_idx] = b.r
            profiler_backup.display_opponent_score[
                rd_idx] = b.display_opponent_score

            # Prepare means to compare
            means = {
                "price":
                np.mean(b.prices[:, :]),
                "score":
                np.mean(b.profits[:, :]),
                "distance":
                np.mean(
                    np.absolute(b.positions[:, 0] - b.positions[:, 1]) / 21)
            }

            # Compare means with each strategies means
            for k, v in strategies.items():
                profiler_backup.fit_scores[rd_idx,
                                           k] = profiler.score(r=b.r,
                                                               means=means,
                                                               strategy=v)

    backup.save(profiler_backup, "data/round_profiler_all.p")

    return profiler_backup
Beispiel #4
0
def get_fit(force):

    backups = backup.get_data(force)

    backups = [b for b in backups if b.pvp]

    m = {
        0.25: score.Score(r=0.25),
        0.50: score.Score(r=0.5)
    }

    fit_backup = BackupFit(size=len(backups*2))

    with tqdm(total=len(backups*2)) as pbar:

        i = 0

        for b in backups:

            for player in (0, 1):

                # Register information
                fit_backup.display_opponent_score[i] = b.display_opponent_score
                fit_backup.r[i] = b.r
                fit_backup.score[i] = np.sum(b.profits[:, player])
                fit_backup.user_id[i] = b.user_id[player]
                fit_backup.room_id[i] = b.room_id
                fit_backup.round_id[i] = b.round_id
                fit_backup.firm_id[i] = player

                # Compute score
                kwargs = {
                    "dm_model": m[b.r],
                    "firm_id": player,
                    "active_player_t0": b.active_player_t0,
                    "positions": b.positions,
                    "prices": b.prices,
                    "t_max": b.t_max
                }

                for str_method in score.Score.names:

                    rm = RunModel(**kwargs, str_method=str_method)
                    sc = rm.run()
                    fit_backup.fit_scores[str_method][i] = sc

                    tqdm.write("[id={}, r={:.2f}, s={}] [{}] score: {:.2f}".format(
                        i, b.r, int(b.display_opponent_score), str_method, sc))

                i += 1
                pbar.update(1)

                tqdm.write("\n")

    backup.save(fit_backup, "data/fit.p")

    return fit_backup
Beispiel #5
0
def save_filtered_data():

    backups = backup.load_data_from_db()

    means = {
        k: v for k, v in zip((0.25, 0.5), _get_bot_mean_profits("random", "profit"))
    }

    filtered_backups = []

    for b in backups:
        for player_id in (0, 1):
            cond = np.mean(b.profits[:, player_id]) > means[b.r]
            if cond:
                filtered_backups.append(b)

    backup.save(obj=filtered_backups, file_name="data/filtered_data.p")
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 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