# Constraint MINLP problem

**URL:** <https://discourse.julialang.org/t/constraint-minlp-problem/61272>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, nonlinear\
**Created:** [May 17, 2021, 12:03am UTC](https://discourse.julialang.org/t/constraint-minlp-problem/61272 "2021-05-17T00:03:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_Javier\_Mar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisco_javier_mar/32/24679_2.png) [@Francisco\_Javier\_Mar](https://discourse.julialang.org/u/Francisco_Javier_Mar)\
**Post date:** [May 17, 2021, 12:03am UTC](https://discourse.julialang.org/t/constraint-minlp-problem/61272/1 "2021-05-17T00:03:15Z")

</div>

Hello. I was wondering if someone could clarify a question for me. I have a problem when it comes to modeling in JuMP. I have a MINLP problem with the following objective function and constraints.

```julia
@NLobjective(model,Min,sum(sum((c_00[i]+c_10[i]*x[i,t] + c_01[i]*n[i,t]+ c_20[i]*(x[i,t])^2 +c_11[i]*x[i,t]*n[i,t] + c_02[i]*(n[i,t])^2) +y[i,t] for i=1:nus)+μ*(z[t])^2 for t=1:nd))

@variable(model,x[i=1:nus,t=1:nd]≥0)
@variable(model, n[i=1:nus, t=0:nd],Int)

for i=1:nus
    fix(n[i, 0], 3)
end
@variable(model,y[i=1:nus,t=1:nd]≥0,Int)
@variable(model,z[t=1:nd]≥0)

@constraint(model,restri[i=1:nus,t=1:nd],y[i,t]≥n[i,t]-n[i,t-1]) (1)
@constraint(model,restric[i=1:nus,t=1:nd],y[i,t]≥n[i,t-1]-n[i,t]) (2) 
@constraint(model,lim_nummaq[i=1:nus,t=1:nd],nmin[i,t]≤n[i,t]≤nmax[i,t]) (3)
@constraint(model,lim_geracao[i=1:nus,t=1:nd],xmin[i,t].*n[i,t]≤x[i,t]≤xmax[i,t].*n[i,t]) (4)
@constraint(model,demanda[t=1:nd],sum(x[i,t] for i=1:nus)+z[t]==d[t]) (5)
@constraint(model,meta_geracao[i=1:nus],sum(x[i,t] for t=1:nd)==metas[i]*24) (6)

```

The restriction (4) is dependent on the variable n [i, t], with xmin and xmax being lower and upper bounds respectively and when I run the model I get the following message: n[i, t] expected to be a number. How do I write this constraint in the correct way?xmin and xmax must depend on n[i,t].

---

<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:** [May 17, 2021, 12:29am UTC](https://discourse.julialang.org/t/constraint-minlp-problem/61272/2 "2021-05-17T00:29:46Z")

</div>

Hi there. Please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) and provide a minimal reproducible example. To be able to help, we need to be able to run your code. In particular, I don’t know the data scubas `xmin` and `xmax`

---

<div class="post-metadata">

**Author:** ![Francisco\_Javier\_Mar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisco_javier_mar/32/24679_2.png) [@Francisco\_Javier\_Mar](https://discourse.julialang.org/u/Francisco_Javier_Mar)\
**Post date:** [May 17, 2021, 12:43am UTC](https://discourse.julialang.org/t/constraint-minlp-problem/61272/3 "2021-05-17T00:43:20Z")

</div>

```julia
using JuMP,AmplNLWriter

us=["Chavante","Capivara"]
d=[1080 864 702 621 594 810 972 1080 1161 1242 1296 1188 1350 1512 1485 1458 1215 1053 999 1053 1107 945 864 783]
metas=[220,320]
xmin=[30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30; 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50]
xmax=[440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440;640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640]
nmin=[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
nmax=[4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4;4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4]
cpp=[3*110,3*160]
cp=3
c_00=[0.1959,-0.006446]
c_10=[1.369,2.227]
c_01=[12.96,38.23]
c_20=[0.001165,0.002172]
c_11=[-0.142,-0.4079]
c_02=[4.353,18.59]
μ=0.2

model = Model(
    () -> AmplNLWriter.Optimizer(
        "C:/GAW/MAC/Downloads/bonmin-win64/bonmin.exe"
    )
)

nus=length(us)
nd=length(d)

@NLobjective(model,Min,sum(sum(cp*(c_00[i]+c_10[i]*x[i,t] + c_01[i]*n[i,t]+ c_20[i]*(x[i,t])^2 +c_11[i]*x[i,t]*n[i,t] + c_02[i]*(n[i,t])^2)+cpp[i]*y[i,t] for i=1:nus)+μ*(z[t])^2 for t=1:nd))

@variable(model,x[i=1:nus,t=1:nd]≥0)
@variable(model, n[i=1:nus, t=0:nd],Int)
for i=1:nus
    fix(n[i, 0], 3)
end
@variable(model,y[i=1:nus,t=1:nd]≥0,Int)
@variable(model,z[t=1:nd]≥0)

@constraint(model,restri[i=1:nus,t=1:nd],y[i,t]≥n[i,t]-n[i,t-1]) (1)
@constraint(model,restric[i=1:nus,t=1:nd],y[i,t]≥n[i,t-1]-n[i,t]) (2)
@constraint(model,lim_nummaq[i=1:nus,t=1:nd],nmin[i,t]≤n[i,t]≤nmax[i,t]) (3)
@constraint(model,lim_geracao[i=1:nus,t=1:nd],xmin[i,t].*n[i,t]≤x[i,t]≤xmax[i,t].*n[i,t]) (4)
@constraint(model,demanda[t=1:nd],sum(x[i,t] for i=1:nus)+z[t]==d[t]) (5)
@constraint(model,meta_geracao[i=1:nus],sum(x[i,t] for t=1:nd)==metas[i]*24) (6)

```

Like this?

---

<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:** [May 17, 2021, 1:42am UTC](https://discourse.julialang.org/t/constraint-minlp-problem/61272/4 "2021-05-17T01:42:22Z")

</div>

1. You need to specify the `@NLobjective` _after_ declaring your variables.
2. You need to split constraint (4) into two constraints, one for each side of the inequality. We used to throw a nicer error message. This is a regression in the latest release. (Edit: issue [https://github.com/jump-dev/JuMP.jl/issues/2599](https://github.com/jump-dev/JuMP.jl/issues/2599))
3. You can use `Bonmin_jll` instead of `bonmin.exe`

Here’s how I would write your code

```nohighlight
using JuMP
import AmplNLWriter
import Bonmin_jll

us = ["Chavante","Capivara"]
d = [
    1080 864 702 621 594 810 972 1080 1161 1242 1296 1188 1350 1512 1485 1458 1215 1053 999 1053 1107 945 864 783
]
metas = [220,320]
xmin = [
    30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
    50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
]
xmax = [
    440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440 440
    640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640 640
]
nmin = [
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
]
nmax = [
    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
]
cpp = [3 * 110, 3 * 160]
cp = 3
c_00 = [0.1959, -0.006446]
c_10 = [1.369, 2.227]
c_01 = [12.96, 38.23]
c_20 = [0.001165, 0.002172]
c_11 = [-0.142, -0.4079]
c_02 = [4.353, 18.59]
μ = 0.2

model = Model() do
    AmplNLWriter.Optimizer(Bonmin_jll.amplexe)
end

nus = length(us)
nd = length(d)

@variables(model, begin
    x[i=1:nus, t=1:nd] >= 0
    n[i=1:nus, t=0:nd], Int
    y[i=1:nus, t=1:nd] >= 0,Int
    z[t=1:nd] >= 0
end)
for i in 1:nus
    fix(n[i, 0], 3)
    for t in 1:nd
        set_lower_bound(n[i, t], nmin[i, t])
        set_upper_bound(n[i, t], nmax[i, t])
    end
end
@NLobjective(
    model,
    Min,
    sum(
        sum(
            cp * (
                c_00[i] + 
                c_10[i] * x[i, t] +
                c_01[i] * n[i, t] + 
                c_20[i] * x[i, t]^2 +
                c_11[i] * x[i, t] * n[i, t] + 
                c_02[i] * n[i, t]^2
            ) + 
            cpp[i] * y[i, t]
            for i = 1:nus
        ) + μ * z[t]^2 
        for t = 1:nd
    )
)
@constraints(model, begin
    [i=1:nus, t=1:nd], y[i, t] >= n[i, t] - n[i, t-1]
    [i=1:nus, t=1:nd], y[i,t] >= n[i, t-1] -n[i, t]
    [i=1:nus, t=1:nd], xmin[i, t] * n[i, t] <= x[i, t]
    [i=1:nus, t=1:nd], x[i, t] <= xmax[i, t] * n[i, t]
    [t=1:nd], sum(x[:, t]) + z[t] == d[t]
    [i=1:nus], sum(x[i, :]) == 24 * metas[i]
end)

```

---

<div class="post-metadata">

**Author:** ![Francisco\_Javier\_Mar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisco_javier_mar/32/24679_2.png) [@Francisco\_Javier\_Mar](https://discourse.julialang.org/u/Francisco_Javier_Mar)\
**Post date:** [May 18, 2021, 2:17am UTC](https://discourse.julialang.org/t/constraint-minlp-problem/61272/5 "2021-05-18T02:17:06Z")

</div>

Thanks for your help. Is there a difference between Bonmin\_jll and bonmin.exe? I run the code with bonmin.exe

---

<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:** [May 18, 2021, 2:30am UTC](https://discourse.julialang.org/t/constraint-minlp-problem/61272/6 "2021-05-18T02:30:11Z")

</div>

> Is there a difference between Bonmin\_jll and bonmin.exe?

No. But you can install Bonmin\_jll via the Julia package manager instead of having to keep a copy of the `.exe` around and remember the path.
