Regarding use of Max in Jump code

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

You need to use a linear reformulation.

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

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)
1 Like

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?

Please post here with a minimal reproducible example.

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)

Take a read of Please read: make it easier to help you.

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.

1 Like

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?

1 Like

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)

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

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

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.

7 Likes

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

@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

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

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

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.

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.

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

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]

Y: ---> [1.0 0.0; 0.0 1.0; 1.0 0.0; 0.0 1.0]
C->0.0    1.0    5.0
  0.0   28.0   **95.0**
  0.0   33.0   50.0
  0.0  114.0  135.0

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

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:

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

Please check whether I have coded constraint 5 correctly or not:
constraint.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]))