示例#1
0
def mixed_strategy_try_out(self, points, iterations):
    best_found = None
    best_found_maxmin = None

    for ite in np.arange(0, iterations):
        if ite % 100 == 0:
            print("Currently at iteration:", ite)
        p1_1 = random_strategy_draw(points, 2)
        p1_2 = random_strategy_draw(points, 2)

        p2_1 = random_strategy_draw(1, 2)
        p2_2 = random_strategy_draw(1, 2)

        x = np.zeros((points, 8))

        c = 0

        for i in np.arange(0, 2):
            for j in np.arange(0, 2):
                x[:, c] = np.multiply(p1_1[:, i], p2_1[:, j])
                c += 1

        for i in np.arange(0, 2):
            for j in np.arange(0, 2):
                x[:, c] = np.multiply(p1_2[:, i], p2_2[:, j])
                c += 1

        x = np.divide(x, 2)

        frequency_pairs = balance_equation_all(self, points, x)

        fd = 1

        # activate the FD function
        if self.FD:
            fd = fd_function(frequency_pairs)
        elif self.rarity:
            fd = mu_function(self, rho_function(frequency_pairs))

        payoffs = np.sum(np.multiply(frequency_pairs, self.payoff_p1), axis=1)

        if self.rarity:
            payoffs = np.multiply(fd, payoffs)
            payoffs = np.multiply(profit_function(fd), payoffs)
            payoffs = payoffs.reshape((payoffs.size, 1))
        else:
            # compute the payoffs with payoffs and FD function
            payoffs = np.multiply(fd, payoffs)
            payoffs = payoffs.reshape((payoffs.size, 1))

        nan_delete = np.where(
            np.isnan(payoffs))  # delete payoffs which are a NaN
        max_payoffs_p1 = np.delete(payoffs, nan_delete[0],
                                   0)  # actually delete them

        threat_candidate_p1 = np.max(max_payoffs_p1)

        if ite == 0:
            best_found = threat_candidate_p1
        elif best_found > threat_candidate_p1:
            best_found = threat_candidate_p1

    for ite in np.arange(0, iterations):
        if ite % 100 == 0:
            print("Currently at iteration:", ite)
        p1_1 = random_strategy_draw(1, 2)
        p1_2 = random_strategy_draw(1, 2)

        p2_1 = random_strategy_draw(points, 2)
        p2_2 = random_strategy_draw(points, 2)

        x = np.zeros((points, 8))

        c = 0

        for i in range(0, 2):
            for j in range(0, 2):
                x[:, c] = np.multiply(p1_1[:, i], p2_1[:, j])
                c += 1

        for i in range(0, 2):
            for j in range(0, 2):
                x[:, c] = np.multiply(p1_2[:, i], p2_2[:, j])
                c += 1

        x = np.divide(x, 2)

        frequency_pairs = balance_equation_all(self, points, x)

        fd = 1

        # activate the FD function
        if self.FD:
            fd = fd_function(frequency_pairs)
        elif self.rarity:
            fd = mu_function(self, rho_function(frequency_pairs))

        payoffs = np.sum(np.multiply(frequency_pairs, self.payoff_p1), axis=1)

        if self.rarity:
            payoffs = np.multiply(fd, payoffs)
            payoffs = np.multiply(profit_function(fd), payoffs)
            payoffs = payoffs.reshape((payoffs.size, 1))
        else:
            # compute the payoffs with payoffs and FD function
            payoffs = np.multiply(fd, payoffs)
            payoffs = payoffs.reshape((payoffs.size, 1))

        nan_delete = np.where(
            np.isnan(payoffs))  # delete payoffs which are a NaN
        min_payoffs_p1 = np.delete(payoffs, nan_delete[0],
                                   0)  # actually delete them

        maxmin_cand_p1 = np.min(min_payoffs_p1)

        if ite == 0:
            best_found_maxmin = maxmin_cand_p1
        elif best_found_maxmin < maxmin_cand_p1:
            best_found_maxmin = maxmin_cand_p1

    print("Best found threat is", best_found)
    print("Best found maxmin is", best_found_maxmin)
示例#2
0
def threat_point_optimized(self, points, show_strat_p1, show_strat_p2,
                           print_text):
    """Optimized threat point algorithm for ETP games"""

    if print_text:
        print("The start of the algorithm for finding the threat point")
        print("First let's find the threat point for Player 1")
        print("")

    start_time = time.time()  # timer start

    y_punisher = random_strategy_draw(
        points, self.payoff_p2_actions)  # draw strategies for the punisher

    frequency_pairs = frequency_pairs_p1(
        self, points, y_punisher)  # sort based on best reply

    # do the balance equations calculations
    frequency_pairs = balance_equation(self, points,
                                       self.payoff_p1_game1.shape[0],
                                       self.payoff_p1_game2.shape[0],
                                       self.payoff_p1_game1.size,
                                       self.total_payoffs, frequency_pairs)

    fd = 1

    # activate the FD function
    if self.FD:
        fd = fd_function(frequency_pairs)
    elif self.rarity:
        fd = mu_function(self, rho_function(frequency_pairs))

    payoffs = np.sum(np.multiply(frequency_pairs, self.payoff_p1), axis=1)

    if self.rarity:
        print("Plotting with rarity active")
        payoffs = np.multiply(fd, payoffs)
        payoffs = np.multiply(profit_function(fd), payoffs)
        payoffs = payoffs.reshape((payoffs.size, 1))
    else:
        # compute the payoffs with payoffs and FD function
        payoffs = np.multiply(fd, payoffs)
        payoffs = payoffs.reshape((payoffs.size, 1))

    max_payoffs = payoffs_sorted(
        points, payoffs,
        (self.payoff_p1_game1.shape[0] * self.payoff_p1_game2.shape[0]))
    # sort the payoffs
    nan_delete = np.where(
        np.isnan(max_payoffs))  # delete payoffs which are a NaN

    max_payoffs_p1 = np.delete(max_payoffs, nan_delete[0],
                               0)  # actually delete them
    threat_point_p1 = np.nanmin(np.nanmax(
        max_payoffs_p1, axis=1))  # determine the threat point

    if print_text:
        print("")
        print("")
        print("Threat point value is", threat_point_p1)
        print("")
        print("")

    if show_strat_p1:
        threat_point_indices_p1 = np.where(max_payoffs_p1 == threat_point_p1)
        found_strategy_p1 = y_punisher[threat_point_indices_p1[0]]
        fnd_strategy_p1 = found_strategy_p1.flatten()
        fnd_strategy_p1[0:2] = fnd_strategy_p1[0:2] / np.sum(
            fnd_strategy_p1[0:2])
        fnd_strategy_p1[2:4] = fnd_strategy_p1[2:4] / np.sum(
            fnd_strategy_p1[2:4])
        print("Player 2 plays stationary strategy:", fnd_strategy_p1)
        print("While player 1 replies with a best pure reply of:",
              self.best_pure_strategies[threat_point_indices_p1[1]])

    end_time = time.time()  # stop the time!

    if print_text:
        print("Seconds done to generate", points, "points",
              end_time - start_time)
        print("")

    # End of algorithm player 1

    # Start of algorithm player 2

    if print_text:
        print("")
        print("")
        print("First start the threat point for player 2")
    start_time_p2 = time.time()  # start the time (for p2)

    x_punisher = random_strategy_draw(
        points, self.payoff_p1_actions)  # draw some awesome strategies

    frequency_pairs = frequency_pairs_p2(
        self, points, self.payoff_p2_actions, self.payoff_p1_actions,
        x_punisher)  # sort them based on best replies

    # do some balance equation accelerator magic
    frequency_pairs = balance_equation(self, points,
                                       self.payoff_p2_game1.shape[1],
                                       self.payoff_p2_game2.shape[1],
                                       self.payoff_p2_game1.size,
                                       self.total_payoffs, frequency_pairs)

    fd = 1

    # activate FD function
    if self.FD:
        fd = fd_function(frequency_pairs)
    elif self.rarity:
        fd = mu_function(self, rho_function(frequency_pairs))

        # payoffs are calculated
    payoffs = np.sum(np.multiply(frequency_pairs, self.payoff_p2), axis=1)

    if self.rarity:
        payoffs = np.multiply(fd, payoffs)
        payoffs = np.multiply(profit_function(fd), payoffs)
        payoffs = payoffs.reshape((payoffs.size, 1))
    else:
        # compute the payoffs with payoffs and FD function
        payoffs = np.multiply(fd, payoffs)
        payoffs = payoffs.reshape((payoffs.size, 1))

    max_payoffs = payoffs_sorted(
        points, payoffs,
        (self.payoff_p2_game1.shape[1] * self.payoff_p2_game2.shape[1]))
    # awesome sorting process
    nan_delete = np.where(np.isnan(max_payoffs))  # look for NaN's

    max_payoffs_p2 = np.delete(max_payoffs, nan_delete[0],
                               0)  # delete them where necessary
    threat_point_p2 = np.nanmin(np.nanmax(
        max_payoffs_p2, axis=1))  # determine the threat point

    if print_text:
        print("")
        print("")
        print("Threat point value is", threat_point_p2)
        print("")
        print("")

    if show_strat_p2:
        threat_point_indices_p2 = np.where(max_payoffs_p2 == threat_point_p2)
        found_strategy = x_punisher[threat_point_indices_p2[0]]
        fnd_strategy = found_strategy.flatten()
        fnd_strategy[0:2] = fnd_strategy[0:2] / np.sum(fnd_strategy[0:2])
        fnd_strategy[2:4] = fnd_strategy[2:4] / np.sum(fnd_strategy[2:4])
        print("Player 1 plays stationairy strategy:", fnd_strategy)
        print("While player 2 replies with a best pure reply of:",
              self.best_pure_strategies[threat_point_indices_p2[1]])

    end_time_p2 = time.time()  # stop the time

    if print_text:
        print("")
        print("Seconds done to generate", points, "points",
              end_time_p2 - start_time_p2)
        print("")
        print("")

    self.threat_point = np.zeros(2)
    self.threat_point = [threat_point_p1,
                         threat_point_p2]  # store the threat point!

    return [threat_point_p1, threat_point_p2]
示例#3
0
ETP = ETPGame(p1_1, p2_1, p1_2, p2_2, trans1_1, trans2_1, trans1_2, trans2_2,
              matrixC)
ETP.activate_rarity()
# ETP.plotting_rare("Rarity")
ETP.activate_hysteresis(1.5)
ETP.adjust_mu(1)

x = np.array([[5 / 7, 0, 0, 0, 2 / 7, 0, 0, 0], [0, 0, 0, 0.5, 0, 0, 0, 0.5]])

res = balance_equation_all(ETP, 2, x)
print(res)

sus = res[0]
nr = res[1]

fd = mu_function(ETP, rho_function(res))
print(fd)

profit = profit_function(fd)
print(profit)

p_sus = fd[0] * (sus[0] * 16 + sus[4] * 4)
result_sus = [p_sus, p_sus]
print("The result of x sus:", result_sus)

p_nr = fd[1] * (nr[3] * 26 + nr[7] * 6.5)
result_nr = [p_nr, p_nr]
print("The result of x nr:", result_nr)

p_rarity_sus = p_sus * profit[0]
p_rarity_nr = p_nr * profit[1]
示例#4
0
def plot_all_rewards(self, points, k):
    print("Now plotting all rewards")

    start_time = time.time()  # timer start

    ## Build a draw function

    draw_payoffs = random_strategy_draw(points, self.total_payoffs)

    print("Payoffs before adjustment of balance equation")

    print("Minimal x0", np.min(draw_payoffs[:, 0]))
    print("Minimal x1", np.min(draw_payoffs[:, 1]))
    print("Minimal x2", np.min(draw_payoffs[:, 2]))
    print("Minimal x3", np.min(draw_payoffs[:, 3]))
    print("Minimal x4", np.min(draw_payoffs[:, 4]))
    print("Minimal x5", np.min(draw_payoffs[:, 5]))
    print("Minimal x6", np.min(draw_payoffs[:, 6]))
    print("Minimal x7", np.min(draw_payoffs[:, 7]))

    print("Minimal over state 1", np.min(draw_payoffs[:, 0:4]))
    print("Minimal over state 2", np.min(draw_payoffs[:, 4:8]))

    print("")
    print("Maximal x0", np.max(draw_payoffs[:, 0]))
    print("Maximal x1", np.max(draw_payoffs[:, 1]))
    print("Maximal x2", np.max(draw_payoffs[:, 2]))
    print("Maximal x3", np.max(draw_payoffs[:, 3]))
    print("Maximal x4", np.max(draw_payoffs[:, 4]))
    print("Maximal x5", np.max(draw_payoffs[:, 5]))
    print("Maximal x6", np.max(draw_payoffs[:, 6]))
    print("Maximal x7", np.max(draw_payoffs[:, 7]))

    print("Maximal over state 1", np.max(draw_payoffs[:, 0:4]))
    print("Maximal over state 2", np.max(draw_payoffs[:, 4:8]))

    print("")
    print("")

    ## Calculate the balance equations

    draw_payoffs = balance_equation_all(self, points, draw_payoffs)

    # End of balance equations

    fd = 1

    # activate the FD function
    if self.FD or self.rarity:
        if self.FD:
            fd = fd_function(draw_payoffs)
        elif self.rarity:
            fd = mu_function(self, rho_function(draw_payoffs))
            # mu_indic = np.where(fd < 0.06)

    print("Payoffs after adjustment of balance equation")

    print("Minimal x0", np.min(draw_payoffs[:, 0]))
    print("Minimal x1", np.min(draw_payoffs[:, 1]))
    print("Minimal x2", np.min(draw_payoffs[:, 2]))
    print("Minimal x3", np.min(draw_payoffs[:, 3]))
    print("Minimal x4", np.min(draw_payoffs[:, 4]))
    print("Minimal x5", np.min(draw_payoffs[:, 5]))
    print("Minimal x6", np.min(draw_payoffs[:, 6]))
    print("Minimal x7", np.min(draw_payoffs[:, 7]))

    print("Minimal over state 1", np.min(draw_payoffs[:, 0:4]))
    print("Minimal over state 2", np.min(draw_payoffs[:, 4:8]))

    print("")
    print("Maximal x0", np.max(draw_payoffs[:, 0]))
    print("Maximal x1", np.max(draw_payoffs[:, 1]))
    print("Maximal x2", np.max(draw_payoffs[:, 2]))
    print("Maximal x3", np.max(draw_payoffs[:, 3]))
    print("Maximal x4", np.max(draw_payoffs[:, 4]))
    print("Maximal x5", np.max(draw_payoffs[:, 5]))
    print("Maximal x6", np.max(draw_payoffs[:, 6]))
    print("Maximal x7", np.max(draw_payoffs[:, 7]))

    print("Maximal over state 1", np.max(draw_payoffs[:, 0:4]))
    print("Maximal over state 2", np.max(draw_payoffs[:, 4:8]))

    print("")
    print("")

    payoffs_p1 = np.sum(np.multiply(draw_payoffs, self.payoff_p1), axis=1)
    payoffs_p2 = np.sum(np.multiply(draw_payoffs, self.payoff_p2), axis=1)

    if self.plotting_rarity == "Rarity":
        payoffs_p1 = np.multiply(fd, payoffs_p1)
        payoffs_p2 = np.multiply(fd, payoffs_p2)
        print("Plotting with rarity active")
        payoffs_p1 = np.multiply(profit_function(fd), payoffs_p1)
        payoffs_p2 = np.multiply(profit_function(fd), payoffs_p2)
    elif self.FD or self.rarity:
        print("Normal plotting active")
        payoffs_p1 = np.multiply(fd, payoffs_p1)
        payoffs_p2 = np.multiply(fd, payoffs_p2)

    # here below we just randomly throw out some stuff

    # payoffs_p1 = np.delete(payoffs_p1, mu_indic[0], 0)
    # payoffs_p2 = np.delete(payoffs_p2, mu_indic[0], 0)

    delete_indic = np.where(np.isnan(payoffs_p1))
    payoffs_p1 = np.delete(payoffs_p1, delete_indic[0], 0)
    payoffs_p2 = np.delete(payoffs_p2, delete_indic[0], 0)

    self.maximal_payoffs = np.zeros(2)
    self.maximal_payoffs = [np.max(payoffs_p1), np.max(payoffs_p2)]

    self.minimal_payoffs = np.zeros(2)
    self.minimal_payoffs = [np.min(payoffs_p1), np.min(payoffs_p2)]

    # all_payoffs = np.array([payoffs_p1, payoffs_p2])
    # all_payoffs = np.transpose(all_payoffs)
    # Convex_Hull_Payoffs = ConvexHull(all_payoffs, qhull_options='QbB')

    plt.figure()
    plt.title("Small Fish Wars with Hysteresis")
    plt.xlabel("Rewards player 1")
    plt.ylabel("Rewards player 2")
    plt.scatter(payoffs_p1, payoffs_p2, s=0.3)

    # plt.figtext(0, 0,'With hysteresis phi at: ' + str(self.phi))
    # plt.figtext(0, -0.05,'And m at: ' + str(self.m))
    # plt.figtext(0, -0.1,'Minimal rewards: ' + str(self.minimal_payoffs))
    # plt.figtext(0, -0.15,'Maximal rewards: ' + str(self.maximal_payoffs))
    plt.axis('equal')
    # plt.xlim(-6, 20)
    # plt.ylim(-6, 20)
    # plt.scatter(12.57, 12.57, color='yellow', label=r'$x^{sus}$')
    # plt.scatter(6.5, 6.5, color='r', label=r'$x^{nr}$')
    # plt.legend()
    plt.savefig('figures/m = 1, phi = 1.5, with x_sus and x_nr.png',
                dpi=300,
                bbox_inches="tight")
    # plt.savefig('figures/without_convex_%d.png'%k, dpi=300, bbox_inches="tight")
    # plt.show()

    # plt.fill(all_payoffs[Convex_Hull_Payoffs.vertices,0],all_payoffs[Convex_Hull_Payoffs.vertices,1],color='y', zorder=5, label="Obtainable rewards")
    end_time = time.time()
    # plt.show()

    print("Total time taken to plot all reward points:", end_time - start_time)
示例#5
0
def optimized_maximin(game, points, show_strat_p1, show_strat_p2):
    """This is an optimized version for determining the maximin result"""

    print("Start of the maximin algorithm")

    # Start of p1 maximin

    start_time = time.time()  # START TIME

    y_punisher = random_strategy_draw(
        points, game.payoff_p1_actions)  # draw some strategies

    frequency_pairs = frequency_pairs_p2(
        game, points, game.payoff_p1_actions, game.payoff_p2_actions,
        y_punisher)  # sort them based on best replies

    # do the balance equations with Aitken's
    frequency_pairs = balance_equation(game, points,
                                       game.payoff_p2_game1.shape[1],
                                       game.payoff_p2_game2.shape[1],
                                       game.payoff_p2_game1.size,
                                       game.total_payoffs, frequency_pairs)

    fd = 1

    # activate FD
    if game.FD:
        fd = fd_function(frequency_pairs)
    elif game.rarity:
        fd = mu_function(game, rho_function(frequency_pairs))

        # payoffs are calculated
    payoffs = np.sum(np.multiply(frequency_pairs, game.payoff_p1), axis=1)

    if game.rarity:
        print("Plotting with rarity active")
        payoffs = np.multiply(fd, payoffs)
        payoffs = np.multiply(profit_function(fd), payoffs)
        payoffs = payoffs.reshape((payoffs.size, 1))
    else:
        # compute the payoffs with payoffs and FD function
        payoffs = np.multiply(fd, payoffs)
        payoffs = payoffs.reshape((payoffs.size, 1))

    max_payoffs = payoffs_sorted(
        points, payoffs,
        (game.payoff_p1_game1.shape[1] * game.payoff_p1_game2.shape[1]))
    # sort the payoffs

    nan_delete = np.where(
        np.isnan(max_payoffs))  # delete results which are NaN (see thesis why)

    max_payoffs = np.delete(max_payoffs, nan_delete[0],
                            0)  # actually delete these payoffs

    print("")
    print("")
    minimax_found = np.nanmax(np.nanmin(max_payoffs,
                                        axis=1))  # look for maximin value
    print("Maximin value for P1 is", minimax_found)
    print("")
    print("")

    if show_strat_p1:
        minimax_indices_p2 = np.where(max_payoffs == minimax_found)
        found_strategy_p2 = y_punisher[minimax_indices_p2[0]]
        fnd_strategy_p2 = found_strategy_p2.flatten()
        fnd_strategy_p2[0:2] = fnd_strategy_p2[0:2] / np.sum(
            fnd_strategy_p2[0:2])
        fnd_strategy_p2[2:4] = fnd_strategy_p2[2:4] / np.sum(
            fnd_strategy_p2[2:4])
        print("Player 1 plays stationary strategy:", fnd_strategy_p2)
        print("While player 2 replies with a best pure reply of:",
              game.best_pure_strategies[minimax_indices_p2[1]])

    end_time = time.time()
    print("Seconds done to generate", points, "points", end_time - start_time)
    print("")
    print("")

    # End of P1 maximin algorithm

    start_time_p2 = time.time()  # start the time

    x_punisher = random_strategy_draw(
        points,
        game.payoff_p2_actions)  # generate new random strategies for punisher

    frequency_pairs = frequency_pairs_p1(game, points,
                                         x_punisher)  # best responses p1

    # balance equations with Delta Squared
    frequency_pairs = balance_equation(game, points,
                                       game.payoff_p1_game1.shape[0],
                                       game.payoff_p1_game2.shape[0],
                                       game.payoff_p1_game1.size,
                                       game.total_payoffs, frequency_pairs)

    fd = 1

    # activate FD function if necessary
    if game.FD:
        fd = fd_function(frequency_pairs)
    elif game.rarity:
        fd = mu_function(game, rho_function(frequency_pairs))

        # payoffs are calculated
    payoffs = np.sum(np.multiply(frequency_pairs, game.payoff_p2), axis=1)

    if game.rarity:
        payoffs = np.multiply(fd, payoffs)
        payoffs = np.multiply(profit_function(fd), payoffs)
        payoffs = payoffs.reshape((payoffs.size, 1))
    else:
        # compute the payoffs with payoffs and FD function
        payoffs = np.multiply(fd, payoffs)
        payoffs = payoffs.reshape((payoffs.size, 1))

    max_payoffs = payoffs_sorted(
        points, payoffs,
        (game.payoff_p1_game1.shape[0] * game.payoff_p1_game2.shape[0]))
    # sort the payoffs

    nan_delete = np.where(np.isnan(max_payoffs))  # check where there are nan's

    max_payoffs = np.delete(max_payoffs, nan_delete[0],
                            0)  # delete these nan's

    minimax_found_p2 = np.nanmax(np.nanmin(
        max_payoffs, axis=1))  # find the maxmin value for p2
    print("Maximin value for P2 is", minimax_found_p2)
    print("")
    print("")

    if show_strat_p2:
        maximin_indices_p2 = np.where(max_payoffs == minimax_found_p2)
        found_strategy = x_punisher[maximin_indices_p2[0]]
        fnd_strategy = found_strategy.flatten()
        fnd_strategy[0:2] = fnd_strategy[0:2] / np.sum(fnd_strategy[0:2])
        fnd_strategy[2:4] = fnd_strategy[2:4] / np.sum(fnd_strategy[2:4])
        print("Player 2 plays stationairy strategy:", fnd_strategy)
        print("While player 2 replies with a best pure reply of:",
              game.best_pure_strategies[maximin_indices_p2[1]])

    end_time_p2 = time.time()  # end the timer
    print("Seconds done to generate", points, "points",
          end_time_p2 - start_time_p2)
    print("")
    print("")