# Regarding use of Max in Jump code

**URL:** <https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837>\
**Category:** Optimization (Mathematical)\
**Created:** [July 26, 2022, 5:51pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837 "2022-07-26T17:51:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [July 26, 2022, 5:51pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/1 "2022-07-26T17:51:42Z")

</div>

Objective function is to minimize Z(i) =T(i)+E(i) ; i = 1:n  
where T(i) = max[x(i) - y(i), 0] and E = max[y(i) - x(i), 0]  
Please help  
Also , how can we take absolute values in Jump

---

<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:** [July 26, 2022, 10:24pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/2 "2022-07-26T22:24:01Z")

</div>

You need to use a linear reformulation.

```julia
using JuMP
n = 3
model = Model()
@variable(model, x[1:n])
@variable(model, y[1:n])
@variable(model, T[1:n] >= 0)
@constraint(model, T[i] >= x[i] - y[i])
@variable(model, E[1:n] >= 0)
@ constraint(model, E[i] >= y[i] - x[i])
@objective(model, Min, sum(T[i] + E[i] for i in 1:n)

```

> how can we take absolute values in Jump

`Z[i]` is the absolute value of `x[i] - y[I]`

Or you can use the NormOneCone: [Standard form · MathOptInterface](https://jump.dev/MathOptInterface.jl/stable/reference/standard_form/#MathOptInterface.NormOneCone)

```julia
using JuMP
n = 3
model = Model()
@variable(model, x[1:n])
@variable(model, y[1:n])
@variable(model, z[1:n] >= 0)
@constraint(model, [i=1:n], [z[i], x[i] - y[i]] in MOI.NormOneCone(2))
@objective(model, Min, sum(z[i] for i in 1:n)

```

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [July 27, 2022, 11:18am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/3 "2022-07-27T11:18:21Z")

</div>

Thanks for the information. I have tried it, but it is not working in many instances. Should I send you the model and the code that I’m trying to use?

---

<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:** [July 27, 2022, 12:01pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/4 "2022-07-27T12:01:34Z")

</div>

Please post here with a minimal reproducible example.

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [July 27, 2022, 12:55pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/5 "2022-07-27T12:55:54Z")

</div>

Please find the attached model with the corresponding JuMP code.  
The minimum value for this problem is 28.

(Attachment Model and JuMP code.docx is missing)

---

<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:** [July 27, 2022, 1:13pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/6 "2022-07-27T13:13:23Z")

</div>

Take a read of [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757).

You should post the code in discourse, not as an attachment. The link explains how to format your code. You should also minimize your code as much as possible to make it easier for people to give advice.

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [July 27, 2022, 1:15pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/7 "2022-07-27T13:15:08Z")

</div>

![JuMP code.jpg](https://global.discourse-cdn.com/julialang/original/3X/9/6/960f178dc631f92ebc2e0c7ab312086eb505ebe8.jpeg)

---

<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:** [July 27, 2022, 1:23pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/8 "2022-07-27T13:23:08Z")

</div>

Please read the link. Post the code as text, not a picture, so that people can copy-paste.

Also: what is the problem? Does the code error, or get the wrong solution? If wrong, how do you know it is wrong?

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [July 27, 2022, 1:25pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/9 "2022-07-27T13:25:45Z")

</div>

```nohighlight

model = Model(Gurobi.Optimizer)

set_optimizer_attribute(model, "TimeLimit", 100)

set_optimizer_attribute(model, "Presolve", 0)

data=

> <br><br>1<br><br> | <br><br>4<br><br> | <br><br>5<br><br> |
> - | - | - |
> <br><br>86<br><br> | <br><br>21<br><br> | <br><br>108<br><br> |
> <br><br>28<br><br> | <br><br>67<br><br> | <br><br>96<br><br> |
> <br><br>32<br><br> | <br><br>17<br><br> | <br><br>50<br><br> |

D_j = data[:,end]

P_ij = data[:,1:end-1]

P_ij = transpose(P_ij)

(m,n)= size(P_ij)

F = 2 #no. of factories

M = 50000 # Big number

@variable(model,X_jk[j in 1:n, k in 1:n],Bin)

@variable(model,Y_kf[k in 1:n, f in 1:F],Bin)

@variable(model,C_ki[k in 1:n, i in 0:m]>=0)

@variable(model,U_k[k in 1:n],Int)

@variable(model,T_k[k in 1:n]>=0)

@variable(model,E_k[k in 1:n]>=0)

for j in 1:n

@constraint(model,sum(X_jk[j,k] for k in 1:n)==1)

end

for k in 1:n

@constraint(model,sum(X_jk[j,k] for j in 1:n)==1)

end

for k in 1:n

@constraint(model,sum(Y_kf[k,f] for f in 1:F)==1)

end

@constraint(model, con3[k in 1:n,i in 1:m; i==0], C_ki[k,i]==0)

@constraint(model, con4[k in 1:n, i in 1:m], C_ki[k,i]>= C_ki[k,i-1] + sum(X_jk[j,k]*P_ij[i,j] for j in 1:n))

@constraint(model, con5[k in 2:n, l in 1:n, i in 1:m, f in 1:F; k>l], C_ki[k,i]>= C_ki[l,i]+sum(X_jk[j,k]*P_ij[i,j] for j in 1:n)-M*(1-Y_kf[k,f])-M*(1-Y_kf[l,f]))

@constraint(model, con6[k in 1:n], U_k[k] == sum(X_jk[j,k]*D_j[j] for j in 1:n))

@constraint(model, con7[k in 1:n,i in 1:m; i==m], T_k[k]>=C_ki[k,i]-U_k[k])

@constraint(model, con8[k in 1:n,i in 1:m; i==m], E_k[k]>=U_k[k]-C_ki[k,i])

@objective(model, Min, sum(T_k[k]+E_k[k] for k in 1:n))

optimize!(model)

```

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [July 27, 2022, 1:44pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/10 "2022-07-27T13:44:19Z")

</div>

I’m getting the wrong answer for many instances. I have cross-checked it by solving the problem by hand.

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [July 28, 2022, 12:39pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/11 "2022-07-28T12:39:28Z")

</div>

Are you working on my problem??? Please respond.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [July 28, 2022, 12:49pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/12 "2022-07-28T12:49:25Z")

</div>

Hi Ali, No-one here is working on your problem for you. We can only offer advice to questions and respond to specific information. The next step would be to provide a short but complete description of why you see something is wrong with your analysis of the problem.

---

<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:** [July 28, 2022, 11:29pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/13 "2022-07-28T23:29:40Z")

</div>

When I run your code, I get the objective value 27. If that’s the wrong value, it’s likely because there is a typo in your constraints. I suggest you double check your formulation.

Also, instead of

```nohighlight
@constraint(model, con8[k in 1:n,i in 1:m; i==m], E_k[k]>=U_k[k]-C_ki[k,i])

```

just write

```julia
@constraint(model, con8[k in 1:n], E_k[k]>=U_k[k]-C_ki[k,m]

```

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [July 29, 2022, 10:16am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/14 "2022-07-29T10:16:26Z")

</div>

First of all, I would like to thank you for giving me that much time. I really appreciate it. I’m also getting an objective value of 27 ( T=27 + E=0), but when I’m solving it by hand, it comes out to be 28 (T=27 + E=1). This is happening because of the incorrect values of variable C. The optimum schedule (by variables X and Y) we got is not matching with the values of variable C. Also, the value changes with the value of M (big number). I have doubt in constraints number 5, i.e,  
@constraint(model, con5[k in 2:n, l in 1:n, i in 1:m, f in 1:F; k\>l], C\_ki[k,i]\>= C\_ki[l,i]+sum(X\_jk[j,k]_P\_ij[i,j] for j in 1:n)-M_(1-Y\_kf[k,f])-M\*(1-Y\_kf[l,f])). Please check it.

![image.png](https://global.discourse-cdn.com/julialang/original/3X/2/6/26d0f11ac3e127c8a46008fae9e892bd6e55f9d0.png)

---

<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:** [July 30, 2022, 12:52am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/15 "2022-07-30T00:52:21Z")

</div>

The optimal objective for the problem you coded is 27. Is it feasible?

How are you solving it by hand? How did you verify that your hand solution is optimal? 27 is better than your solution of 28. If there’s a mistake, double check your data and the indices, etc.

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [August 2, 2022, 5:14am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/16 "2022-08-02T05:14:22Z")

</div>

I have checked it once again. Actually, the optimal solution (which is obviously feasible) that we are getting is not matching with the objective function value. I have written down all the possible combinations, and since it is a very small problem, that’s why I was able to solve it by hand. Out of all these combinations, the minimum value that I got is 28.

---

<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:** [August 2, 2022, 5:25am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/17 "2022-08-02T05:25:29Z")

</div>

What is your Julia code, and what are the values of the solution you think is optimal?

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [August 2, 2022, 6:01am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/18 "2022-08-02T06:01:10Z")

</div>

My problem is about assigning n jobs to F factories. In the given problem N=4 , F=2. Optimal solution that we should got is X: —\> [1.0 0.0 0.0 0.0; 0.0 0.0 0.0 1.0; 0.0 1.0 0.0 0.0; 0.0 0.0 1.0 0.0]

```julia
Y: ---> [1.0 0.0; 0.0 1.0; 1.0 0.0; 0.0 1.0]

```

```julia
C->0.0 1.0 5.0

```

```julia
  0.0 28.0 **95.0**
  0.0 33.0 50.0
  0.0 114.0 135.0

```

---

<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:** [August 2, 2022, 6:55am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/19 "2022-08-02T06:55:44Z")

</div>

If I fix your values of `X` and `Y`, is still get the objective value of 27:

```julia
X = [1 0 0 0; 0 0 0 1; 0 1 0 0; 0 0 1 0]
Y = [1 0; 0 1; 1 0; 0 1]
@constraint(model, X_jk .== X)
@constraint(model, Y_kf .== Y)

```

Note that `C[2, 2]` can take the value 96, so your computed value of 95 is probably incorrect.

You can verify it outside of JuMP:

```julia
X = [1 0 0 0; 0 0 0 1; 0 1 0 0; 0 0 1 0]
Y = [1 0; 0 1; 1 0; 0 1]
C = [0 1 5; 0 28 96; 0 33 50; 0 114 135]
for k in 1:n, i in 1:m
    @assert C[k, i+1] >= C[k, i] + sum(X[j, k] * P_ij[i, j] for j in 1:n)
end
for k in 2:n, l in 1:n, i in 1:m, f in 1:F
    if k > l
        @assert C[k, i+1] >= C[l, i+1] + sum(X[j,k]*P_ij[i,j] for j in 1:n) - M * (1 - Y[k, f]) - M * (1 - Y[l, f])
    end
end

```

---

<div class="post-metadata">

**Author:** ![Ali9266](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Ali9266](https://discourse.julialang.org/u/Ali9266)\
**Post date:** [August 2, 2022, 7:28am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/20 "2022-08-02T07:28:26Z")

</div>

Please check whether I have coded constraint 5 correctly or not:  
 ![constraint.png](https://global.discourse-cdn.com/julialang/original/3X/e/4/e425547f969974edabadcf7f9c9490181a4e692b.png)

Julia code:  
@constraint(model, con5[k in 2:n, l in 1:n, i in 1:m, f in 1:F; k\>l], C\_ki[k,i]\>= C\_ki[l,i]+sum(X\_jk[j,k]_P\_ij[i,j] for j in 1:n)-M_(1-Y\_kf[k,f])-M\*(1-Y\_kf[l,f]))

[Next page](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837.md?page=2)
