From 27c0bb411a658534cedb2cb42f973e22afa263c6 Mon Sep 17 00:00:00 2001 From: Nicholas Dinsmore Date: Mon, 11 Mar 2019 17:08:05 -0400 Subject: [PATCH 1/7] Change copyconstraints to do multiple constraints at the same time. --- src/Bridges/bridgeoptimizer.jl | 21 ++++++++++++++++++++- src/Utilities/copy.jl | 23 ++++++++++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Bridges/bridgeoptimizer.jl b/src/Bridges/bridgeoptimizer.jl index 95dcc57627..2c8979988a 100644 --- a/src/Bridges/bridgeoptimizer.jl +++ b/src/Bridges/bridgeoptimizer.jl @@ -323,6 +323,25 @@ function MOI.add_constraint(b::AbstractBridgeOptimizer, f::MOI.AbstractFunction, return MOI.add_constraint(b.model, f, s) end end +function MOI.add_constraints(b::AbstractBridgeOptimizer, f::Vector{F}, + s::Vector{S}) where {F <: MOI.AbstractFunction, S <: MOI.AbstractSet} + if is_bridged(b, F, S) + # We compute `BridgeType` first as `concrete_bridge_type` calls + # `bridge_type` which might throw an `UnsupportedConstraint` error in + # which case, we do not want any modification to have been done + BridgeType = concrete_bridge_type(b, F, S) + # `add_constraint` might throw an `UnsupportedConstraint` but no + # modification has been done in the previous line + cis = MOI.add_constraints(b.bridged, f, s) + for (n,ci) in enumerate(cis) + @assert !haskey(b.bridges, ci) + b.bridges[ci] = BridgeType(b, f[n], s[n]) + end + return cis + else + return MOI.add_constraints(b.model, f, s) + end +end function MOI.modify(b::AbstractBridgeOptimizer, ci::CI, change::MOI.AbstractFunctionModification) if is_bridged(b, typeof(ci)) @@ -343,4 +362,4 @@ end MOI.add_variable(b::AbstractBridgeOptimizer) = MOI.add_variable(b.model) MOI.add_variables(b::AbstractBridgeOptimizer, n) = MOI.add_variables(b.model, n) -# TODO add transform \ No newline at end of file +# TODO add transform diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index 98407c136d..7df749e585 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -151,14 +151,27 @@ Copy the constraints of type `F`-in-`S` from the model `src` the model `dest` an function copyconstraints!(dest::MOI.ModelLike, src::MOI.ModelLike, copy_names::Bool, idxmap::IndexMap, ::Type{F}, ::Type{S}) where {F<:MOI.AbstractFunction, S<:MOI.AbstractSet} # Copy constraints cis_src = MOI.get(src, MOI.ListOfConstraintIndices{F, S}()) - for ci_src in cis_src - f_src = MOI.get(src, MOI.ConstraintFunction(), ci_src) - f_dest = mapvariables(idxmap, f_src) - s = MOI.get(src, MOI.ConstraintSet(), ci_src) - ci_dest = MOI.add_constraint(dest, f_dest, s) + # for ci_src in cis_src + # @show ci_src + # f_src = MOI.get(src, MOI.ConstraintFunction(), ci_src) + # f_dest = mapvariables(idxmap, f_src) + # s = MOI.get(src, MOI.ConstraintSet(), ci_src) + # @show ci_dest = MOI.add_constraint(dest, f_dest, s) + # idxmap.conmap[ci_src] = ci_dest + # end + + + f_src = MOI.get(src, MOI.ConstraintFunction(), cis_src) + # idxmap is wraped with [] to signify singlton + f_dest = mapvariables.([idxmap], f_src) + s = MOI.get(src, MOI.ConstraintSet(), cis_src) + cis_dest = MOI.add_constraints(dest, f_dest, s) + for (ci_src,ci_dest) in zip(cis_src,cis_dest) idxmap.conmap[ci_src] = ci_dest end + + pass_attributes(dest, src, copy_names, idxmap, cis_src) end From c517814edc62fabcf188acd2b4a08ec834a342eb Mon Sep 17 00:00:00 2001 From: Nicholas Dinsmore Date: Mon, 11 Mar 2019 17:08:16 -0400 Subject: [PATCH 2/7] Cleanup --- src/Utilities/copy.jl | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index 7df749e585..773f2c5434 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -151,27 +151,14 @@ Copy the constraints of type `F`-in-`S` from the model `src` the model `dest` an function copyconstraints!(dest::MOI.ModelLike, src::MOI.ModelLike, copy_names::Bool, idxmap::IndexMap, ::Type{F}, ::Type{S}) where {F<:MOI.AbstractFunction, S<:MOI.AbstractSet} # Copy constraints cis_src = MOI.get(src, MOI.ListOfConstraintIndices{F, S}()) - # for ci_src in cis_src - # @show ci_src - # f_src = MOI.get(src, MOI.ConstraintFunction(), ci_src) - # f_dest = mapvariables(idxmap, f_src) - # s = MOI.get(src, MOI.ConstraintSet(), ci_src) - # @show ci_dest = MOI.add_constraint(dest, f_dest, s) - # idxmap.conmap[ci_src] = ci_dest - # end - - f_src = MOI.get(src, MOI.ConstraintFunction(), cis_src) # idxmap is wraped with [] to signify singlton f_dest = mapvariables.([idxmap], f_src) s = MOI.get(src, MOI.ConstraintSet(), cis_src) cis_dest = MOI.add_constraints(dest, f_dest, s) - for (ci_src,ci_dest) in zip(cis_src,cis_dest) + for (ci_src,ci_dest) in zip(cis_src, cis_dest) idxmap.conmap[ci_src] = ci_dest end - - - pass_attributes(dest, src, copy_names, idxmap, cis_src) end From 14d3e43f88b0f8c998be25ff8cb2a28cea26d4ec Mon Sep 17 00:00:00 2001 From: Nicholas Dinsmore Date: Mon, 11 Mar 2019 22:32:34 -0400 Subject: [PATCH 3/7] use add_variables instead of add_variable --- src/Utilities/copy.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index 773f2c5434..cf46c22ff6 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -184,8 +184,9 @@ function default_copy_to(dest::MOI.ModelLike, src::MOI.ModelLike, copy_names::Bo # Copy variables vis_src = MOI.get(src, MOI.ListOfVariableIndices()) - for vi in vis_src - idxmap.varmap[vi] = MOI.add_variable(dest) + vars=MOI.add_variables(dest, length(vis_src)) + for (vi,var) in zip(vis_src, vars) + idxmap.varmap[vi] = var end # Copy variable attributes From c3ae8f5def95d404bb1cd90a15a0444e563942af Mon Sep 17 00:00:00 2001 From: Nicholas Dinsmore Date: Tue, 12 Mar 2019 08:56:35 -0400 Subject: [PATCH 4/7] cleanup --- src/Bridges/bridgeoptimizer.jl | 3 ++- src/Utilities/copy.jl | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Bridges/bridgeoptimizer.jl b/src/Bridges/bridgeoptimizer.jl index 2c8979988a..5e41779ac7 100644 --- a/src/Bridges/bridgeoptimizer.jl +++ b/src/Bridges/bridgeoptimizer.jl @@ -324,7 +324,8 @@ function MOI.add_constraint(b::AbstractBridgeOptimizer, f::MOI.AbstractFunction, end end function MOI.add_constraints(b::AbstractBridgeOptimizer, f::Vector{F}, - s::Vector{S}) where {F <: MOI.AbstractFunction, S <: MOI.AbstractSet} + s::Vector{S}) where { F <: MOI.AbstractFunction, + S <: MOI.AbstractSet} if is_bridged(b, F, S) # We compute `BridgeType` first as `concrete_bridge_type` calls # `bridge_type` which might throw an `UnsupportedConstraint` error in diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index cf46c22ff6..d83dcd682a 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -152,11 +152,10 @@ function copyconstraints!(dest::MOI.ModelLike, src::MOI.ModelLike, copy_names::B # Copy constraints cis_src = MOI.get(src, MOI.ListOfConstraintIndices{F, S}()) f_src = MOI.get(src, MOI.ConstraintFunction(), cis_src) - # idxmap is wraped with [] to signify singlton - f_dest = mapvariables.([idxmap], f_src) + f_dest = mapvariables.(Ref(idxmap), f_src) s = MOI.get(src, MOI.ConstraintSet(), cis_src) cis_dest = MOI.add_constraints(dest, f_dest, s) - for (ci_src,ci_dest) in zip(cis_src, cis_dest) + for (ci_src, ci_dest) in zip(cis_src, cis_dest) idxmap.conmap[ci_src] = ci_dest end pass_attributes(dest, src, copy_names, idxmap, cis_src) @@ -184,8 +183,8 @@ function default_copy_to(dest::MOI.ModelLike, src::MOI.ModelLike, copy_names::Bo # Copy variables vis_src = MOI.get(src, MOI.ListOfVariableIndices()) - vars=MOI.add_variables(dest, length(vis_src)) - for (vi,var) in zip(vis_src, vars) + vars = MOI.add_variables(dest, length(vis_src)) + for (vi, var) in zip(vis_src, vars) idxmap.varmap[vi] = var end From aec6fe090fab4f0a6fcafecdf7a06a27f5868e43 Mon Sep 17 00:00:00 2001 From: Nicholas Dinsmore Date: Wed, 13 Mar 2019 10:31:25 -0400 Subject: [PATCH 5/7] comment change --- src/Bridges/bridgeoptimizer.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Bridges/bridgeoptimizer.jl b/src/Bridges/bridgeoptimizer.jl index 5e41779ac7..4af7f035dd 100644 --- a/src/Bridges/bridgeoptimizer.jl +++ b/src/Bridges/bridgeoptimizer.jl @@ -330,9 +330,11 @@ function MOI.add_constraints(b::AbstractBridgeOptimizer, f::Vector{F}, # We compute `BridgeType` first as `concrete_bridge_type` calls # `bridge_type` which might throw an `UnsupportedConstraint` error in # which case, we do not want any modification to have been done + # See add_constraint for why we we call `concrete_bridge_type` separately + # before `add_constraints` BridgeType = concrete_bridge_type(b, F, S) - # `add_constraint` might throw an `UnsupportedConstraint` but no - # modification has been done in the previous line + # `add_constraints` might throw an `UnsupportedConstraint` but no + # modification has been done in the previous line cis = MOI.add_constraints(b.bridged, f, s) for (n,ci) in enumerate(cis) @assert !haskey(b.bridges, ci) From 28bf751252f7467b8a45872b548cce465d3ed361 Mon Sep 17 00:00:00 2001 From: Nicholas Dinsmore Date: Wed, 13 Mar 2019 10:36:00 -0400 Subject: [PATCH 6/7] Remove redundant comment --- src/Bridges/bridgeoptimizer.jl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Bridges/bridgeoptimizer.jl b/src/Bridges/bridgeoptimizer.jl index 4af7f035dd..985ddc55dd 100644 --- a/src/Bridges/bridgeoptimizer.jl +++ b/src/Bridges/bridgeoptimizer.jl @@ -327,10 +327,7 @@ function MOI.add_constraints(b::AbstractBridgeOptimizer, f::Vector{F}, s::Vector{S}) where { F <: MOI.AbstractFunction, S <: MOI.AbstractSet} if is_bridged(b, F, S) - # We compute `BridgeType` first as `concrete_bridge_type` calls - # `bridge_type` which might throw an `UnsupportedConstraint` error in - # which case, we do not want any modification to have been done - # See add_constraint for why we we call `concrete_bridge_type` separately + # See add_constraint for why we we call `concrete_bridge_type` separately # before `add_constraints` BridgeType = concrete_bridge_type(b, F, S) # `add_constraints` might throw an `UnsupportedConstraint` but no From f0217f4178f450cc91a90fb1a5e26970c2d367ff Mon Sep 17 00:00:00 2001 From: Nicholas Dinsmore Date: Wed, 13 Mar 2019 10:52:39 -0400 Subject: [PATCH 7/7] spacing --- src/Bridges/bridgeoptimizer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bridges/bridgeoptimizer.jl b/src/Bridges/bridgeoptimizer.jl index 985ddc55dd..ca7b9290b0 100644 --- a/src/Bridges/bridgeoptimizer.jl +++ b/src/Bridges/bridgeoptimizer.jl @@ -333,7 +333,7 @@ function MOI.add_constraints(b::AbstractBridgeOptimizer, f::Vector{F}, # `add_constraints` might throw an `UnsupportedConstraint` but no # modification has been done in the previous line cis = MOI.add_constraints(b.bridged, f, s) - for (n,ci) in enumerate(cis) + for (n, ci) in enumerate(cis) @assert !haskey(b.bridges, ci) b.bridges[ci] = BridgeType(b, f[n], s[n]) end