-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
urban_plan.jl
73 lines (62 loc) · 2.64 KB
/
urban_plan.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See http://github.com/jump-dev/JuMP.jl
#############################################################################
using JuMP, GLPK, Test
"""
example_urban_plan()
An "urban planning" problem. Based on
http://www.puzzlor.com/2013-08_UrbanPlanning.html
"""
function example_urban_plan()
model = Model(GLPK.Optimizer)
# x is indexed by row and column
@variable(model, 0 <= x[1:5, 1:5] <= 1, Int)
# y is indexed by R or C, the points, and an index in 1:5. Note how JuMP
# allows indexing on arbitrary sets.
rowcol = ["R", "C"]
points = [5, 4, 3, -3, -4, -5]
@variable(model, 0 <= y[rowcol, points, 1:5] <= 1, Int)
# Objective - combine the positive and negative parts
@objective(model, Max, sum(
3 * (y["R", 3, i] + y["C", 3, i])
+ 1 * (y["R", 4, i] + y["C", 4, i])
+ 1 * (y["R", 5, i] + y["C", 5, i])
- 3 * (y["R", -3, i] + y["C", -3, i])
- 1 * (y["R", -4, i] + y["C", -4, i])
- 1 * (y["R", -5, i] + y["C", -5, i])
for i in 1:5)
)
# Constrain the number of residential lots
@constraint(model, sum(x) == 12)
# Add the constraints that link the auxiliary y variables to the x variables
for i = 1:5
@constraints(model, begin
# Rows
y["R", 5, i] <= 1 / 5 * sum(x[i, :]) # sum = 5
y["R", 4, i] <= 1 / 4 * sum(x[i, :]) # sum = 4
y["R", 3, i] <= 1 / 3 * sum(x[i, :]) # sum = 3
y["R", -3, i] >= 1 - 1 / 3 * sum(x[i, :]) # sum = 2
y["R", -4, i] >= 1 - 1 / 2 * sum(x[i, :]) # sum = 1
y["R", -5, i] >= 1 - 1 / 1 * sum(x[i, :]) # sum = 0
# Columns
y["C", 5, i] <= 1 / 5 * sum(x[:, i]) # sum = 5
y["C", 4, i] <= 1 / 4 * sum(x[:, i]) # sum = 4
y["C", 3, i] <= 1 / 3 * sum(x[:, i]) # sum = 3
y["C", -3, i] >= 1 - 1 / 3 * sum(x[:, i]) # sum = 2
y["C", -4, i] >= 1 - 1 / 2 * sum(x[:, i]) # sum = 1
y["C", -5, i] >= 1 - 1 / 1 * sum(x[:, i]) # sum = 0
end)
end
# Solve it
JuMP.optimize!(model)
@test JuMP.termination_status(model) == MOI.OPTIMAL
@test JuMP.primal_status(model) == MOI.FEASIBLE_POINT
@test JuMP.objective_value(model) ≈ 14.0
end
example_urban_plan()