Modeladvanced
Security game
Also called Stackelberg security game.
A Stackelberg game in which a defender commits to a randomized allocation of resources over targets and an attacker picks a target to attack.
A security game is a two-player game between a defender and an attacker. The defender has identical resources, such as patrols or guards, and targets to protect. The defender first commits to a randomized way to place the resources. The attacker observes the coverage probabilities, not the random draw, and attacks one target. The solution concept is the Stackelberg equilibrium, and the general method for two-player games in normal form is the multiple LPs algorithm. This page is about the model: the payoffs depend only on the coverage of the attacked target, so the defender's exponentially many pure strategies reduce to coverage numbers. Stackelberg routing is a different use of the word, for a leader who controls part of the traffic in a network.
Notation: targets are . are the defender's payoffs when is attacked while it is covered or uncovered, and are the attacker's. is the probability that target is covered.
The machine
The defender's pure strategies are the sets of at most targets; each resource covers one target. In some versions every resource must be used, and the sets have exactly targets. A mixed strategy is a distribution over these sets, and its coverage vector is , with the probability that is in the drawn set. The attacker's pure strategies are the targets. If the attacker hits , the expected payoffs are
They depend only on , and not on the coverage of the other targets or on the correlation between them. The attacker plays a best response to , and ties are broken in the defender's favor. This is the strong Stackelberg equilibrium. Without that rule the defender's optimum may not be attained, as in any Stackelberg game.
Two facts make the coverage vector the right state:
- Every feasible vector can be sampled. A vector with and is the coverage of some distribution over sets of at most targets. Comb sampling gives one: lay the end to end on the interval , draw uniformly from , and cover the targets whose pieces contain . Each piece has length at most 1, so no target is hit twice, and target is covered with probability exactly . If , exactly targets are covered every time.
- Only the coverage matters. Two mixed strategies with the same give both players the same payoff on every target, so the defender can search over instead of over distributions.
What it counts
The input is payoffs and the number . The normal form of the same game has rows (or more, with sets of all sizes up to ), so a direct use of the multiple LPs algorithm on the bimatrix game is exponential in . In the coverage form, the cost is counted in linear programs with variables and constraints, and in the arithmetic to sample a set from , which is operations.
Relation to other models
- Bimatrix Stackelberg games. A security game is a bimatrix game with one row for each set of targets and one column for each target. The coverage form is a compact way to write the same LPs.
- Bayesian attackers. With several attacker types and a prior over them, the problem is NP-hard already in the bimatrix setting, and exact methods use a mixed integer program with binary variables for the attacked target of each type; see Bayesian games.
- Zero-sum security games. If and for every , the defender's commitment value is the value of the zero-sum game, by the minimax theorem, and it can be found by one LP.
- Schedules. If a resource can cover a set of targets (a schedule), the coverage vectors that can be sampled form a smaller polytope, and the problem can become hard.
Known results in the model
The optimal coverage comes from one LP for each target : among the coverage vectors under which is a best target for the attacker, find the one that is best for the defender.
SECURITY-GAME-SSE(Ud_c, Ud_u, Ua_c, Ua_u, k)- 1
for each target t = 1..T: - 2
LP_t: maximize c_t (Ud_c[t] - Ud_u[t]) + Ud_u[t] subject to c_s (Ua_c[s] - Ua_u[s]) + Ua_u[s] ≤ c_t (Ua_c[t] - Ua_u[t]) + Ua_u[t] for every s ≠ t ▷ t is a best target- 3
0 ≤ c_s ≤ 1 for every s, Σ_s c_s ≤ k ▷ Σ_s c_s = k if no resource may stay idle - 4
if LP_t is infeasible: skip t - 5
keep (value of LP_t, its solution c, t) if the value is the largest so far - 6
return the kept (value, c, t); sample the set of covered targets from c by comb sampling
- Polynomial time. The algorithm solves LPs, each with variables, so the optimal commitment is found in time polynomial in and the bit length of the payoffs. The proof is the same as for the multiple LPs algorithm: every coverage vector makes some target a best target, so it is feasible for that LP, and the best LP optimum is attained with the tie rule. Kiekintveld and others (2009) gave compact formulations of this kind that scale to games with many targets.
- Idle resources can help. If the defender wants the attacker to hit a target with a high payoff when covered, full coverage of that target pushes the attacker away. Take with , , and . If all three resources must be used, , the attacker hits target 2 and the defender gets 1. With , the optimum is : the attacker is indifferent between all three targets, hits target 1 by the tie rule, and the defender gets .
- More resources never hurt when resources may stay idle, because the feasible sets of the LPs grow with . The example above shows that this is false when every resource must be used.
- Indifference at the optimum. At an optimal , the attacker is usually indifferent between several targets, and the defender's coverage on each of them is set by the indifference principle.
- Schedules. Korzhyk, Conitzer and Parr (2010) studied the complexity when resources cover schedules of targets: some cases have polynomial algorithms and others are NP-hard.
- Stackelberg and Nash. Korzhyk and others (2011) showed that the Nash equilibria of a security game are interchangeable, and that under a natural restriction on the schedules, every Stackelberg strategy of the defender is also a Nash equilibrium strategy. So the defender can commit without knowing whether the attacker observes the coverage.
Example. Take three targets and one resource, with , , and .
| target | LP feasible | optimum of LP_t |
|---|---|---|
| 1 | yes | , value |
| 2 | yes | value |
| 3 | no | the attacker gets at least elsewhere |
At the attacker gets on targets 1 and 2 and on target 3, and the defender gets on targets 1 and 2. Target 3 cannot be the attacker's best target: with one resource, the attacker's payoffs on targets 1 and 2 cannot both drop to 1 or less, because that needs and . Comb sampling covers target 1 with probability and target 2 with probability .
The Python code solves LP_t for every target with the shared exact simplex code, with an option to require , and samples a set by comb sampling. The tests check the example, the idle resource example, monotonicity in , the comb sampling marginals, and, on 40 random games, the same value as the multiple LPs algorithm on the full normal form, in both versions.
Code
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]
security_game.py 105 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/security-game/. The authors are listed in prov/ZA3ZMX.jsonl.
"""Strong Stackelberg equilibria of security games by one compact LP per target, in Fraction.
A game has T targets and k identical defender resources; each resource covers one target.
For target t, dc[t] > du[t] are the defender's payoffs when t is attacked while covered or
uncovered, and ac[t] < au[t] are the attacker's. The defender commits to a coverage vector
c with 0 <= c[t] <= 1 and sum(c) <= k (or sum(c) = k if all resources must be used); the
attacker attacks a target with the highest expected payoff, breaking ties in the defender's
favor.
"""
from fractions import Fraction
from agtlp import lp_max
def utilities(game, c, t):
"""(defender, attacker) expected payoffs when target t is attacked under coverage c."""
dc, du, ac, au = game
ct = Fraction(c[t])
return ct * dc[t] + (1 - ct) * du[t], ct * ac[t] + (1 - ct) * au[t]
def best_for_target(game, k, t, use_all=False):
"""Return (value, c) of the best coverage under which t is a best target to attack, or None.
With use_all, the coverage must sum to exactly k (no resource stays idle), and k <= T.
"""
dc, du, ac, au = game
T = len(dc)
if use_all and not 0 <= k <= T:
raise ValueError("use_all needs 0 <= k <= T")
obj = [Fraction(0)] * T
obj[t] = Fraction(dc[t] - du[t])
A_ub, b_ub = [], []
for s in range(T):
if s != t:
# Attacker: c_s (ac_s - au_s) + au_s <= c_t (ac_t - au_t) + au_t.
row = [Fraction(0)] * T
row[s] = Fraction(ac[s] - au[s])
row[t] = -Fraction(ac[t] - au[t])
A_ub.append(row)
b_ub.append(Fraction(au[t] - au[s])) # @line L2
for s in range(T):
A_ub.append([Fraction(int(r == s)) for r in range(T)])
b_ub.append(Fraction(1))
if use_all:
status, c, value, _ = lp_max(obj, A_ub, b_ub, [[Fraction(1)] * T], [Fraction(k)])
else:
A_ub.append([Fraction(1)] * T)
b_ub.append(Fraction(k)) # @line L3
status, c, value, _ = lp_max(obj, A_ub, b_ub)
if status != "optimal": # @line L4
return None
return value + du[t], c
def strong_stackelberg(game, k, use_all=False):
"""Return (value, c, t): the defender's optimal coverage, its value and the attacked target."""
best = None
for t in range(len(game[0])): # @line L1
res = best_for_target(game, k, t, use_all)
if res is not None and (best is None or res[0] > best[0]): # @line L5
best = (res[0], res[1], t)
return best # @line L6
def attacker_response(game, c):
"""The attacker's best target under c, with ties broken in the defender's favor."""
T = len(game[0])
pay = [utilities(game, c, t) for t in range(T)]
top = max(a for _, a in pay)
return max((t for t in range(T) if pay[t][1] == top), key=lambda t: (pay[t][0], -t))
def comb_mixture(c):
"""Write coverage c (entries in [0, 1]) as a distribution over sets of at most ceil(sum c) targets.
Lay the c[t] end to end on [0, sum c]. For an offset u in [0, 1), cover the targets whose
intervals contain u, u + 1, u + 2, ... . Each target is covered for a set of offsets of
measure c[t], and no target twice, since its interval is at most 1 long. Return a list of
(probability, sorted tuple of targets), one entry for each interval of offsets.
"""
c = [Fraction(v) for v in c]
ends, total = [], Fraction(0)
for v in c:
total += v
ends.append(total)
cuts = sorted({Fraction(0), Fraction(1)} | {e - int(e) for e in ends if 0 < e - int(e)})
out = []
for lo, hi in zip(cuts, cuts[1:]):
u = (lo + hi) / 2
cover, start = [], Fraction(0)
for t, e in enumerate(ends):
# Target t holds the points in [start, e); find a point u + r in it.
r = 0
while u + r < e:
if u + r >= start:
cover.append(t)
break
r += 1
start = e
out.append((hi - lo, tuple(cover)))
return out
test_security_game.py 120 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/security-game/. The authors are listed in prov/ZA3ZMX.jsonl.
import random
from fractions import Fraction
from itertools import combinations
from agtlp import lp_max
from security_game import attacker_response, best_for_target, comb_mixture, strong_stackelberg, utilities
F = Fraction
# Three targets, one resource. Defender: covered / uncovered; attacker: covered / uncovered.
PAGE_GAME = ([1, 1, 0], [-5, -3, -1], [-1, -1, -1], [5, 3, 1])
def normal_form_commitment(game, k, use_all=False):
"""The optimal commitment over all sets of at most k targets (exactly k with use_all),
by one LP per attacked target on the normal form."""
dc, du, ac, au = game
T = len(dc)
sizes = [k] if use_all else range(k + 1)
subsets = [S for r in sizes for S in combinations(range(T), r)]
A = [[dc[t] if t in S else du[t] for t in range(T)] for S in subsets]
B = [[ac[t] if t in S else au[t] for t in range(T)] for S in subsets]
m = len(subsets)
best = None
for j in range(T):
A_ub = [[B[i][s] - B[i][j] for i in range(m)] for s in range(T) if s != j]
status, _, value, _ = lp_max([A[i][j] for i in range(m)], A_ub, [0] * len(A_ub), [[1] * m], [1])
if status == "optimal" and (best is None or value > best):
best = value
return best
def random_game(rng, T):
dc = [rng.randint(0, 6) for _ in range(T)]
du = [v - rng.randint(1, 8) for v in dc]
ac = [rng.randint(-6, 0) for _ in range(T)]
au = [v + rng.randint(1, 8) for v in ac]
return dc, du, ac, au
def check(game, k, value, c, t, use_all=False):
T = len(game[0])
assert all(0 <= v <= 1 for v in c) and (sum(c) == k if use_all else sum(c) <= k)
att = [utilities(game, c, s)[1] for s in range(T)]
assert att[t] == max(att)
assert utilities(game, c, t)[0] == value
assert utilities(game, c, attacker_response(game, c))[0] == value
def check_mixture(c, k):
mix = comb_mixture(c)
assert sum(p for p, _ in mix) == 1 and all(p > 0 for p, _ in mix)
for _, S in mix:
assert len(S) == len(set(S)) and len(S) <= k
for t in range(len(c)):
assert sum(p for p, S in mix if t in S) == c[t]
def test_page_example():
value, c, t = strong_stackelberg(PAGE_GAME, 1)
assert (value, c, t) == (F(-7, 5), [F(3, 5), F(2, 5), F(0)], 0)
check(PAGE_GAME, 1, value, c, t)
assert [utilities(PAGE_GAME, c, s)[1] for s in range(3)] == [F(7, 5), F(7, 5), 1]
assert [utilities(PAGE_GAME, c, s)[0] for s in range(3)] == [F(-7, 5), F(-7, 5), -1]
assert best_for_target(PAGE_GAME, 1, 2) is None
assert normal_form_commitment(PAGE_GAME, 1) == value == normal_form_commitment(PAGE_GAME, 1, True)
assert comb_mixture(c) == [(F(3, 5), (0,)), (F(2, 5), (1,))]
def test_random_games_against_normal_form():
rng = random.Random(1123)
for _ in range(40):
T = rng.randint(1, 4)
k = rng.randint(1, T)
game = random_game(rng, T)
for use_all in (False, True):
value, c, t = strong_stackelberg(game, k, use_all)
check(game, k, value, c, t, use_all)
check_mixture(c, k)
assert value == normal_form_commitment(game, k, use_all)
def test_idle_resources_can_help():
# With 3 resources on 3 targets, using all of them forces the attacker to target 1,
# while leaving target 0 partly open draws the attack there.
game = ([6, 1, 0], [0, 0, -4], [-3, 0, 0], [2, 6, 3])
assert strong_stackelberg(game, 3, use_all=True)[0] == 1
value, c, t = strong_stackelberg(game, 3)
assert value == F(12, 5) and t == 0 and c[0] < 1
def test_more_resources_never_hurt():
rng = random.Random(8)
for _ in range(30):
T = rng.randint(2, 5)
game = random_game(rng, T)
values = [strong_stackelberg(game, k)[0] for k in range(0, T + 1)]
assert all(a <= b for a, b in zip(values, values[1:]))
def test_comb_mixture_examples():
check_mixture([F(1, 2), F(1, 2), F(1, 2), F(1, 2)], 2)
assert comb_mixture([F(1, 2), F(1, 2), F(1, 2), F(1, 2)]) == [(F(1, 2), (0, 2)), (F(1, 2), (1, 3))]
check_mixture([F(1), F(1, 3), F(2, 3)], 2)
rng = random.Random(3)
for _ in range(200):
T = rng.randint(1, 6)
c = [F(rng.randint(0, 6), 6) for _ in range(T)]
k = -(-sum(c) // 1)
check_mixture(c, max(k, 0))
def test_no_resources():
game = ([1, 2], [-4, -1], [-1, -1], [3, 2])
value, c, t = strong_stackelberg(game, 0)
assert (value, c, t) == (-4, [0, 0], 0)
assert best_for_target(game, 0, 1) is None
References
- Security and Game Theory, the security game model, coverage vectors, strong Stackelberg equilibria and the deployed systems
- Christopher Kiekintveld, Manish Jain, Jason Tsai, James Pita, Fernando Ordonez and Milind Tambe, Computing optimal randomized resource allocations for massive security games, Proceedings of AAMAS 2009, pp. 689 to 696 (compact formulations in terms of the coverage of each target, for games with many targets)
- Dmytro Korzhyk, Vincent Conitzer and Ronald Parr, Complexity of computing optimal Stackelberg strategies in security resource allocation games, Proceedings of AAAI 2010, the complexity of the optimal commitment when resources cover schedules of targets
- Dmytro Korzhyk, Zhengyu Yin, Christopher Kiekintveld, Vincent Conitzer and Milind Tambe, Stackelberg vs. Nash in security games: an extended investigation of interchangeability, equivalence, and uniqueness, Journal of Artificial Intelligence Research 41, 2011, pp. 297 to 327 (Nash equilibria are interchangeable, and under a natural restriction every Stackelberg strategy is a Nash strategy)
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 | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag ZA3ZMX Edit this page History Report an error Markdown