Skip to content

Commit 37dee37

Browse files
authored
Merge pull request #472 from JuliaOpt/bl/broadcastable
Make models broadcastable without Ref
2 parents 2208394 + c654d3f commit 37dee37

File tree

6 files changed

+11
-6
lines changed

6 files changed

+11
-6
lines changed

src/Bridges/detbridge.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function LogDetBridge{T}(model, f::MOI.VectorAffineFunction{T}, s::MOI.LogDetCon
8080
d = s.side_dimension
8181
t, D, Δ, sdindex = extract_eigenvalues(model, f, d)
8282
l = MOI.addvariables!(model, d)
83-
lcindex = sublog.(Ref(model), l, D, T)
83+
lcindex = sublog.(model, l, D, T)
8484
tlindex = subsum(model, t, l, T)
8585

8686
LogDetBridge(Δ, l, sdindex, lcindex, tlindex)

src/MathOptInterface.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Abstract supertype for objects that implement the "Model" interface for defining
88
an optimization problem.
99
"""
1010
abstract type ModelLike end
11+
@static if VERSION >= v"0.7-"
12+
# This allows to use `ModelLike`s in broadcast calls without the need to
13+
# embed it in a `Ref`
14+
Base.broadcastable(model::ModelLike) = Ref(model)
15+
end
1116

1217
"""
1318
AbstractOptimizer

src/Test/UnitTests/basic_constraint_tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function basic_constraint_test_helper(model::MOI.ModelLike, config::TestConfig,
221221

222222
@testset "isvalid" begin
223223
c_indices = MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
224-
@test all(MOI.isvalid.(Ref(model), c_indices))
224+
@test all(MOI.isvalid.(model, c_indices))
225225
end
226226

227227
if delete

src/Utilities/parser.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ end
196196

197197
# Used for Vector{Symbol}, Vector{ParsedScalarAffineTerm}, Vector{ParsedVectorAffineTerm},
198198
# Vector{ParsedScalarQuadraticTerm} and Vector{ParsedVectorQuadraticTerm}
199-
parsedtoMOI(model, s::Vector) = parsedtoMOI.(Ref(model), s)
199+
parsedtoMOI(model, s::Vector) = parsedtoMOI.(model, s)
200200

201201
parsedtoMOI(model, s::Union{Float64, Int64}) = s
202202

src/attributes.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function get end
157157
# We want to avoid being too specific in the type arguments to avoid method ambiguity.
158158
# For model, get(::ModelLike, ::AbstractVariableAttribute, ::Vector{VariableIndex}) would not allow
159159
# to define get(::SomeModel, ::AnyAttribute, ::Vector)
160-
get(model::ModelLike, attr::AnyAttribute, idxs::Vector) = get.(Ref(model), Ref(attr), idxs)
160+
get(model::ModelLike, attr::AnyAttribute, idxs::Vector) = get.(model, Ref(attr), idxs)
161161

162162
function get(model::ModelLike, attr::AnyAttribute, args...)
163163
throw(ArgumentError("ModelLike of type $(typeof(model)) does not support accessing the attribute $attr"))
@@ -284,7 +284,7 @@ set!(model, ConstraintFunction(), c, SingleVariable(v1)) # Error
284284
"""
285285
function set! end
286286
# See note with get
287-
set!(model::ModelLike, attr::Union{AbstractVariableAttribute, AbstractConstraintAttribute}, idxs::Vector, vector_of_values::Vector) = set!.(Ref(model), Ref(attr), idxs, vector_of_values)
287+
set!(model::ModelLike, attr::Union{AbstractVariableAttribute, AbstractConstraintAttribute}, idxs::Vector, vector_of_values::Vector) = set!.(model, Ref(attr), idxs, vector_of_values)
288288

289289
function set!(model::ModelLike, attr::AnyAttribute, args...)
290290
set!_fallback_error(model, attr, args...)

src/constraints.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ This call is equivalent to `addconstraint!.(model, funcs, sets)` but may be more
7575
function addconstraints! end
7676

7777
# default fallback
78-
addconstraints!(model::ModelLike, funcs, sets) = addconstraint!.(Ref(model), funcs, sets)
78+
addconstraints!(model::ModelLike, funcs, sets) = addconstraint!.(model, funcs, sets)
7979

8080
"""
8181
## Transform Constraint Set

0 commit comments

Comments
 (0)