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

caching bridge optimizer copy improvement #696

Merged
merged 7 commits into from
Mar 13, 2019
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
21 changes: 20 additions & 1 deletion src/Bridges/bridgeoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`
blegat marked this conversation as resolved.
Show resolved Hide resolved
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))
Expand All @@ -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
# TODO add transform
16 changes: 8 additions & 8 deletions src/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down