Skip to content

Commit 55af1ee

Browse files
authored
Style improvements to aff_expr.jl (#2749)
1 parent ce402d6 commit 55af1ee

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/aff_expr.jl

+41-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# Operator overloads in src/operators.jl
1616
#############################################################################
1717

18-
# Utilities for OrderedDict
1918
function _add_or_set!(dict::OrderedDict{K,V}, k::K, v::V) where {K,V}
2019
# Adding zero terms to this dictionary leads to unacceptable performance
2120
# degradations. See, e.g., https://github.com/jump-dev/JuMP.jl/issues/1946.
@@ -52,11 +51,13 @@ function _new_ordered_dict(
5251
end
5352
return dict
5453
end
54+
5555
# Shortcut for one and two arguments to avoid creating an empty dict and add
5656
# elements one by one with `JuMP._add_or_set!`
5757
function _new_ordered_dict(::Type{K}, ::Type{V}, kv::Pair) where {K,V}
5858
return OrderedDict{K,V}(kv)
5959
end
60+
6061
function _new_ordered_dict(
6162
::Type{K},
6263
::Type{V},
@@ -77,6 +78,7 @@ function _build_aff_expr(constant::V, coef::V, var::K) where {V,K}
7778
terms[var] = coef
7879
return GenericAffExpr{V,K}(constant, terms)
7980
end
81+
8082
function _build_aff_expr(
8183
constant::V,
8284
coef1::V,
@@ -171,15 +173,21 @@ end
171173
function Base.iszero(expr::GenericAffExpr)
172174
return iszero(expr.constant) && all(iszero, values(expr.terms))
173175
end
176+
174177
function Base.zero(::Type{GenericAffExpr{C,V}}) where {C,V}
175178
return GenericAffExpr{C,V}(zero(C), OrderedDict{V,C}())
176179
end
180+
181+
Base.zero(a::GenericAffExpr) = zero(typeof(a))
182+
177183
function Base.one(::Type{GenericAffExpr{C,V}}) where {C,V}
178184
return GenericAffExpr{C,V}(one(C), OrderedDict{V,C}())
179185
end
180-
Base.zero(a::GenericAffExpr) = zero(typeof(a))
186+
181187
Base.one(a::GenericAffExpr) = one(typeof(a))
188+
182189
Base.copy(a::GenericAffExpr) = GenericAffExpr(copy(a.constant), copy(a.terms))
190+
183191
Base.broadcastable(a::GenericAffExpr) = Ref(a)
184192

185193
# Needed for cases when Julia uses `x == 0` instead of `iszero(x)` (e.g., in the
@@ -192,7 +200,7 @@ Base.:(==)(x::GenericAffExpr, y::Number) = isempty(x.terms) && x.constant == y
192200
Return the coefficient associated with variable `v` in the affine expression `a`.
193201
"""
194202
coefficient(a::GenericAffExpr{C,V}, v::V) where {C,V} = get(a.terms, v, zero(C))
195-
coefficient(a::GenericAffExpr{C,V}, v1::V, v2::V) where {C,V} = zero(C)
203+
coefficient(::GenericAffExpr{C,V}, ::V, ::V) where {C,V} = zero(C)
196204

197205
"""
198206
drop_zeros!(expr::GenericAffExpr)
@@ -312,24 +320,27 @@ linear part of the affine expression.
312320
linear_terms(aff::GenericAffExpr) = LinearTermIterator(aff)
313321

314322
_reverse_pair_to_tuple(p::Pair) = (p.second, p.first)
323+
315324
function Base.iterate(lti::LinearTermIterator)
316325
ret = iterate(lti.aff.terms)
317326
if ret === nothing
318-
return nothing
327+
return
319328
else
320329
return _reverse_pair_to_tuple(ret[1]), ret[2]
321330
end
322331
end
332+
323333
function Base.iterate(lti::LinearTermIterator, state)
324334
ret = iterate(lti.aff.terms, state)
325335
if ret === nothing
326-
return nothing
336+
return
327337
else
328338
return _reverse_pair_to_tuple(ret[1]), ret[2]
329339
end
330340
end
331341
Base.length(lti::LinearTermIterator) = length(lti.aff.terms)
332-
function Base.eltype(lti::LinearTermIterator{GenericAffExpr{C,V}}) where {C,V}
342+
343+
function Base.eltype(::LinearTermIterator{GenericAffExpr{C,V}}) where {C,V}
333344
return Tuple{C,V}
334345
end
335346

@@ -459,6 +470,7 @@ end
459470
function Base.convert(::Type{GenericAffExpr{T,V}}, v::V) where {T,V}
460471
return GenericAffExpr(zero(T), v => one(T))
461472
end
473+
462474
function Base.convert(::Type{GenericAffExpr{T,V}}, v::_Constant) where {T,V}
463475
return GenericAffExpr{T,V}(convert(T, _constant_to_number(v)))
464476
end
@@ -474,14 +486,17 @@ const AffExpr = GenericAffExpr{Float64,VariableRef}
474486
# Check all coefficients are finite, i.e. not NaN, not Inf, not -Inf
475487
function _assert_isfinite(a::AffExpr)
476488
for (coef, var) in linear_terms(a)
477-
isfinite(coef) || error("Invalid coefficient $coef on variable $var.")
489+
if !isfinite(coef)
490+
error("Invalid coefficient $coef on variable $var.")
491+
end
478492
end
479493
if isnan(a.constant)
480494
error(
481495
"Expression contains an invalid NaN constant. This could be " *
482496
"produced by `Inf - Inf`.",
483497
)
484498
end
499+
return
485500
end
486501

487502
"""
@@ -490,8 +505,6 @@ end
490505
Return the value of the `GenericAffExpr` `v` associated with result index
491506
`result` of the most-recent solution returned by the solver.
492507
493-
Replaces `getvalue` for most use cases.
494-
495508
See also: [`result_count`](@ref).
496509
"""
497510
function value(a::GenericAffExpr; result::Int = 1)
@@ -504,6 +517,7 @@ function check_belongs_to_model(a::GenericAffExpr, model::AbstractModel)
504517
for variable in keys(a.terms)
505518
check_belongs_to_model(variable, model)
506519
end
520+
return
507521
end
508522

509523
# Note: No validation is performed that the variables in the AffExpr belong to
@@ -554,42 +568,50 @@ See also: [`moi_function_type`](@ref).
554568
function jump_function_type end
555569

556570
moi_function(a::GenericAffExpr) = MOI.ScalarAffineFunction(a)
571+
557572
function moi_function_type(::Type{<:GenericAffExpr{T}}) where {T}
558573
return MOI.ScalarAffineFunction{T}
559574
end
560575

561576
function AffExpr(m::Model, f::MOI.ScalarAffineFunction)
562-
aff = AffExpr()
577+
aff = AffExpr(f.constant)
563578
for t in f.terms
564579
add_to_expression!(aff, t.coefficient, VariableRef(m, t.variable))
565580
end
566-
aff.constant = f.constant
567581
return aff
568582
end
583+
569584
function jump_function_type(
570585
::Model,
571586
::Type{MOI.ScalarAffineFunction{T}},
572587
) where {T}
573588
return GenericAffExpr{T,VariableRef}
574589
end
590+
575591
function jump_function(model::Model, f::MOI.ScalarAffineFunction{T}) where {T}
576592
return GenericAffExpr{T,VariableRef}(model, f)
577593
end
594+
578595
function jump_function_type(
579596
::Model,
580597
::Type{MOI.VectorAffineFunction{T}},
581598
) where {T}
582599
return Vector{GenericAffExpr{T,VariableRef}}
583600
end
601+
584602
function jump_function(model::Model, f::MOI.VectorAffineFunction{T}) where {T}
585603
return GenericAffExpr{T,VariableRef}[
586604
GenericAffExpr{T,VariableRef}(model, f) for f in MOIU.eachscalar(f)
587605
]
588606
end
589607

590608
"""
591-
_fill_vaf!(terms::Vector{<:MOI.VectorAffineTerm}, offset::Int, oi::Int,
592-
aff::AbstractJuMPScalar)
609+
_fill_vaf!(
610+
terms::Vector{<:MOI.VectorAffineTerm},
611+
offset::Int,
612+
oi::Int,
613+
aff::AbstractJuMPScalar,
614+
)
593615
594616
Fills the vectors terms at indices starting at `offset+1` with the affine terms
595617
of `aff`. The output index for all terms is `oi`. Return the index of the last
@@ -613,7 +635,10 @@ function _fill_vaf!(
613635
end
614636

615637
function MOI.VectorAffineFunction(affs::Vector{AffExpr})
616-
len = sum(aff -> length(linear_terms(aff)), affs)
638+
len = 0
639+
for aff in affs
640+
len += length(linear_terms(aff))
641+
end
617642
terms = Vector{MOI.VectorAffineTerm{Float64}}(undef, len)
618643
constant = Vector{Float64}(undef, length(affs))
619644
offset = 0
@@ -623,11 +648,9 @@ function MOI.VectorAffineFunction(affs::Vector{AffExpr})
623648
end
624649
return MOI.VectorAffineFunction(terms, constant)
625650
end
651+
626652
moi_function(a::Vector{<:GenericAffExpr}) = MOI.VectorAffineFunction(a)
653+
627654
function moi_function_type(::Type{<:Vector{<:GenericAffExpr{T}}}) where {T}
628655
return MOI.VectorAffineFunction{T}
629656
end
630-
631-
# TODO: Find somewhere to put this error message.
632-
#add_constraint(m::Model, c::Array{AffExprConstraint}) =
633-
# error("The operators <=, >=, and == can only be used to specify scalar constraints. If you are trying to add a vectorized constraint, use the element-wise dot comparison operators (.<=, .>=, or .==) instead")

0 commit comments

Comments
 (0)