예제 #1
0
rows, cols = settings.payoff_matrix_row.shape

# algorithm settings
rate = 10 ** -5
iters = 2 * 10 ** 5


import matplotlib.pyplot as plt
for _ in range(10):
    str_row_init = utils.randomize_mixed_strategy(rows)
    str_col_init = utils.randomize_mixed_strategy(cols)
    fpi = FPI_2P_Stgy([str_row_init, str_col_init], [settings.payoff_matrix_row, settings.payoff_matrix_col])
    fpi.converge(rate, iters)
    eqpt = fpi.eqpt()
    FPI_2P_Stgy.display_eqpt(eqpt)
    row_x_a, row_y_a = utils.barycentric_to_cartesian(np.array(fpi.get_stats()['str_row_l']))
    col_x_a, col_y_a = utils.barycentric_to_cartesian(np.array(fpi.get_stats()['str_col_l']))
    plt.plot(row_x_a, row_y_a, color='red', alpha=.5)
    plt.plot(col_x_a, col_y_a, color='blue', alpha=.5)

import matplotlib.patches as mpatches
ax = plt.gca()
triangle = mpatches.Polygon(np.array([[0, 0], [np.sqrt(2) / 2, np.sqrt(6) / 2], [np.sqrt(2), 0]]), fc="gray", alpha=.2)
ax.add_artist(triangle)
plt.axis('scaled')
plt.xlim(0, np.sqrt(2))
plt.ylim(0, np.sqrt(6) / 2)
plt.tight_layout()
plt.savefig('tunnel_3x3.png')
plt.show()
    print('the game must be 3x3 for plotting on simplex.')
    sys.exit(1)

fpi = FPI_2P_Regret_Vector(
    [settings.str_row_init, settings.str_col_init],
    [settings.payoff_matrix_row, settings.payoff_matrix_col])
if display_game:
    fpi.print_game()
fpi.converge(r, iters)
FPI_2P_Regret_Vector.display_eqpt(fpi.eqpt())
if use_LH:
    fpi.lemke_howson()

stats = fpi.get_stats()
# essential: transform point in 2-simplex to 2-D plane
row_x_a, row_y_a = utils.barycentric_to_cartesian(np.array(stats['str_row_l']))
col_x_a, col_y_a = utils.barycentric_to_cartesian(np.array(stats['str_col_l']))
temp_a = np.array(stats['vgv_row_l'])
temp_a = np.apply_along_axis(lambda x: x / x.sum(), 1, temp_a)
row_rv_x_a, row_rv_y_a = utils.barycentric_to_cartesian(temp_a)
temp_a = np.array(stats['vgv_col_l'])
temp_a = np.apply_along_axis(lambda x: x / x.sum(), 1, temp_a)
col_rv_x_a, col_rv_y_a = utils.barycentric_to_cartesian(temp_a)

import matplotlib.pyplot as plt
plt.plot(row_x_a, row_y_a, color='red', alpha=.5)
plt.plot(row_rv_x_a, row_rv_y_a, color='red', linestyle='--', alpha=.5)
plt.plot(col_x_a, col_y_a, color='blue', alpha=.5)
plt.plot(col_rv_x_a, col_rv_y_a, color='blue', linestyle='--', alpha=.5)
plt.axvline(row_x_a[-1], color='red', linestyle='dotted', alpha=.3, zorder=-1)
plt.axhline(row_y_a[-1], color='red', linestyle='dotted', alpha=.3, zorder=-1)
예제 #3
0
파일: FPI_nP_path.py 프로젝트: cniclsh/eqpt
        eqpt = game.eqpt()
        vgs_ag_old = vgs_ag_cur

game.show_eqpt(eqpt)

# plotting
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
plt.figure(figsize=(utils.xy_x, utils.xy_x))
ax = plt.gca()
triangle = mpatches.Polygon(np.array([[0, 0], [np.sqrt(2) / 2, np.sqrt(6) / 2], [np.sqrt(2), 0]]), fc="gray", alpha=.1)
ax.add_artist(triangle)

plt.setp(ax.get_xticklabels(), visible=False)
plt.setp(ax.get_yticklabels(), visible=False)
for tick in ax.xaxis.get_major_ticks(): tick.set_visible(False)
for tick in ax.yaxis.get_major_ticks(): tick.set_visible(False)
for tick in ax.xaxis.get_minor_ticks(): tick.set_visible(False)
for tick in ax.yaxis.get_minor_ticks(): tick.set_visible(False)
plt.axis('scaled')
plt.xlim(0, np.sqrt(2))
plt.ylim(0, np.sqrt(6) / 2)

for aplayer in game.players:
    x_a, y_a = utils.barycentric_to_cartesian(np.array(aplayer.path_l))
    ax.plot(x_a, y_a, alpha=.5, zorder=2)

plt.tight_layout()
plt.savefig('nP-path.png', bbox_inches='tight')
plt.show()
예제 #4
0
    distance_ratio_l.append(distance_ratio)
    # print(distance_ratio)
    if distance >= distance_old:
        print(i, 'Lipschitz K>=1, wrong: %s >= %s' % (distance, distance_old))
        sys.exit(1)
    if distance_ratio <= distance_ratio_old:
        print(i, 'Lipschitz K decreasing, unexpected!')
        # sys.exit(1)
    distance_old = distance
    distance_ratio_old = distance_ratio

print('final strategy:', s1, s2)
print('final VG:', vertex_gain_1, vertex_gain_2)

# essential: transform point in 2-simplex to 2-D plane
s1_x_a, s1_y_a = utils.barycentric_to_cartesian(np.array(s1_l))
s2_x_a, s2_y_a = utils.barycentric_to_cartesian(np.array(s2_l))

import matplotlib.pyplot as plt

plt.plot(s1_x_a, s1_y_a, color='red', alpha=.5)
plt.plot(s2_x_a, s2_y_a, color='blue', alpha=.5)
plt.axvline(s1_x_a[-1], color='red', linestyle='dotted', alpha=.3, zorder=-1)
plt.axhline(s1_y_a[-1], color='red', linestyle='dotted', alpha=.3, zorder=-1)
plt.axvline(s2_x_a[-1], color='blue', linestyle='dotted', alpha=.3, zorder=-1)
plt.axhline(s2_y_a[-1], color='blue', linestyle='dotted', alpha=.3, zorder=-1)
import matplotlib.patches as mpatches

ax = plt.gca()
triangle = mpatches.Polygon(np.array([[0, 0], [np.sqrt(2) / 2,
                                               np.sqrt(6) / 2],
예제 #5
0
# run the FPI
for str_row_init, str_col_init in zip(settings.str_row_init_l,
                                      settings.str_col_init_l):
    if str_row_init.shape[0] != 3 or str_col_init.shape[0] != 3:
        print('the game must be 3x3 for plotting on 2D-simplex.')
        continue
    fpi = FPI_2P_Stgy([str_row_init, str_col_init],
                      [settings.payoff_matrix_row, settings.payoff_matrix_col])
    if settings.display_game: fpi.print_game()
    fpi.converge(settings.rate, settings.iters)
    FPI_2P_Stgy.display_eqpt(fpi.eqpt())
    if settings.use_LH: fpi.lemke_howson()

    stats = fpi.get_stats()
    # essential: transform point in 2-simplex to 2-D plane
    row_x_a, row_y_a = utils.barycentric_to_cartesian(
        np.array(stats['str_row_l']))
    col_x_a, col_y_a = utils.barycentric_to_cartesian(
        np.array(stats['str_col_l']))
    plt.plot(row_x_a, row_y_a, color='red', alpha=.5, zorder=2)
    plt.plot(col_x_a, col_y_a, color='blue', alpha=.5, zorder=2)

for eqpt in settings.eqpts:
    x_row, y_row = utils.barycentric_to_cartesian(eqpt[0])
    x_col, y_col = utils.barycentric_to_cartesian(eqpt[1])
    markersize = 60
    plt.scatter(x_row,
                y_row,
                s=markersize,
                marker='x',
                color='black',
                zorder=5)