.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/optimization/game_theory/plot_01_zero_sum_games.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_optimization_game_theory_plot_01_zero_sum_games.py: Von Neumann's minimax theorem: optimal mixed strategies ============================================================= Solves two zero-sum games as linear programs: rock-paper-scissors, whose optimal strategy is uniform with value zero, and a lopsided variant in which the row player's rock beats scissors for double stakes, which tilts the game in the row player's favour. In every game, the row player's guaranteed payoff equals the column player's guaranteed loss. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.optimization import solve_zero_sum_game .. GENERATED FROM PYTHON SOURCE LINES 19-21 Rock-paper-scissors and a lopsided variant ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-41 .. code-block:: Python moves = ["rock", "paper", "scissors"] games = { "standard": np.array([[0, -1, 1], [1, 0, -1], [-1, 1, 0]], dtype=float), "row's rock wins double": np.array([[0, -1, 2], [1, 0, -1], [-1, 1, 0]], dtype=float), } fig, ax = plt.subplots() width = 0.35 for i, (name, payoff) in enumerate(games.items()): result = solve_zero_sum_game(payoff) guaranteed = np.min(result.row_strategy @ payoff) conceded = np.max(payoff @ result.col_strategy) print(f"{name}: p = {result.row_strategy.round(4)}, value = {round(result.value, 10) + 0.0:.4f}") print(f" row player guarantees {round(guaranteed, 10) + 0.0:.4f}; column player concedes at most {round(conceded, 10) + 0.0:.4f}") ax.bar(np.arange(3) + (i - 0.5) * width, result.row_strategy, width, label=name) ax.set_xticks(range(3), moves) ax.set_ylabel("probability in the optimal mixed strategy") ax.legend() ax.set_title("Optimal mixed strategies (von Neumann, 1928)") .. image-sg:: /api/gallery/optimization/game_theory/images/sphx_glr_plot_01_zero_sum_games_001.png :alt: Optimal mixed strategies (von Neumann, 1928) :srcset: /api/gallery/optimization/game_theory/images/sphx_glr_plot_01_zero_sum_games_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none standard: p = [0.3333 0.3333 0.3333], value = 0.0000 row player guarantees 0.0000; column player concedes at most 0.0000 row's rock wins double: p = [0.25 0.4167 0.3333], value = 0.0833 row player guarantees 0.0833; column player concedes at most 0.0833 Text(0.5, 1.0, 'Optimal mixed strategies (von Neumann, 1928)') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.033 seconds) .. _sphx_glr_download_api_gallery_optimization_game_theory_plot_01_zero_sum_games.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_zero_sum_games.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_zero_sum_games.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_zero_sum_games.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_