Skip to content

Broadcast attributes #513

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

Merged
merged 1 commit into from
Aug 28, 2018
Merged
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
15 changes: 13 additions & 2 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ abstract type AbstractConstraintAttribute end

const AnyAttribute = Union{AbstractOptimizerAttribute, AbstractModelAttribute, AbstractVariableAttribute, AbstractConstraintAttribute}

@static if VERSION >= v"0.7-"
# This allows to use attributes in broadcast calls without the need to
# embed it in a `Ref`
Base.broadcastable(attribute::AnyAttribute) = Ref(attribute)
end

"""
struct UnsupportedAttribute{AttrType} <: UnsupportedError
attr::AttrType
Expand Down Expand Up @@ -183,7 +189,7 @@ function get end
# We want to avoid being too specific in the type arguments to avoid method ambiguity.
# For model, get(::ModelLike, ::AbstractVariableAttribute, ::Vector{VariableIndex}) would not allow
# to define get(::SomeModel, ::AnyAttribute, ::Vector)
get(model::ModelLike, attr::AnyAttribute, idxs::Vector) = get.(model, Ref(attr), idxs)
get(model::ModelLike, attr::AnyAttribute, idxs::Vector) = get.(model, attr, idxs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this fallback or should users just broadcast themselves?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some solvers may do something faster, e.g. if they have a fixed cost that can be shared when getting by group


function get(model::ModelLike, attr::AnyAttribute, args...)
throw(ArgumentError("ModelLike of type $(typeof(model)) does not support accessing the attribute $attr"))
Expand Down Expand Up @@ -264,7 +270,12 @@ set(model, ConstraintFunction(), c, SingleVariable(v1)) # Error
"""
function set end
# See note with get
set(model::ModelLike, attr::Union{AbstractVariableAttribute, AbstractConstraintAttribute}, idxs::Vector, vector_of_values::Vector) = set.(model, Ref(attr), idxs, vector_of_values)
function set(model::ModelLike,
attr::Union{AbstractVariableAttribute,
AbstractConstraintAttribute},
idxs::Vector, vector_of_values::Vector)
return set.(model, attr, idxs, vector_of_values)
end

function set(model::ModelLike, attr::AnyAttribute, args...)
set_fallback_error(model, attr, args...)
Expand Down