Skip to content

Commit

Permalink
small fixes, cover the constraint substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
exaexa committed Dec 8, 2023
1 parent 2406c04 commit 008697a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/linear_value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ end
"""
$(TYPEDSIGNATURES)
Substitute anything vector-like as variable values into a [`LinearValue`](@ref) and
return the result.
Substitute anything vector-like as variable values into a [`LinearValue`](@ref)
and return the result.
"""
substitute(x::LinearValue, y) =
sum(idx == 0 ? x.weights[i] : x.weights[i] * y[idx] for (i, idx) in enumerate(x.idxs))

"""
$(TYPEDSIGNATURES)
Shortcut for making a [`LinearValue`](@ref) out of a linear combination defined by
the `SparseVector`.
Shortcut for making a [`LinearValue`](@ref) out of a linear combination defined
by the `SparseVector`.
"""
LinearValue(x::SparseVector{Float64}) =
let (idxs, weights) = findnz(x)
Expand Down
9 changes: 8 additions & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ end
@test C.bound(2 * -convert(C.Constraint, (C.variable(bound = 123.0))) / 2) == -123.0

x = C.variable().value
s = :a^C.Constraint(x) + :b^C.Constraint(x * x - x)
s = :a^C.Constraint(x, 5.0) + :b^C.Constraint(x * x - x, (4.0, 6.0))
@test C.value(s.a).idxs == [1]
@test C.value(s.b).idxs == [(0, 2), (2, 2)]
vars = [C.LinearValue([1], [1.0]), C.LinearValue([2], [1.0])]
@test C.substitute(s.a, vars).bound == s.a.bound
@test C.substitute(s.a, vars).value.idxs == s.a.value.idxs
@test C.substitute(s.a, vars).value.weights == s.a.value.weights
@test C.substitute(s.b, vars).bound == s.b.bound
@test C.substitute(s.b, vars).value.idxs == s.b.value.idxs
@test C.substitute(s.b, vars).value.weights == s.b.value.weights
end

@testset "Constraint tree operations" begin
Expand Down

0 comments on commit 008697a

Please sign in to comment.