Skip to content
Merged
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
25 changes: 20 additions & 5 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,21 @@ end

eval_variables(value_fn::Function, f::MOI.VariableIndex) = value_fn(f)

function eval_variables(value_fn::Function, f::MOI.ScalarAffineFunction)
function eval_variables(
value_fn::F,
f::MOI.ScalarAffineFunction,
) where {F<:Function}
out = f.constant
for t in f.terms
out += eval_variables(value_fn, t)
end
return out
end

function eval_variables(value_fn::Function, f::MOI.ScalarQuadraticFunction)
function eval_variables(
value_fn::F,
f::MOI.ScalarQuadraticFunction,
) where {F<:Function}
out = f.constant
for a in f.affine_terms
out += eval_variables(value_fn, a)
Expand All @@ -149,19 +155,28 @@ function eval_variables(value_fn::Function, f::MOI.ScalarQuadraticFunction)
return out
end

function eval_variables(value_fn::Function, f::MOI.VectorOfVariables)
function eval_variables(
value_fn::F,
f::MOI.VectorOfVariables,
) where {F<:Function}
return map(value_fn, f.variables)
end

function eval_variables(value_fn::Function, f::MOI.VectorAffineFunction)
function eval_variables(
value_fn::F,
f::MOI.VectorAffineFunction,
) where {F<:Function}
out = copy(f.constants)
for t in f.terms
out[t.output_index] += eval_variables(value_fn, t.scalar_term)
end
return out
end

function eval_variables(value_fn::Function, f::MOI.VectorQuadraticFunction)
function eval_variables(
value_fn::F,
f::MOI.VectorQuadraticFunction,
) where {F<:Function}
out = copy(f.constants)
for t in f.affine_terms
out[t.output_index] += eval_variables(value_fn, t.scalar_term)
Expand Down
15 changes: 15 additions & 0 deletions test/Utilities/test_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ function test_eval_variables()
@test MOI.output_dimension(fvq) == 2
@test MOI.Utilities.eval_variables(vi -> vals[vi], fvq) ≈ [13, 1]
@test MOI.Utilities.eval_variables(vi -> vals[vi], fvq) ≈ [13, 1]
model = MOI.Utilities.Model{Float64}()
value_fn = vi -> vals[vi]
for (func, expected) in (
(z, 5),
(fvv, [3, 5, 1]),
(fsa, 22.0),
(fva, [12.0, 7.0]),
(fsq, 16.0),
(fvq, [13.0, 1.0]),
)
@test (@inferred MOI.Utilities.eval_variables(value_fn, func)) ==
expected
@test (@inferred MOI.Utilities.eval_variables(value_fn, model, func)) ==
expected
end
return
end

Expand Down
Loading