Skip to content
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

Implement value for GenericQuadExpr. #1588

Merged
merged 5 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion src/quad_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,20 @@ function Base.copy(q::GenericQuadExpr, new_model::Model)
copy(q.qcoeffs), copy(q.aff, new_model))
end

# TODO: result_value for QuadExpr
# Requires that foo(::GenericAffExpr) is defined.
function value(ex::GenericQuadExpr{CoefType, VarType},
foo::Function) where {CoefType, VarType}
# The return type of appling map to ::VarType.
MapVarType = Base.promote_op(foo, VarType)
# Later, we're going to multiply two MapVarType together
MapVarType2 = Base.promote_op(*, MapVarType, MapVarType)
# We're also going to multiply a constant with ::MapVarType2
RetType = Base.promote_op(*, CoefType, MapVarType2)
Copy link
Member

Choose a reason for hiding this comment

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

I was once told that it is not safe to call promote_op on top of promote_op, see JuliaLang/julia#18218 (comment).
So maybe something like RetType = Base.promote_op((c, v) -> c * foo(v) * foo(v), CoefType, VarType) is better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I never understood the argument so I am not surprised to see that I am not following it :-P
In fact, it appears that using promote_op is just bad: JuliaLang/julia#26344 (comment)
So the best way is just to play with typeof, zero, one and oneunit. To choose whether to use one or oneunit you need to think what would happen if the type was frim Unitful. For choosing between zero and one, you have some freedom but you need to avoid dividing by zero.
Maybe the best way here is doing typeof(oneunit(CoefType) * oneunit(MapVarType) * oneunit(MapVarType)). We are kind of forced to use promote_op to get MapVarType since one ... would on JuMP.VariableRef would not really do what we want.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we have to use promote_op here because the variables get passed through foo...

Alternatively, we just avoid the value result_value misdirection and write a result_value function knowing that it will get a Float64 back...

ret = convert(RetType, foo(ex.aff))
odow marked this conversation as resolved.
Show resolved Hide resolved
for (vars, coef) in ex.terms
ret += coef * foo(vars.a) * foo(vars.b)
end
return ret
end

JuMP.result_value(ex::JuMP.GenericQuadExpr) = value(ex, JuMP.result_value)
13 changes: 13 additions & 0 deletions test/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ function expressions_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType
@test @inferred(JuMP.value(expr2, i -> 1.0)) == 2.0
end

@testset "value for GenericQuadExpr" begin
# 1 + 2x(1) + 3x(2)
affine_term = JuMP.GenericAffExpr(1.0, 1 => 2.0, 2 => 3.0)
# 1 + 2x(1) + 3x(2) + 4x(1)^2 + 5x(1)*x(2) + 6x(2)^2
expr = JuMP.GenericQuadExpr(affine_term,
JuMP.UnorderedPair(1, 1) => 4.0,
JuMP.UnorderedPair(1, 2) => 5.0,
JuMP.UnorderedPair(2, 2) => 6.0)
@test typeof(@inferred(JuMP.value(expr, i -> 1.0))) == Float64
@test @inferred(JuMP.value(expr, i -> 1.0)) == 16
@test @inferred(JuMP.value(expr, i -> 2.0)) == 62
end

@testset "add_to_expression!(::GenericAffExpr{C,V}, ::V)" begin
aff = JuMP.GenericAffExpr(1.0, :a => 2.0)
@test JuMP.isequal_canonical(JuMP.add_to_expression!(aff, :b),
Expand Down
2 changes: 2 additions & 0 deletions test/generate_and_solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@
@test JuMP.result_dual(c1) == -1.0
@test JuMP.result_dual(c2) == 2.0
@test JuMP.result_dual(c3) == 3.0

@test JuMP.result_value(2 * x + 3 * y * x) == 2.0
end

@testset "SOC" begin
Expand Down