Uncapacitated Facility Location
A company must decide which warehouses to open and which customers each one serves. Opening warehouse costs ; serving customer from warehouse costs . The uncapacitated facility location problem (UFL) asks for a set of open facilities and an assignment of every client to an open facility minimizing the total opening plus connection cost. In the metric version the costs come from distances between facilities and clients and satisfy the triangle inequality. UFL is a favourite test bed for approximation techniques: LP rounding, primal-dual, dual fitting and local search all give constant factors for it. This lesson is an overview, with the primal-dual 3-approximation of Jain and Vazirani (2001) worked out and run.
The LP and its dual
With = "facility is open" and = "client uses facility ":
The dual has a natural reading: is the budget client brings. Of it, pays the connection to , and anything above that, , is offered towards opening facility . A facility cannot be offered more than its opening cost.
The Jain-Vazirani algorithm
Phase 1 (grow the budgets). All clients start unconnected with . Raise the budgets of all unconnected clients at the same speed (think of time , with ).
- When reaches , the edge becomes tight, and from then on contributes to facility .
- When the contributions to a facility reach , it is temporarily opened, and every client with a tight edge to it is connected and stops growing.
- A client whose budget reaches for an already temporarily open is connected immediately and stops.
Phase 1 ends when every client is connected. The final (with ) is a feasible dual.
Phase 2 (resolve conflicts). Two temporarily open facilities conflict if some client contributes positively to both. Go through them in order of opening time and keep a maximal set of pairwise non-conflicting facilities. Open exactly , and connect every client to its nearest open facility.
Theorem (Jain and Vazirani, 2001). For metric costs, the solution satisfies
so it is a 3-approximation.
Proof sketch. Each facility in is paid for exactly by the contributions of its clients, and no client contributes to two facilities of , so is covered by disjoint parts of the budgets. A client with a tight edge to some (a directly connected client) pays its connection and its contribution out of , in total exactly . Any other client was connected in phase 1 through a temporarily open facility with . If , it conflicts with some opened no later, through a client with tight edges to both. Because opened no later than and stopped growing when its first tight facility opened, (where is the opening time of ), and by the triangle inequality . Summing, connection costs plus three times the opening costs are at most , and the dual value is at most OPT.
The bound is stronger than a 3-approximation: opening costs are counted three times on the left. Algorithms with this Lagrangian multiplier preserving property are exactly what is needed to handle the related -median problem by Lagrangian relaxation, the original motivation of Jain and Vazirani.
import random
from fractions import Fraction as Fr
from itertools import combinations
from scipy.optimize import linprog
rng = random.Random(3)
fac = [(rng.randint(0, 20), rng.randint(0, 20)) for _ in range(6)]
cli = [(rng.randint(0, 20), rng.randint(0, 20)) for _ in range(12)]
f = [rng.randint(8, 25) for _ in fac]
c = [[abs(a[0] - b[0]) + abs(a[1] - b[1]) for b in cli] for a in fac] # Manhattan distance: a metric
F, D = range(len(fac)), range(len(cli))
def jain_vazirani():
v, frozen, opened, t = {}, set(), {}, Fr(0)
def paid(i, t):
return sum(max(0, (v[j] if j in frozen else t) - c[i][j]) for j in D)
while len(frozen) < len(cli):
cands = [Fr(c[i][j]) for i in F for j in D if j not in frozen and c[i][j] > t]
for i in F:
if i not in opened:
rate = sum(1 for j in D if j not in frozen and c[i][j] <= t)
if rate:
cands.append(t + (f[i] - paid(i, t)) / rate)
t = min(x for x in cands if x >= t)
for i in F: # facilities fully paid at time t open
if i not in opened and paid(i, t) >= f[i]:
opened[i] = t
for j in D: # clients with a tight edge to an open facility stop
if j not in frozen and any(c[i][j] <= t for i in opened):
frozen.add(j)
v[j] = t
order = sorted(opened, key=lambda i: (opened[i], i))
I = []
for i in order: # phase 2: maximal non-conflicting set
if all(not any(v[j] > c[i][j] and v[j] > c[k][j] for j in D) for k in I):
I.append(i)
cost = sum(f[i] for i in I) + sum(min(c[i][j] for i in I) for j in D)
return I, cost, sum(v.values()), order
def optimum():
return min(sum(f[i] for i in S) + sum(min(c[i][j] for i in S) for j in D)
for k in range(1, len(fac) + 1) for S in combinations(F, k))
def lp_value():
nf, nc = len(fac), len(cli)
cost = f + [c[i][j] for i in F for j in D]
A, b = [], []
for j in D: # -sum_i x_ij <= -1
A.append([0] * nf + [-1 if jj == j else 0 for i in F for jj in D]); b.append(-1)
for i in F:
for j in D: # x_ij - y_i <= 0
row = [0] * (nf + nf * nc)
row[i], row[nf + i * nc + j] = -1, 1
A.append(row); b.append(0)
return linprog(cost, A_ub=A, b_ub=b, bounds=[(0, None)] * len(cost), method="highs").fun
I, alg, dual, temp = jain_vazirani()
print("temporarily opened (in order):", temp, " kept:", I)
print(f"dual sum {float(dual):.2f} <= LP {float(lp_value()):.2f} <= OPT {optimum()} <= ALG {alg} <= 3 * dual = {float(3 * dual):.2f}")temporarily opened (in order): [0, 1, 4] kept: [0, 1]
dual sum 123.00 <= LP 124.00 <= OPT 124 <= ALG 124 <= 3 * dual = 369.00
Facility 4 opened in phase 1 but conflicted with an earlier one and was closed again. On this instance the result is optimal, and the dual is within one unit of the LP: the guarantee of 3 is a worst case that random instances rarely approach.
In C++
Phase 1 as an event simulation in floating point, phase 2 and the final assignment, on a small line instance: facilities and clients on a road, with distance .
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
int main() {
std::vector<double> facPos = {0, 5, 10, 15}, f = {10, 10, 10, 10};
std::vector<double> cliPos = {1, 3, 9, 12, 14, 19, 22, 28, 31};
int nf = facPos.size(), nc = cliPos.size();
auto c = [&](int i, int j) { return std::fabs(facPos[i] - cliPos[j]); };
std::vector<double> v(nc, 0), openTime(nf, -1);
std::vector<bool> frozen(nc, false);
double t = 0;
auto paid = [&](int i, double tt) {
double s = 0;
for (int j = 0; j < nc; j++) s += std::max(0.0, (frozen[j] ? v[j] : tt) - c(i, j));
return s;
};
while (std::count(frozen.begin(), frozen.end(), true) < nc) {
double next = 1e18;
for (int i = 0; i < nf; i++)
for (int j = 0; j < nc; j++) if (!frozen[j] && c(i, j) > t) next = std::min(next, c(i, j));
for (int i = 0; i < nf; i++) {
if (openTime[i] >= 0) continue;
int rate = 0;
for (int j = 0; j < nc; j++) if (!frozen[j] && c(i, j) <= t) rate++;
if (rate) next = std::min(next, t + (f[i] - paid(i, t)) / rate);
}
t = next;
for (int i = 0; i < nf; i++) if (openTime[i] < 0 && paid(i, t) >= f[i] - 1e-9) openTime[i] = t;
for (int j = 0; j < nc; j++)
for (int i = 0; i < nf; i++)
if (!frozen[j] && openTime[i] >= 0 && c(i, j) <= t + 1e-9) { frozen[j] = true; v[j] = t; }
}
std::vector<int> order;
for (int i = 0; i < nf; i++) if (openTime[i] >= 0) order.push_back(i);
std::sort(order.begin(), order.end(), [&](int a, int b) { return openTime[a] < openTime[b]; });
std::vector<int> I;
for (int i : order) {
bool ok = true;
for (int k : I)
for (int j = 0; j < nc; j++) if (v[j] > c(i, j) + 1e-9 && v[j] > c(k, j) + 1e-9) ok = false;
if (ok) I.push_back(i);
}
double cost = 0, dual = 0;
for (int i : I) cost += f[i];
for (int j = 0; j < nc; j++) {
double best = 1e18;
for (int i : I) best = std::min(best, c(i, j));
cost += best;
dual += v[j];
}
std::printf("temporarily opened:");
for (int i : order) std::printf(" %d (t = %.2f)", i, openTime[i]);
std::printf("\nkept:");
for (int i : I) std::printf(" %d", i);
std::printf("\ncost %.2f, dual %.2f, cost / dual %.3f\n", cost, dual, cost / dual);
}temporarily opened: 2 (t = 5.67) 3 (t = 6.67) 0 (t = 7.00)
kept: 2 0
cost 91.00, dual 73.67, cost / dual 1.235
Facility 3 opened second, but a client between positions 10 and 15 contributed to both 2 and 3, so phase 2 keeps only the earlier one, and the clients near 15 travel to facility 2. The cost stays well within three times the dual.
Without the triangle inequality
The metric assumption is essential. Hochbaum (1982) observed that set cover is a special case of non-metric UFL: make every set a facility with opening cost , every element a client, and let if and (in practice a huge number) otherwise. A solution of finite cost opens a family of sets covering every element, and its cost is the weight of that family, so the two optima coincide and the hardness of set cover carries over. Greedy over "stars" (a facility with a group of clients) gives the matching upper bound.
from itertools import combinations
sets = {"A": ({1, 2, 3}, 4), "B": ({3, 4, 5}, 4), "C": ({5, 6, 1}, 4), "D": ({2, 4, 6}, 5), "E": ({1, 2, 3, 4, 5, 6}, 13)}
elements = sorted(set().union(*(S for S, _ in sets.values())))
BIG = 10 ** 6
f = {name: w for name, (S, w) in sets.items()}
c = {(name, e): 0 if e in S else BIG for name, (S, _) in sets.items() for e in elements}
def ufl_cost(open_set):
return sum(f[i] for i in open_set) + sum(min(c[(i, e)] for i in open_set) for e in elements)
def set_cover_cost(chosen):
covered = set().union(*(sets[i][0] for i in chosen))
return sum(sets[i][1] for i in chosen) if covered == set(elements) else None
names = list(sets)
subsets = [S for k in range(1, len(names) + 1) for S in combinations(names, k)]
best_ufl = min(subsets, key=ufl_cost)
best_sc = min((S for S in subsets if set_cover_cost(S) is not None), key=set_cover_cost)
print("UFL optimum:", best_ufl, ufl_cost(best_ufl))
print("set cover optimum:", best_sc, set_cover_cost(best_sc))UFL optimum: ('A', 'B', 'C') 12
set cover optimum: ('A', 'B', 'C') 12
Both problems choose the same sets at the same cost. In the metric version this construction is impossible: an element at distance 0 from two sets would force those sets to be at distance 0 from each other.
The landscape
| Result | Ratio | Method |
|---|---|---|
| Hochbaum (1982), non-metric costs | greedy, as set cover; optimal up to the constant, since set cover reduces to non-metric UFL | |
| Shmoys, Tardos and Aardal (1997) | 3.16 | LP rounding with filtering: the first constant factor |
| Jain and Vazirani (2001) | 3 | primal-dual (this lesson) |
| Jain, Mahdian, Markakis, Saberi and Vazirani (2003) | 1.61 | greedy analysed by dual fitting with a factor-revealing LP |
| Byrka (2007); Li (2013) | 1.5; 1.488 | LP rounding combined with the greedy algorithm |
| Guha and Khuller (1999) | no 1.463 | hardness, under the assumption NP DTIME |
The gap between 1.463 and 1.488 is one of the smallest between known algorithms and hardness for a classical problem. The capacitated version (each facility serves at most clients) is much harder for LP methods, since its natural LP has an unbounded integrality gap, yet local search gives constant factors.
The mini project of this course puts facility location next to set cover in one toolkit: Mini Project: A Set Cover and Facility Location Toolkit.
Test yourself
In the Jain-Vazirani dual, what does represent?
- The connection cost of client
- The part of client 's budget offered towards opening facility , beyond what the connection costs
- The opening cost of facility
- A primal variable
Why do two kept facilities never share a contributing client?
- Because facilities are far apart
- Phase 2 keeps a set of pairwise non-conflicting facilities, where conflict means a client contributes positively to both
- Because each client contributes to one facility only in phase 1
- Because of complementary slackness
A client whose phase-1 facility was closed is reconnected at cost at most what?
- , through a path of three edges whose costs are bounded by budgets, using the triangle inequality
- OPT