From fd36aabcc53c6367ba1fbab83ea0269e292edbcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 22 Feb 2021 23:29:11 +0100 Subject: [PATCH] Simplify --- src/Utilities/model.jl | 54 +++++++++++++------------- src/Utilities/vector_of_constraints.jl | 8 ++-- test/Utilities/model.jl | 4 +- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Utilities/model.jl b/src/Utilities/model.jl index 75da399640..684bd4bd91 100644 --- a/src/Utilities/model.jl +++ b/src/Utilities/model.jl @@ -704,9 +704,27 @@ function MOI.is_empty(model::AbstractModel) isempty(model.objective.terms) && iszero(model.objective.constant) && iszero(model.num_variables_created) && - iszero(model.nextconstraintid) + mapreduce_constraints(MOI.is_empty, &, model, true) +end +function MOI.empty!(model::AbstractModel{T}) where {T} + model.name = "" + model.senseset = false + model.sense = MOI.FEASIBILITY_SENSE + model.objectiveset = false + model.objective = zero(MOI.ScalarAffineFunction{T}) + model.num_variables_created = 0 + model.variable_indices = nothing + model.single_variable_mask = UInt8[] + model.lower_bound = T[] + model.upper_bound = T[] + empty!(model.var_to_name) + model.name_to_var = nothing + empty!(model.con_to_name) + model.name_to_con = nothing + broadcastcall(MOI.empty!, model) end + function MOI.copy_to(dest::AbstractModel, src::MOI.ModelLike; kws...) return automatic_copy_to(dest, src; kws...) end @@ -776,6 +794,8 @@ MOIU.broadcastvcat(_getfuns, model) """ function broadcastvcat end +function mapreduce_constraints end + # Macro to generate Model abstract type Constraints{F} end @@ -828,6 +848,8 @@ _getCV(s::SymbolFun) = :($(s.cname){T,$(_getC(s))}()) _callfield(f, s::SymbolFS) = :($f(model.$(_field(s)))) _broadcastfield(b, s::SymbolFS) = :($b(f, model.$(_field(s)))) +_mapreduce_field(s::SymbolFS) = :(cur = $MOIU.mapreduce_constraints(f, op, model.$(_field(s)), cur)) +_mapreduce_constraints(s::SymbolFS) = :(cur = op(cur, f(model.$(_field(s))))) # This macro is for expert/internal use only. Prefer the concrete Model type # instantiated below. @@ -933,10 +955,8 @@ mutable struct LPModel{T} <: MOIU.AbstractModel{T} var_to_name::Dict{MOI.VariableIndex, String} # If `nothing`, the dictionary hasn't been constructed yet. name_to_var::Union{Dict{String, MOI.VariableIndex}, Nothing} - nextconstraintid::Int64 con_to_name::Dict{MOI.ConstraintIndex, String} name_to_con::Union{Dict{String, MOI.ConstraintIndex}, Nothing} - constrmap::Vector{Int} scalaraffinefunction::LPModelScalarConstraints{T, MOI.ScalarAffineFunction{T}} vectorofvariables::LPModelVectorConstraints{T, MOI.VectorOfVariables} vectoraffinefunction::LPModelVectorConstraints{T, MOI.VectorAffineFunction{T}} @@ -1021,10 +1041,8 @@ macro model( var_to_name::Dict{$VI,String} # If `nothing`, the dictionary hasn't been constructed yet. name_to_var::Union{Dict{String,$VI},Nothing} - nextconstraintid::Int64 con_to_name::Dict{$CI,String} name_to_con::Union{Dict{String,$CI},Nothing} - constrmap::Vector{Int} # Constraint Reference value ci -> index in array in Constraints # A useful dictionary for extensions to store things. These are # _not_ copied between models! ext::Dict{Symbol,Any} @@ -1043,24 +1061,8 @@ macro model( function $MOIU.broadcastvcat(f::Function, model::$esc_model_name) return vcat($(_broadcastfield.(Ref(:(broadcastvcat)), funs)...)) end - function $MOI.empty!(model::$esc_model_name{T}) where {T} - model.name = "" - model.senseset = false - model.sense = $MOI.FEASIBILITY_SENSE - model.objectiveset = false - model.objective = zero($MOI.ScalarAffineFunction{T}) - model.num_variables_created = 0 - model.variable_indices = nothing - model.single_variable_mask = UInt8[] - model.lower_bound = T[] - model.upper_bound = T[] - empty!(model.var_to_name) - model.name_to_var = nothing - model.nextconstraintid = 0 - empty!(model.con_to_name) - model.name_to_con = nothing - empty!(model.constrmap) - return $(Expr(:block, _callfield.(Ref(:($MOI.empty!)), funs)...)) + function $MOIU.mapreduce_constraints(f::Function, op::Function, model::$esc_model_name, cur) + return $(Expr(:block, _mapreduce_field.(funs)...)) end end for (cname, sets) in ((scname, scalar_sets), (vcname, vector_sets)) @@ -1072,8 +1074,8 @@ macro model( function $MOIU.broadcastvcat(f::Function, model::$cname) return vcat($(_callfield.(:f, sets)...)) end - function $MOI.empty!(model::$cname) - return $(Expr(:block, _callfield.(Ref(:($MOI.empty!)), sets)...)) + function $MOIU.mapreduce_constraints(f::Function, op::Function, model::$cname, cur) + return $(Expr(:block, _mapreduce_constraints.(sets)...)) end end end @@ -1135,10 +1137,8 @@ macro model( T[], Dict{$VI,String}(), nothing, - 0, Dict{$CI,String}(), nothing, - Int[], Dict{Symbol,Any}(), $(_getCV.(funs)...), ) diff --git a/src/Utilities/vector_of_constraints.jl b/src/Utilities/vector_of_constraints.jl index ac4554d206..1bae7303e2 100644 --- a/src/Utilities/vector_of_constraints.jl +++ b/src/Utilities/vector_of_constraints.jl @@ -22,9 +22,8 @@ struct VectorOfConstraints{F<:MOI.AbstractFunction,S<:MOI.AbstractSet} <: MOI.Mo end end -function MOI.empty!(v::VectorOfConstraints) - empty!(v.constraints) -end +MOI.is_empty(v::VectorOfConstraints) = isempty(v.constraints) +MOI.empty!(v::VectorOfConstraints) = empty!(v.constraints) function MOI.add_constraint( v::VectorOfConstraints{F,S}, @@ -126,7 +125,8 @@ function _vector_of_variables_with( vi::MOI.VariableIndex, ) rm = MOI.ConstraintIndex{MOI.VectorOfVariables}[] - for (f, s) in values(v.constraints) + for (ci, fs) in v.constraints + f, s = fs if vi in f.variables if length(f.variables) > 1 # If `supports_dimension_update(s)` then the variable will be diff --git a/test/Utilities/model.jl b/test/Utilities/model.jl index 5c0cabfe72..dfd9a173fb 100644 --- a/test/Utilities/model.jl +++ b/test/Utilities/model.jl @@ -220,8 +220,8 @@ end loc1 = MOI.get(model, MOI.ListOfConstraints()) loc2 = Vector{Tuple{DataType, DataType}}() - function _pushloc(constrs::Vector{MOIU.ConstraintEntry{F, S}}) where {F, S} - if !isempty(constrs) + function _pushloc(v::MOI.Utilities.VectorOfConstraints{F, S}) where {F, S} + if !MOI.is_empty(v) push!(loc2, (F, S)) end end