-
Notifications
You must be signed in to change notification settings - Fork 92
Add set conversion bridge #2536
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c2a40ed
Add set conversion bridge
blegat 88e9cf6
Remove debug
blegat 6e4b9c2
Add doc and tests
blegat 3a4f4cb
fix format
blegat 30e439c
Fixes
blegat 40c870c
Fixes
blegat 889b40b
Fix format
blegat 1c8f2bb
Fix
blegat a32e2e8
Update src/Bridges/Constraint/bridges/set_conversion.jl
blegat 1f60a36
Add test
blegat 77a5de0
Update src/Bridges/Constraint/bridges/set_conversion.jl
odow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright (c) 2017: Miles Lubin and contributors | ||
# Copyright (c) 2017: Google Inc. | ||
# | ||
# Use of this source code is governed by an MIT-style license that can be found | ||
# in the LICENSE.md file or at https://opensource.org/licenses/MIT. | ||
|
||
""" | ||
odow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SetConversionBridge{T,S2,S1,F} <: | ||
MOI.Bridges.Constraint.SetMapBridge{T,S2,S1,F,F} | ||
|
||
`SetConversionBridge` implements the following reformulations: | ||
|
||
* ``f(x) \\in S1`` into ``f(x) \\in S2`` | ||
|
||
In order to add this bridge, you need to create a bridge specific | ||
for a given type `T` and set `S2`: | ||
```julia | ||
MOI.Bridges.add_bridge(model, MOI.Bridges.Constraint.SetConversionBridge{T,S2}) | ||
``` | ||
In order to define a bridge with `S2` specified but `T` unspecified, for example | ||
for `JuMP.add_bridge`, you can use | ||
```julia | ||
const MyBridge{T,S1,F} = MOI.Bridges.Constraint.SetConversionBridge{T,S2,S1,F} | ||
``` | ||
|
||
See also [`FunctionConversionBridge`](@ref). | ||
|
||
## Source node | ||
|
||
`SetConversionBridge` supports: | ||
|
||
* `F` in `S1` | ||
|
||
## Target nodes | ||
|
||
`SetConversionBridge` creates: | ||
|
||
* `F` in `S2` | ||
""" | ||
struct SetConversionBridge{T,S2,S1,F} <: | ||
MOI.Bridges.Constraint.SetMapBridge{T,S2,S1,F,F} | ||
constraint::MOI.ConstraintIndex{F,S2} | ||
end | ||
|
||
function MOI.supports_constraint( | ||
::Type{SetConversionBridge{T,S2}}, | ||
::Type{F}, | ||
::Type{S1}, | ||
) where {T,F<:MOI.AbstractFunction,S1<:MOI.AbstractSet,S2} | ||
return isfinite(MOI.Bridges.Constraint.conversion_cost(S2, S1)) | ||
end | ||
|
||
function MOI.Bridges.Constraint.concrete_bridge_type( | ||
::Type{SetConversionBridge{T,S2}}, | ||
::Type{F}, | ||
::Type{S1}, | ||
) where {T,F<:MOI.AbstractFunction,S1<:MOI.AbstractSet,S2} | ||
return SetConversionBridge{T,S2,S1,F} | ||
end | ||
|
||
function MOI.Bridges.Constraint.conversion_cost( | ||
::Type{<:MOI.AbstractSet}, | ||
::Type{<:MOI.AbstractSet}, | ||
) | ||
return Inf | ||
end | ||
|
||
function MOI.Bridges.bridging_cost( | ||
::Type{<:SetConversionBridge{T,S2,S1}}, | ||
) where {T,S2,S1} | ||
return MOI.Bridges.Constraint.conversion_cost(S2, S1) | ||
end | ||
|
||
function MOI.Bridges.map_set( | ||
::Type{<:SetConversionBridge{T,S2,S1}}, | ||
set::S1, | ||
) where {T,S2,S1} | ||
return convert(S2, set) | ||
end | ||
|
||
function MOI.Bridges.inverse_map_set( | ||
::Type{<:SetConversionBridge{T,S2,S1}}, | ||
set::S2, | ||
) where {T,S2,S1} | ||
return convert(S1, set) | ||
end | ||
|
||
function MOI.Bridges.map_function(::Type{<:SetConversionBridge}, func) | ||
return func | ||
end | ||
|
||
function MOI.Bridges.inverse_map_function(::Type{<:SetConversionBridge}, func) | ||
return func | ||
end | ||
|
||
function MOI.Bridges.adjoint_map_function(::Type{<:SetConversionBridge}, func) | ||
return func | ||
end | ||
|
||
function MOI.Bridges.inverse_adjoint_map_function( | ||
::Type{<:SetConversionBridge}, | ||
func, | ||
) | ||
return func | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright (c) 2017: Miles Lubin and contributors | ||
# Copyright (c) 2017: Google Inc. | ||
# | ||
# Use of this source code is governed by an MIT-style license that can be found | ||
# in the LICENSE.md file or at https://opensource.org/licenses/MIT. | ||
|
||
module TestConstraintSetConversion | ||
|
||
using Test | ||
|
||
import MathOptInterface as MOI | ||
|
||
function runtests() | ||
for name in names(@__MODULE__; all = true) | ||
if startswith("$(name)", "test_") | ||
@testset "$(name)" begin | ||
getfield(@__MODULE__, name)() | ||
end | ||
end | ||
end | ||
return | ||
end | ||
|
||
struct Zero <: MOI.AbstractScalarSet end | ||
|
||
function MOI.Bridges.Constraint.conversion_cost( | ||
::Type{MOI.EqualTo{Float64}}, | ||
::Type{Zero}, | ||
) | ||
return 1.0 | ||
end | ||
|
||
Base.convert(::Type{MOI.EqualTo{Float64}}, ::Zero) = MOI.EqualTo(0.0) | ||
|
||
function Base.convert(::Type{Zero}, s::MOI.EqualTo) | ||
if !iszero(s.value) | ||
throw(InexactError(convert, (Zero, s))) | ||
end | ||
return Zero() | ||
end | ||
|
||
# Does not make sense that this is convertible but it's | ||
# just to test `conversion_cost` | ||
function MOI.Bridges.Constraint.conversion_cost( | ||
::Type{MOI.LessThan{Float64}}, | ||
::Type{Zero}, | ||
) | ||
return 10.0 | ||
end | ||
|
||
const EqualToBridge{T,S1,F} = | ||
MOI.Bridges.Constraint.SetConversionBridge{T,MOI.EqualTo{T},S1,F} | ||
|
||
function test_runtests() | ||
MOI.Bridges.runtests( | ||
EqualToBridge, | ||
model -> begin | ||
x = MOI.add_variable(model) | ||
MOI.add_constraint(model, x, Zero()) | ||
end, | ||
model -> begin | ||
x = MOI.add_variable(model) | ||
MOI.add_constraint(model, x, MOI.EqualTo(0.0)) | ||
end, | ||
) | ||
return | ||
end | ||
|
||
function test_conversion_cost(T = Float64) | ||
model = MOI.Utilities.Model{T}() | ||
bridged = MOI.Bridges.LazyBridgeOptimizer(model) | ||
MOI.Bridges.add_bridge( | ||
bridged, | ||
MOI.Bridges.Constraint.SetConversionBridge{T,MOI.LessThan{T}}, | ||
) | ||
@test MOI.Bridges.bridge_type(bridged, MOI.VariableIndex, Zero) == | ||
MOI.Bridges.Constraint.SetConversionBridge{ | ||
T, | ||
MOI.LessThan{T}, | ||
Zero, | ||
MOI.VariableIndex, | ||
} | ||
MOI.Bridges.add_bridge( | ||
bridged, | ||
MOI.Bridges.Constraint.SetConversionBridge{T,MOI.EqualTo{T}}, | ||
) | ||
@test MOI.Bridges.bridge_type(bridged, MOI.VariableIndex, Zero) == | ||
MOI.Bridges.Constraint.SetConversionBridge{ | ||
T, | ||
MOI.EqualTo{T}, | ||
Zero, | ||
MOI.VariableIndex, | ||
} | ||
end | ||
|
||
end # module | ||
|
||
TestConstraintSetConversion.runtests() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.