Skip to content

Conflict report #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
moi_heuristic::Any # ::Union{HeuristicCb, Nothing}
objective_sense::Union{Nothing,MOI.OptimizationSense}
objective_function_set::Bool
conflict_status::MOI.ConflictStatusCode

function Optimizer(; kwargs...)
scip = Ref{Ptr{SCIP_}}(C_NULL)
Expand Down Expand Up @@ -73,6 +74,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
nothing,
nothing,
false,
MOI.COMPUTE_CONFLICT_NOT_CALLED,
)
finalizer(free_scip, o)

Expand Down Expand Up @@ -281,6 +283,7 @@ function MOI.empty!(o::Optimizer)
end
o.objective_sense = nothing
o.objective_function_set = false
o.conflict_status = MOI.COMPUTE_CONFLICT_NOT_CALLED
o.moi_separator = nothing
o.moi_heuristic = nothing
return nothing
Expand Down Expand Up @@ -432,3 +435,4 @@ include(joinpath("MOI_wrapper", "results.jl"))
include(joinpath("MOI_wrapper", "conshdlr.jl"))
include(joinpath("MOI_wrapper", "sepa.jl"))
include(joinpath("MOI_wrapper", "heuristic.jl"))
include(joinpath("MOI_wrapper", "conflict.jl"))
111 changes: 111 additions & 0 deletions src/MOI_wrapper/conflict.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

"""
Computes a set of constraints which could not be satisfied when trying to minimize the total violations.

Given the problem:
```
(P) min_x c^⊤ x
s.t. F(x) ∈ S_i ∀ i in 1…m
```

the analysis uses a feasibility relaxation based on slack variables and indicator constraints:
```
(M) min_{x, z} ∑_i z_i
s.t. z_i = 0 → F(x) ∈ S_i ∀ i in 1…m
z ∈ {0,1}ᵐ
```

If (P) is infeasible, (M) has an optimal value above 1.
All constraints with `z = 1` had to be violated.
"""
function compute_minimum_unsatisfied_constraints!(o::Optimizer)
if o.conflict_status != MOI.COMPUTE_CONFLICT_NOT_CALLED
error("Conflict computation is destructive for the model and cannot be called twice.")
end
# free the transformed problem first
if LibSCIP.SCIPgetStage(o) != LibSCIP.SCIP_STAGE_PROBLEM
@SCIP_CALL LibSCIP.SCIPfreeTransform(o)
end
# first transform all variable bound constraints to constraint bounds
for (F, S) in MOI.get(o, MOI.ListOfConstraintTypesPresent())
sname = replace(string(S), "MathOptInterface." => "", "{Float64}" => "")
if Tuple{F, S} <: Tuple{VI, BOUNDS}
for (idx, c_index) in enumerate(MOI.get(o, MOI.ListOfConstraintIndices{F,S}()))
s = MOI.get(o, MOI.ConstraintSet(), c_index)
MOI.delete(o, c_index)
vi = MOI.VariableIndex(c_index.value)
ci_new = MOI.add_constraint(o, 1.0 * vi, s)
MOI.set(o, MOI.ConstraintName(), ci_new, "varcons_$(c_index.value)_$sname")
end
end
end
# we need names for all constraints
for (F, S) in MOI.get(o, MOI.ListOfConstraintTypesPresent())
if F === VI
continue
end
for (idx, c_index) in enumerate(MOI.get(o, MOI.ListOfConstraintIndices{F,S}()))
if MOI.get(o, MOI.ConstraintName(), c_index) == ""
cons_ptr = cons(o, c_index)
handler_name = unsafe_string(SCIPconshdlrGetName(SCIPconsGetHdlr(cons_ptr)))
cons_name = "$(handler_name)_moi_$(idx)"
MOI.set(o, MOI.ConstraintName(), c_index, cons_name)
end
end
end
success = Ref{LibSCIP.SCIP_Bool}(SCIP.FALSE)
@SCIP_CALL LibSCIP.SCIPtransformMinUC(o, success)
if success[] != SCIP.TRUE
error("Failed to compute the minimum unsatisfied constraints system.\nSome constraint types may not support the required transformations")
end
MOI.optimize!(o)
st = MOI.get(o, MOI.TerminationStatus())
if st != MOI.OPTIMAL
error("Unexpected status $st when computing conflicts")
end
o.conflict_status = if MOI.get(o, MOI.ObjectiveValue()) > 0
MOI.CONFLICT_FOUND
else
MOI.NO_CONFLICT_EXISTS
end
return
end

"""
Model attribute representing whether why the Minimum Unsatisfiable Constraint analysis terminated.
"""
struct UnsatisfiableSystemStatus <: MOI.AbstractModelAttribute end

attribute_value_type(::UnsatisfiableSystemStatus) = MOI.ConflictStatusCode

MOI.get(o::Optimizer, ::UnsatisfiableSystemStatus) = o.conflict_status

"""
Attribute representing whether the constraint could be satisfied in the Minimum Unsatisfiable Constraint analysis.

Note that this is different from a constraint belonging to an Irreducible Infeasible Subsystem.
"""
struct ConstraintSatisfiabilityStatus <: MOI.AbstractConstraintAttribute end

function attribute_value_type(::ConstraintSatisfiabilityStatus)
return MOI.ConflictParticipationStatusCode
end

function MOI.get(o::Optimizer, ::ConstraintSatisfiabilityStatus, index::MOI.ConstraintIndex{MOI.VariableIndex})
o.conflict_status == MOI.CONFLICT_FOUND || error("no conflict")
# we cannot determine whether variable constraint (integer, binary, variable bounds) participate
return MOI.MAYBE_IN_CONFLICT
end

function MOI.get(o::Optimizer, ::ConstraintSatisfiabilityStatus, index::MOI.ConstraintIndex)
o.conflict_status == MOI.CONFLICT_FOUND || error("no conflict")
c_name = MOI.get(o, MOI.ConstraintName(), index)
slack_name = "$(c_name)_master"
ptr = SCIPfindVar(o, slack_name)
if ptr == C_NULL
error("No constraint name corresponds to the index $index - name $c_name")
end
sol = SCIPgetBestSol(o)
slack_value = SCIPgetSolVal(o, sol, ptr)
return slack_value > 0.5 ? MOI.IN_CONFLICT : MOI.NOT_IN_CONFLICT
end
42 changes: 42 additions & 0 deletions test/MOI_conflicts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using SCIP
import MathOptInterface as MOI
using Test

function base_satisfiability_problem()
optimizer = SCIP.Optimizer()

x, y, z = MOI.add_variables(optimizer, 3)
MOI.add_constraint(optimizer, x, MOI.LessThan(1.0))
MOI.add_constraint(optimizer, y, MOI.LessThan(1.0))
MOI.add_constraint(optimizer, z, MOI.LessThan(1.0))

c = MOI.add_constraint(
optimizer,
MOI.VectorOfVariables([x, y, z]),
MOI.SOS1([1.0, 2.0, 3.0]),
)
return optimizer, x, y, z, c
end

@testset "Minimum Unsatisfiable System" begin
optimizer, x, y, z, c = base_satisfiability_problem()
MOI.optimize!(optimizer)

MOI.get(optimizer, MOI.TerminationStatus()) == MOI.OPTIMAL
SCIP.compute_minimum_unsatisfied_constraints!(optimizer)

@test MOI.get(optimizer, SCIP.UnsatisfiableSystemStatus()) == MOI.NO_CONFLICT_EXISTS

optimizer, x, y, z, c = base_satisfiability_problem()
c2 = MOI.add_constraint(optimizer, 1.0 * x + y + z, MOI.GreaterThan(2.0))

MOI.set(optimizer, MOI.ConstraintName(), c2, "lincons2")

MOI.optimize!(optimizer)
MOI.get(optimizer, MOI.TerminationStatus()) == MOI.INFEASIBLE

SCIP.compute_minimum_unsatisfied_constraints!(optimizer)
@test MOI.get(optimizer, SCIP.UnsatisfiableSystemStatus()) == MOI.CONFLICT_FOUND

@test Int(MOI.get(optimizer, MOI.ConstraintSatisfiabilityStatus(), c)) + Int(MOI.get(optimizer, MOI.ConstraintSatisfiabilityStatus(), c2)) ≥ 1
end