diff --git a/src/Utilities/objective_container.jl b/src/Utilities/objective_container.jl index 62272536f9..5effee8f26 100644 --- a/src/Utilities/objective_container.jl +++ b/src/Utilities/objective_container.jl @@ -116,58 +116,29 @@ function MOI.supports( return true end -function _get_single_variable(o::ObjectiveContainer{T}) where {T} - return o.single_variable::MOI.VariableIndex -end - -function _get_scalar_affine(o::ObjectiveContainer{T}) where {T} - return o.scalar_affine::MOI.ScalarAffineFunction{T} -end - -function _get_scalar_quadratic(o::ObjectiveContainer{T}) where {T} - return o.scalar_quadratic::MOI.ScalarQuadraticFunction{T} -end - -function _get_scalar_nonlinear(o::ObjectiveContainer{T}) where {T} - return o.scalar_nonlinear::MOI.ScalarNonlinearFunction -end - -function _get_vector_variables(o::ObjectiveContainer{T}) where {T} - return o.vector_variables::MOI.VectorOfVariables -end - -function _get_vector_affine(o::ObjectiveContainer{T}) where {T} - return o.vector_affine::MOI.VectorAffineFunction{T} -end - -function _get_vector_quadratic(o::ObjectiveContainer{T}) where {T} - return o.vector_quadratic::MOI.VectorQuadraticFunction{T} -end - -function _get_vector_nonlinear(o::ObjectiveContainer{T}) where {T} - return o.vector_nonlinear::MOI.VectorNonlinearFunction -end +_not_nothing(x) = x +_not_nothing(::Nothing) = error("Unexpected internal error.") function MOI.get( o::ObjectiveContainer{T}, ::MOI.ObjectiveFunction{F}, ) where {T,F} if o.scalar_affine !== nothing - return convert(F, _get_scalar_affine(o)) + return convert(F, _not_nothing(o.scalar_affine)) elseif o.single_variable !== nothing - return convert(F, _get_single_variable(o)) + return convert(F, _not_nothing(o.single_variable)) elseif o.scalar_quadratic !== nothing - return convert(F, _get_scalar_quadratic(o)) + return convert(F, _not_nothing(o.scalar_quadratic)) elseif o.scalar_nonlinear !== nothing - return convert(F, _get_scalar_nonlinear(o)) + return convert(F, _not_nothing(o.scalar_nonlinear)) elseif o.vector_variables !== nothing - return convert(F, _get_vector_variables(o)) + return convert(F, _not_nothing(o.vector_variables)) elseif o.vector_affine !== nothing - return convert(F, _get_vector_affine(o)) + return convert(F, _not_nothing(o.vector_affine)) elseif o.vector_quadratic !== nothing - return convert(F, _get_vector_quadratic(o)) + return convert(F, _not_nothing(o.vector_quadratic)) elseif o.vector_nonlinear !== nothing - return convert(F, _get_vector_nonlinear(o)) + return convert(F, _not_nothing(o.vector_nonlinear)) end # The default if no objective is set. return convert(F, zero(MOI.ScalarAffineFunction{T})) @@ -294,11 +265,14 @@ function MOI.modify( change::MOI.AbstractFunctionModification, ) where {T} if o.single_variable !== nothing - o.single_variable = modify_function!(_get_single_variable(o), change) + o.single_variable = + modify_function!(_not_nothing(o.single_variable), change) elseif o.scalar_affine !== nothing - o.scalar_affine = modify_function!(_get_scalar_affine(o), change) + o.scalar_affine = + modify_function!(_not_nothing(o.scalar_affine), change) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = modify_function!(_get_scalar_quadratic(o), change) + o.scalar_quadratic = + modify_function!(_not_nothing(o.scalar_quadratic), change) elseif o.scalar_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -308,11 +282,14 @@ function MOI.modify( ), ) elseif o.vector_variables !== nothing - o.vector_variables = modify_function!(_get_vector_variables(o), change) + o.vector_variables = + modify_function!(_not_nothing(o.vector_variables), change) elseif o.vector_quadratic !== nothing - o.vector_quadratic = modify_function!(_get_vector_quadratic(o), change) + o.vector_quadratic = + modify_function!(_not_nothing(o.vector_quadratic), change) elseif o.vector_affine !== nothing - o.vector_affine = modify_function!(_get_vector_affine(o), change) + o.vector_affine = + modify_function!(_not_nothing(o.vector_affine), change) elseif o.vector_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -336,13 +313,14 @@ end function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) if o.single_variable !== nothing - if x == _get_single_variable(o) + if x == _not_nothing(o.single_variable) _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = remove_variable(_get_scalar_affine(o), x) + o.scalar_affine = remove_variable(_not_nothing(o.scalar_affine), x) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = remove_variable(_get_scalar_quadratic(o), x) + o.scalar_quadratic = + remove_variable(_not_nothing(o.scalar_quadratic), x) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -352,14 +330,16 @@ function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) ), ) elseif o.vector_variables !== nothing - o.vector_variables = remove_variable(_get_vector_variables(o), x) + o.vector_variables = + remove_variable(_not_nothing(o.vector_variables), x) if isempty(o.vector_variables.variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing - o.vector_affine = remove_variable(_get_vector_affine(o), x) + o.vector_affine = remove_variable(_not_nothing(o.vector_affine), x) elseif o.vector_quadratic !== nothing - o.vector_quadratic = remove_variable(_get_vector_quadratic(o), x) + o.vector_quadratic = + remove_variable(_not_nothing(o.vector_quadratic), x) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -375,13 +355,14 @@ end function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) keep = v -> !(v in x) if o.single_variable !== nothing - if _get_single_variable(o) in x + if _not_nothing(o.single_variable) in x _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = filter_variables(keep, _get_scalar_affine(o)) + o.scalar_affine = filter_variables(keep, _not_nothing(o.scalar_affine)) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = filter_variables(keep, _get_scalar_quadratic(o)) + o.scalar_quadratic = + filter_variables(keep, _not_nothing(o.scalar_quadratic)) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -391,14 +372,16 @@ function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) ), ) elseif o.vector_variables !== nothing - o.vector_variables = filter_variables(keep, _get_vector_variables(o)) + o.vector_variables = + filter_variables(keep, _not_nothing(o.vector_variables)) if isempty(o.vector_variables.variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing - o.vector_affine = filter_variables(keep, _get_vector_affine(o)) + o.vector_affine = filter_variables(keep, _not_nothing(o.vector_affine)) elseif o.vector_quadratic !== nothing - o.vector_quadratic = filter_variables(keep, _get_vector_quadratic(o)) + o.vector_quadratic = + filter_variables(keep, _not_nothing(o.vector_quadratic)) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed(