# Problem in my object function because of NaN in my vector

**URL:** https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [June 21, 2023, 5:44pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670 "2023-06-21T17:44:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![lara](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@lara](https://discourse.julialang.org/u/lara)
#### Post date: [June 21, 2023, 5:44pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/1 "2023-06-21T17:44:24Z")

</div>

Hi, all!

I have this code, already working great.  
My objective function is  
@NLobjective(model, Min, sum(total\_price[i] for i in 1:21))

When I add a new constraint, this total\_price vector assumes some NaN values and then the otimization return this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/0/200a11909d87be56ba8fb41db89732bcea0f1f2e.png)

It atributes NaN to all my variables because can’t sum the values with NaN.

There is a way to substitute these NaN, inside JuMP, for zero so the sum works?

Thank you a lot for your help!!

---

<div class="post-metadata">

### Author: ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)
#### Post date: [June 21, 2023, 6:23pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/2 "2023-06-21T18:23:09Z")

</div>

Do you know where the NaNs come from? Maybe we should avoid this situation altogether.

---

<div class="post-metadata">

### Author: ![lara](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@lara](https://discourse.julialang.org/u/lara)
#### Post date: [June 21, 2023, 6:47pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/3 "2023-06-21T18:47:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lara](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@lara](https://discourse.julialang.org/u/lara)
#### Post date: [June 21, 2023, 6:51pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/4 "2023-06-21T18:51:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [June 21, 2023, 7:33pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/5 "2023-06-21T19:33:27Z")

</div>

Why does price become NaN?

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?

---

<div class="post-metadata">

### Author: ![lara](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@lara](https://discourse.julialang.org/u/lara)
#### Post date: [June 21, 2023, 8:01pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/6 "2023-06-21T20:01:53Z")

</div>

Ow, okay. I see.

I was trying to do an example smaller and I identify that error.  
The error is that I use some equations like that:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/d/cd042f460c60887f32b39f9e20cee33217885bc0.png)  
DELTA is my variable. When DELTA==0, the denominator become zero.

Trying to write an example closer to my code, it would be something like

> using DataFrames, XLSX, JuMP, Ipopt, Juniper  
> 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)  
> FLOW = [321,123,34,646,324]  
> DN = [150, 170, 200, 400, 250]  
> ALPHA =   
> for i in 1:5  
> alpha = @NLexpression(model,((FLOW[i]_DELTA[i])/1000)/(3.14159265359_((DN[i]\*DELTA[i])/2000)^2))  
> push!(ALPHA, alpha)  
> 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)

If run just the base code, without the JuMP arguments, it appears the NaN.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/c/4ccaaef6bd3d306b9e734827fd5c77302258042a.png)

Do you know a way I can fix that? I cannot change the equations.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [June 21, 2023, 8:26pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/7 "2023-06-21T20:26:27Z")

</div>

> [@lara](#):
>
> Do you know a way I can fix that? I cannot change the equations.

What we are saying is that you _must_ change the equations. Your problem is not defined otherwise.

Why do you need `DELTA[i]` in the denominator? Isn’t it equivalent just to have `DELTA[i]` in the numerator only?

---

<div class="post-metadata">

### Author: ![lara](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@lara](https://discourse.julialang.org/u/lara)
#### Post date: [June 21, 2023, 10:21pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/8 "2023-06-21T22:21:08Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [June 21, 2023, 10:32pm UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/9 "2023-06-21T22:32:41Z")

</div>

Can you provide the larger equation? Someone might have suggestions.

---

<div class="post-metadata">

### Author: ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)
#### Post date: [June 22, 2023, 7:19am UTC](https://discourse.julialang.org/t/problem-in-my-object-function-because-of-nan-in-my-vector/100670/10 "2023-06-22T07:19:12Z")

</div>

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:

```julia
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.
