Metrical Task Systems
Paging, list update and ski rental look different, but they share a structure: a system is in one of several states, each request makes some states expensive, and changing state costs something too. Borodin, Linial and Saks (1987; journal version 1992) captured this in one model, the metrical task system (MTS). It is general enough to contain almost every online problem of this course, and simple enough that its competitive ratio is known exactly for deterministic algorithms: on every metric with states. This lesson defines the model, computes OPT with the work function, runs the work-function algorithm and a greedy rule, and summarizes the randomized story.
The model
A metrical task system consists of a finite metric space with states and a starting state . A request is a task , the cost of processing it in each state. On task an online algorithm moves from its state to a state of its choice, after seeing , and pays
The total cost is the sum over all tasks. Infinite task costs forbid states. Examples:
- Paging with pages and cache size : states are the possible cache contents, the distance between two contents is the number of pages in which they differ, and a request for page costs 0 in every state containing and elsewhere.
- -server (next lesson): states are server configurations, distances are minimum-cost matchings.
- Ski rental is a two-state system with and every day's task costing 1 in "renting" and 0 in "owning". (MTS distances are symmetric, so moving back is allowed but never useful here.)
OPT and the work function
The offline optimum is a shortest path in a layered graph, computed by dynamic programming. The work function is the least cost of serving and ending in state :
(The move into before processing is already included in , because a work function satisfies .) The work function summarizes everything about the past that matters for the future, and it is the basis of the best deterministic algorithm.
Two online algorithms
- Greedy: move to the state minimizing , the cheapest response to the current task alone.
- Work-function algorithm (WFA): move to the state minimizing . It balances the offline cost of ending in against the cost of getting there now. Among the minimizers it picks one where the offline optimum ending there actually processed the task there, ; such a minimizer always exists, and without this tie rule WFA can stall in a state whose cost the work function no longer sees.
WFA-STEP(state, w, task) // w = work function before the task
0 for every s: w'(s) = min over x of ( w(x) + task(x) + d(x, s) )
1 next = a state minimizing w'(s) + d(state, s)
with w'(next) = w(next) + task(next) // exists; ties: stay
2 pay d(state, next) + task(next)
3 return next, w'Theorem (Borodin, Linial and Saks, 1992). For every metric space with states, the deterministic competitive ratio of metrical task systems is exactly : some deterministic algorithm is -competitive, and no deterministic algorithm is better on any -point metric. The work-function algorithm achieves as well.
The program compares greedy and WFA with OPT on seeded random tasks over 6 states on a line, and then runs an adversarial task sequence on two states.
import random
def wfa_step(d, w, cur, tau):
n = len(d)
new = [min(w[x] + tau[x] + d[x][s] for x in range(n)) for s in range(n)]
best = min(new[s] + d[cur][s] for s in range(n))
cands = [s for s in range(n) if new[s] + d[cur][s] == best and new[s] == w[s] + tau[s]]
nxt = cur if cur in cands else cands[0] # such a state always exists
return nxt, new, d[cur][nxt] + tau[nxt]
def greedy_step(d, cur, tau):
nxt = min(range(len(d)), key=lambda s: (d[cur][s] + tau[s], s != cur, s))
return nxt, d[cur][nxt] + tau[nxt]
def run(d, tasks, start=0):
w = [d[start][s] for s in range(len(d))]
cw = cg = start
cost_w = cost_g = 0
for tau in tasks:
cw, w, c = wfa_step(d, w, cw, tau)
cost_w += c
cg, c = greedy_step(d, cg, tau)
cost_g += c
return cost_w, cost_g, min(w)
n = 6
d = [[30 * abs(i - j) for j in range(n)] for i in range(n)] # states on a line, spacing 30
rng = random.Random(6)
tasks, centre = [], 0
for _ in range(2000): # a cheap region that jumps now and then
if rng.random() < 0.05:
centre = rng.randrange(n)
tasks.append([abs(s - centre) * rng.randint(1, 3) for s in range(n)])
wfa, greedy, opt = run(d, tasks)
print(f"random tasks on a 6-state line: OPT {opt}, WFA {wfa} ({wfa / opt:.2f}), greedy {greedy} ({greedy / opt:.2f})")
# two states at distance 100; the adversary charges 1 to whichever state the algorithm is in
d2 = [[0, 100], [100, 0]]
for name in ["WFA", "greedy"]:
w, cur, cost = [0, 100], 0, 0
for t in range(6000):
tau = [1 if s == cur else 0 for s in range(2)]
if name == "WFA":
cur, w, c = wfa_step(d2, w, cur, tau)
else:
w = [min(w[x] + tau[x] + d2[x][s] for x in range(2)) for s in range(2)]
cur, c = greedy_step(d2, cur, tau)
cost += c
print(f"adversary on 2 states, {name:6}: ALG {cost:5}, OPT {min(w):4}, ratio {cost / min(w):.3f}")random tasks on a 6-state line: OPT 3412, WFA 5961 (1.75), greedy 8500 (2.49)
adversary on 2 states, WFA : ALG 8871, OPT 3000, ratio 2.957
adversary on 2 states, greedy: ALG 6000, OPT 100, ratio 60.000
Greedy never moves against this adversary (staying costs only 1 per task, moving costs 100), so it pays forever while OPT moves once and pays nothing more: greedy's ratio grows with the length of the sequence. WFA moves when the work function says the other state has become cheap enough, and its ratio is close to , the best possible for deterministic algorithms on two states.
In C++
The work-function algorithm on a line metric with states, with tasks that make one moving region cheap, compared with the offline optimum. The work function update costs per task.
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <random>
#include <vector>
int main() {
const int n = 8, m = 5000;
std::vector<std::vector<long long>> d(n, std::vector<long long>(n));
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) d[i][j] = 30LL * std::abs(i - j);
std::mt19937 rng(8);
std::vector<long long> w(n);
for (int s = 0; s < n; ++s) w[s] = d[0][s];
int cur = 0, centre = 0;
long long cost = 0;
for (int t = 0; t < m; ++t) {
if (rng() % 20 == 0) centre = rng() % n; // the cheap region jumps
std::vector<long long> tau(n), nw(n);
for (int s = 0; s < n; ++s) tau[s] = std::abs(s - centre) * (1 + rng() % 3);
for (int s = 0; s < n; ++s) {
nw[s] = w[0] + tau[0] + d[0][s];
for (int x = 1; x < n; ++x) nw[s] = std::min(nw[s], w[x] + tau[x] + d[x][s]);
}
long long target = nw[0] + d[cur][0];
for (int s = 1; s < n; ++s) target = std::min(target, nw[s] + d[cur][s]);
int best = -1; // a minimizer that processed the task there
for (int s = 0; s < n; ++s)
if (nw[s] + d[cur][s] == target && nw[s] == w[s] + tau[s] && (best < 0 || s == cur)) best = s;
w = nw;
cost += d[cur][best] + tau[best];
cur = best;
}
long long opt = *std::min_element(w.begin(), w.end());
std::printf("n = %d states, %d tasks: OPT %lld, WFA %lld, ratio %.3f (bound 2n - 1 = %d)\n",
n, m, opt, cost, double(cost) / opt, 2 * n - 1);
}n = 8 states, 5000 tasks: OPT 11228, WFA 23402, ratio 2.084 (bound 2n - 1 = 15)
Ski rental as a task system
The general guarantee has a price. Run WFA on ski rental written as a two-state task system (renting and owning at distance , each day costing 1 in "renting" and 0 in "owning"), and compare it with the break-even rule, which knows that moving back is never useful.
def wfa_step(d, w, cur, tau):
n = len(d)
new = [min(w[x] + tau[x] + d[x][s] for x in range(n)) for s in range(n)]
best = min(new[s] + d[cur][s] for s in range(n))
cands = [s for s in range(n) if new[s] + d[cur][s] == best and new[s] == w[s] + tau[s]]
return (cur if cur in cands else cands[0]), new
B = 10
d = [[0, B], [B, 0]] # state 0 = renting, state 1 = owning
worst_wfa = worst_be = 0
for season in range(1, 4 * B):
w, cur, cost = [0, B], 0, 0
for day in range(season):
tau = [1, 0]
nxt, w = wfa_step(d, w, cur, tau)
cost += d[cur][nxt] + tau[nxt]
cur = nxt
opt = min(season, B)
break_even = season if season < B else 2 * B - 1
worst_wfa = max(worst_wfa, cost / opt)
worst_be = max(worst_be, break_even / opt)
print(f"B = {B}: worst ratio of WFA {worst_wfa:.2f} (2n - 1 = 3), of break-even {worst_be:.2f} (2 - 1/B)")B = 10: worst ratio of WFA 3.00 (2n - 1 = 3), of break-even 1.90 (2 - 1/B)
WFA rents for days before buying, because in a symmetric task system the adversary could later make "owning" expensive and force a move back; it pays against , exactly the bound . The break-even rule exploits the special structure of ski rental and achieves . The MTS bound is the price of handling every task sequence on the metric, not just this one.
The lower bound idea
The lower bound uses the adversary of the two-state experiment, generalized: it always gives a small positive cost to the algorithm's current state and 0 elsewhere. Every online algorithm must eventually move or pay forever; when it moves, it pays the distance. Borodin, Linial and Saks compare the online cost with the average of offline strategies and show that the online algorithm pays at least times that average. For the uniform metric this is easy to see in the limit : the online algorithm is chased through all states in a cycle, while an offline algorithm can sit in the state that the online algorithm will visit last.
Randomized MTS
Against an oblivious adversary randomization helps exponentially, as in paging:
| Metric | Randomized ratio | Reference |
|---|---|---|
| uniform, states | between and | Borodin, Linial and Saks, 1992 (refined by Irani and Seiden, 1998) |
| every -point metric (upper bound) | Bubeck, Cohen, Lee and Lee, 2019; Coester and Lee, 2019 | |
| every -point metric (lower bound) | Bubeck, Coester and Rabani, 2023 (earlier ) | |
| some -point metrics (lower bound) | Bubeck, Coester and Rabani, 2023 |
So the worst-case randomized ratio over all -point metrics is , settled only in 2023, while for specific metrics (uniform, for instance) it is . The upper bounds come from embedding the metric into hierarchically separated trees and running online mirror descent with an entropic regularizer on the tree, a technique also behind recent -server results (Randomized k-Server).
Test yourself
In a metrical task system, what does an online algorithm pay for task when it moves from to ?
What is the deterministic competitive ratio of metrical task systems on an -point metric?
Why is the greedy rule not competitive for MTS?
- It is too slow
- An adversary charging a tiny cost to its current state keeps it from ever moving, so it pays forever while OPT moves once
- It always moves
- It ignores distances
What did Bubeck, Coester and Rabani (2023) show about randomized MTS?
- That holds on every metric
- That some -point metrics need ratio , matching the known upper bound
- That randomization does not help
- That the deterministic ratio is