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

Simplify parametric scalar affine function #155

Merged
merged 3 commits into from
Aug 6, 2024
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
29 changes: 12 additions & 17 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,6 @@ function _add_constraint_with_parameters_on_function(
set::S,
) where {T,S}
pf = ParametricAffineFunction(f)
_cache_set_constant!(pf, set)
if model.constraints_interpretation == ONLY_BOUNDS
if length(pf.v) == 1 && isone(MOI.coefficient(pf.v[]))
poi_ci = _add_vi_constraint(model, pf, set)
Expand All @@ -592,24 +591,25 @@ function _add_constraint_with_parameters_on_function(
)
end
elseif model.constraints_interpretation == ONLY_CONSTRAINTS
poi_ci = _add_saf_constraint(model, pf, set)
poi_ci = MOI.add_constraint(model, pf, set)
elseif model.constraints_interpretation == BOUNDS_AND_CONSTRAINTS
if length(pf.v) == 1 && isone(MOI.coefficient(pf.v[]))
poi_ci = _add_vi_constraint(model, pf, set)
else
poi_ci = _add_saf_constraint(model, pf, set)
poi_ci = MOI.add_constraint(model, pf, set)
end
end
return poi_ci
end

function _add_saf_constraint(
function MOI.add_constraint(
model::Optimizer,
pf::ParametricAffineFunction{T},
set::S,
) where {T,S}
_cache_set_constant!(pf, set)
_update_cache!(pf, model)
inner_ci = MOI.Utilities.normalize_and_add_constraint(
inner_ci = MOI.add_constraint(
model.optimizer,
MOI.ScalarAffineFunction{T}(pf.v, 0.0),
_set_with_new_constant(set, pf.current_constant),
Expand All @@ -630,8 +630,9 @@ function _add_vi_constraint(
pf::ParametricAffineFunction{T},
set::S,
) where {T,S}
_cache_set_constant!(pf, set)
_update_cache!(pf, model)
inner_ci = MOI.Utilities.normalize_and_add_constraint(
inner_ci = MOI.add_constraint(
model.optimizer,
pf.v[].variable,
_set_with_new_constant(set, pf.current_constant),
Expand Down Expand Up @@ -707,13 +708,10 @@ function _add_constraint_with_parameters_on_function(
_update_cache!(pf, model)

func = _current_function(pf)
f_quad = if !_is_affine(func)
if !_is_affine(func)
fq = func
inner_ci = MOI.Utilities.normalize_and_add_constraint(
model.optimizer,
fq,
s,
)
inner_ci =
MOI.Utilities.normalize_and_add_constraint(model.optimizer, fq, s)
model.last_quad_add_added += 1
outer_ci = MOI.ConstraintIndex{MOI.ScalarQuadraticFunction{T},S}(
model.last_quad_add_added,
Expand All @@ -722,11 +720,8 @@ function _add_constraint_with_parameters_on_function(
model.constraint_outer_to_inner[outer_ci] = inner_ci
else
fa = MOI.ScalarAffineFunction(func.affine_terms, func.constant)
inner_ci = MOI.Utilities.normalize_and_add_constraint(
model.optimizer,
fa,
s,
)
inner_ci =
MOI.Utilities.normalize_and_add_constraint(model.optimizer, fa, s)
model.last_quad_add_added += 1
outer_ci = MOI.ConstraintIndex{MOI.ScalarQuadraticFunction{T},S}(
model.last_quad_add_added,
Expand Down
66 changes: 33 additions & 33 deletions src/parametric_functions.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
abstract type ParametricFunction{T} end

mutable struct ParametricQuadraticFunction{T}
function _cache_set_constant!(
f::ParametricFunction{T},
s::Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}},
) where {T}
f.set_constant = MOI.constant(s)
return
end

function _cache_set_constant!(
::ParametricFunction{T},
::MOI.AbstractScalarSet,
) where {T}
return
end

mutable struct ParametricQuadraticFunction{T} <: ParametricFunction{T}
# helper to efficiently update affine terms
affine_data::Dict{MOI.VariableIndex,T}
affine_data_np::Dict{MOI.VariableIndex,T}
Expand Down Expand Up @@ -147,21 +163,6 @@ function _current_function(f::ParametricQuadraticFunction{T}) where {T}
return MOI.ScalarQuadraticFunction{T}(f.vv, affine, f.current_constant)
end

function _cache_set_constant!(
f::ParametricQuadraticFunction{T},
s::Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}},
) where {T}
f.set_constant = MOI.constant(s)
return
end

function _cache_set_constant!(
::ParametricQuadraticFunction{T},
::MOI.AbstractScalarSet,
) where {T}
return
end

function _parametric_constant(
model,
f::ParametricQuadraticFunction{T},
Expand Down Expand Up @@ -263,7 +264,7 @@ function _update_cache!(f::ParametricQuadraticFunction{T}, model) where {T}
return nothing
end

mutable struct ParametricAffineFunction{T}
mutable struct ParametricAffineFunction{T} <: ParametricFunction{T}
# constant * parameter
p::Vector{MOI.ScalarAffineTerm{T}}
# constant * variable
Expand All @@ -278,7 +279,21 @@ end

function ParametricAffineFunction(f::MOI.ScalarAffineFunction{T}) where {T}
v, p = _split_affine_terms(f.terms)
return ParametricAffineFunction{T}(p, v, f.constant, zero(T), zero(T))
return ParametricAffineFunction(p, v, f.constant)
end

function ParametricAffineFunction(
terms_p::Vector{MOI.ScalarAffineTerm{T}},
terms_v::Vector{MOI.ScalarAffineTerm{T}},
constant::T,
) where {T}
return ParametricAffineFunction{T}(
terms_p,
terms_v,
constant,
zero(T),
zero(T),
)
end

function _split_affine_terms(terms::Vector{MOI.ScalarAffineTerm{T}}) where {T}
Expand Down Expand Up @@ -322,21 +337,6 @@ function _current_function(f::ParametricAffineFunction{T}) where {T}
return MOI.ScalarAffineFunction{T}(f.v, f.current_constant)
end

function _cache_set_constant!(
f::ParametricAffineFunction{T},
s::Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}},
) where {T}
f.set_constant = MOI.constant(s)
return
end

function _cache_set_constant!(
f::ParametricAffineFunction{T},
s::MOI.AbstractScalarSet,
) where {T}
return
end

function _parametric_constant(model, f::ParametricAffineFunction{T}) where {T}
# do not add set_function here
param_constant = f.c
Expand Down
Loading