Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Sep 18, 2023
1 parent 470e90f commit ca75339
Showing 1 changed file with 40 additions and 57 deletions.
97 changes: 40 additions & 57 deletions src/Utilities/objective_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand Down Expand Up @@ -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 =

Check warning on line 268 in src/Utilities/objective_container.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/objective_container.jl#L268

Added line #L268 was not covered by tests
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(
Expand All @@ -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 =

Check warning on line 285 in src/Utilities/objective_container.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/objective_container.jl#L285

Added line #L285 was not covered by tests
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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit ca75339

Please sign in to comment.