Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Feb 22, 2021
1 parent dac42f8 commit fd36aab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
54 changes: 27 additions & 27 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -776,6 +794,8 @@ MOIU.broadcastvcat(_getfuns, model)
"""
function broadcastvcat end

function mapreduce_constraints end

# Macro to generate Model
abstract type Constraints{F} end

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}}
Expand Down Expand Up @@ -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}
Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -1135,10 +1137,8 @@ macro model(
T[],
Dict{$VI,String}(),
nothing,
0,
Dict{$CI,String}(),
nothing,
Int[],
Dict{Symbol,Any}(),
$(_getCV.(funs)...),
)
Expand Down
8 changes: 4 additions & 4 deletions src/Utilities/vector_of_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fd36aab

Please sign in to comment.