Skip to content
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

Fix distance_to_set for sets without a definition in MOI #699

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions src/Constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ end

AbstractTrees.children(c::Constraint) = (c.child,)

# A fallback. Define a new method if `MOI.Utilities.distance_to_set`
# is not defined.
# A fallback. Define a new method if `MOI.Utilities.distance_to_set` is not
# defined.
function is_feasible(x, set, tol)
return MOI.Utilities.distance_to_set(x, set) <= tol
try
return MOI.Utilities.distance_to_set(x, set) <= tol
odow marked this conversation as resolved.
Show resolved Hide resolved
catch
odow marked this conversation as resolved.
Show resolved Hide resolved
return true # default to `true`
odow marked this conversation as resolved.
Show resolved Hide resolved
end
end

function is_feasible(x::AbstractMatrix, set::MOI.AbstractVectorSet, tol)
return is_feasible(vec(x), set, tol)
end

function is_feasible(x::Number, set::MOI.AbstractVectorSet, tol)
Expand Down
26 changes: 26 additions & 0 deletions test/test_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,32 @@ function test_RelativeEntropyEpiConeSquare()
return
end

function test_distance_to_set_matrix()
x = Variable(2, 2)
y = Variable()
fix!(x, [1 0; 0 1])
# Constraint has a fixed `Matrix` value.
model = minimize(y, [sum(x; dims = 1) <= 1, y >= 1])
solve!(model, SCS.Optimizer; silent = true)
@test ≈(model.optval, 1.0; atol = 1e-6)
odow marked this conversation as resolved.
Show resolved Hide resolved
return
end

function test_distance_to_set_undefined()
t = Variable()
fix!(t, 2)
x = Variable(2, 2)
fix!(x, [1 0; 0 1])
y = Variable()
# This constraint is fixed, and `MOI.distance_to_set` is not defined for it,
# but it should still work without erroring.
c = Convex.Constraint(vcat(t, vec(x)), MOI.NormSpectralCone(2, 2))
model = minimize(y, [c, y >= 1])
solve!(model, SCS.Optimizer; silent = true)
@test ≈(model.optval, 1.0; atol = 1e-6)
return
end

end # TestConstraints

TestConstraints.runtests()
Loading