|
| 1 | + |
| 2 | +""" |
| 3 | +Computes a set of constraints which could not be satisfied when trying to minimize the total violations. |
| 4 | +
|
| 5 | +Given the problem: |
| 6 | +``` |
| 7 | +(P) min_x c^⊤ x |
| 8 | +s.t. F(x) ∈ S_i ∀ i in 1…m |
| 9 | +``` |
| 10 | +
|
| 11 | +the analysis uses a feasibility relaxation based on slack variables and indicator constraints: |
| 12 | +``` |
| 13 | +(M) min_{x, z} ∑_i z_i |
| 14 | +s.t. z_i = 0 → F(x) ∈ S_i ∀ i in 1…m |
| 15 | + z ∈ {0,1}ᵐ |
| 16 | +``` |
| 17 | +
|
| 18 | +If (P) is infeasible, (M) has an optimal value above 1. |
| 19 | +All constraints with `z = 1` had to be violated. |
| 20 | +""" |
| 21 | +function compute_minimum_unsatisfied_constraints!(o::Optimizer) |
| 22 | + if o.conflict_status != MOI.COMPUTE_CONFLICT_NOT_CALLED |
| 23 | + error("Conflict computation is destructive for the model and cannot be called twice.") |
| 24 | + end |
| 25 | + # free the transformed problem first |
| 26 | + if LibSCIP.SCIPgetStage(o) != LibSCIP.SCIP_STAGE_PROBLEM |
| 27 | + @SCIP_CALL LibSCIP.SCIPfreeTransform(o) |
| 28 | + end |
| 29 | + # first transform all variable bound constraints to constraint bounds |
| 30 | + for (F, S) in MOI.get(o, MOI.ListOfConstraintTypesPresent()) |
| 31 | + sname = replace(string(S), "MathOptInterface." => "", "{Float64}" => "") |
| 32 | + if Tuple{F, S} <: Tuple{VI, BOUNDS} |
| 33 | + for (idx, c_index) in enumerate(MOI.get(o, MOI.ListOfConstraintIndices{F,S}())) |
| 34 | + s = MOI.get(o, MOI.ConstraintSet(), c_index) |
| 35 | + MOI.delete(o, c_index) |
| 36 | + vi = MOI.VariableIndex(c_index.value) |
| 37 | + ci_new = MOI.add_constraint(o, 1.0 * vi, s) |
| 38 | + MOI.set(o, MOI.ConstraintName(), ci_new, "varcons_$(c_index.value)_$sname") |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + # we need names for all constraints |
| 43 | + for (F, S) in MOI.get(o, MOI.ListOfConstraintTypesPresent()) |
| 44 | + if F === VI |
| 45 | + continue |
| 46 | + end |
| 47 | + for (idx, c_index) in enumerate(MOI.get(o, MOI.ListOfConstraintIndices{F,S}())) |
| 48 | + if MOI.get(o, MOI.ConstraintName(), c_index) == "" |
| 49 | + cons_ptr = cons(o, c_index) |
| 50 | + handler_name = unsafe_string(SCIPconshdlrGetName(SCIPconsGetHdlr(cons_ptr))) |
| 51 | + cons_name = "$(handler_name)_moi_$(idx)" |
| 52 | + MOI.set(o, MOI.ConstraintName(), c_index, cons_name) |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + success = Ref{LibSCIP.SCIP_Bool}(SCIP.FALSE) |
| 57 | + @SCIP_CALL LibSCIP.SCIPtransformMinUC(o, success) |
| 58 | + if success[] != SCIP.TRUE |
| 59 | + error("Failed to compute the minimum unsatisfied constraints system.\nSome constraint types may not support the required transformations") |
| 60 | + end |
| 61 | + MOI.optimize!(o) |
| 62 | + st = MOI.get(o, MOI.TerminationStatus()) |
| 63 | + if st != MOI.OPTIMAL |
| 64 | + error("Unexpected status $st when computing conflicts") |
| 65 | + end |
| 66 | + o.conflict_status = if MOI.get(o, MOI.ObjectiveValue()) > 0 |
| 67 | + MOI.CONFLICT_FOUND |
| 68 | + else |
| 69 | + MOI.NO_CONFLICT_EXISTS |
| 70 | + end |
| 71 | + return |
| 72 | +end |
| 73 | + |
| 74 | +""" |
| 75 | +Model attribute representing whether why the Minimum Unsatisfiable Constraint analysis terminated. |
| 76 | +""" |
| 77 | +struct UnsatisfiableSystemStatus <: MOI.AbstractModelAttribute end |
| 78 | + |
| 79 | +attribute_value_type(::UnsatisfiableSystemStatus) = MOI.ConflictStatusCode |
| 80 | + |
| 81 | +MOI.get(o::Optimizer, ::UnsatisfiableSystemStatus) = o.conflict_status |
| 82 | + |
| 83 | +""" |
| 84 | +Attribute representing whether the constraint could be satisfied in the Minimum Unsatisfiable Constraint analysis. |
| 85 | +
|
| 86 | +Note that this is different from a constraint belonging to an Irreducible Infeasible Subsystem. |
| 87 | +""" |
| 88 | +struct ConstraintSatisfiabilityStatus <: MOI.AbstractConstraintAttribute end |
| 89 | + |
| 90 | +function attribute_value_type(::ConstraintSatisfiabilityStatus) |
| 91 | + return MOI.ConflictParticipationStatusCode |
| 92 | +end |
| 93 | + |
| 94 | +function MOI.get(o::Optimizer, ::ConstraintSatisfiabilityStatus, index::MOI.ConstraintIndex{MOI.VariableIndex}) |
| 95 | + o.conflict_status == MOI.CONFLICT_FOUND || error("no conflict") |
| 96 | + # we cannot determine whether variable constraint (integer, binary, variable bounds) participate |
| 97 | + return MOI.MAYBE_IN_CONFLICT |
| 98 | +end |
| 99 | + |
| 100 | +function MOI.get(o::Optimizer, ::ConstraintSatisfiabilityStatus, index::MOI.ConstraintIndex) |
| 101 | + o.conflict_status == MOI.CONFLICT_FOUND || error("no conflict") |
| 102 | + c_name = MOI.get(o, MOI.ConstraintName(), index) |
| 103 | + slack_name = "$(c_name)_master" |
| 104 | + ptr = SCIPfindVar(o, slack_name) |
| 105 | + if ptr == C_NULL |
| 106 | + error("No constraint name corresponds to the index $index - name $c_name") |
| 107 | + end |
| 108 | + sol = SCIPgetBestSol(o) |
| 109 | + slack_value = SCIPgetSolVal(o, sol, ptr) |
| 110 | + return slack_value > 0.5 ? MOI.IN_CONFLICT : MOI.NOT_IN_CONFLICT |
| 111 | +end |
0 commit comments