-
-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathfeasibility_checker.jl
210 lines (181 loc) · 5.87 KB
/
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
201
202
203
204
205
206
207
208
209
210
# 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/.
using LinearAlgebra
function _last_primal_solution(model::Model)
if !has_values(model)
error(
"No primal solution is available. You must provide a point at " *
"which to check feasibility.",
)
end
return Dict(v => value(v) for v in all_variables(model))
end
"""
primal_feasibility_report(
model::Model,
point::AbstractDict{VariableRef,Float64} = _last_primal_solution(model),
atol::Float64 = 0.0,
skip_missing::Bool = false,
)::Dict{Any,Float64}
Given a dictionary `point`, which maps variables to primal values, return a
dictionary mapping the constraint reference of each constraint in `model` to the
distance between the point and the nearest feasible point, if the distance is
greater than `atol`.
## Notes
* If `skip_missing = true`, constraints containing variables that are not in
`point` will be ignored.
* If `skip_missing = false` and a partial primal solution is provided, an error
will be thrown.
* If no point is provided, the primal solution from the last time the model was
solved is used.
## Examples
```jldoctest; setup=:(using JuMP)
julia> model = Model();
julia> @variable(model, 0.5 <= x <= 1);
julia> primal_feasibility_report(model, Dict(x => 0.2))
Dict{Any,Float64} with 1 entry:
x ≥ 0.5 => 0.3
```
"""
function primal_feasibility_report(
model::Model,
point::AbstractDict{VariableRef,Float64} = _last_primal_solution(model);
atol::Float64 = 0.0,
skip_missing::Bool = false,
)
function point_f(x::VariableRef)
fx = get(point, x, missing)
if ismissing(fx) && !skip_missing
error(
"point does not contain a value for variable $x. Provide a " *
"value, or pass `skip_missing = true`.",
)
end
return fx
end
violated_constraints = Dict{Any,Float64}()
for (F, S) in list_of_constraint_types(model)
_add_infeasible_constraints(
model,
F,
S,
violated_constraints,
point_f,
atol,
)
end
if num_nl_constraints(model) > 0
if skip_missing
error(
"`skip_missing = true` is not allowed when nonlinear " *
"constraints are present.",
)
end
_add_infeasible_nonlinear_constraints(
model,
violated_constraints,
point_f,
atol,
)
end
return violated_constraints
end
function _add_infeasible_constraints(
model::Model,
::Type{F},
::Type{S},
violated_constraints::Dict{Any,Float64},
point_f::Function,
atol::Float64,
) where {F,S}
for con in all_constraints(model, F, S)
obj = constraint_object(con)
d = _distance_to_set(value.(obj.func, point_f), obj.set)
if d > atol
violated_constraints[con] = d
end
end
return
end
function _add_infeasible_nonlinear_constraints(
model::Model,
violated_constraints::Dict{Any,Float64},
point_f::Function,
atol::Float64,
)
evaluator = NLPEvaluator(model)
MOI.initialize(evaluator, Symbol[])
g = zeros(num_nl_constraints(model))
MOI.eval_constraint(evaluator, g, point_f.(all_variables(model)))
for (i, con) in enumerate(model.nlp_data.nlconstr)
d = max(0.0, con.lb - g[i], g[i] - con.ub)
if d > atol
cref =
ConstraintRef(model, NonlinearConstraintIndex(i), ScalarShape())
violated_constraints[cref] = d
end
end
return
end
function _distance_to_set(::Any, set::MOI.AbstractSet)
return error(
"Feasibility checker for set type $(typeof(set)) has not been " *
"implemented yet.",
)
end
_distance_to_set(::Missing, ::MOI.AbstractSet) = 0.0
###
### MOI.AbstractScalarSets
###
function _distance_to_set(x::T, set::MOI.LessThan{T}) where {T<:Real}
return max(x - set.upper, zero(T))
end
function _distance_to_set(x::T, set::MOI.GreaterThan{T}) where {T<:Real}
return max(set.lower - x, zero(T))
end
function _distance_to_set(x::T, set::MOI.EqualTo{T}) where {T<:Number}
return abs(set.value - x)
end
function _distance_to_set(x::T, set::MOI.Interval{T}) where {T<:Real}
return max(x - set.upper, set.lower - x, zero(T))
end
function _distance_to_set(x::T, ::MOI.ZeroOne) where {T<:Real}
return min(abs(x - zero(T)), abs(x - one(T)))
end
function _distance_to_set(x::T, ::MOI.Integer) where {T<:Real}
return abs(x - round(x))
end
function _distance_to_set(x::T, set::MOI.Semicontinuous{T}) where {T<:Real}
return min(max(x - set.upper, set.lower - x, zero(T)), abs(x))
end
function _distance_to_set(x::T, set::MOI.Semiinteger{T}) where {T<:Real}
d = max(ceil(set.lower) - x, x - floor(set.upper), abs(x - round(x)))
return min(d, abs(x))
end
###
### MOI.AbstractVectorSets
###
function _check_dimension(v::AbstractVector, s)
if length(v) != MOI.dimension(s)
throw(DimensionMismatch("Mismatch between value and set"))
end
return
end
function _distance_to_set(x::Vector{T}, set::MOI.Nonnegatives) where {T<:Real}
_check_dimension(x, set)
return LinearAlgebra.norm(max(-xi, zero(T)) for xi in x)
end
function _distance_to_set(x::Vector{T}, set::MOI.Nonpositives) where {T<:Real}
_check_dimension(x, set)
return LinearAlgebra.norm(max(xi, zero(T)) for xi in x)
end
function _distance_to_set(x::Vector{T}, set::MOI.Zeros) where {T<:Number}
_check_dimension(x, set)
return LinearAlgebra.norm(x)
end
function _distance_to_set(x::Vector{T}, set::MOI.Reals) where {T<:Real}
_check_dimension(x, set)
return zero(T)
end