I have a vector a that can contain several different types of elements of the expressions QuadExpr, NonlinearExpression, AffExpr, VariableRef. However, I want that if one of the elements (expressions) in a is a NonlinearExpression or a QuadExpression this function should be used:
function add_pen!(pen, model::JuMP.Model, a::Union{Vector{QuadExpr}, Vector{NonlinearExpression}}, b, i,len)
pen[i] = @NLexpression(model, sum(a[j] * b[j] for j in 1:len));
end
If a contains neither NonlinearExpression nor QuadExpression this function should be used:
function add_pen!(pen, model::JuMP.Model, a, b, i,len)
pen[i] = @expression(model, sum(a[j] * b[j] for j in 1:len));
end
I know this will not work as a::Union{Vector{QuadExpr}, Vector{NonlinearExpression}} means that a is either a Vector of only QuadExpression or NonlinearExpressions. Is it possible to use multiple-dispatch functions in this case or do I need to use the if and else statements instead? Like,
if any(x -> typeof(x) == NonlinearExpression, a)
pen[i] = @NLexpression ...
end
You could dispatch on types of
varargs.I will make the example code without
JuMPso it is easier to replicate (you can of course change to your JuMP types).Consider the following two types:
You can define the following functions:
Now let us test it:
You can see that
varagsmakes it possible to dispatch on the various combination ofVectorelements. Note that this works correctly regardless the fact the the type of elements ofVectorabove isAny:In your
add_pen!you will need to moveato the end to be the last parameter.