# Optimization error when using JuMP

**URL:** https://discourse.julialang.org/t/optimization-error-when-using-jump/76160
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [February 10, 2022, 1:42pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160 "2022-02-10T13:42:21Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [February 10, 2022, 1:42pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/1 "2022-02-10T13:42:21Z")

</div>

Follow is the optimization problem I defined:  
Packages and constants:

> using JuMP, Ipopt
> 
> const R0 = 0.1803
> 
> Lmin, Lnom, Lmax = 0.8, 2.4, 3.5
> 
> w1 = 0.4
> 
> w2 = 0.6
> 
> Rfc1 = 0.1803 + 0.01
> 
> Rfc2 = 0.1803
> 
> L11, L21 = 2.1, 2.1
> 
> Dindf = repeat([250], 2)
> 
> Ldf = [5, 4]

Functions:

> function α(x)  
> a = 0.0019727939  
> b = 0.0078887  
> α\_0 = 0.006226  
> if Lmin \<= x \< Lnom  
> return a \* (x-Lnom)^2 + α\_0  
> elseif Lnom \<= x \<= Lmax  
> return b \* (x-Lnom)^2 + α\_0  
> end  
> end  
> function obj(x, y)  
> f0dL = (w1 \* (x-L11)^2 + w2 \* (y-L21)^2) \* Dv\_dL \* 3600  
> return (w1 \* α(x) + w2\*α(y))\*Dindf[1] + f0dL  
> end

Do optimization:

> model = Model(Ipopt.Optimizer)
> 
> @variable(model, Lmin\<=x\<=Lmax, start=Lmin)
> 
> @variable(model, Lmin\<=y\<=Lmax, start=Ldf[1]-Lmin)
> 
> @NLconstraint(model, x+y==Ldf[1])
> 
> register(model, :obj, 2, obj; autodiff=true)
> 
> @NLobjective(model, Min, obj(x,y))
> 
> optimize!(model)

In this case, my objective function is _obj(x,y)_, α(x) is  
another generic function called by _obj(x,y)_.  
I received an error message that mainly says:  
\*_MethodError: no method matching (::Float64, ::Nothing)_  
It seems like the definition problem in α(x). How can I fix this error?

---

<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: [February 10, 2022, 2:46pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/2 "2022-02-10T14:46:26Z")

</div>

First: Lnom, xLdf, yLdf are not defined in your example, but I am assuming you have them somewhere.  
Second: your `α` doesn’t give any value if x is smaller than `Lmin` or larger than `Lmax`. You start the optization with `x=0.5 <Lmin=0.8`, so it will not run. Even if you start somewhere else, like `x=1`, then Ipopt may run into trouble when solving the problem.

This runs:

```julia
using JuMP, Ipopt

const R0 = 0.1803

Lmin, Lmax = 0.8, 3.5
Lnom = 2.0
w1 = 0.4

w2 = 0.6

Rfc1 = 0.1803 + 0.01

Rfc2 = 0.1803

L11, L21 = 2.1, 2.1

Dindf = repeat([250], 2)

Ldf = [5, 4]

function α(x)
    a = 0.0019727939
    b = 0.0078887
    α_0 = 0.006226
    if Lmin <= x < Lnom
        return a * (x - Lnom)^2 + α_0
    elseif Lnom <= x <= Lmax
        return b * (x - Lnom)^2 + α_0
    else
        return 0.0 ##New part here
    end
end
function obj(x, y)
    f0dL = (w1 * (Ldf[1] - L11)^2 + w2 * (Ldf[1] - L21)^2) * 1 * 3600
    return (w1 * α(x)) * Dindf[1] + f0dL
end

model = Model(Ipopt.Optimizer)

lb = maximum(Lmin ./ Ldf)

ub = minimum(Lmax ./ Ldf)

@variable(model, lb <= x <= ub, start = 1.0)

@variable(model, lb <= y <= ub, start = 1.0)

@NLconstraint(model, x + y == 1)

register(model, :obj, 2, obj; autodiff = true)

@NLobjective(model, Min, obj(x, y))

optimize!(model)

```

(I created the missing values)

I think you `α` needs work outside Julia. How is it possible to require x\>=0.8, y\>=0.8 in `α(x),α(y)` and then enforce constraint `x+y=1`?

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [February 10, 2022, 3:39pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/3 "2022-02-10T15:39:16Z")

</div>

Hi, thank you very much for the detailed checking.  
I am sorry for the mistake in giving example. I have tried to correct the example.  
For the point you mentioned that the start point is not defined in α(x),α(y) this is due to  
my mistake in writing the example (It is because the **values I defined are wrong** , \*\*the function \*\*  
**form is actually what I needed)**.

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [February 10, 2022, 3:46pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/4 "2022-02-10T15:46:54Z")

</div>

I have checked your example. I think you are right, it is  
because for the simulation case when α(x) returns nothing it  
will go into an error. I will carefully check my constraints settings.

Can you help to give some advice on defining the function α(x) here:  
In my program I need x to satisfy: Lmin \<= x \<= Lmax, outside of  
the range is not considered (due to my problem settings). Is it needed to  
assign some values for an outside range like you did set them to 0.0?

---

<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: [February 10, 2022, 4:33pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/5 "2022-02-10T16:33:57Z")

</div>

Yes, I think your `α` should always give a result. Ipopt may temporarily violate constraints when solving your problem, so `α` should be able to give a value for x even if x is not inside the required range. I don’t know if zero is the right answer, though. Maybe [this topic](https://discourse.julialang.org/t/jump-nonlinear-optimization-constraints-broken-to-o-1e-9/75182) will be of use?

---

<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: [February 10, 2022, 4:39pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/6 "2022-02-10T16:39:46Z")

</div>

This sounds like a bad solution, your \alpha function would not be continuously differentiable. You should introduce the correct bounds on your variables (that is, if \alpha is defined for x \in [L\_{min}, L\_{max}], then x should be in [L\_{min}, L\_{max}].)

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [February 10, 2022, 5:10pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/7 "2022-02-10T17:10:12Z")

</div>

Thanks, Blob! I am going to check the link.

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [February 10, 2022, 5:15pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/8 "2022-02-10T17:15:30Z")

</div>

Thanks Cvanaret. Yes, I see your point and agree.  
But my optimization is from practical usage, actually  
each part of the expression in the α function (i.e. [Lmin, Lnom] and [Lnom, Lmax])  
is fitted from some data. So it is not likely able to be expressed with one formula over [Lmin, Lmax].

---

<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: [February 10, 2022, 7:11pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/9 "2022-02-10T19:11:36Z")

</div>

Yes, I see that \alpha is defined piecewise. But this doesn’t change the problem: if \alpha isn’t defined for some values of x, then x shouldn’t take these values.

In the definition of \alpha, you can have some partitions (ie [L\_{min}, L\_{nom}] or [L\_{nom}, L\_{max}]) over which you fitted some models, that’s fine. What matters is the overall domain of \alpha.

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [February 10, 2022, 8:07pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/10 "2022-02-10T20:07:23Z")

</div>

Yes. I agree. For the last point you mean if my problem limits x within Lmin  
to Lmax, then I should not define α(x) for outside of this domain (e.g. x \< Lmin)?

---

<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: [February 10, 2022, 10:37pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/11 "2022-02-10T22:37:50Z")

</div>

Yes, the domain of \alpha and the bounds of x should be consistent. Otherwise, it makes no sense: why would you choose a value for the optimal x at which \alpha is not defined?

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [February 10, 2022, 11:06pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/12 "2022-02-10T23:06:39Z")

</div>

I see. For constrained optimization, it is not easy,  
especially when the objective function is related to  
a piecewise-defined function.  
For the case as said by Blob, if it **raises an error** because  
objective return nothing, then maybe it is needed to  
construct some values for undefined domain, maybe,  
could be something like adding a **penalty** to the outside of  
function original definition domain.

---

<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: [February 10, 2022, 11:48pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/13 "2022-02-10T23:48:24Z")

</div>

The immense majority of solvers satisfy bound constraints at each iteration. So if you provide the correct bounds for your variables, it will **never** happen that x lies outside of its bounds, by extension it will never happen that \alpha is undefined.

I tend to see “undefined domain” errors as a **modeling flaw**. Most of the time you can reformulate your problem to avoid out-of-domain evaluations.  
For example, the function \sqrt{1 - k(1 - x^2)} is not defined when the radical is negative. Instead of penalizing this scenario in the objective function, introduce a new variable y \ge 0 and a new constraint y = 1 - k(1 - x^2), and use the quantity \sqrt{y}. Since the bound constraints are satisfied at each iteration, it will never happen that the square root is evaluated at a negative number (differentiability at y = 0 is another problem 😋).

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [February 10, 2022, 11:57pm UTC](https://discourse.julialang.org/t/optimization-error-when-using-jump/76160/14 "2022-02-10T23:57:16Z")

</div>

Cool. Thanks a lot for clarifying the out-of-bound issue and great example.  
Your remarks really improve my understanding of such problems.
