The facility location problem
Страница в процессе перевода. |
This tutorial was generated using Literate.jl. Download the source as a .jl
file.
This tutorial was originally contributed by Mathieu Tanneau and Alexis Montoison.
Uncapacitated facility location
Problem description
We are given
-
A set of clients
-
A set of sites where a facility can be built
Decision variables Decision variables are split into two categories:
-
Binary variable indicates whether facility is built or not
-
Binary variable indicates whether client is assigned to facility
Objective The objective is to minimize the total cost of serving all clients. This costs breaks down into two components:
-
Fixed cost of building a facility.
In this example, this cost is .
-
Cost of serving clients from the assigned facility.
In this example, the cost of serving client from facility is the Euclidean distance between the two.
Constraints
-
Each customer must be served by exactly one facility
-
A facility cannot serve any client unless it is open
MILP formulation
The problem can be formulated as the following MILP:
where the first set of constraints ensures that each client is served exactly once, and the second set of constraints ensures that no client is served from an unopened facility.
Problem data
To ensure reproducibility, we set the random number seed:
Random.seed!(314)
Random.TaskLocalRNG()
Here’s the data we need:
# Number of clients
m = 12
# Number of facility locations
n = 5
# Clients' locations
x_c, y_c = rand(m), rand(m)
# Facilities' potential locations
x_f, y_f = rand(n), rand(n)
# Fixed costs
f = ones(n);
# Distance
c = zeros(m, n)
for i in 1:m
for j in 1:n
c[i, j] = LinearAlgebra.norm([x_c[i] - x_f[j], y_c[i] - y_f[j]], 2)
end
end
Display the data
Plots.scatter(
x_c,
y_c;
label = "Clients",
markershape = :circle,
markercolor = :blue,
)
Plots.scatter!(
x_f,
y_f;
label = "Facility",
markershape = :square,
markercolor = :white,
markersize = 6,
markerstrokecolor = :red,
markerstrokewidth = 2,
)
JuMP implementation
Create a JuMP model
model = Model(HiGHS.Optimizer)
set_silent(model)
@variable(model, y[1:n], Bin);
@variable(model, x[1:m, 1:n], Bin);
# Each client is served exactly once
@constraint(model, client_service[i in 1:m], sum(x[i, j] for j in 1:n) == 1);
# A facility must be open to serve a client
@constraint(model, open_facility[i in 1:m, j in 1:n], x[i, j] <= y[j]);
@objective(model, Min, f' * y + sum(c .* x));
Solve the uncapacitated facility location problem with HiGHS
optimize!(model)
@assert is_solved_and_feasible(model)
println("Optimal value: ", objective_value(model))
Optimal value: 5.7018394545724185
Visualizing the solution
The threshold 1e-5 ensure that edges between clients and facilities are drawn when x[i, j] ≈ 1
.
x_is_selected = isapprox.(value.(x), 1; atol = 1e-5);
y_is_selected = isapprox.(value.(y), 1; atol = 1e-5);
p = Plots.scatter(
x_c,
y_c;
markershape = :circle,
markercolor = :blue,
label = nothing,
)
Plots.scatter!(
x_f,
y_f;
markershape = :square,
markercolor = [(y_is_selected[j] ? :red : :white) for j in 1:n],
markersize = 6,
markerstrokecolor = :red,
markerstrokewidth = 2,
label = nothing,
)
for i in 1:m, j in 1:n
if x_is_selected[i, j]
Plots.plot!(
[x_c[i], x_f[j]],
[y_c[i], y_f[j]];
color = :black,
label = nothing,
)
end
end
p
Capacitated facility location
Problem formulation
The capacitated variant introduces a capacity constraint on each facility, that is, clients have a certain level of demand to be served, while each facility only has finite capacity which cannot be exceeded.
Specifically,
-
The demand of client is denoted by
-
The capacity of facility is denoted by
The capacity constraints then write
Note that, if is set to , the capacity constraint above automatically forces to .
Thus, the capacitated facility location can be formulated as follows
For simplicity, we will assume that there is enough capacity to serve the demand, that is, there exists at least one feasible solution.
We need some new data:
# Demands
a = rand(1:3, m);
# Capacities
q = rand(5:10, n);
Display the data
Plots.scatter(
x_c,
y_c;
label = nothing,
markershape = :circle,
markercolor = :blue,
markersize = 2 .* (2 .+ a),
)
Plots.scatter!(
x_f,
y_f;
label = nothing,
markershape = :rect,
markercolor = :white,
markersize = q,
markerstrokecolor = :red,
markerstrokewidth = 2,
)
JuMP implementation
Create a JuMP model
model = Model(HiGHS.Optimizer)
set_silent(model)
@variable(model, y[1:n], Bin);
@variable(model, x[1:m, 1:n], Bin);
# Each client is served exactly once
@constraint(model, client_service[i in 1:m], sum(x[i, :]) == 1);
# Capacity constraint
@constraint(model, capacity, x' * a .<= (q .* y));
# Objective
@objective(model, Min, f' * y + sum(c .* x));
Solve the problem
optimize!(model)
@assert is_solved_and_feasible(model)
println("Optimal value: ", objective_value(model))
Optimal value: 6.1980444155009975
Visualizing the solution
The threshold 1e-5 ensure that edges between clients and facilities are drawn when x[i, j] ≈ 1
.
x_is_selected = isapprox.(value.(x), 1; atol = 1e-5);
y_is_selected = isapprox.(value.(y), 1; atol = 1e-5);
Display the solution
p = Plots.scatter(
x_c,
y_c;
label = nothing,
markershape = :circle,
markercolor = :blue,
markersize = 2 .* (2 .+ a),
)
Plots.scatter!(
x_f,
y_f;
label = nothing,
markershape = :rect,
markercolor = [(y_is_selected[j] ? :red : :white) for j in 1:n],
markersize = q,
markerstrokecolor = :red,
markerstrokewidth = 2,
)
for i in 1:m, j in 1:n
if x_is_selected[i, j]
Plots.plot!(
[x_c[i], x_f[j]],
[y_c[i], y_f[j]];
color = :black,
label = nothing,
)
end
end
p