Hi @Artemisa_Fontinele, welcome to the forum.
You can achieve this using indicator constraints: Constraints · JuMP. The tricky part is that you need to introduce a binary variable with is 0 or 1 at the correct time.
Here’s how I would write your code. I haven’t tested it because I don’t know the values for c
, l
, etc, so let me know if there are any issues.
using CPLEX, JuMP
function MILP(c, l, ps, d, time_limit)
N = size(c,1)
model = Model(
optimizer_with_attributes(
CPLEX.Optimizer,
"CPX_PARAM_TILIM" => time_limit,
"CPX_PARAM_THREADS" => 1,
),
)
@variable(model, X[1:N, 1:N], Bin)
@objective(model, Min, sum(c .* X))
for i in 1:N, j in 1:N
if l[i, j] == 0
fix(X[i, j], 0)
end
end
for i in 1:N
if i in ps
for j in 1:N
if l[i, j] == 1
fix(X[j, i], 0)
fix(X[i, j], 1)
end
end
end
if !(i in ps) && !(i in d)
# if sum(X[:, i]) == 0 => sum(X[i, :]) >= 2
# if sum(X[:, i]) >= 1 => sum(X[i, :]) == 1
# Define zi ∈ {0, 1}, which is 0 if sum(X[:, i]) == 0 and 1 otherwise
zi = @variable(model, binary = true)
@constraints(model, begin
sum(X[:, i]) <= N * zi
sum(X[:, i]) >= zi
!zi --> {sum(X[i, :]) >= 2}
zi --> {sum(X[i, :]) == 1}
end)
end
if (i in d)
@constraint(model, sum(X[:, i]) >= 1)
@constraint(model, sum(X[i, :]) == 0)
end
end
optimize!(model)
zIP = objective_value(model)
tzIP = solve_time_sec(model)
X_values = value.(X)
return zIP, tzIP, X_values
end