|
3 | 3 | # Use of this source code is governed by an MIT-style license that can be found |
4 | 4 | # in the LICENSE.md file or at https://opensource.org/licenses/MIT. |
5 | 5 |
|
6 | | -import MathOptIIS as MOIIS |
| 6 | +function _add_result(out::Data, model, iis, meta::MOIIS.BoundsData) |
| 7 | + @assert length(iis.constraints) == 2 |
| 8 | + err = InfeasibleBounds{Float64}( |
| 9 | + MOI.get(model, MOI.ConstraintFunction(), iis.constraints[1]), |
| 10 | + meta.lower_bound, |
| 11 | + meta.upper_bound, |
| 12 | + ) |
| 13 | + push!(out.infeasible_bounds, err) |
| 14 | + return |
| 15 | +end |
| 16 | + |
| 17 | +function _add_result(out::Data, model, iis, meta::MOIIS.IntegralityData) |
| 18 | + @assert length(iis.constraints) >= 2 |
| 19 | + err = InfeasibleIntegrality{Float64}( |
| 20 | + MOI.get(model, MOI.ConstraintFunction(), iis.constraints[1]), |
| 21 | + meta.lower_bound, |
| 22 | + meta.upper_bound, |
| 23 | + meta.set, |
| 24 | + ) |
| 25 | + push!(out.infeasible_integrality, err) |
| 26 | + return |
| 27 | +end |
| 28 | + |
| 29 | +function _add_result(out::Data, model, iis, meta::MOIIS.RangeData) |
| 30 | + @assert length(iis.constraints) >= 1 |
| 31 | + for con in iis.constraints |
| 32 | + if con isa MOI.ConstraintIndex{MOI.VariableIndex} |
| 33 | + continue |
| 34 | + end |
| 35 | + err = InfeasibleConstraintRange{Float64}( |
| 36 | + con, |
| 37 | + meta.lower_bound, |
| 38 | + meta.upper_bound, |
| 39 | + meta.set, |
| 40 | + ) |
| 41 | + push!(out.constraint_range, err) |
| 42 | + break |
| 43 | + end |
| 44 | + return |
| 45 | +end |
| 46 | + |
| 47 | +function _add_result(out::Data, model, iis, meta) |
| 48 | + push!(out.iis, IrreducibleInfeasibleSubset(iis.constraints)) |
| 49 | + return |
| 50 | +end |
| 51 | + |
| 52 | +function _instantiate_with_modify(optimizer, ::Type{T}) where {T} |
| 53 | + model = MOI.instantiate(optimizer) |
| 54 | + if !MOI.supports_incremental_interface(model) |
| 55 | + # Don't use `default_cache` for the cache because, for example, SCS's |
| 56 | + # default cache doesn't support modifying coefficients of the constraint |
| 57 | + # matrix. JuMP uses the default cache with SCS because it has an outer |
| 58 | + # layer of caching; we don't have that here, so we can't use the |
| 59 | + # default. |
| 60 | + # |
| 61 | + # We could revert to using the default cache if we fix this in MOI. |
| 62 | + cache = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}()) |
| 63 | + model = MOI.Utilities.CachingOptimizer(cache, model) |
| 64 | + end |
| 65 | + return MOI.Bridges.full_bridge_optimizer(model, T) |
| 66 | +end |
7 | 67 |
|
8 | 68 | function MathOptAnalyzer.analyze( |
9 | 69 | ::Analyzer, |
10 | 70 | model::MOI.ModelLike; |
11 | 71 | optimizer = nothing, |
12 | 72 | ) |
13 | | - out = Data() |
14 | | - T = Float64 |
15 | | - |
16 | 73 | solver = MOIIS.Optimizer() |
17 | 74 | MOI.set(solver, MOIIS.InfeasibleModel(), model) |
18 | | - |
19 | 75 | if optimizer !== nothing |
20 | | - MOI.set(solver, MOIIS.InnerOptimizer(), optimizer) |
| 76 | + MOI.set( |
| 77 | + solver, |
| 78 | + MOIIS.InnerOptimizer(), |
| 79 | + () -> _instantiate_with_modify(optimizer, Float64), |
| 80 | + ) |
21 | 81 | end |
22 | | - |
23 | 82 | MOI.compute_conflict!(solver) |
24 | | - |
25 | | - data = solver.results |
26 | | - |
27 | | - for iis in data |
28 | | - meta = iis.metadata |
29 | | - if typeof(meta) <: MOIIS.BoundsData |
30 | | - constraints = iis.constraints |
31 | | - @assert length(constraints) == 2 |
32 | | - func = MOI.get(model, MOI.ConstraintFunction(), constraints[1]) |
33 | | - push!( |
34 | | - out.infeasible_bounds, |
35 | | - InfeasibleBounds{T}(func, meta.lower_bound, meta.upper_bound), |
36 | | - ) |
37 | | - elseif typeof(meta) <: MOIIS.IntegralityData |
38 | | - constraints = iis.constraints |
39 | | - @assert length(constraints) >= 2 |
40 | | - func = MOI.get(model, MOI.ConstraintFunction(), constraints[1]) |
41 | | - push!( |
42 | | - out.infeasible_integrality, |
43 | | - InfeasibleIntegrality{T}( |
44 | | - func, |
45 | | - meta.lower_bound, |
46 | | - meta.upper_bound, |
47 | | - meta.set, |
48 | | - ), |
49 | | - ) |
50 | | - elseif typeof(meta) <: MOIIS.RangeData |
51 | | - constraints = iis.constraints |
52 | | - @assert length(constraints) >= 1 |
53 | | - # main_con = nothing |
54 | | - for con in constraints |
55 | | - if !(typeof(con) <: MOI.ConstraintIndex{MOI.VariableIndex}) |
56 | | - push!( |
57 | | - out.constraint_range, |
58 | | - InfeasibleConstraintRange{T}( |
59 | | - con, |
60 | | - meta.lower_bound, |
61 | | - meta.upper_bound, |
62 | | - meta.set, |
63 | | - ), |
64 | | - ) |
65 | | - break |
66 | | - end |
67 | | - end |
68 | | - else |
69 | | - push!(out.iis, IrreducibleInfeasibleSubset(iis.constraints)) |
70 | | - end |
| 83 | + out = Data() |
| 84 | + for iis in solver.results |
| 85 | + _add_result(out, model, iis, iis.metadata) |
71 | 86 | end |
72 | 87 | return out |
73 | 88 | end |
0 commit comments