forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_feasibility_checker.jl
200 lines (183 loc) · 6.03 KB
/
test_feasibility_checker.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
# 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 https://mozilla.org/MPL/2.0/.
module TestFeasibilityChecker
using JuMP
using Test
function test_no_solution()
model = Model()
@variable(model, x, Bin)
@test_throws ErrorException primal_feasibility_report(model)
end
function test_primal_solution()
model = Model(() -> MOIU.MockOptimizer(MOIU.Model{Float64}()))
@variable(model, x, Bin)
optimize!(model)
mock = unsafe_backend(model)
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mock, MOI.PrimalStatus(), MOI.FEASIBLE_POINT)
MOI.set(mock, MOI.VariablePrimal(), optimizer_index(x), 1.0)
report = primal_feasibility_report(model)
@test isempty(report)
end
function test_primal_solution_func()
model = Model(() -> MOIU.MockOptimizer(MOIU.Model{Float64}()))
@variable(model, x, Bin)
optimize!(model)
mock = unsafe_backend(model)
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mock, MOI.PrimalStatus(), MOI.FEASIBLE_POINT)
MOI.set(mock, MOI.VariablePrimal(), optimizer_index(x), 1.0)
report = primal_feasibility_report(model) do xi
return value(xi)
end
@test isempty(report)
return
end
function test_feasible()
model = Model()
@variable(model, x, Bin)
@variable(model, 0 <= y <= 2, Int)
@variable(model, z == 0.5)
@constraint(model, x + y + z >= 0.5)
report =
primal_feasibility_report(model, Dict(x => 0.0, y => 0.0, z => 0.5))
@test isempty(report)
end
function test_missing()
model = Model()
@variable(model, x, Bin)
@variable(model, 0 <= y <= 2, Int)
@variable(model, z == 0.5)
@constraint(model, x + y + z >= 0.5)
report =
primal_feasibility_report(model, Dict(z => 0.0); skip_missing = true)
@test report[FixRef(z)] == 0.5
@test length(report) == 1
end
function test_missing_error()
model = Model()
@variable(model, x, Bin)
@variable(model, 0 <= y <= 2, Int)
point = Dict(x => 0.1)
err = ErrorException(
"point does not contain a value for variable $(y). Provide a value, " *
"or pass `skip_missing = true`.",
)
@test_throws err primal_feasibility_report(model, point)
end
function test_bounds()
model = Model()
@variable(model, x, Bin)
@variable(model, 0 <= y <= 2, Int)
@variable(model, z == 0.5)
point = Dict(x => 0.1, y => 2.1, z => 0.0)
report = primal_feasibility_report(model, point)
@test report[BinaryRef(x)] ≈ 0.1
@test report[UpperBoundRef(y)] ≈ 0.1
@test report[IntegerRef(y)] ≈ 0.1
@test report[FixRef(z)] ≈ 0.5
@test length(report) == 4
end
function test_scalar_affine()
model = Model()
@variable(model, x)
@constraint(model, c1, x <= 0.5)
@constraint(model, c2, x >= 1.25)
@constraint(model, c3, x == 1.1)
@constraint(model, c4, 0 <= x <= 0.5)
report = primal_feasibility_report(model, Dict(x => 1.0))
@test report[c1] ≈ 0.5
@test report[c2] ≈ 0.25
@test report[c3] ≈ 0.1
@test report[c4] ≈ 0.5
@test length(report) == 4
end
function test_scalar_affine_func()
model = Model()
@variable(model, x)
@constraint(model, c1, x <= 0.5)
@constraint(model, c2, x >= 1.25)
@constraint(model, c3, x == 1.1)
@constraint(model, c4, 0 <= x <= 0.5)
report = primal_feasibility_report(model) do _
return 1.0
end
@test report[c1] ≈ 0.5
@test report[c2] ≈ 0.25
@test report[c3] ≈ 0.1
@test report[c4] ≈ 0.5
@test length(report) == 4
return
end
function test_scalar_quadratic()
model = Model()
@variable(model, x)
@constraint(model, c1, x^2 + x <= 0.5)
@constraint(model, c2, x^2 - x >= 1.25)
@constraint(model, c3, x^2 + x == 1.1)
@constraint(model, c4, 0 <= x^2 + x <= 0.5)
report = primal_feasibility_report(model, Dict(x => 1.0))
@test report[c1] ≈ 1.5
@test report[c2] ≈ 1.25
@test report[c3] ≈ 0.9
@test report[c4] ≈ 1.5
@test length(report) == 4
end
function test_vector()
model = Model()
@variable(model, x[1:3])
@constraint(model, c1, x in MOI.Nonnegatives(3))
@constraint(model, c2, x in MOI.Nonpositives(3))
@constraint(model, c3, x in MOI.Reals(3))
@constraint(model, c4, x in MOI.Zeros(3))
point = Dict(x[1] => 1.0, x[2] => -1.0, x[3] => 0.0)
report = primal_feasibility_report(model, point)
@test report[c1] ≈ 1.0
@test report[c2] ≈ 1.0
@test !haskey(report, c3)
@test report[c4] ≈ sqrt(2)
@test length(report) == 3
end
function test_vector_affine()
model = Model()
@variable(model, x[1:3])
@constraint(model, c1, 2 * x in MOI.Nonnegatives(3))
@constraint(model, c2, 2 * x in MOI.Nonpositives(3))
@constraint(model, c3, 2 * x in MOI.Reals(3))
@constraint(model, c4, 2 * x in MOI.Zeros(3))
point = Dict(x[1] => 1.0, x[2] => -1.0, x[3] => 0.0)
report = primal_feasibility_report(model, point)
@test report[c1] ≈ 2.0
@test report[c2] ≈ 2.0
@test !haskey(report, c3)
@test report[c4] ≈ sqrt(8)
@test length(report) == 3
end
function test_nonlinear()
model = Model()
@variable(model, x)
@NLconstraint(model, c1, sin(x) <= 0.0)
@NLconstraint(model, c2, sin(x) <= 1.0)
@NLconstraint(model, c3, exp(x) >= 2.0)
@NLconstraint(model, c4, x + x^2 - x^3 == 0.5)
report = primal_feasibility_report(model, Dict(x => 0.5))
@test report[c1] ≈ sin(0.5)
@test !haskey(report, c2)
@test report[c3] ≈ 2 - exp(0.5)
@test report[c4] ≈ 0.125
end
function test_nonlinear_missing()
model = Model()
@variable(model, x)
@NLconstraint(model, c1, sin(x) <= 0.0)
@test_throws(
ErrorException(
"`skip_missing = true` is not allowed when nonlinear constraints " *
"are present.",
),
primal_feasibility_report(model, Dict(x => 0.5); skip_missing = true)
)
end
end