From 68f5fc686926219955954fe7dafe9e2e1561f2ba Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 13 May 2021 13:05:25 +1200 Subject: [PATCH 1/3] Simplify ListOfConstraintTypesPresent in Bridges --- src/Bridges/bridge_optimizer.jl | 47 ++++++++++----------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/src/Bridges/bridge_optimizer.jl b/src/Bridges/bridge_optimizer.jl index 8277799b49..dc60a4b7b0 100644 --- a/src/Bridges/bridge_optimizer.jl +++ b/src/Bridges/bridge_optimizer.jl @@ -695,51 +695,32 @@ function MOI.get( end return s end + function MOI.get( b::AbstractBridgeOptimizer, attr::MOI.ListOfConstraintTypesPresent, ) - if Constraint.has_bridges(Constraint.bridges(b)) && - Variable.has_bridges(Variable.bridges(b)) - set_of_types = Constraint.list_of_key_types(Constraint.bridges(b)) + list_of_types = Set(MOI.get(b.model, attr)) + if Constraint.has_bridges(Constraint.bridges(b)) + union!( + list_of_types, + Constraint.list_of_key_types(Constraint.bridges(b)) + ) + end + if Variable.has_bridges(Variable.bridges(b)) union!( - set_of_types, + list_of_types, Variable.list_of_constraint_types(Variable.bridges(b)), ) - # There may be types already in `list_of_types` of a supported constraint - # that was force-bridged because a variable in the `SingleVariable` or - # `VectorOfVariables` function was bridged even though the constraint type - # is supported by `b.model`. As `set_of_types` is a set, these duplicates - # are merge automatically. - union!(set_of_types, MOI.get(b.model, attr)) - list_of_types = collect(set_of_types) - elseif Constraint.has_bridges(Constraint.bridges(b)) - # There should be no duplicate so no need to do `Set` union. - list_of_types = [ - MOI.get(b.model, attr) - collect(Constraint.list_of_key_types(Constraint.bridges(b))) - ] - elseif Variable.has_bridges(Variable.bridges(b)) - set_of_types = Variable.list_of_constraint_types(Variable.bridges(b)) - union!(set_of_types, MOI.get(b.model, attr)) - list_of_types = collect(set_of_types) - else - list_of_types = copy(MOI.get(b.model, attr)) end # Some constraint types show up in `list_of_types` including when all the # constraints of that type have been created by bridges and not by the user. # The code in `NumberOfConstraints` takes care of removing these constraints # from the counter so we can rely on it to remove these constraint types. - types_to_remove = findall( - iszero.( - map( - FS -> MOI.get(b, MOI.NumberOfConstraints{FS...}()), - list_of_types, - ), - ), - ) - deleteat!(list_of_types, types_to_remove) - return list_of_types + filter!(list_of_types) do (F, S) + return MOI.get(b, MOI.NumberOfConstraints{F,S}()) > 0 + end + return collect(list_of_types) end # Model an optimizer attributes From b55e2b9ba47be37b04b9da4ee007bad7ea4b762d Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 13 May 2021 13:28:55 +1200 Subject: [PATCH 2/3] Update bridge_optimizer.jl --- src/Bridges/bridge_optimizer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bridges/bridge_optimizer.jl b/src/Bridges/bridge_optimizer.jl index dc60a4b7b0..41fee18e24 100644 --- a/src/Bridges/bridge_optimizer.jl +++ b/src/Bridges/bridge_optimizer.jl @@ -704,7 +704,7 @@ function MOI.get( if Constraint.has_bridges(Constraint.bridges(b)) union!( list_of_types, - Constraint.list_of_key_types(Constraint.bridges(b)) + Constraint.list_of_key_types(Constraint.bridges(b)), ) end if Variable.has_bridges(Variable.bridges(b)) From 13f284fd5c15643385eb5595f0078f8ec0ef4ce7 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 13 May 2021 14:14:41 +1200 Subject: [PATCH 3/3] Fixes --- src/Bridges/bridge_optimizer.jl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Bridges/bridge_optimizer.jl b/src/Bridges/bridge_optimizer.jl index dc60a4b7b0..92c0429d14 100644 --- a/src/Bridges/bridge_optimizer.jl +++ b/src/Bridges/bridge_optimizer.jl @@ -700,19 +700,20 @@ function MOI.get( b::AbstractBridgeOptimizer, attr::MOI.ListOfConstraintTypesPresent, ) - list_of_types = Set(MOI.get(b.model, attr)) + list_of_types = MOI.get(b.model, attr) if Constraint.has_bridges(Constraint.bridges(b)) - union!( + append!( list_of_types, - Constraint.list_of_key_types(Constraint.bridges(b)) + Constraint.list_of_key_types(Constraint.bridges(b)), ) end if Variable.has_bridges(Variable.bridges(b)) - union!( + append!( list_of_types, Variable.list_of_constraint_types(Variable.bridges(b)), ) end + unique!(list_of_types) # Some constraint types show up in `list_of_types` including when all the # constraints of that type have been created by bridges and not by the user. # The code in `NumberOfConstraints` takes care of removing these constraints @@ -720,7 +721,7 @@ function MOI.get( filter!(list_of_types) do (F, S) return MOI.get(b, MOI.NumberOfConstraints{F,S}()) > 0 end - return collect(list_of_types) + return list_of_types end # Model an optimizer attributes