Algorithm Search⌘K

Algorithmadvanced

Lipton-Markakis-Mehta algorithm

Also called LMM algorithm, quasi-polynomial approximate Nash.

Finds an epsilon-Nash equilibrium in quasi-polynomial time by searching over uniform mixtures of O(log n / epsilon^2) actions.

Time
n^O(log n / e^2) worst
Space
n^O(log n / e^2) extra
Where
e = the additive approximation epsilon; n = the larger number of pure strategies of the two players
Properties
deterministicnot exactofflineiterative
Origin
Richard J. Lipton, Evangelos Markakis, Aranyak Mehta, 2003

The Lipton-Markakis-Mehta (LMM) algorithm finds an epsilon-Nash equilibrium of a two-player game in strategic form (a bimatrix game) with payoffs in [0,1][0, 1]. Its key fact is a sampling theorem: every game has an ε\varepsilon-Nash equilibrium in which each player mixes uniformly over a multiset of only k=O(log⁡n/ε2)k = O(\log n / \varepsilon^2) pure strategies. The algorithm tries all such pairs and checks each one exactly, which takes nO(log⁡n/ε2)n^{O(\log n / \varepsilon^2)} time, quasi-polynomial for every fixed ε\varepsilon. This is far faster than the exponential worst case of exact methods. Rubinstein's theorem shows that, under a hypothesis about PPAD, it cannot be improved much for small constant ε\varepsilon.

In plain words

Two players each mix many moves with odd weights. Draw a few dozen moves at random from each mix, and let each player pick from its own sample with equal chance. With high probability every move scores about the same against the sample as against the full mix, because averages of many random draws are close to their mean. So a pair of small, plain mixes is almost as stable as a real equilibrium. We do not know the real equilibrium, so we try every pair of small, plain mixes and test each one. The number of tries grows a little faster than any polynomial in the number of moves, but much slower than an exponential.

The problem

The input is two m×nm \times n matrices AA and BB with entries in [0,1][0, 1] and a number ε>0\varepsilon > 0. Here mm and nn are the numbers of pure strategies of the row player and of the column player. With row rr and column cc, the row player gets ArcA_{rc} and the column player gets BrcB_{rc}, and both maximize. A pair of mixed strategies (x,y)(x, y) is an ε\varepsilon-Nash equilibrium if

max⁡r(Ay)r−xTAy≤εandmax⁡c(xTB)c−xTBy≤ε.\max_r (Ay)_r - x^{\mathsf T} A y \le \varepsilon \quad\text{and}\quad \max_c (x^{\mathsf T} B)_c - x^{\mathsf T} B y \le \varepsilon.

Pure deviations are enough: a mixed strategy is a best response if and only if its support is inside the set of pure best responses, and a mixed deviation earns an average of pure deviation payoffs. A strategy is kk-uniform if it is uniform on a multiset of kk pure strategies, so that each probability is a multiple of 1/k1/k. The output is a pair of kk-uniform strategies that is an ε\varepsilon-Nash equilibrium.

The algorithm

  1. LMM(A, B, ε) ▷ A, B are m by n with entries in [0, 1]
  2. 1k ← the least integer with k > 2 ln(2(m + n)) / ε²
  3. 2for each multiset R of k rows
  4. 3x ← the uniform mix on R, with multiplicity
  5. 4for each multiset C of k columns
  6. 5y ← the uniform mix on C, with multiplicity
  7. 6if max_r (A y)_r - xᵀ A y ≤ ε and max_c (xᵀ B)_c - xᵀ B y ≤ ε
  8. 7return (x, y)

The code computes k⋅Ayk \cdot Ay and its maximum once for each column multiset, and k⋅xTBk \cdot x^{\mathsf T} B and its maximum once for each row multiset. Then line L6 costs O(k)O(k) for a pair, in exact integer and rational arithmetic, with no tolerance. The constant 2 in line L1 comes from the proof below. Other texts state other constants, and any kk above the bound works.

Example

Take rock-paper-scissors with the payoffs scaled to [0,1][0, 1]: a win gives 1, a draw 1/21/2 and a loss 0, and B=ATB = A^{\mathsf T}. The only equilibrium is the uniform pair, with k=3k = 3. The smallest largest gain over all pairs of kk-uniform strategies is:

kk 1 2 3 4 5
best gain 1/21/2 1/41/4 0 1/81/8 1/101/10

So the search with ε=1/4\varepsilon = 1/4 already succeeds with k=2k = 2, and the first pair it accepts has both players uniform on {rock,paper}\{\text{rock}, \text{paper}\}. Against that mix, rock earns 1/41/4, paper 3/43/4 and scissors 1/21/2, so each player earns 1/21/2 and gains 1/41/4 by a switch to paper. The bound of line L1 is much larger: it gives k=17k = 17 for a 2×22 \times 2 game with ε=1/2\varepsilon = 1/2, and for ε=1/10\varepsilon = 1/10 it gives k=738k = 738, 11991199 and 16591659 for n×nn \times n games with n=10n = 10, 100100 and 10001000. The value grows like log⁡n\log n, but the constant 2/ε22/\varepsilon^2 makes the method useful in theory more than in practice.

Correctness

Let (x∗,y∗)(x^*, y^*) be a Nash equilibrium, which exists by Nash's theorem, with payoffs vAv_A and vBv_B. Draw kk rows independently from x∗x^* and kk columns from y∗y^*, and let x′x' and y′y' be uniform on the samples. For a fixed row rr, (Ay′)r(Ay')_r is a mean of kk independent values in [0,1][0, 1] with mean (Ay∗)r(Ay^*)_r. By the Hoeffding inequality it is more than ε/2\varepsilon/2 away with probability at most 2e−kε2/22e^{-k\varepsilon^2/2}, and the same holds for each column payoff (x′TB)c(x'^{\mathsf T} B)_c. By the union bound, the probability that one of the m+nm + n payoffs is off by ε/2\varepsilon/2 is at most 2(m+n)e−kε2/2<12(m + n) e^{-k\varepsilon^2/2} < 1. So, by the probabilistic method, some sample has all of them close.

For that sample, no row earns more than vA+ε/2v_A + \varepsilon/2 against y′y'. Every sampled row lies in the support of x∗x^*, so it earns exactly vAv_A against y∗y^* by the best-response fact, and at least vA−ε/2v_A - \varepsilon/2 against y′y'. So x′x' earns at least vA−ε/2v_A - \varepsilon/2, and the row gain is at most ε\varepsilon. The column player is the same. So a kk-uniform ε\varepsilon-Nash equilibrium exists, the loops reach it, and line L6 accepts it. The proof file has the details.

Proof: Some pair of k-uniform strategies is an epsilon-Nash equilibrium, so the search succeedsfull

Let (A,B)(A, B) be an m×nm \times n bimatrix game with all entries in [0,1][0, 1], where mm and nn are the numbers of pure strategies of the row player and of the column player. A mixed strategy is kk-uniform if it is the uniform distribution on a multiset of kk pure strategies, so that each probability is a multiple of 1/k1/k. A pair (x,y)(x, y) is an ε\varepsilon-Nash equilibrium if max⁡r(Ay)r−xTAy≤ε\max_r (Ay)_r - x^{\mathsf T} A y \le \varepsilon and max⁡c(xTB)c−xTBy≤ε\max_c (x^{\mathsf T} B)_c - x^{\mathsf T} B y \le \varepsilon. Pure deviations are enough here, because the payoff of a mixed deviation is an average of payoffs of pure deviations.

Lemma 1 (Hoeffding). Let X1,…,XkX_1, \dots, X_k be independent random variables with values in [0,1][0, 1] and the same mean μ\mu, and let Xˉ=1k∑tXt\bar X = \frac1k \sum_t X_t. For every t>0t > 0, Pr⁡[∣Xˉ−μ∣≥t]≤2e−2kt2\Pr[|\bar X - \mu| \ge t] \le 2 e^{-2 k t^2}.

This is the Hoeffding inequality, used here without proof.

Lemma 2 (best response fact). If (x∗,y∗)(x^*, y^*) is a Nash equilibrium, then (Ay∗)r=max⁡r′(Ay∗)r′(A y^*)_r = \max_{r'} (A y^*)_{r'} for every rr with xr∗>0x^*_r > 0, and (x∗TB)c=max⁡c′(x∗TB)c′(x^{*\mathsf T} B)_c = \max_{c'} (x^{*\mathsf T} B)_{c'} for every cc with yc∗>0y^*_c > 0.

Proof. x∗TAy∗x^{*\mathsf T} A y^* is the average of the numbers (Ay∗)r(A y^*)_r with weights xr∗x^*_r. If a row of the support earned less than the maximum, moving its weight to a best row would raise the average, so x∗x^* would not be a best response. The same holds for the column player. □\square

Theorem 1 (existence of a k-uniform approximate equilibrium). Let (x∗,y∗)(x^*, y^*) be any Nash equilibrium, with payoffs vA=x∗TAy∗v_A = x^{*\mathsf T} A y^* and vB=x∗TBy∗v_B = x^{*\mathsf T} B y^*. If k>2ln⁡(2(m+n))/ε2k > 2 \ln(2(m + n)) / \varepsilon^2, there is a pair (x′,y′)(x', y') of kk-uniform strategies with supports inside the supports of x∗x^* and y∗y^* such that (x′,y′)(x', y') is an ε\varepsilon-Nash equilibrium, and moreover ∣x′TAy′−vA∣≤ε/2|x'^{\mathsf T} A y' - v_A| \le \varepsilon/2 and ∣x′TBy′−vB∣≤ε/2|x'^{\mathsf T} B y' - v_B| \le \varepsilon/2.

Proof. A Nash equilibrium exists by Nash's theorem. Draw rows r1,…,rkr_1, \dots, r_k independently from x∗x^*, and columns c1,…,ckc_1, \dots, c_k independently from y∗y^*. Let x′x' be uniform on the multiset {r1,…,rk}\{r_1, \dots, r_k\} and y′y' uniform on {c1,…,ck}\{c_1, \dots, c_k\}. Every rtr_t is in the support of x∗x^* and every csc_s in the support of y∗y^*.

Fix a row rr. Then (Ay′)r=1k∑sArcs(A y')_r = \frac1k \sum_s A_{r c_s} is the mean of kk independent values in [0,1][0, 1], each with mean ∑cyc∗Arc=(Ay∗)r\sum_c y^*_c A_{rc} = (A y^*)_r. By Lemma 1 with t=ε/2t = \varepsilon/2, ∣(Ay′)r−(Ay∗)r∣≥ε/2|(A y')_r - (A y^*)_r| \ge \varepsilon/2 has probability at most 2e−kε2/22 e^{-k \varepsilon^2 / 2}. In the same way, for a fixed column cc, ∣(x′TB)c−(x∗TB)c∣≥ε/2|(x'^{\mathsf T} B)_c - (x^{*\mathsf T} B)_c| \ge \varepsilon/2 has probability at most 2e−kε2/22 e^{-k \varepsilon^2/2}. By the union bound over the mm rows and the nn columns, the probability that at least one of these m+nm + n events happens is at most 2(m+n)e−kε2/22(m + n) e^{-k \varepsilon^2 / 2}, which is less than 1 because kε2/2>ln⁡(2(m+n))k \varepsilon^2 / 2 > \ln(2(m + n)). So with positive probability, and therefore for at least one outcome of the draws, the event EE holds:

∣(Ay′)r−(Ay∗)r∣<ε/2 for all rows r,∣(x′TB)c−(x∗TB)c∣<ε/2 for all columns c.|(A y')_r - (A y^*)_r| < \varepsilon/2 \text{ for all rows } r, \qquad |(x'^{\mathsf T} B)_c - (x^{*\mathsf T} B)_c| < \varepsilon/2 \text{ for all columns } c.

Fix such an outcome. Row player. By EE and Lemma 2, max⁡r(Ay′)r<max⁡r(Ay∗)r+ε/2=vA+ε/2\max_r (A y')_r < \max_r (A y^*)_r + \varepsilon/2 = v_A + \varepsilon/2. Also

x′TAy′=1k∑t(Ay′)rt>1k∑t((Ay∗)rt−ε/2)=vA−ε/2,x'^{\mathsf T} A y' = \frac1k \sum_t (A y')_{r_t} > \frac1k \sum_t \big((A y^*)_{r_t} - \varepsilon/2\big) = v_A - \varepsilon/2,

because each rtr_t is in the support of x∗x^*, so (Ay∗)rt=vA(A y^*)_{r_t} = v_A by Lemma 2. So the row gain is less than ε\varepsilon. Column player. In the same way, max⁡c(x′TB)c<vB+ε/2\max_c (x'^{\mathsf T} B)_c < v_B + \varepsilon/2, and x′TBy′=1k∑s(x′TB)cs>vB−ε/2x'^{\mathsf T} B y' = \frac1k \sum_s (x'^{\mathsf T} B)_{c_s} > v_B - \varepsilon/2, because each csc_s is in the support of y∗y^*. So the column gain is less than ε\varepsilon.

Payoffs. We showed x′TAy′>vA−ε/2x'^{\mathsf T} A y' > v_A - \varepsilon/2, and x′TAy′≤max⁡r(Ay′)r<vA+ε/2x'^{\mathsf T} A y' \le \max_r (A y')_r < v_A + \varepsilon/2. The same holds for BB. □\square

Theorem 2 (correctness of the search). Let kk be the value of line L1, the least integer with k>2ln⁡(2(m+n))/ε2k > 2 \ln(2(m + n)) / \varepsilon^2. Then the algorithm returns a pair, and every pair it returns is a kk-uniform ε\varepsilon-Nash equilibrium.

Proof. Lines L2 and L4 go through all multisets of kk rows and all multisets of kk columns, so lines L3 and L5 produce every pair of kk-uniform strategies. Line L6 compares max⁡r(Ay)r−xTAy\max_r (Ay)_r - x^{\mathsf T} A y and max⁡c(xTB)c−xTBy\max_c (x^{\mathsf T} B)_c - x^{\mathsf T} B y with ε\varepsilon, which is the definition of an ε\varepsilon-Nash equilibrium (pure deviations are enough). So a returned pair is an ε\varepsilon-Nash equilibrium. By Theorem 1 at least one pair passes line L6, so the loops do not end without a return. □\square

Running time. There are (m+k−1k)\binom{m + k - 1}{k} multisets of kk rows and (n+k−1k)\binom{n + k - 1}{k} multisets of kk columns. For each column multiset the code computes k⋅Ayk \cdot A y and its maximum in O(km)O(km) time, and for each row multiset it computes k⋅xTBk \cdot x^{\mathsf T} B and its maximum in O(kn)O(kn) time. Then one pair costs O(k)O(k): the payoff k2 xTAyk^2 \, x^{\mathsf T} A y is the sum of kk entries of k⋅Ayk \cdot A y, and in the same way for BB. The total is

O((m+k−1k)(n+k−1k) k+((m+k−1k)+(n+k−1k)) k(m+n)).O\Big(\tbinom{m+k-1}{k}\tbinom{n+k-1}{k}\, k + \big(\tbinom{m+k-1}{k} + \tbinom{n+k-1}{k}\big)\, k (m + n)\Big).

Since (n+k−1k)≤(n+k)k\binom{n + k - 1}{k} \le (n + k)^k and k=O(log⁡(m+n)/ε2)k = O(\log(m + n)/\varepsilon^2), this is (m+n)O(log⁡(m+n)/ε2)(m + n)^{O(\log(m + n)/\varepsilon^2)}, which is quasi-polynomial for every fixed ε\varepsilon.

Complexity

There are (m+k−1k)\binom{m+k-1}{k} row multisets and (n+k−1k)\binom{n+k-1}{k} column multisets. With the precomputation, the time is O((m+k−1k)(n+k−1k)k+((m+k−1k)+(n+k−1k))k(m+n))O\big(\binom{m+k-1}{k}\binom{n+k-1}{k} k + (\binom{m+k-1}{k} + \binom{n+k-1}{k}) k(m + n)\big). With nn the larger of the two strategy counts and k=O(log⁡n/ε2)k = O(\log n / \varepsilon^2), this is nO(log⁡n/ε2)n^{O(\log n/\varepsilon^2)}. The code keeps the column table, which takes (n+k−1k)\binom{n+k-1}{k} entries of O(m)O(m) numbers. Recomputing it for every row multiset gives polynomial space and the same time bound up to a factor kmk m.

For a fixed ε\varepsilon this is quasi-polynomial. For ε=1/poly(n)\varepsilon = 1/\text{poly}(n) it is exponential, and this is expected: finding an ε\varepsilon-Nash equilibrium for ε=1/nc\varepsilon = 1/n^{c} is PPAD-complete (Chen, Deng and Teng), see PPAD-completeness of Nash.

Properties

  • Small supports. Each player uses at most k=O(log⁡n/ε2)k = O(\log n / \varepsilon^2) pure strategies, all with probabilities that are multiples of 1/k1/k.
  • Payoffs are kept. For every Nash equilibrium there is a kk-uniform ε\varepsilon-Nash equilibrium whose two payoffs are each within ε/2\varepsilon/2 of it. So the search can also approximate the best equilibrium payoffs, for example the best social welfare, up to ε\varepsilon, in the same time.
  • Deterministic. The randomness is only in the proof. The search is exhaustive and exact.
  • Additive approximation. The guarantee needs payoffs on the scale [0,1][0, 1].

Variants and improvements

  • Polynomial time for a fixed ε\varepsilon above 1/31/3. The DMP algorithm gives 1/21/2 in linear time, and LP-based methods reach 1/3+δ1/3 + \delta in polynomial time (Deligkas, Fasoulakis and Markakis, ESA 2022). Whether every constant ε>0\varepsilon > 0 has a polynomial-time algorithm is open as of November 2026.
  • Lower bounds. Rubinstein (FOCS 2016) proved that, under the exponential time hypothesis for PPAD, there is a constant ε>0\varepsilon > 0 such that ε\varepsilon-Nash equilibria of n×nn \times n games need nlog⁡1−o(1)nn^{\log^{1 - o(1)} n} time, so LMM is close to optimal for small constants. A 2026 preprint of Golowich claims the full dependence N(log⁡N/ε2)1−o(1)N^{(\log N / \varepsilon^2)^{1 - o(1)}} under stronger conjectures, and it is not yet peer reviewed as of November 2026.
  • Small supports cannot be avoided. Althöfer (1994) showed games where an approximation needs supports of logarithmic size, and Feder, Nazerzadeh and Saberi (EC 2007) showed that for every ε<1/2\varepsilon < 1/2 some games need supports of size Ω(log⁡n)\Omega(\log n).
  • Sparse and low-rank games. Barman (STOC 2015) used an approximate version of Carathéodory's theorem to get smaller supports, and so faster searches, when the matrices have few nonzero entries per column. Lipton, Markakis and Mehta also treat games whose payoff matrices have low rank.
  • Zero-sum games. For B=−AB = -A the same sampling argument goes back to Althöfer (1994) and Lipton and Young (1994), but there a linear program gives an exact answer in polynomial time.
  • Near-optimal equilibria. Finding an ε\varepsilon-Nash equilibrium whose welfare is near the best one is as hard as finding a planted clique (Hazan and Krauthgamer), so the quasi-polynomial time is also needed for that question unless planted clique is easier than believed. For exact optimal equilibria see the hardness of optimal Nash equilibria.

When to use it

Use it in proofs and as an upper bound: it shows that approximate Nash equilibria in two-player games are not PPAD-hard for constant ε\varepsilon unless PPAD has quasi-polynomial algorithms. Use the search with a small kk chosen by hand on small games, and grow kk until the gain is small enough, as in the example. For real games of medium size, an exact method such as Lemke-Howson, or a method with a constant bound such as DMP, is faster.

Pitfalls

  • Taking the bound of line L1 at face value. It is a worst-case bound for the proof. For ε=1/10\varepsilon = 1/10 it already asks for hundreds of samples, and the search over pairs then takes far too long. Small games often succeed with a much smaller kk.
  • Sets instead of multisets. The proof samples with replacement, so the strategies are uniform on multisets. Uniform mixes on sets of size kk are a smaller family, and the theorem does not cover them.
  • Payoffs outside [0,1][0, 1]. Hoeffding's bound uses the range of the payoffs. Scale the payoffs, or multiply ε\varepsilon by the range.
  • Tolerance in the check. With floats, a pair can pass line L6 only because of rounding. The code works with integers and rationals and compares exactly.

Implementations

The Python code follows lines L1 to L7 with exact arithmetic, and also gives the exhaustive minimum of the gain over all kk-uniform pairs. The tests check the sample size against the bound, matching pennies and rock-paper-scissors for small kk, the exact mixed equilibrium of battle of the sexes with k=3k = 3 and ε=0\varepsilon = 0, and the agreement of the fast gains with the shared exact checker on every pair. On random games they check that the search with a given kk fails exactly when the exhaustive minimum is above ε\varepsilon, and that the search with the kk of line L1 always succeeds.

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

agtgame.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Shared helper of the algorithmic game theory pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Finite games in normal form, with exact payoffs and exact equilibrium checks.

A game is a pair (sizes, pay). sizes is a tuple (k_1, ..., k_n) of the numbers of pure
strategies of the n players; the pure strategies of player i are 0, ..., k_i - 1. pay is a
dict that maps each profile, a tuple s = (s_1, ..., s_n), to a tuple of n Fraction
utilities, which each player maximizes. A cost game passes the utilities u = -c.

A mixed profile sigma is a list of n lists; sigma[i][a] is the probability, a Fraction,
that player i plays a. A distribution on profiles (for correlated and coarse correlated
equilibria) is a dict from profiles to Fraction; missing profiles have probability 0.

All checks are exact: there is no tolerance anywhere. There is no LP and no equilibrium
algorithm in this module.
"""

from fractions import Fraction
from itertools import product


def profiles(sizes):
    """Return the list of all pure profiles, in lexicographic order."""
    return list(product(*(range(k) for k in sizes)))


def from_bimatrix(A, B):
    """Return the game of the bimatrix (A, B): the row player gets A[r][c], the column player B[r][c]."""
    m, n = len(A), len(A[0])
    if len(B) != m or any(len(row) != n for row in A) or any(len(row) != n for row in B):
        raise ValueError("A and B must be m by n matrices of the same shape")
    pay = {(r, c): (Fraction(A[r][c]), Fraction(B[r][c])) for r in range(m) for c in range(n)}
    return (m, n), pay


def random_game(rng, sizes, lo, hi):
    """Return a game with integer utilities drawn uniformly from lo to hi (inclusive), as Fraction."""
    sizes = tuple(sizes)
    pay = {s: tuple(Fraction(rng.randint(lo, hi)) for _ in sizes) for s in profiles(sizes)}
    return sizes, pay


def _replace(s, i, a):
    return s[:i] + (a,) + s[i + 1:]


def _weight(sigma, s, skip=None):
    w = Fraction(1)
    for k, a in enumerate(s):
        if k != skip:
            w *= sigma[k][a]
            if w == 0:
                return w
    return w


def expected_utility(game, i, sigma):
    """Return u_i(sigma), the expected utility of player i when the players draw independently."""
    sizes, pay = game
    return sum((_weight(sigma, s) * pay[s][i] for s in profiles(sizes)), Fraction(0))


def deviation_payoffs(game, i, sigma):
    """Return the list of u_i(a, sigma_-i) for every pure strategy a of player i."""
    sizes, pay = game
    out = [Fraction(0)] * sizes[i]
    for s in profiles(sizes):
        w = _weight(sigma, s, skip=i)
        if w:
            out[s[i]] += w * pay[s][i]
    return out


def pure_best_responses(game, i, sigma):
    """Return the sorted list of the pure strategies of player i that maximize u_i(a, sigma_-i)."""
    d = deviation_payoffs(game, i, sigma)
    best = max(d)
    return [a for a, v in enumerate(d) if v == best]


def max_gain(game, sigma):
    """Return the largest gain of any player from a pure deviation: max_i (max_a u_i(a, sigma_-i) - u_i(sigma)).

    The result is never negative, because u_i(sigma) is an average of the deviation payoffs.
    """
    sizes, _ = game
    gain = Fraction(0)
    for i in range(len(sizes)):
        d = deviation_payoffs(game, i, sigma)
        u = sum((p * v for p, v in zip(sigma[i], d)), Fraction(0))
        gain = max(gain, max(d) - u)
    return gain


def is_distribution(p):
    """Return True if p (a sequence, or a dict of probabilities) is nonnegative and sums to exactly 1."""
    vals = list(p.values()) if isinstance(p, dict) else list(p)
    return all(v >= 0 for v in vals) and sum(vals, Fraction(0)) == 1


def _is_mixed_profile(game, sigma):
    sizes, _ = game
    return len(sigma) == len(sizes) and all(
        len(sigma[i]) == k and is_distribution(sigma[i]) for i, k in enumerate(sizes))


def is_nash(game, sigma):
    """Return True if sigma is a mixed Nash equilibrium: no pure deviation gains for any player."""
    return _is_mixed_profile(game, sigma) and max_gain(game, sigma) == 0


def is_epsilon_nash(game, sigma, eps):
    """Return True if no player gains more than eps by a pure deviation (the additive form)."""
    return _is_mixed_profile(game, sigma) and max_gain(game, sigma) <= eps


def _check_dist(game, dist):
    sizes, _ = game
    allowed = set(profiles(sizes))
    return all(s in allowed for s in dist) and is_distribution(dist)


def is_correlated(game, dist):
    """Return True if dist is a correlated equilibrium.

    For every player i and every pair a, b of its pure strategies, the sum over the profiles s
    with s_i = a of dist(s) (u_i(s) - u_i(b, s_-i)) is at least 0.
    """
    sizes, pay = game
    if not _check_dist(game, dist):
        return False
    for i, k in enumerate(sizes):
        for a in range(k):
            rows = [(q, s) for s, q in dist.items() if s[i] == a and q]
            for b in range(k):
                if b != a and sum((q * (pay[s][i] - pay[_replace(s, i, b)][i]) for q, s in rows), Fraction(0)) < 0:
                    return False
    return True


def is_coarse_correlated(game, dist):
    """Return True if dist is a coarse correlated equilibrium.

    For every player i and every pure strategy b, E_{s ~ dist} u_i(s) >= E_{s ~ dist} u_i(b, s_-i).
    """
    sizes, pay = game
    if not _check_dist(game, dist):
        return False
    for i, k in enumerate(sizes):
        here = sum((q * pay[s][i] for s, q in dist.items()), Fraction(0))
        for b in range(k):
            if sum((q * pay[_replace(s, i, b)][i] for s, q in dist.items()), Fraction(0)) > here:
                return False
    return True


def pure_nash_brute(game):
    """Return the list of all pure Nash equilibria, found by checking every profile."""
    sizes, pay = game
    out = []
    for s in profiles(sizes):
        if all(pay[s][i] >= pay[_replace(s, i, b)][i] for i in range(len(sizes)) for b in range(sizes[i])):
            out.append(s)
    return out
lipton_markakis_mehta_algorithm.py 88 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/lipton-markakis-mehta-algorithm/. The authors are listed in prov/JTYPN5.jsonl.
"""The Lipton-Markakis-Mehta search over k-uniform strategies, in exact rational arithmetic.

The row player gets A[i][j] and the column player gets B[i][j]; both maximize. A strategy is
k-uniform if it is the uniform distribution on a multiset of k pure strategies, so every
probability is a multiple of 1/k. If the payoffs are in [0, 1] and k > 2 ln(2(m + n)) / eps^2,
some pair of k-uniform strategies is an eps-Nash equilibrium, so the search succeeds.
"""

import math
from fractions import Fraction
from itertools import combinations_with_replacement


def sample_size(m, n, eps):
    """Return the least integer k with k > 2 ln(2(m + n)) / eps^2."""
    eps = Fraction(eps)
    bound = 2 * math.log(2 * (m + n)) / float(eps * eps)
    k = max(1, math.floor(bound))
    while k <= bound:
        k += 1
    return k


def _mix(multiset, size, k):
    p = [Fraction(0)] * size
    for a in multiset:
        p[a] += Fraction(1, k)
    return p


def k_uniform_pairs(A, B, k):
    """Yield (x, y, row gain, column gain) for every pair of k-uniform strategies.

    The work for one pair is O(k), after O(k (m + n)) work for each multiset.
    """
    A = [[Fraction(v) for v in row] for row in A]
    B = [[Fraction(v) for v in row] for row in B]
    m, n = len(A), len(A[0])
    cols = []
    for C in combinations_with_replacement(range(n), k):
        kAy = [sum(A[r][c] for c in C) for r in range(m)]  # k times A y
        cols.append((C, kAy, max(kAy)))
    for R in combinations_with_replacement(range(m), k):
        kxB = [sum(B[r][c] for r in R) for c in range(n)]  # k times x^T B
        best_c = max(kxB)
        x = None
        for C, kAy, best_r in cols:
            row_gain = Fraction(best_r - sum(kAy[r] for r in R) / k, k)
            col_gain = Fraction(best_c - sum(kxB[c] for c in C) / k, k)
            if x is None:
                x = _mix(R, m, k)
            yield x, _mix(C, n, k), row_gain, col_gain


def lmm(A, B, eps, k=None):
    """Return the first pair of k-uniform strategies that is an eps-Nash equilibrium, or None."""
    eps = Fraction(eps)
    m, n = len(A), len(A[0])
    if k is None:
        k = sample_size(m, n, eps)  # @line L1
    A = [[Fraction(v) for v in row] for row in A]
    B = [[Fraction(v) for v in row] for row in B]
    cols = []
    for C in combinations_with_replacement(range(n), k):
        kAy = [sum(A[r][c] for c in C) for r in range(m)]
        cols.append((C, kAy, max(kAy)))
    for R in combinations_with_replacement(range(m), k):  # @line L2
        kxB = [sum(B[r][c] for r in R) for c in range(n)]  # @line L3
        best_c = max(kxB)
        for C, kAy, best_r in cols:  # @line L4
            # y is uniform on C. kAy and best_r were computed once for each C.
            kkAxy = sum(kAy[r] for r in R)  # @line L5
            kkBxy = sum(kxB[c] for c in C)  # k^2 times x^T B y, and kkAxy is k^2 times x^T A y
            if best_r * k - kkAxy <= eps * k * k and best_c * k - kkBxy <= eps * k * k:  # @line L6
                return _mix(R, m, k), _mix(C, n, k)  # @line L7
    return None


def best_k_uniform(A, B, k):
    """Return (gain, x, y) for a pair of k-uniform strategies with the least largest gain."""
    best = None
    for x, y, gr, gc in k_uniform_pairs(A, B, k):
        g = max(gr, gc)
        if best is None or g < best[0]:
            best = (g, x, y)
    return best
test_lipton_markakis_mehta_algorithm.py 92 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/lipton-markakis-mehta-algorithm/. The authors are listed in prov/JTYPN5.jsonl.

import math
import random
from fractions import Fraction

from agtgame import from_bimatrix, is_epsilon_nash, is_nash, max_gain
from lipton_markakis_mehta_algorithm import best_k_uniform, k_uniform_pairs, lmm, sample_size

F = Fraction
HALF = F(1, 2)

RPS = [[HALF, 0, 1], [1, HALF, 0], [0, 1, HALF]]  # rock-paper-scissors scaled to [0, 1]
RPS_T = [list(r) for r in zip(*RPS)]


def random_unit_matrix(rng, m, n, den):
    return [[F(rng.randint(0, den), den) for _ in range(n)] for _ in range(m)]


def is_k_uniform(p, k):
    return sum(p) == 1 and all(v >= 0 and (v * k).denominator == 1 for v in p)


def test_sample_size():
    for m, n, eps in [(2, 2, HALF), (3, 3, HALF), (2, 2, F(1, 4)), (10, 7, F(1, 10))]:
        k = sample_size(m, n, eps)
        bound = 2 * math.log(2 * (m + n)) / float(eps) ** 2
        assert k > bound >= k - 1
    assert sample_size(2, 2, HALF) == 17
    assert sample_size(3, 3, HALF) == 20


def test_small_k_on_check_games():
    mp_A, mp_B = [[1, 0], [0, 1]], [[0, 1], [1, 0]]
    assert best_k_uniform(mp_A, mp_B, 1)[0] == 1
    g, x, y = best_k_uniform(mp_A, mp_B, 2)
    assert g == 0 and x == [HALF, HALF] and y == [HALF, HALF]
    assert best_k_uniform(RPS, RPS_T, 1)[0] == HALF
    g, x, y = best_k_uniform(RPS, RPS_T, 3)
    assert g == 0 and x == [F(1, 3)] * 3 and y == [F(1, 3)] * 3


def test_exact_equilibria_on_the_grid_are_found_with_eps_zero():
    # Battle of the sexes: the mixed equilibrium (2/3, 1/3), (1/3, 2/3) is 3-uniform.
    A, B = [[2, 0], [0, 1]], [[1, 0], [0, 2]]
    x, y = lmm(A, B, 0, k=3)
    assert is_nash(from_bimatrix(A, B), [x, y])
    x, y = lmm(RPS, RPS_T, 0, k=3)
    assert x == [F(1, 3)] * 3 and y == [F(1, 3)] * 3
    assert lmm(RPS, RPS_T, 0, k=2) is None


def test_pair_gains_agree_with_the_helper():
    rng = random.Random(11)
    for _ in range(10):
        m, n, k = rng.randint(1, 3), rng.randint(1, 3), rng.randint(1, 3)
        A, B = random_unit_matrix(rng, m, n, 6), random_unit_matrix(rng, m, n, 6)
        g = from_bimatrix(A, B)
        count = 0
        for x, y, gr, gc in k_uniform_pairs(A, B, k):
            count += 1
            assert is_k_uniform(x, k) and is_k_uniform(y, k)
            assert max(gr, gc) == max_gain(g, [x, y])
        assert count == math.comb(m + k - 1, k) * math.comb(n + k - 1, k)


def test_lmm_with_given_k_matches_the_exhaustive_minimum():
    rng = random.Random(5)
    for _ in range(40):
        m, n, k = rng.randint(1, 3), rng.randint(1, 3), rng.randint(1, 3)
        A, B = random_unit_matrix(rng, m, n, 4), random_unit_matrix(rng, m, n, 4)
        best = best_k_uniform(A, B, k)[0]
        for eps in [0, F(1, 8), F(1, 4), HALF]:
            out = lmm(A, B, eps, k=k)
            assert (out is None) == (best > eps)
            if out is not None:
                assert is_epsilon_nash(from_bimatrix(A, B), list(out), eps)


def test_theorem_size_always_succeeds():
    rng = random.Random(20261120)
    cases = [(2, 2, HALF)] * 12 + [(2, 3, HALF)] * 6 + [(3, 3, HALF)] * 4 + [(2, 2, F(1, 4))] * 4
    for m, n, eps in cases:
        A, B = random_unit_matrix(rng, m, n, 10), random_unit_matrix(rng, m, n, 10)
        k = sample_size(m, n, eps)
        out = lmm(A, B, eps)
        assert out is not None
        x, y = out
        assert is_k_uniform(x, k) and is_k_uniform(y, k)
        assert is_epsilon_nash(from_bimatrix(A, B), [x, y], eps)

History

Althöfer (1994) and Lipton and Young (1994) showed that zero-sum games have approximately optimal strategies with small supports, by sampling. Lipton, Markakis and Mehta extended the argument to general two-player games, and to approximate equilibrium payoffs, at the ACM Conference on Electronic Commerce in 2003. After the PPAD-completeness results of 2005 and 2006, the quasi-polynomial algorithm became the main reason to expect that constant approximations are easier than exact equilibria. Rubinstein's 2016 lower bound showed that, under a hypothesis on PPAD, the quasi-polynomial time is close to the truth.

References

  1. Richard J. Lipton, Evangelos Markakis and Aranyak Mehta, Playing large games using simple strategies, Proceedings of the 4th ACM Conference on Electronic Commerce (EC 2003), 36-41, the existence of epsilon-equilibria with k-uniform strategies for k = O(log n / epsilon^2), the quasi-polynomial search, and the approximation of equilibrium payoffs
  2. Aviad Rubinstein, Settling the complexity of computing approximate two-player Nash equilibria, Proceedings of the 57th IEEE Symposium on Foundations of Computer Science (FOCS 2016), 258-265, the matching quasi-polynomial lower bound under the exponential time hypothesis for PPAD
  3. Constantinos Daskalakis, Aranyak Mehta and Christos H. Papadimitriou, A note on approximate Nash equilibria, Theoretical Computer Science 410(17) (2009), 1581-1588, the remark that sublogarithmic supports cannot give better than 1/2, after Althöfer and Feder, Nazerzadeh and Saberi

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example