@@ -618,38 +618,55 @@ function test_models_equal(model1::MOI.ModelLike, model2::MOI.ModelLike, variabl
618618end
619619
620620
621- _hasvar (v:: VI , vi:: Vector{VI} ) = v in vi
622- _hasvar (v:: VI , vi:: VI ) = v == vi
623- _hasvar (t:: MOI.ScalarAffineTerm , vi) = _hasvar (t. variable_index, vi)
624- _hasvar (t:: MOI.ScalarQuadraticTerm , vi) = _hasvar (t. variable_index_1, vi) || _hasvar (t. variable_index_2, vi)
625- _hasvar (t:: Union{MOI.VectorAffineTerm, MOI.VectorQuadraticTerm} , vi) = _hasvar (t. scalar_term, vi)
621+ _keep_all (keep:: Function , v:: MOI.VariableIndex ) = keep (v)
622+ _keep_all (keep:: Function , t:: MOI.ScalarAffineTerm ) = keep (t. variable_index)
623+ _keep_all (keep:: Function , t:: MOI.ScalarQuadraticTerm ) = keep (t. variable_index_1) && keep (t. variable_index_2)
624+ _keep_all (keep:: Function , t:: Union{MOI.VectorAffineTerm, MOI.VectorQuadraticTerm} ) = _keep_all (keep, t. scalar_term)
626625# Removes terms or variables in `vis_or_terms` that contains the variable of index `vi`
627- function _rmvar (vis_or_terms :: Vector , vi )
628- return vis_or_terms[. ! _hasvar .(vis_or_terms, Ref (vi))]
626+ function _filter_variables (keep :: Function , variables_or_terms :: Vector )
627+ return filter (el -> _keep_all (keep, el), variables_or_terms)
629628end
630629
631630"""
632- remove_variable(f::AbstractFunction, vi::VariableIndex )
631+ filter_variables(keep::Function, f::AbstractFunction )
633632
634- Return a new function `f` with the variable vi removed.
633+ Return a new function `f` with the variable `vi` such that `!keep(vi)` removed.
635634"""
636- function remove_variable end
637- function remove_variable (f :: MOI.SingleVariable , vi :: MOI.VariableIndex )
638- if f. variable == vi
635+ function filter_variables end
636+ function filter_variables (keep :: Function , f :: MOI.SingleVariable )
637+ if ! keep ( f. variable)
639638 error (" Cannot remove variable from a `SingleVariable` function of the" ,
640639 " same variable." )
641640 end
642641 return f
643642end
644- function remove_variable (f:: VVF , vi)
645- VVF (_rmvar (f. variables, vi))
643+ function filter_variables (keep:: Function , f:: MOI.VectorOfVariables )
644+ return MOI. VectorOfVariables (_filter_variables (keep, f. variables))
645+ end
646+ function filter_variables (
647+ keep:: Function , f:: Union{MOI.ScalarAffineFunction, MOI.VectorAffineFunction} )
648+ return typeof (f)(_filter_variables (keep, f. terms), MOI. constant (f))
646649end
647- function remove_variable (f:: Union{SAF, VAF} , vi)
648- typeof (f)(_rmvar (f. terms, vi), MOI. constant (f))
650+ function filter_variables (
651+ keep, f:: Union{MOI.ScalarQuadraticFunction, MOI.VectorQuadraticFunction} )
652+ return typeof (f)(
653+ _filter_variables (keep, f. affine_terms),
654+ _filter_variables (keep, f. quadratic_terms),
655+ MOI. constant (f))
656+ end
657+
658+ """
659+ remove_variable(f::AbstractFunction, vi::VariableIndex)
660+
661+ Return a new function `f` with the variable vi removed.
662+ """
663+ function remove_variable (f:: MOI.AbstractFunction , vi:: MOI.VariableIndex )
664+ return filter_variables (v -> v != vi, f)
649665end
650- function remove_variable (f:: Union{SQF, VQF} , vi)
651- terms = _rmvar .((f. affine_terms, f. quadratic_terms), Ref (vi))
652- typeof (f)(terms... , MOI. constant (f))
666+ function remove_variable (f:: MOI.AbstractFunction , vis:: Vector{MOI.VariableIndex} )
667+ # Create a `Set` to test membership in `vis` in O(1).
668+ set = Set (vis)
669+ return filter_variables (vi -> ! (vi in set), f)
653670end
654671
655672"""
@@ -671,7 +688,7 @@ function modify_function(f::VQF, change::MOI.VectorConstantChange)
671688end
672689function _modifycoefficient (terms:: Vector{<:MOI.ScalarAffineTerm} , variable:: VI , new_coefficient)
673690 terms = copy (terms)
674- i = something (findfirst (t -> _hasvar (t, variable) , terms), 0 )
691+ i = something (findfirst (t -> t . variable_index == variable, terms), 0 )
675692 if iszero (i)
676693 # The variable was not already in the function
677694 if ! iszero (new_coefficient)
@@ -701,7 +718,7 @@ function _modifycoefficients(n, terms::Vector{<:MOI.VectorAffineTerm}, variable:
701718 rowmap = Dict (c[1 ]=> i for (i,c) in enumerate (new_coefficients))
702719 del = Int[]
703720 for i in 1 : length (terms)
704- if _hasvar ( terms[i], variable)
721+ if terms[i]. scalar_term . variable_index == variable
705722 row = terms[i]. output_index
706723 j = Base. get (rowmap, row, 0 )
707724 if ! iszero (j) # If it is zero, it means that the row should not be changed
0 commit comments