I have binary variables, and I use them to calculate each price.
Total_price is the sum of price[1]+price[2]+price[3].
When the variable is 0, the price[i] returns NaN
I tried to use isnan(x), but it requires the use of conditions and the JuMP doesn’t recognize them.
I was searching for some function/way that substitute NaN for zero without using if’s.
This is almost certainly a modeling error that you should fix. Checking for isnan wont work because it is non differentiable. Can you provide a reproducible example?
I just created this example so you could understand the error. The equation in my code is way more complicated than that.
I’ll try to think a solution to that. Maybe the error is embedded to other point, not exactly that one.
Anyway, thank you a lot for your help!
I think what has been said is that the solver cannot handle NaNs so we need to make sure that it never gets one. It doesn’t necessarily mean that we need to change the equations, but we may have to rewrite them. If the NaN is due to possible division by zero, you can introduce additional variables and rewrite division as multiplication, like num/den=t which means num=t*den. Basically this runs:
using JuMP, Ipopt, Juniper
FLOW = [321,123,34,646,324]
DN = [150, 170, 200, 400, 250]
ALPHA = []
optimizer = Juniper.Optimizer
nl_solver = optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0)
model = Model(optimizer_with_attributes(optimizer, "nl_solver"=>nl_solver))
@variable(model, DELTA[1:5], Bin)
####New here#################
@variable(model, aux[1:5]>=0) ######New variable
@NLexpression(model,num[i=1:5],((FLOW[i]*DELTA[i])/1000)) #####Numerator
@NLexpression(model,den[i=1:5],3.14159265359((DN[i]*DELTA[i])/2000)^2) #####Denominator
@NLconstraint(model,auxCstr[i=1:5],num[i]==aux[i]*den[i]) #####Multiplication instead of division - always feasible
for i in 1:5
push!(ALPHA, aux[i])
end
#############################
@NLconstraint(model, DELTA[2]==0)
@NLconstraint(model, DELTA[5]==0)
@NLobjective(model, Min, sum(ALPHA[i] for i in 1:5))
optim = optimize!(model)
Other sources of NaN will have to be rewritten in a different way, though.