Skip to content

Commit

Permalink
Simplify diff further
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Mar 4, 2021
1 parent 52314bf commit b44c470
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
28 changes: 17 additions & 11 deletions src/Utilities/cachingoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,11 @@ end

function MOI.add_constrained_variable(
m::CachingOptimizer,
set::S,
) where {S<:MOI.AbstractScalarSet}
MOI.supports_add_constrained_variable(m, S)
set::MOI.AbstractScalarSet,
)
if !MOI.supports_add_constrained_variable(m, typeof(set))
throw(MOI.UnsupportedConstraint{MOI.SingleVariable,typeof(set)}())
end
if m.state == MOIU.ATTACHED_OPTIMIZER
if m.mode == MOIU.AUTOMATIC
try
Expand Down Expand Up @@ -429,9 +431,11 @@ function MOI.supports_add_constrained_variables(
end
function MOI.add_constrained_variables(
m::CachingOptimizer,
set::S,
) where {S<:MOI.AbstractVectorSet}
MOI.supports_add_constrained_variables(m, S)
set::MOI.AbstractVectorSet,
)
if !MOI.supports_add_constrained_variables(m, typeof(set))
throw(MOI.UnsupportedConstraint{MOI.VectorOfVariables,typeof(set)}())
end
if m.state == ATTACHED_OPTIMIZER
if m.mode == AUTOMATIC
try
Expand Down Expand Up @@ -473,10 +477,12 @@ end

function MOI.add_constraint(
m::CachingOptimizer,
func::F,
set::S,
) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet}
MOI.supports_constraint(m, F, S)
func::MOI.AbstractFunction,
set::MOI.AbstractSet,
)
if !MOI.supports_constraint(m, typeof(func), typeof(set))
throw(MOI.UnsupportedConstraint{typeof(func),typeof(set)}())
end
if m.state == ATTACHED_OPTIMIZER
if m.mode == AUTOMATIC
try
Expand Down Expand Up @@ -768,7 +774,7 @@ function MOI.supports(
attr::Union{MOI.AbstractModelAttribute,MOI.AbstractOptimizerAttribute},
)
return MOI.supports(m.model_cache, attr) &&
(m.state == NO_OPTIMIZER || MOI.supports(m.optimizer, attr))
(m.state == NO_OPTIMIZER || MOI.supports(m.optimizer, attr))
end

function MOI.supports(
Expand Down
19 changes: 9 additions & 10 deletions src/instantiate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ end

"""
instantiate(optimizer_constructor;
with_bridge_type::Union{Nothing,Type}=nothing,
with_names::Bool = false)
with_bridge_type::Union{Nothing, Type}=nothing,
with_names::Bool=false)
Creates an instance of optimizer by calling
`optimizer_constructor.optimizer_constructor()` and setting the parameters in
Expand Down Expand Up @@ -124,15 +124,14 @@ function instantiate(
optimizer = _instantiate_and_check(optimizer_constructor)
if with_bridge_type === nothing
return optimizer
elseif Utilities.supports_default_copy_to(optimizer, with_names)
return Bridges.full_bridge_optimizer(optimizer, with_bridge_type)
else
cache = Utilities.UniversalFallback(Utilities.Model{with_bridge_type}())
return Bridges.full_bridge_optimizer(
Utilities.CachingOptimizer(cache, optimizer),
with_bridge_type,
)
end
if !Utilities.supports_default_copy_to(optimizer, with_names)
universal_fallback =
Utilities.UniversalFallback(Utilities.Model{with_bridge_type}())
optimizer = Utilities.CachingOptimizer(universal_fallback, optimizer)
end
return Bridges.full_bridge_optimizer(optimizer, with_bridge_type)
end

# Add a fallback so we don't add bridges on-top-of bridges!
instantiate(optimizer::Bridges.LazyBridgeOptimizer; kwargs...) = optimizer

0 comments on commit b44c470

Please sign in to comment.