diff --git a/src/Bridges/bridgeoptimizer.jl b/src/Bridges/bridgeoptimizer.jl index 95dcc57627..ca7b9290b0 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) + # 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 + # 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..d83dcd682a 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -151,14 +151,13 @@ 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) + f_src = MOI.get(src, MOI.ConstraintFunction(), cis_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) idxmap.conmap[ci_src] = ci_dest end - pass_attributes(dest, src, copy_names, idxmap, cis_src) end @@ -184,8 +183,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