forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probmod.jl
315 lines (288 loc) · 9.78 KB
/
probmod.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#############################################################################
# JuMP
# An algebraic modelling langauge for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################
# test/probmod.jl
# Testing problem modification
# Must be run as part of runtests.jl, as it needs a list of solvers.
#############################################################################
using JuMP, FactCheck
# If solvers not loaded, load them (i.e running just these tests)
!isdefined(:lp_solvers) && include("solvers.jl")
facts("[probmod] Testing problem modification basics") do
for solver in lp_solvers
context("With solver $(typeof(solver))") do
# max 1.1x + 1.0y
# st x + y <= 3
# 0 <= x <= 3
# 1 <= y <= 3
# x* = 2, y* = 1
m = Model(solver=solver)
@defVar(m, 0 <= x <= 3)
@defVar(m, 1 <= y <= 3)
@setObjective(m, :Max, 1.1x + 1.0y)
maincon = @addConstraint(m, x + y <= 3)
@fact solve(m) => :Optimal
@fact m.internalModelLoaded => true
@fact getValue(x) => roughly(2.0, 1e-6)
@fact getValue(y) => roughly(1.0, 1e-6)
# Test adding a variable
# max 1.1x + 1.0y + 100.0z
# st x + y + z <= 3
# 0 <= x <= 3
# 1 <= y <= 3
# 0 <= z <= 5
# x* = 0, y* = 1, z* = 2
@defVar(m, 0 <= z <= 5, objective=100.0, inconstraints=[maincon], coefficients=[1.0])
@fact solve(m) => :Optimal
@fact getValue(x) => roughly(0.0, 1e-6)
@fact getValue(y) => roughly(1.0, 1e-6)
@fact getValue(z) => roughly(2.0, 1e-6)
@fact getObjectiveValue(m) => roughly(201.0, 1e-6)
# Test changing bounds
# max 1.1x + 1.0y + 100.0z
# st x + y + z <= 3
# 0 <= x <= 3
# 0 <= y <= 3
# 0 <= z <= 2
# x* = 1, y* = 0, z* = 2
setLower(y, 0.0)
setUpper(z, 2.0)
@fact solve(m) => :Optimal
@fact getValue(x) => roughly(1.0, 1e-6)
@fact getValue(y) => roughly(0.0, 1e-6)
@fact getValue(z) => roughly(2.0, 1e-6)
m.internalModelLoaded = false
end
end
end
facts("[probmod] Testing problem modification part two") do
for solver in ip_solvers
context("With solver $(typeof(solver))") do
# max 1.1x + 1.0y + 100.0z
# st x + y + z <= 3
# 0 <= x <= 3
# 1 <= y <= 3
# 0 <= z <= 0
m = Model(solver=solver)
@defVar(m, 0 <= x <= 3)
@defVar(m, 0 <= y <= 3)
@defVar(m, 0 <= z <= 0)
@setObjective(m, :Max, 1.1x + 1.0y + 100.0z)
maincon = @addConstraint(m, x + y + z <= 3)
@fact solve(m) => :Optimal
# Test changing problem type
# max 1.1x + 1.0y + 100.0z
# st x + y + z <= 3
# 0 <= x <= 3
# 0 <= y <= 3
# 0 <= z <= 1.5, Integer
# x* = 2, y* = 0, z* = 1
setUpper(z, 1.5)
m.colCat[3] = :Int
@fact solve(m) => :Optimal
@fact getValue(x) => roughly(2.0, 1e-6)
@fact getValue(y) => roughly(0.0, 1e-6)
@fact getValue(z) => roughly(1.0, 1e-6)
# Test changing constraint bound (<= constraint)
# max 1.1x + 1.0y + 100.0z
# st x + y + z <= 2
# 0 <= x <= 3
# 0 <= y <= 3
# 0 <= z <= 1.5, Integer
# x* = 1, y* = 0, z* = 1
chgConstrRHS(maincon, 2.0)
@fact solve(m) => :Optimal
@fact getValue(x) => roughly(1.0, 1e-6)
@fact getValue(y) => roughly(0.0, 1e-6)
@fact getValue(z) => roughly(1.0, 1e-6)
# Test adding a constraint
# max 1.1x + 1.0y + 100.0z
# st x + y + z <= 2
# x + z <= 0
# 0 <= x <= 3
# 0 <= y <= 3
# 0 <= z <= 1.5, Integer
# x* = 0, y* = 2, z* = 0
xz0ref = @addConstraint(m, x + z <=0)
@fact solve(m) => :Optimal
@fact getValue(x) => roughly(0.0, 1e-6)
@fact getValue(y) => roughly(2.0, 1e-6)
@fact getValue(z) => roughly(0.0, 1e-6)
# Test changing constraint bound (>= constraint)
# max 1.1x + 1.0y + 100.0z
# st x + y + z <= 2
# x + z <= 1
# x + y >= 1
# 0 <= x <= 3
# 0 <= y <= 3
# 0 <= z <= 1.5, Integer
# x* = 1, y* = 0, z* = 1
chgConstrRHS(xz0ref, 2.0)
xyg0ref = @addConstraint(m, x + y >= 0)
@fact solve(m) => :Optimal
chgConstrRHS(xyg0ref, 1)
@fact solve(m) => :Optimal
@fact getValue(x) => roughly(1.0, 1e-6)
@fact getValue(y) => roughly(0.0, 1e-6)
@fact getValue(z) => roughly(1.0, 1e-6)
end
end
end
facts("[probmod] Test adding a range constraint and modifying it") do
m = Model()
@defVar(m, x)
rangeref = @addConstraint(m, -10 <= x <= 10)
@fact_throws chgConstrRHS(rangeref, 11)
end
facts("[probmod] Test adding a 'decoupled' variable (#205)") do
for solver in lp_solvers
context("With solver $(typeof(solver))") do
m = Model(solver=solver)
@defVar(m, x >= 0)
@setObjective(m, Min, x)
solve(m)
@defVar(m, y >= 0)
@addConstraint(m, x + y == 1)
@setObjective(m, Min, 2x+y)
solve(m)
@fact getValue(x) => roughly(0.0, 1e-6)
@fact getValue(y) => roughly(1.0, 1e-6)
end
end
end
facts("[probmod] Test buildInternalModel") do
for solver in lp_solvers
context("With solver $(typeof(solver))") do
m = Model(solver=solver)
@defVar(m, x >= 0)
@defVar(m, y >= 0)
@addConstraint(m, x + y == 1)
@setObjective(m, Max, y)
buildInternalModel(m)
@fact getInternalModel(m) => not(nothing)
@fact m.internalModelLoaded => true
stat = solve(m)
@fact stat => :Optimal
@fact getValue(x) => roughly( 0.0, 1e-6)
@fact getValue(y) => roughly( 1.0, 1e-6)
@fact getObjectiveValue(m) => roughly(1.0, 1e-6)
@fact getDual(x) => roughly(-1.0, 1e-6)
@fact getDual(y) => roughly( 0.0, 1e-6)
end
end
end
facts("[probmod] Test buildInternalModel with MIP") do
for solver in ip_solvers
context("With solver $(typeof(solver))") do
m = Model(solver=solver)
@defVar(m, x >= 0, Int)
@defVar(m, y, Bin)
@addConstraint(m, x + y == 1)
@setObjective(m, Max, y)
buildInternalModel(m)
@fact getInternalModel(m) => not(nothing)
@fact m.internalModelLoaded => true
stat = solve(m)
@fact stat => :Optimal
@fact getValue(x) => roughly( 0.0, 1e-6)
@fact getValue(y) => roughly( 1.0, 1e-6)
@fact getObjectiveValue(m) => roughly(1.0, 1e-6)
end
end
end
facts("[probmod] Test bound modification on binaries") do
for solver in ip_solvers
context("With solver $(typeof(solver))") do
# Test semantics for modifying bounds on binary variables:
# Variables should be restricted to the intersection of
# {0,1} and their bounds.
mod = Model(solver=solver)
@defVar(mod, x, Bin)
@setObjective(mod, Max, x)
solve(mod)
@fact getValue(x) => roughly(1.0)
setUpper(x, 2.0)
solve(mod)
@fact getValue(x) => roughly(1.0)
setUpper(x, 0.0)
solve(mod)
@fact getValue(x) => roughly(0.0)
# same thing, other direction
mod = Model(solver=solver)
@defVar(mod, x, Bin)
@setObjective(mod, Min, x)
solve(mod)
@fact getValue(x) => roughly(0.0)
setLower(x, -1.0)
solve(mod)
@fact getValue(x) => roughly(0.0)
setLower(x, 1.0)
solve(mod)
@fact getValue(x) => roughly(1.0)
end
end
end
facts("[probmod] Applicable regressions") do
function methods_test(solvername, solverobj, supp)
mod = Model(solver=solverobj)
@defVar(mod, x >= 0)
@addConstraint(mod, 2x == 2)
solve(mod, suppress_warnings=true)
internal_mod = getInternalModel(mod)
context(solvername) do
for (it,(meth, args)) in enumerate(mpb_methods)
if supp[it]
@fact applicable(meth, internal_mod, args...) => true
@fact method_exists(meth, map(typeof, tuple(internal_mod, args...))) => true
end
end
end
end
const mpb_methods =
[(MathProgBase.addquadconstr!, (Cint[1],Float64[1.0],Cint[1],Cint[1],Float64[1],'>',1.0)),
(MathProgBase.setquadobjterms!, (Cint[1], Cint[1], Float64[1.0])),
(MathProgBase.addconstr!, ([1],[1.0],1.0,1.0)),
(MathProgBase.addsos1!, ([1],[1.0])),
(MathProgBase.addsos2!, ([1],[1.0])),
(MathProgBase.addvar!, ([1],[1.0],1.0,1.0,1.0)),
(MathProgBase.setvarLB!, ([1.0],)),
(MathProgBase.setvarUB!, ([1.0],)),
(MathProgBase.setconstrLB!, ([1.0],)),
(MathProgBase.setconstrUB!, ([1.0],)),
(MathProgBase.setobj!, ([1.0],)),
(MathProgBase.setsense!, (:Min,)),
(MathProgBase.setvartype!, ([:Cont],)),
(MathProgBase.getinfeasibilityray, ()),
(MathProgBase.getunboundedray, ()),
(MathProgBase.getreducedcosts, ()),
(MathProgBase.getconstrduals, ()),
(MathProgBase.setwarmstart!, ([1.0]))]
if grb
supp = (true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true)
methods_test("Gurobi", Gurobi.GurobiSolver(OutputFlag=0), supp)
end
if cpx
supp = (true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true)
methods_test("CPLEX", CPLEX.CplexSolver(), supp)
end
if cbc
supp = (false,false,true,false,false,true,true,true,true,true,true,true,true,true,true,true,true,false)
methods_test("Clp", Clp.ClpSolver(), supp)
end
if cbc
supp = (false,false,false,false,false,false,false,false,false,false,false,true,true,false,false,false,false,false)
methods_test("Cbc", Cbc.CbcSolver(), supp)
end
if glp
supp = (false,false,true,false,false,true,true,true,true,true,true,true,false,true,true,true,true,false)
methods_test("GLPK LP", GLPKMathProgInterface.GLPKSolverLP(), supp)
supp = (false,false,true,false,false, true,true,true,true,true,true,true,true,false,false,false,false,false)
methods_test("GLPK MIP", GLPKMathProgInterface.GLPKSolverMIP(), supp)
end
if mos
supp = (true,true,true,false,false,true,true,true,true,true,true,true,true,true,true,true,true,false)
methods_test("Mosek", Mosek.MosekSolver(), supp)
end
end