Algorithmadvanced
Computing an optimal strategy to commit to
Also called Conitzer-Sandholm multiple LPs algorithm.
Finds the leader's optimal mixed commitment in a two-player normal-form game by one linear program per follower action.
- Time
- poly(m, n, L) worst
- Space
- O(m n) extra
- Where
- L = the bit length of the payoffs; m = the number of pure strategies of the leader; n = the number of pure strategies of the follower
- Model
- Real RAM
- Properties
- deterministicexactofflineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Vincent Conitzer, Tuomas Sandholm, 2006
This algorithm computes the best mixed strategy for a leader to commit to in a two-player game in normal form, when the follower sees the commitment and plays a best response. It solves one linear program for each pure strategy of the follower: among the mixed strategies to which is a best response, find the one that is best for the leader; then take the best of these. The solution concept, the Stackelberg equilibrium, has its own page, and security games are the model in which this computation is used most, with a compact strategy space for the defender. Stackelberg routing is a different use of the word, for a leader who controls part of the traffic in a network.
In plain words
Suppose you must announce how you will randomize before the other player moves, and the other player will then do what is best for them. You can use this to your advantage: you choose your mix so that the reply you like is the one they want. For each possible reply, ask: among all mixes that make this reply a best choice for them, which mix is best for me? Each question is a small linear program. Answer all of them and keep the best. If the other player is indifferent between replies, assume they pick the one you prefer; this is the usual rule, and without it the best mix may not exist.
The problem
Input: an bimatrix game , where the leader (row player) has pure strategies and payoffs , and the follower (column player) has pure strategies and payoffs . Here and count strategies, not players. The leader commits to . The follower sees and plays a best response ; a pure best response always exists, and ties are broken in the leader's favor. Output: an and that maximize the leader's payoff , which is a strong Stackelberg equilibrium.
The algorithm
OPTIMAL-COMMITMENT(A, B)- 1
for each column j = 1..n: - 2
LP_j: maximize Σ_i x_i A[i][j] subject to Σ_i x_i (B[i][k] - B[i][j]) ≤ 0 for every k ≠ j ▷ j is a best responseΣ_i x_i = 1, x ≥ 0- 3
if LP_j is infeasible: skip j ▷ no mixed strategy makes j a best response - 4
keep (value of LP_j, its solution x, j) if the value is the largest so far - 5
return the kept (value, x, j)
The constraints of LP_j describe the region of the simplex where is a best response. These regions cover the simplex, so at least one LP is feasible. Each region is a polytope, and the leader's payoff against is linear on it, so each LP has an optimum at a vertex of its region.
Example
Let and , with rows , for the leader and columns , for the follower. For the leader, strictly dominates , so the only Nash equilibrium is , with payoff 1 to the leader.
| LP | best response region | objective | optimum |
|---|---|---|---|
| , value 1 | |||
| , value |
The algorithm returns with the follower playing : the follower is indifferent and breaks the tie for the leader, who gets . Committing to the pure strategy gives 2, and the Nash equilibrium gives 1. The optimal commitment puts as much weight on as it can while stays a best response, which is an instance of the indifference principle at the boundary of a region.
Correctness
For every , the follower's choice is a best response to , so is feasible for LP_j, and the leader's payoff is at most the value of LP_j. So no commitment gives more than the largest LP value. Conversely, the optimal of the LP with the largest value makes a best response, and if the follower has another best response that is better for the leader, is also feasible for LP_k with a larger value, which is impossible. So with ties broken for the leader, the follower plays a column that gives the leader exactly the largest LP value. The proof file has the details, and also shows that this value is at least the leader's payoff in every Nash equilibrium.
Proof: The best of the n LPs is the leader's optimal commitment, and it is at least every Nash equilibrium payofffull
Let be an bimatrix game with the leader as the row player. For let , the set of the follower's pure best responses, and let the leader's commitment payoff be (ties broken for the leader). The goal is and an that attains it. For each , let , which is the feasible set of LP_j, and let when .
Lemma 1 (the LPs are well defined). Each is a polytope, possibly empty, and the sets cover . Each LP_j with has an optimum.
Proof. is the simplex cut by the linear inequalities , so it is a bounded polyhedron. For every , the set is nonempty because it is the set of maximizers of a finite list, so lies in some . A linear function on a nonempty polytope attains its maximum.
Theorem. Let be a column with the largest and an optimal solution of LP_, as returned by lines L1 to L5. Then , and the follower's tie-broken best response to gives the leader exactly .
Proof. Upper bound: for any , let be the follower's choice. Then , so . Hence for all . Attainment: , so and . With the upper bound, , so is attained at . If the tie rule picks some , the leader gets by the definition of .
Termination. The loop runs times, and each LP is solved by the simplex method with Bland's rule, which ends on every input.
Proposition (pure follower responses suffice). If the follower may also play a mixed best response , the leader's best payoff for a fixed is still .
Proof. A mixed best response puts weight only on , by the best response condition, so .
Proposition (commitment beats Nash). For every Nash equilibrium of , .
Proof. In the equilibrium is a best response to , so by the previous proposition .
The same argument with pure shows that is at least the value of every pure commitment.
Proposition (zero-sum games). If , then equals the value of the matrix game .
Proof. For , the follower's best responses to minimize , so and , which is the maximin value. By the minimax theorem it equals .
Complexity
The algorithm solves linear programs, each with variables and constraints, so the time is times the time of one LP. With a polynomial-time LP method this is polynomial in , and the bit length of the payoffs. The code uses the exact simplex method with rational numbers. The space is for one LP at a time.
For comparison, finding a Nash equilibrium of a bimatrix game is PPAD-complete, so commitment is easier to compute than a Nash equilibrium of the same game.
Properties
- At least as good as any Nash equilibrium. The leader's commitment value is at least the leader's payoff in every Nash equilibrium of , and at least the value of the best pure commitment.
- Zero-sum games. If , the commitment value is the value of the game: committing to a maximin strategy is optimal, and the minimax theorem says the leader cannot do better.
- Exact. All LP data are rational, so the optimal is rational and the code finds it exactly.
- Follower plays pure. It is enough to consider pure best responses: a mixed best response is a mixture of pure ones and gives the leader at most the best of them.
Variants and improvements
- One LP. The LPs can be merged into one LP over a correlated distribution of the pairs (leader action, follower action), with a best response constraint for each recommended follower action. For two players its value equals the best value of the LPs: in a feasible distribution, the leader's conditional strategy given each recommended follower action is feasible for LP_j, so the leader's payoff is an average of values that are each at most the best LP value.
- Bayesian followers. With several follower types and a prior over them, finding the optimal commitment is NP-hard (Conitzer and Sandholm). The usual exact method is a mixed integer program with one binary variable for each type and action, as in the DOBSS algorithm of Paruchuri and others; see Bayesian games.
- Security games. When the leader's pure strategies are allocations of resources to targets, is exponential, but the payoffs depend only on the coverage of each target, so the LPs can be written with coverage variables; see security games.
- Weak Stackelberg. If the follower breaks ties against the leader, the supremum of the leader's payoff need not be attained. The strong value can be approached from inside a region by moving slightly away from its boundary, where the follower's best response is unique.
When to use it
Use it when one player can credibly announce a randomized policy before the other acts, as in patrols, inspections, audits and screening, and the game is given in normal form with a modest number of actions for both players. For many follower types or compact strategy spaces, use the MIP and security game methods instead.
Pitfalls
- Tie-breaking. The optimum usually makes the follower indifferent between two or more actions, so the tie rule matters. In practice the leader moves a little into the region of the preferred action, at a small loss, so that the follower's best response is strict.
- Leader's payoffs, follower's constraints. The objective of LP_j uses and the constraints use ; mixing them up gives the follower's commitment problem instead.
- Not a Nash equilibrium. The pair need not be a Nash equilibrium of the simultaneous game: in the example, the leader's commitment is not a best response to .
- Commitment must be credible. The model assumes the follower observes the mixed strategy, or learns it over time, but not the outcome of the randomization before acting.
Implementations
The Python code builds LP_j for each column and solves it with the shared exact simplex code. A helper computes the follower's best response with ties broken for the leader. The tests check the example above, an infeasible LP for a dominated column, and the tie rule. On 150 random games, the result is compared with a brute-force search over the vertices of all best response regions. The tests also check that the commitment value is at least every Nash equilibrium payoff found by support enumeration and every pure commitment, and that it equals the minimax value on 100 random zero-sum games.
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.
# SPDX-License-Identifier: MIT
# Shared helper of the algorithmic game theory pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Exact linear programming in Fraction, by the two-phase simplex method with Bland's rule.
lp_max(c, A_ub, b_ub, A_eq=None, b_eq=None) maximizes c x subject to A_ub x <= b_ub,
A_eq x = b_eq and x >= 0. lp_min does the same for minimization. Free variables must be
split by the caller (x = x+ - x-). Every input number is converted to Fraction, so the
result is exact: there is no tolerance anywhere.
Both functions return (status, x, value, dual). status is "optimal", "infeasible" or
"unbounded". When status is not "optimal", x, value and dual are None. dual has one entry
per constraint, the A_ub rows first and then the A_eq rows. It is the vector of shadow
prices: the value is sum_i b_i dual_i (strong duality). For lp_max, dual_i >= 0 on the
A_ub rows and A^T dual >= c. For lp_min, dual_i <= 0 on the A_ub rows and A^T dual <= c.
The entries for the A_eq rows have no sign condition.
Bland's rule (the smallest improving column enters, and among the rows with the smallest
ratio the one with the smallest basic variable leaves) makes the method finite on every
input, degenerate inputs included. There is no game algorithm in this module.
"""
from fractions import Fraction
def _frac(v):
return v if isinstance(v, Fraction) else Fraction(v)
def _pivot(T, obj, basis, r, j):
p = T[r][j]
row = [v / p for v in T[r]]
T[r] = row
for k in range(len(T)):
f = T[k][j]
if k != r and f != 0:
T[k] = [a - f * b for a, b in zip(T[k], row)]
f = obj[j]
if f != 0:
obj[:] = [a - f * b for a, b in zip(obj, row)]
basis[r] = j
def _simplex(T, obj, basis, ncols):
"""Maximize. obj[j] is the reduced cost of column j and obj[-1] is minus the value."""
while True:
enter = next((j for j in range(ncols) if obj[j] > 0), None)
if enter is None:
return "optimal"
best = None
for r, row in enumerate(T):
a = row[enter]
if a > 0:
key = (row[-1] / a, basis[r])
if best is None or key < best[0]:
best = (key, r)
if best is None:
return "unbounded"
_pivot(T, obj, basis, best[1], enter)
def _objective_row(cost, T, basis):
obj = list(cost) + [Fraction(0)]
for r, j in enumerate(basis):
f = obj[j]
if f != 0:
obj = [a - f * b for a, b in zip(obj, T[r])]
return obj
def _any_solution(M, rhs, nvars):
"""Return one solution y of M y = rhs (a consistent system), with the free variables 0."""
rows = [list(M[k]) + [rhs[k]] for k in range(len(M))]
pivots = []
r = 0
for col in range(nvars):
p = next((k for k in range(r, len(rows)) if rows[k][col] != 0), None)
if p is None:
continue
rows[r], rows[p] = rows[p], rows[r]
pv = rows[r][col]
rows[r] = [v / pv for v in rows[r]]
for k in range(len(rows)):
f = rows[k][col]
if k != r and f != 0:
rows[k] = [a - f * b for a, b in zip(rows[k], rows[r])]
pivots.append(col)
r += 1
if r == len(rows):
break
y = [Fraction(0)] * nvars
for k, col in enumerate(pivots):
y[col] = rows[k][-1]
return y
def _matrix(A, width, name):
A = [[_frac(v) for v in row] for row in (A or [])]
if any(len(row) != width for row in A):
raise ValueError(name + " must have one column per variable")
return A
def lp_max(c, A_ub, b_ub, A_eq=None, b_eq=None):
"""Maximize c x subject to A_ub x <= b_ub, A_eq x = b_eq and x >= 0, exactly."""
c = [_frac(v) for v in c]
n = len(c)
A_ub = _matrix(A_ub, n, "A_ub")
A_eq = _matrix(A_eq, n, "A_eq")
b_ub = [_frac(v) for v in (b_ub or [])]
b_eq = [_frac(v) for v in (b_eq or [])]
if len(b_ub) != len(A_ub) or len(b_eq) != len(A_eq):
raise ValueError("b_ub and b_eq must have one entry per row")
m1, m2 = len(A_ub), len(A_eq)
m = m1 + m2
width = n + m1
# Rows in the original orientation, with a slack column for each A_ub row.
orig = [A_ub[i] + [Fraction(int(k == i)) for k in range(m1)] for i in range(m1)]
orig += [A_eq[i] + [Fraction(0)] * m1 for i in range(m2)]
rhs = b_ub + b_eq
# A row gets an artificial variable if it is an equation or if its right side is negative.
need = [i >= m1 or rhs[i] < 0 for i in range(m)]
nart = sum(need)
T, basis = [], []
a = 0
for i in range(m):
sign = -1 if rhs[i] < 0 else 1
row = [sign * v for v in orig[i]] + [Fraction(0)] * nart + [sign * rhs[i]]
if need[i]:
row[width + a] = Fraction(1)
basis.append(width + a)
a += 1
else:
basis.append(n + i)
T.append(row)
if nart:
# Phase 1: maximize minus the sum of the artificial variables.
obj = _objective_row([Fraction(0)] * width + [Fraction(-1)] * nart, T, basis)
_simplex(T, obj, basis, width + nart)
if obj[-1] != 0:
return "infeasible", None, None, None
keep = []
for r in range(m):
if basis[r] >= width:
j = next((j for j in range(width) if T[r][j] != 0), None)
if j is None:
continue # the row is a combination of the other rows
_pivot(T, [Fraction(0)] * (width + nart + 1), basis, r, j)
keep.append(r)
T = [T[r][:width] + [T[r][-1]] for r in keep]
basis = [basis[r] for r in keep]
# Phase 2: the original objective, without the artificial columns.
cost = c + [Fraction(0)] * m1
obj = _objective_row(cost, T, basis)
if _simplex(T, obj, basis, width) == "unbounded":
return "unbounded", None, None, None
x = [Fraction(0)] * n
for r, j in enumerate(basis):
if j < n:
x[j] = T[r][-1]
value = sum((ci * xi for ci, xi in zip(c, x)), Fraction(0))
# The dual solves y^T A_B = c_B, where A_B holds the basic columns of the original rows.
M = [[orig[i][j] for i in range(m)] for j in basis]
dual = _any_solution(M, [cost[j] for j in basis], m)
return "optimal", x, value, dual
def lp_min(c, A_ub, b_ub, A_eq=None, b_eq=None):
"""Minimize c x subject to A_ub x <= b_ub, A_eq x = b_eq and x >= 0, exactly."""
status, x, value, dual = lp_max([-_frac(v) for v in c], A_ub, b_ub, A_eq, b_eq)
if status != "optimal":
return status, None, None, None
return status, x, -value, [-v for v in dual]
stackelberg_commitment_algorithm.py 48 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/stackelberg-commitment-algorithm/. The authors are listed in prov/3741KV.jsonl.
"""The optimal mixed strategy to commit to in a bimatrix game, by one LP per follower action.
The leader is the row player with payoff matrix A, the follower is the column player with
payoff matrix B, both m x n. The leader commits to a mixed strategy x; the follower sees x
and plays a best response, breaking ties in the leader's favor (the strong Stackelberg
convention). For each column j, an LP finds the best x among those to which j is a best
response; the answer is the best of these LPs.
"""
from fractions import Fraction
from agtlp import lp_max
def best_for_column(A, B, j):
"""Return (value, x) of max x . A[:, j] over the x in the simplex with j a best response to x.
Return None if no mixed strategy makes j a best response.
"""
m, n = len(A), len(A[0])
c = [A[i][j] for i in range(m)]
# sum_i x_i B[i][k] <= sum_i x_i B[i][j] for every other column k.
A_ub = [[B[i][k] - B[i][j] for i in range(m)] for k in range(n) if k != j] # @line L2
status, x, value, _ = lp_max(c, A_ub, [0] * len(A_ub), [[1] * m], [1])
if status != "optimal": # @line L3
return None
return value, x
def optimal_commitment(A, B):
"""Return (value, x, j): the leader's optimal commitment x, its value, and the follower's column j."""
best = None
for j in range(len(A[0])): # @line L1
res = best_for_column(A, B, j)
if res is not None and (best is None or res[0] > best[0]): # @line L4
best = (res[0], res[1], j)
return best # @line L5
def follower_response(A, B, x):
"""The follower's best response to x, with ties broken in the leader's favor."""
m, n = len(A), len(A[0])
pay = [sum(Fraction(x[i]) * B[i][k] for i in range(m)) for k in range(n)]
top = max(pay)
ties = [k for k in range(n) if pay[k] == top]
return max(ties, key=lambda k: (sum(Fraction(x[i]) * A[i][k] for i in range(m)), -k))
test_stackelberg_commitment_algorithm.py 152 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/stackelberg-commitment-algorithm/. The authors are listed in prov/3741KV.jsonl.
import random
from fractions import Fraction
from itertools import combinations
from agtlp import lp_max
from stackelberg_commitment_algorithm import best_for_column, follower_response, optimal_commitment
F = Fraction
def solve_square(K, b):
r = len(K)
rows = [[F(v) for v in K[k]] + [F(b[k])] for k in range(r)]
for col in range(r):
p = next((k for k in range(col, r) if rows[k][col] != 0), None)
if p is None:
return None
rows[col], rows[p] = rows[p], rows[col]
rows[col] = [v / rows[col][col] for v in rows[col]]
for k in range(r):
f = rows[k][col]
if k != col and f != 0:
rows[k] = [a - f * c for a, c in zip(rows[k], rows[col])]
return [rows[k][-1] for k in range(r)]
def leader_payoff(A, x, j):
return sum(F(x[i]) * A[i][j] for i in range(len(A)))
def brute_force(A, B):
"""Best leader payoff over the vertices of all regions {x in simplex : j is a best response}."""
m, n = len(A), len(A[0])
best = None
for j in range(n):
# Constraints: x_i >= 0 and (B[:, j] - B[:, k]) . x >= 0; a vertex makes m - 1 of them tight.
cons = [[int(t == i) for t in range(m)] for i in range(m)]
cons += [[B[i][j] - B[i][k] for i in range(m)] for k in range(n) if k != j]
for S in combinations(range(len(cons)), m - 1):
x = solve_square([cons[s] for s in S] + [[1] * m], [0] * (m - 1) + [1])
if x is None or any(sum(F(c[i]) * x[i] for i in range(m)) < 0 for c in cons):
continue
v = leader_payoff(A, x, j)
if best is None or v > best:
best = v
return best
def support_enumeration(A, B):
m, n = len(A), len(A[0])
out = []
for k in range(1, min(m, n) + 1):
for I in combinations(range(m), k):
for J in combinations(range(n), k):
sy = solve_square([[A[i][j] for j in J] + [-1] for i in I] + [[1] * k + [0]], [0] * k + [1])
sx = solve_square([[B[i][j] for i in I] + [-1] for j in J] + [[1] * k + [0]], [0] * k + [1])
if sy is None or sx is None or min(sy[:k]) < 0 or min(sx[:k]) < 0:
continue
x, y = [F(0)] * m, [F(0)] * n
for i, p in zip(I, sx):
x[i] = p
for j, p in zip(J, sy):
y[j] = p
if all(sum(A[i][j] * y[j] for j in J) <= sy[k] for i in range(m)) and \
all(sum(x[i] * B[i][j] for i in I) <= sx[k] for j in range(n)):
out.append((x, y, sy[k]))
return out
def random_game(rng, m, n, lo=-9, hi=9):
return ([[rng.randint(lo, hi) for _ in range(n)] for _ in range(m)],
[[rng.randint(lo, hi) for _ in range(n)] for _ in range(m)])
def check(A, B, value, x, j):
m, n = len(A), len(A[0])
assert all(v >= 0 for v in x) and sum(x) == 1
pay = [sum(x[i] * B[i][k] for i in range(m)) for k in range(n)]
assert pay[j] == max(pay)
assert leader_payoff(A, x, j) == value
assert leader_payoff(A, x, follower_response(A, B, x)) == value
def test_page_example():
A = [[1, 3], [0, 2]]
B = [[1, 0], [0, 1]]
assert best_for_column(A, B, 0) == (1, [1, 0])
assert best_for_column(A, B, 1) == (F(5, 2), [F(1, 2), F(1, 2)])
value, x, j = optimal_commitment(A, B)
assert (value, x, j) == (F(5, 2), [F(1, 2), F(1, 2)], 1)
check(A, B, value, x, j)
# The only Nash equilibrium gives the leader 1, and committing to a pure strategy gives 2.
assert [e[2] for e in support_enumeration(A, B)] == [1]
assert max(leader_payoff(A, [int(t == i) for t in range(2)],
follower_response(A, B, [int(t == i) for t in range(2)])) for i in range(2)) == 2
def test_infeasible_column():
# Column 1 is strictly dominated for the follower, so its LP is infeasible.
A = [[0, 9], [1, 9]]
B = [[1, 0], [1, 0]]
assert best_for_column(A, B, 1) is None
assert optimal_commitment(A, B) == (1, [0, 1], 0)
def test_random_games_against_vertex_enumeration():
rng = random.Random(5150)
for _ in range(150):
A, B = random_game(rng, rng.randint(1, 4), rng.randint(1, 4))
value, x, j = optimal_commitment(A, B)
check(A, B, value, x, j)
assert value == brute_force(A, B)
def test_commitment_beats_every_nash_and_pure_commitment():
rng = random.Random(77)
for _ in range(100):
m, n = rng.randint(1, 3), rng.randint(1, 3)
A, B = random_game(rng, m, n)
value, _, _ = optimal_commitment(A, B)
eqs = support_enumeration(A, B)
assert eqs
for x, y, _ in eqs:
assert value >= sum(x[i] * A[i][k] * y[k] for i in range(m) for k in range(n))
for i in range(m):
e = [int(t == i) for t in range(m)]
assert value >= leader_payoff(A, e, follower_response(A, B, e))
def test_zero_sum_value_is_the_minimax_value():
rng = random.Random(404)
for _ in range(100):
m, n = rng.randint(1, 4), rng.randint(1, 4)
A = [[rng.randint(-9, 9) for _ in range(n)] for _ in range(m)]
B = [[-v for v in row] for row in A]
value, _, _ = optimal_commitment(A, B)
# maximize v+ - v- subject to v+ - v- <= sum_i x_i A[i][k] for every k, sum x = 1.
c = [1, -1] + [0] * m
A_ub = [[1, -1] + [-A[i][k] for i in range(m)] for k in range(n)]
status, _, minimax, _ = lp_max(c, A_ub, [0] * n, [[0, 0] + [1] * m], [1])
assert status == "optimal" and value == minimax
def test_ties_are_broken_for_the_leader():
# The follower is indifferent between the columns at every x; the leader gets the larger column.
A = [[1, 4], [2, 0]]
B = [[5, 5], [5, 5]]
value, x, j = optimal_commitment(A, B)
assert (value, x, j) == (4, [1, 0], 1)
History
Heinrich von Stackelberg studied leader-follower competition in markets in 1934, with pure strategies. Conitzer and Sandholm (2006) gave the multiple LPs algorithm for mixed commitments in two-player normal-form games and showed that the problem is NP-hard for Bayesian games with several follower types. Mixed integer programs for Bayesian followers and compact programs for resource allocation followed, and were used in deployed security systems; see security games.
References
- Vincent Conitzer and Tuomas Sandholm, Computing the optimal strategy to commit to, Proceedings of the 7th ACM Conference on Electronic Commerce (EC 2006), 82-90, the multiple LPs algorithm for two-player normal-form games and the NP-hardness for Bayesian games with several follower types
- Security and Game Theory, Stackelberg security games, strong Stackelberg equilibria and the algorithms used in deployed systems
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 3741KV Edit this page History Report an error Markdown