-
Notifications
You must be signed in to change notification settings - Fork 113
/
mixed_integer_linear_program.jl
266 lines (227 loc) · 8.32 KB
/
mixed_integer_linear_program.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
struct MixedIntegerLinearProgram{T} <: PolyhedralObject{T}
feasible_region::Polyhedron{T}
polymake_milp::Polymake.BigObject
convention::Symbol
parent_field::Field
MixedIntegerLinearProgram{T}(
fr::Polyhedron{T}, milp::Polymake.BigObject, c::Symbol, parent_field::Field
) where {T<:scalar_types} = new{T}(fr, milp, c, parent_field)
end
# no default = `QQFieldElem` here; scalar type can be derived from the feasible region
mixed_integer_linear_program(p::Polyhedron{T}, x...) where {T<:scalar_types} =
MixedIntegerLinearProgram{T}(p, x..., coefficient_field(p))
@doc raw"""
mixed_integer_linear_program(P, c; integer_variables = [], k = 0, convention = :max)
The mixed integer linear program on the feasible set `P` (a Polyhedron) with
respect to the function x ↦ dot(c,x)+k, where $x_i\in\mathbb{Z}$ for all $i$ in
`integer_variables`. If `integer_variables` is empty, or not specified, all
entries of `x` are required to be integral. If this is not intended, consider
using a [`linear_program`](@ref) instead.
"""
function mixed_integer_linear_program(
P::Polyhedron{T},
objective::AbstractVector;
integer_variables=Int64[],
k=0,
convention=:max,
) where {T<:scalar_types}
if convention != :max && convention != :min
throw(ArgumentError("convention must be set to :min or :max."))
end
ambDim = ambient_dim(P)
if isnothing(integer_variables) || isempty(integer_variables)
integer_variables = 1:ambDim
end
size(objective, 1) == ambDim || error("objective has wrong dimension.")
milp = Polymake.polytope.MixedIntegerLinearProgram{_scalar_type_to_polymake(T)}(;
LINEAR_OBJECTIVE=homogenize(objective, k), INTEGER_VARIABLES=Vector(integer_variables)
)
if convention == :max
Polymake.attach(milp, "convention", "max")
elseif convention == :min
Polymake.attach(milp, "convention", "min")
end
Polymake.add(pm_object(P), "MILP", milp)
MixedIntegerLinearProgram{T}(P, milp, convention, coefficient_field(P))
end
function mixed_integer_linear_program(
::Type{T},
A::Union{Oscar.MatElem,AbstractMatrix},
b,
c::AbstractVector;
integer_variables=Vector{Int64}([]),
k=0,
convention=:max,
) where {T<:scalar_types}
P = polyhedron(T, A, b)
return mixed_integer_linear_program(
P,
c,
coefficient_field(P);
integer_variables=integer_variables,
k=k,
convention=convention,
)
end
pm_object(milp::MixedIntegerLinearProgram) = milp.polymake_milp
###############################################################################
###############################################################################
### Display
###############################################################################
###############################################################################
function describe(io::IO, MILP::MixedIntegerLinearProgram)
c = dehomogenize(MILP.polymake_milp.LINEAR_OBJECTIVE)
k = MILP.polymake_milp.LINEAR_OBJECTIVE[1]
print(io, "The mixed integer linear program\n")
if MILP.convention == :max
print(io, " max")
elseif MILP.convention == :min
print(io, " min")
end
if is_unicode_allowed()
print(io, "{c⋅x + k | x ∈ P}\n")
else
print(io, "{c*x + k | x in P}\n")
end
print(io, "where P is a " * string(typeof(MILP.feasible_region)))
print(io, "\n c=")
print(io, string(c'))
print(io, "\n k=")
print(io, string(k))
print(io, "\n ")
ivar = _integer_variables(MILP)
if length(ivar) == 0
print(io, "all entries of x in ZZ")
else
print(io, join(["x" * string(i) for i in ivar], ",") * " in ZZ")
end
end
Base.show(io::IO, MILP::MixedIntegerLinearProgram) =
print(io, "Mixed integer linear program")
###############################################################################
###############################################################################
### Access
###############################################################################
###############################################################################
_integer_variables(milp::MixedIntegerLinearProgram) = milp.polymake_milp.INTEGER_VARIABLES
@doc raw"""
objective_function(MILP::MixedIntegerLinearProgram; as = :pair)
Return the objective function x ↦ dot(c,x)+k of the mixed integer linear program MILP.
The allowed values for `as` are
* `pair`: Return the pair `(c,k)`
* `function`: Return the objective function as a function.
"""
function objective_function(
milp::MixedIntegerLinearProgram{T}; as::Symbol=:pair
) where {T<:scalar_types}
if as == :pair
return Vector{T}(dehomogenize(milp.polymake_milp.LINEAR_OBJECTIVE)),
convert(T, milp.polymake_milp.LINEAR_OBJECTIVE[1])
elseif as == :function
(c, k) = objective_function(milp; as=:pair)
return x -> sum(x .* c) + k
else
throw(ArgumentError("Unsupported `as` argument: $as"))
end
end
@doc raw"""
feasible_region(milp::MixedIntegerLinearProgram)
Return the feasible region of the mixed integer linear program `milp`, which is
a `Polyhedron`.
"""
feasible_region(milp::MixedIntegerLinearProgram) = milp.feasible_region
###############################################################################
###############################################################################
### Solving of mixed integer linear programs
###############################################################################
###############################################################################
@doc raw"""
optimal_solution(MILP::MixedIntegerLinearProgram)
Return either a point of the feasible region of `MILP` which optimizes the
objective function of `MILP`, or `nothing` if no such point exists.
# Examples
Take the square $[-1/2,3/2]^2$ and optimize $[1,1]$ in different settings.
```jldoctest
julia> c = cube(2, -1//2, 3//2)
Polytope in ambient dimension 2
julia> milp = mixed_integer_linear_program(c, [1,1], integer_variables=[1])
Mixed integer linear program
julia> optimal_solution(milp)
2-element PointVector{QQFieldElem}:
1
3//2
julia> milp = mixed_integer_linear_program(c, [1,1])
Mixed integer linear program
julia> optimal_solution(milp)
2-element PointVector{QQFieldElem}:
1
1
```
"""
function optimal_solution(milp::MixedIntegerLinearProgram{T}) where {T<:scalar_types}
opt_vert = nothing
if milp.convention == :max
opt_vert = milp.polymake_milp.MAXIMAL_SOLUTION
else
opt_vert = milp.polymake_milp.MINIMAL_SOLUTION
end
if !isnothing(opt_vert)
return point_vector(coefficient_field(milp), dehomogenize(opt_vert))::PointVector{T}
else
return nothing
end
end
@doc raw"""
optimal_value(MILP::MixedIntegerLinearProgram)
Return, if it exists, the optimal value of the objective function of `MILP`
over the feasible region of `MILP`. Otherwise, return `-inf` or `inf` depending
on convention.
# Examples
Take the square $[-1/2,3/2]^2$ and optimize $[1,1]$ in different settings.
```jldoctest
julia> c = cube(2, -1//2, 3//2)
Polytope in ambient dimension 2
julia> milp = mixed_integer_linear_program(c, [1,1], integer_variables=[1])
Mixed integer linear program
julia> optimal_value(milp)
5/2
julia> milp = mixed_integer_linear_program(c, [1,1])
Mixed integer linear program
julia> optimal_value(milp)
2
```
"""
function optimal_value(milp::MixedIntegerLinearProgram{T}) where {T<:scalar_types}
if milp.convention == :max
# TODO: consider inf
return milp.polymake_milp.MAXIMAL_VALUE
else
return milp.polymake_milp.MINIMAL_VALUE
end
end
@doc raw"""
solve_milp(MILP::MixedIntegerLinearProgram)
Return a pair `(m,v)` where the optimal value `m` of the objective function of
`MILP` is attained at `v` (if `m` exists). If the optimum is not attained, `m`
may be `inf` or `-inf` in which case `v` is `nothing`.
# Examples
Take the square $[-1/2,3/2]^2$ and optimize $[1,1]$ in different settings.
```jldoctest
julia> c = cube(2, -1//2, 3//2)
Polytope in ambient dimension 2
julia> milp = mixed_integer_linear_program(c, [1,1], integer_variables=[1])
Mixed integer linear program
julia> solve_milp(milp)
(5/2, QQFieldElem[1, 3//2])
julia> milp = mixed_integer_linear_program(c, [1,1])
Mixed integer linear program
julia> solve_milp(milp)
(2, QQFieldElem[1, 1])
```
"""
solve_milp(milp::MixedIntegerLinearProgram) = optimal_value(milp), optimal_solution(milp)
@doc raw"""
ambient_dim(MILP::MixedIntegerLinearProgram)
Return the ambient dimension of the feasible reagion of `MILP`.
"""
ambient_dim(milp::MixedIntegerLinearProgram) = ambient_dim(feasible_region(milp))