chc.games¶
Stability tier: experimental.
Its research half moved to chc.research.games in 0.19.0.
Game-theoretic control: agent equilibria + differentiable Stackelberg (leader) control.
Marketplaces have strategic, mobile agents, so SUTVA fails -- the platform is a Stackelberg leader
over the agents' equilibrium, and its action is optimised accounting for the induced best response.
These are the reusable methods (equilibrium solver + bilevel allocator); the benchmark task that
scores them (the zone-incentive game) lives in causaldyn-bench.
The solver is a certified fixed point, not a fixed iteration count. fixed_point runs to
a tolerance under lax.while_loop and differentiates by the implicit function theorem, so
(a) non-convergence is reported in EquilibriumSolution instead of returned silently,
(b) the backward pass costs one adjoint solve rather than a tape over every iterate, and
(c) one contraction certificate covers both passes at once: the adjoint iteration
w <- w_bar + (dT/dx)^T w is the Neumann series for (I - dT/dx)^{-1}, so it converges
exactly when the forward map contracts.
The three certificates that price this solver and the leader on generated congestion games --
congestion_contraction_certificate, which checks (c),
equilibrium_monotonicity_certificate and
equilibrium_transfer_certificate -- moved to chc.research.games
in 0.19.0.
The leader's ascent, stackelberg_allocation, finds a local maximum of any objective. For
the logit congestion game with completed rides as the objective,
exact_congestion_allocation finds the global one, and the vertex that certifies it.
EquilibriumSolution
¶
Bases: Module
A solved fixed point together with the evidence that it is one.
converged is a traced boolean, so it cannot raise inside jit -- checking it (or feeding
it to a certificate) is the caller's job. That is the point: a non-equilibrium can no longer
leave this module disguised as an equilibrium.
CongestionAllocation
dataclass
¶
The global optimum exact_congestion_allocation returns, and its certificate.
The allocation completes value rides. At its equilibrium the zones at_demand hold
exactly their demand, partial holds more drivers than its floor and fewer than its demand,
and every other zone sits at its floor share at level, funded by nothing but its share of
the budget left over, which moves no driver. Reaching that costs spent, at most the budget.
No allocation completes more: at level_below, a level just below level, every vertex
costs cost_below or more, above the budget, or no vertex fills the deficit without a zone
passing its demand (cost_below is inf); and an allocation that completed more would reach
a vertex of a level that low (the exchange step of exact_congestion_allocation).
shadow_price is what one more unit of budget completes there, the budget's Lagrange
multiplier: on SharedStateMarket(seed=0..7) at budgets from 0.5 to 60 it equals central
differences of value in the budget to 1e-8, and it is 0 where more budget completes
nothing. It is 0 too where a positive budget cannot lower the level below the free one by the
bisection's last step: lowering the level by delta costs at most delta * sum(q) / q_p
through any zone p with room (phi concave, the floors convex in the level), so a budget
short of it buys at most mass*beta*delta/budget rides a unit
(validation/marketplace_shadow_price.mac). Zone values (100, 0, -50) at a budget of 3 read
nan there before 0.24.0.
PerDriverAllocation
dataclass
¶
The optimum per_driver_congestion_allocation returns, and the bound certifying it.
Each driver in zone i is paid allocation[i], so the money paid is
mass * sum(shares * allocation), spent, at most the budget to the solver's tolerance.
The allocation completes value rides; no allocation whose money paid is within the budget
completes more than bound, the Lagrangian dual's value at the multipliers read off the
optimum, so gap bounds the rides the allocation leaves undone. shadow_price is the
budget's multiplier, what one more unit of money completes there.
project_simplex(v, z)
¶
Euclidean projection of v onto {u >= 0, sum u = z} (Duchi et al. 2008).
fixed_point(f, params, x_init, *, tol=1e-06, max_iter=500)
¶
Solve x = f(params, x) to a tolerance, differentiable in params (implicit, not tape).
tol is on the RELATIVE residual (see _relative_residual). The iteration runs until
the residual is at most tol, or for max_iter steps; converged reads it against
max(tol, 64 eps), eps the machine epsilon of x_init's dtype. In float64 that is
tol down to 1.4e-14. In float32 the residual of a solve that sits on the equilibrium cannot
fall below its own rounding: on the logs of SharedStateMarket(seed=0..29), 400 blocks each
and both payments, the logit congestion solve ended above 1e-6 on 88 of 24,000 blocks, at most
2.0e-6, 16.6 epsilons: read against tol alone, the flag was False on all of them. The
returned x does not depend on the floor: such a solve iterates to max_iter as before.
Reverse-mode only: custom_vjp defines no JVP, so jax.jacfwd through the solve will fail.
The gradient is the exact one at the fixed point, independent of how many iterations the forward
solve took -- which is what makes the tolerance-based lax.while_loop usable at all, since
JAX has no reverse rule for it.
softmax_congestion_equilibrium(attract, u, congestion, mass, beta=2.5, *, damping=0.5, tol=1e-06, max_iter=500)
¶
Agent best-response equilibrium: the damped softmax congestion fixed point of the mass.
Agents flow toward higher value attract + u - congestion*x/mass (crowding lowers it); the
iteration T(x) = (1-d)*x + d*mass*softmax(beta*value) converges to the Wardrop/logit
equilibrium (sum x = mass, since the softmax Jacobian annihilates the constants). The
uniform Jacobian bound certifies contraction when 0 < damping < 4/(2 + beta*congestion) --
at the default d = 1/2 that is exactly beta*congestion < 6. Above it the iteration
may 2-cycle (the bound is sufficient, not necessary), which is why the result
carries its own residual; the remedy is a smaller damping, not a different game, since the
equilibrium itself stays non-expansive to operator perturbations
(equilibrium_transfer_certificate).
congestion_damping returns the fastest contracting choice for a given game.
congestion_damping(beta, congestion)
¶
Modulus-optimal damping 4/(4 + beta*congestion) for the logit congestion iteration.
The two extreme eigenvalues of T' are 1-d and 1 - d*(1 + kappa/2); equalising their
magnitudes gives d* = 4/(4+kappa), and any 0 < d < 4/(2+kappa) clears the uniform bound.
So EVERY game has a contracting damping -- the kappa < 6 ceiling is a property of the
hard-coded d = 1/2, not of the game. Kept opt-in: the default stays 1/2 so shipped
numbers do not move.
congestion_contraction_modulus(beta, congestion, damping=0.5)
¶
Certified contraction modulus mu = 1 - ||T'||_2 of the damped logit congestion map.
T'(x) = (1-d)*I - d*beta*c*(diag(s) - s s^T), and v^T (diag(s) - s s^T) v = Var_s(v), so
by Popoviciu the softmax Jacobian has spectrum in [0, 1/2] (sharp at s = (1/2,1/2,0..)).
T' is symmetric, hence ||T'||_2 <= max(1-d, d*(1 + beta*c/2) - 1), and that UNIFORM
bound leaves a positive modulus iff 0 < d < 4/(2 + beta*c) -- at d = 1/2 exactly
beta*c < 6. Both sides of the interval bind: at d = 0 the map is the identity and the
1-d branch is already 1. Sharp over the class, SUFFICIENT for a fixed game: the worst-case
eigenvalue is attained only at a two-point uniform s; measured, the default map still
converges at beta*c = 7 and 2-cycles from 8. Derived in
validation/congestion_contraction.mac, cross-checked in .py;
proofs/congestion_contraction.v proves the d = 1/2 case, beta*c < 6, and the
general-d interval is Maxima's.
This is the SOLVER's rate, not the equilibrium's conditioning -- see
equilibrium_transfer_certificate, where using 1/mu as a sensitivity constant is
measured to overstate the truth by up to 200x.
stackelberg_allocation(objective, n, budget, steps=400, lr=0.05, *, start=None)
¶
Differentiable-bilevel leader allocation: an ascent on objective(u) over the budget
simplex.
objective is evaluated through the equilibrium (so jax.grad differentiates the bilevel
problem). Adam handles the scale; the plan is projected onto the budget simplex each step, and
the best feasible allocation seen is returned.
The ascent starts at the even split and ends at a local maximum at best, not the global one:
the objective through an equilibrium need not be concave in u, and steps may end it
short of either. Three zones of attraction (0.1, 0.5, 2.0), worth (1, 1, 0.2) a driver,
at congestion 1.5 and mass 6, end with the whole budget of 1 on the first zone, worth 2.973,
where the whole budget on the second is worth 3.184. Compare the result with the vertices or
other starts where the global plan matters. Where the objective is the rides a logit
congestion equilibrium completes, exact_congestion_allocation finds the global plan:
on the 30 markets SharedStateMarket(seed=0..29) in float64, 2500 steps of this ascent
ended short of it on 17, by up to 0.215 rides.
start, where given, replaces the even split: the ascent starts at its projection onto the
budget simplex.
Raises:
| Type | Description |
|---|---|
ValueError
|
on a |
exact_congestion_allocation(value, demand, beta, congestion, mass, budget)
¶
The split of a lump-sum incentive budget that completes the most rides through a logit congestion equilibrium, found exactly, with the vertex that certifies it.
The game is chc.marketplace.SharedStateMarket's. mass drivers settle at the
shares s = x / mass = softmax(beta * (value + u - congestion * s)), the incentive
u >= 0 with sum(u) = budget adds to each zone's value, and zone i completes
min(demand_i, mass * s_i) rides. value is a zone's value to a driver before crowding
and incentive: the market's attract, a fitted equilibrium's attract * demand.
sis the equilibrium underuexactly wherephi_i(s_i) = log(s_i)/beta + congestion*s_i - value_iequalsu_iplus one constant shared by every zone (Berry 1994's inversion). So the least budget that reachesspays each zonephi_i(s_i) - min_j phi_j(s_j), and what is left of the budget, spread evenly, moves no driver.- The zones paid nothing share one level
muand sit at their floor sharesg_i(mu),W(beta*congestion*exp(beta*(mu + value_i)))/(beta*congestion)withWLambert's, increasing and convex inmu. An allocation whose least level ismuholds every zone at its floor or above, so it completes at mostU(mu) = mass - sum_i (mass*g_i(mu) - demand_i)+, which falls asmurises. U(mu)is reached where the deficit1 - sum(g(mu))can be filled, with no zone past its demand, for at most the budget.phiis concave, so the least such cost is a concave minimisation over a box cut by a hyperplane, and lies at a vertex (Rockafellar 1970, §32): every funded zone at its demand but at most one. That cost falls asmurises, so bisection finds the least level the budget reaches.- That level's
Uis the global maximum: given an allocation with a funded zone past its demand, raise the level, holding the other funded zones and letting that zone give up the drivers the floors take. The bill falls, at-#funded - phi_k' * sum(g'), and the drivers wasted do not grow, until no funded zone is past its demand: a vertex of a level the budget reaches.
Global separable concave minimisation is NP-hard (Moré and Vavasis 1991), and the enumeration
is the price of the certificate: a level reads k * 2**(k - 1) vertices over the k
zones whose floor lies below their demand, at most 24,576 on the 12 zones of
SharedStateMarket, and a solve at most 2*j + 51 levels, where the search below the free
level doubles its reach j times: 50 or 51 on each of the markets
SharedStateMarket(seed=0..29) at either precision, 0.30 to 1.25 million vertices in all.
Past 20 zones it refuses. Exact where the objective is completed rides and the budget a lump
sum paid into the drivers' value;
validation/exact_congestion_allocation.mac checks the inversion, the floor, the bound and
a vertex's cost at a rational point. In float64 throughout, so the optimum does not move with
the caller's precision; an equilibrium solved to a tolerance reproduces value to that
tolerance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
ArrayLike
|
|
required |
demand
|
ArrayLike
|
|
required |
beta
|
float
|
the drivers' sensitivity to value, positive. |
required |
congestion
|
float
|
the crowding penalty, non-negative. |
required |
mass
|
float
|
the drivers, positive. |
required |
budget
|
float
|
the incentive to split, non-negative. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
on a |
per_driver_congestion_allocation(value, demand, beta, congestion, mass, budget)
¶
The incentive per driver that completes the most rides through a logit congestion equilibrium when the money paid, not the sum of the incentives, is the budget, with the dual bound that certifies it.
The game is exact_congestion_allocation's: mass drivers settle at the shares
s = softmax(beta * (value + u - congestion * s)) and zone i completes
min(demand_i, mass * s_i). Here each driver in zone i is paid u_i >= 0, so the money
paid is sum_i u_i * mass * s_i (Ong, Freund and Crapis 2021, Lyft's repositioning
incentives). By Berry's (1994) inversion the cheapest u reaching s pays
phi_i(s_i) - min_j phi_j(s_j), phi_i(s) = log(s)/beta + congestion*s - value_i, so the
money that reaches s is mass * (sum_i s_i*phi_i(s_i) - min_j phi_j(s_j)). s*phi(s)
is convex, its second derivative (2*beta*congestion*s + 1)/(beta*s), and phi is
concave, so -min phi is convex: the shares the budget reaches are a convex set, and the
rides, concave in the shares, are maximised by a convex program. Paid as a lump sum,
sum(u) = budget, the reachable shares are not convex, and
exact_congestion_allocation enumerates vertices instead.
The program is solved through its KKT conditions. The zones paid nothing share one level t
at their floor shares, as in exact_congestion_allocation; the paid zones below their
demand share one margin psi_i(s_i) = phi_i(s_i) + s_i*phi_i'(s_i), the slope of their
cost, since a zone's cost mass*s*(phi(s) - t) is convex: the deficit the floors leave is
filled to one margin, the least money that fills it, with no zone past its demand. That least
money falls as t rises, and the rides a level allows,
mass - sum_i (mass*g_i(t) - demand_i)+, fall too, so bisection finds the least level the
budget reaches, as for the lump sum. Where the drivers outnumber the demand, the search stops
at the level that fills every zone to its demand. The multipliers are read off the optimum
and the Lagrangian dual is evaluated there, zone by zone, each zone's term a one-dimensional
concave supremum: bound is at least the rides of any allocation within the budget (weak
duality), whatever the multipliers, and gap is the certificate. On the markets
SharedStateMarket(seed=0..5) at budgets from 0 to 20 it reads below 1e-12.
validation/per_driver_allocation.mac checks the convexity, the cost of the shares, the
margin's inverse, the dual's per-zone supremum and the KKT reading. In float64 throughout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
ArrayLike
|
|
required |
demand
|
ArrayLike
|
|
required |
beta
|
float
|
the drivers' sensitivity to value, positive. |
required |
congestion
|
float
|
the crowding penalty, non-negative. |
required |
mass
|
float
|
the drivers, positive. |
required |
budget
|
float
|
the money to pay, non-negative. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
on a |