# 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:** 11\
**Page:** 2

<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, 9:16am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/21 "2022-08-02T09:16:52Z")

</div>

It looked correct. How did you compute `C_ki[2, 2]` to be 95? It really can be 96 according to those equations and the data you provided.

---

<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, 1:19pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/22 "2022-08-02T13:19:17Z")

</div>

According to the solution it provides, jobs j3 and j2 should process in factory 2 in the order j3—\>j2. Now according to the data provided as pij, job j3 takes 28 units of time on machine 1 and 67 units of time on machine 2 (Row 3 of pij). Therefore its completion time will be 28+67=95 units. And due date for job j3 provided as dj (3rd element) is 96. Therefore there should be an earliness of 1 unit.

---

<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, 9:06pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/23 "2022-08-02T21:06:38Z")

</div>

This is an imprecision in your formulation. There’s nothing stopping `C[2, 2]` from taking the value 96 to avoid the earliness penalty.

In this case, what you typed matches your mathematical formulation, and JuMP is finding the optimal solution to that formulation. If that’s not the answer you want, then you’ll need to have another think about the formulation of your 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:** [August 3, 2022, 3:57am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/24 "2022-08-03T03:57:27Z")

</div>

Ok Sir. I will do it again. Thank you so much for your time. It is much appreciated.

---

<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 3, 2022, 5:57am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/25 "2022-08-03T05:57:45Z")

</div>

![image.png](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed61245d999d608329ca854a9af583c6ec63242d.png)

Please tell me how to code this equation in Julia when there is a **'not equal** ’ condition in the index.

---

<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 3, 2022, 7:15am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/26 "2022-08-03T07:15:34Z")

</div>

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

```

(Not sure `k` should start at `0`? `X` is only defined for `k=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:** [August 3, 2022, 7:35am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/27 "2022-08-03T07:35:32Z")

</div>

![image.png](https://global.discourse-cdn.com/julialang/original/3X/5/3/53143300435cdb8383ac8afcb447a0d12faeb9c2.png)

Thanks, it worked. Now, I’m stuck in these equations. The first one is a constraint and the second one is a decision variable.

---

<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 3, 2022, 7:18pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/28 "2022-08-03T19:18:29Z")

</div>

You can introduce filters on the set of constraints using the `@constraint(model, [set; condition], ...)` syntax, which you already used for `k > l` and `i == m`. You just need a `k != j` instead.

For the second part, it’s probably easiest to create `X` as the full dense matrix, then use

```julia
for i in 1:n
    fix(x[i, i], 0.0; force = true)
end

```

to essentially disable the variable. Then your sums can go over the `j=k` index, but nothing will happen because `x[j,k]` is `0`.

---

<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 5, 2022, 5:10am UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/29 "2022-08-05T05:10:07Z")

</div>

When I tried this, an error occurred in the following constraint.  
 ![image.png](https://global.discourse-cdn.com/julialang/original/3X/9/4/9496b330daf932409a510a1533c4524b4d7254cf.png)

```julia
**Jump code: @constraint(model, con1[j in 1:n], sum(X_kj[0,j]==F for j 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:** [August 5, 2022, 5:11pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/30 "2022-08-05T17:11:46Z")

</div>

Error is rectified by doing @constraint(model, con1[j in 1:n], sum(X\_kj[0,j] for j in 1:n)==F).

![image.png](https://global.discourse-cdn.com/julialang/original/3X/9/4/9496b330daf932409a510a1533c4524b4d7254cf.png)

---

<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:** [August 5, 2022, 11:57pm UTC](https://discourse.julialang.org/t/regarding-use-of-max-in-jump-code/84837/31 "2022-08-05T23:57:31Z")

</div>

> [@Ali9266](#):
>
> @constraint(model, con1[j in 1:n], sum(X\_kj[0,j] for j in 1:n)==F).

You don’t need to index the constraint over j:

```julia
@constraint(model, con1, sum(X_kj[0,j] for j in 1:n)==F).

```

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