From b27d3a00fc4212d0fd4b010bfd84b4edac103fce Mon Sep 17 00:00:00 2001 From: Joaquim Garcia Date: Thu, 4 Jul 2024 14:05:19 -0300 Subject: [PATCH 1/3] simplify affine affine and add API for direct usage --- src/MOI_wrapper.jl | 9 ++--- src/parametric_functions.jl | 66 ++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index d2fca5f..9b96413 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -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) @@ -592,22 +591,23 @@ 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( model.optimizer, @@ -630,6 +630,7 @@ 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( model.optimizer, diff --git a/src/parametric_functions.jl b/src/parametric_functions.jl index bc29a4d..d5236d6 100644 --- a/src/parametric_functions.jl +++ b/src/parametric_functions.jl @@ -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} @@ -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}, @@ -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 @@ -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} @@ -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 From 6453eea527b60cc31fd981cbeb4ad24c961f06df Mon Sep 17 00:00:00 2001 From: Joaquim Garcia Date: Thu, 4 Jul 2024 14:41:20 -0300 Subject: [PATCH 2/3] simplify add constraint --- src/MOI_wrapper.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index 9b96413..e13a286 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -609,7 +609,7 @@ function MOI.add_constraint( ) 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), @@ -632,7 +632,7 @@ function _add_vi_constraint( ) 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), @@ -708,7 +708,7 @@ 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, From 4297b489fe306cf184bce4ace8f212172eacea50 Mon Sep 17 00:00:00 2001 From: Joaquim Garcia Date: Thu, 4 Jul 2024 21:20:26 -0300 Subject: [PATCH 3/3] fix format --- src/MOI_wrapper.jl | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index e13a286..797f4a7 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -710,11 +710,8 @@ function _add_constraint_with_parameters_on_function( func = _current_function(pf) 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, @@ -723,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,