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

WIP: use add_constraints in macros #2748

Closed
wants to merge 3 commits into from
Closed
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
48 changes: 48 additions & 0 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,54 @@ function _moi_add_constraint(
return MOI.add_constraint(model, f, s)
end

"""
add_constraints(
model::Model,
cons::Vector{ScalarConstraint{F,S}},
name::String = "",
) where {F,S}

Add a vector of constraints `cons` to `Model model` and set their names to
`name`.
"""
function add_constraints(
model::Model,
cons::Vector{ScalarConstraint{F,S}},
name::String = "",
) where {F,S}
check_belongs_to_model.(cons, model)
funcs = moi_function.(cons)
sets = moi_set.(cons)
indices = _moi_add_constraints(
backend(model),
funcs,
sets,
name,
)::Vector{MOI.ConstraintIndex{eltype(funcs),S}}
model.is_model_dirty = true
return [ConstraintRef(model, i, ScalarShape()) for i in indices]
end

function _moi_add_constraints(
model::MOI.ModelLike,
funcs::Vector{F},
sets::Vector{S},
name::String = "",
) where {F,S}
indices = MOI.add_constraints(model, funcs, sets)
flag = MOI.supports(model, MOI.ConstraintName(), MOI.ConstraintIndex{F,S})
if !isempty(name) && F != MOI.VariableIndex && flag
for index in indices
MOI.set(model, MOI.ConstraintName(), index, name)
end
end
return indices
end

function add_constraints(model::AbstractModel, cons, name::String = "")
return add_constraint.(model, cons, name)
end

"""
add_constraint(model::Model, con::AbstractConstraint, name::String="")

Expand Down
2 changes: 1 addition & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ function _constraint_macro(
if vectorized
# For vectorized constraints, we set every constraint to have the same
# name.
constraintcall = :(add_constraint.($model, $buildcall, $name_expr))
constraintcall = :(add_constraints($model, $buildcall, $name_expr))
else
constraintcall = :(add_constraint($model, $buildcall, $name_expr))
end
Expand Down