issue with Model.addGenConstrMax()
AnsweredI have a mathematical model \(P\) for which I optimize two cost functions say \(F_1\) and \(F_2\) subject to a set of constraints.
In \(F_2\), I want it to be included only when its expression violates a certain quantity. In fact, \(F_2\) involves the product of two decision variables to be specific and its quantity should be multiplied by a penalty parameter say \(\sigma\) only when \(\sum_{\mathcal{R}_i \in \mathcal{R}}\sum_{p_{(s, d)} \in \mathcal{P}} \sum_{(v^i_k, v^i_l) \in \mathcal{L}^i} x^{i, k}_{s}\times x^{i, l}_{d} d_{(s, d)}-\alpha^{k,l}\) is greater than \(0\).
$$F_2=\sigma max\{\sum_{\mathcal{R}_i \in \mathcal{R}}\sum_{p_{(s, d)} \in \mathcal{P}} \sum_{(v^i_k, v^i_l) \in \mathcal{L}^i} x^{i, k}_{s}\times x^{i, l}_{d} d_{(s, d)}-\alpha^{k,l},0\}.$$
\(x\) is a binary decision variable, \(d\) is a constant.
How do I model and implement this and what should be the constraints I need to add ?
-
Hi Laaziz,
Assuming you are minimizing \(F_2\) in the objective and \(F_2\) can take non-negative values, you can simply add the following constraint to your model:
\[F_2 \geq \sigma(\sum_{R_i \in R} \sum_{p(s,d) \in P)}\sum_{(v_k^i,v_l^i)\in L^i} x^{i,k}_s \times x^{i,l}_d d_{(s,d)} -\alpha^{k,l})\]
Since \(F_2\) is minimized, it will equal the right-hand side (RHS) if the RHS of the constraint is positive. However, if the RHS is negative or zero, \(F_2\) will take the value zero.
Best regards,
Simran0 -
Hi Simran,
Thank you for your proposition. I tried it but I am facing a KeyError: 'Missing constraint index' issue.
Please check out this implementation and let me know what I need to fix.
Actually, I do not want F2 to take negative values, it should be x_i_j_k[s, i, k] * x_i_j_k[d, i, l] * d[s,d] - alpha[(k, l)] multiplied by sigma when it is greater than 0 and should take 0 when less than or equal to 0.All the best,
Laaziz
omega_2 = quicksum(x_i_j_k[s, i, k] * x_i_j_k[d, i, l] * sigma * d[s,d] - alpha[(k, l)]
for i in R.keys()
for (k, l) in L[i].keys()
for (s, d) in P.keys())
my_model.addConstrs(
(omega_2 >= quicksum(x_i_j_k[s, i, k] * x_i_j_k[d, i, l] * sigma * d[s,d] - alpha[(k, l)]
for i in R.keys()
for (k, l) in L[i].keys()
for (s, d) in P.keys())),
"C9")
my_model.ModelSense = GRB.MINIMIZE
my_model.setObjective(omega_1 + omega_2)
my_model.optimize()1 -
Hi Laaziz,
The error KeyError: 'Missing constraint index' is happening because you are adding a single constraint using the model.addConstrs() method. Please use model.addConstr() to add a single constraint. Please have a look at the article KeyError: 'Missing constraint index'.
If \(F_2\) has the default lower bound of zero, then it will not take a negative value. With the below constraint, it will be x_i_j_k[s, i, k] * x_i_j_k[d, i, l] * d[s,d] - alpha[(k, l)] multiplied by sigma when it is greater than 0 and will be zero otherwise.
\[F_2 \geq \sigma(\sum_{R_i \in R} \sum_{p(s,d) \in P)}\sum_{(v_k^i,v_l^i)\in L^i} x^{i,k}_s \times x^{i,l}_d d_{(s,d)} -\alpha^{k,l})\]
This constraint can be modelled as:
model.addConstr( omega_2 >= sigma * ( quicksum(x_i_j_k[s, i, k] * x_i_j_k[d, i, l] * d[s,d]) - alpha[(k, l)] for i in R.keys() for (k, l) in L[i].keys() for (s, d) in P.keys() ), "C9")
Best regards,
Simran0 -
Hi Simran,
Actually, I am getting a negative cost function value. In fact, the decision variables are binary so I am not sure why it does not work as I expected. What do you mean by the default lower bound for F2 ?
Best regards,
Laaziz
0 -
Hi Laziz,
By default, all variables in Gurobi have a lower bound of zero. Meaning they can only take non-negative values. The lower bound of a variable can be modified using the variable's LB attribute, i.e. using var.LB.
Once the model is solved, you can query the values of variables in your objective function to see which one is taking the negative value and start debugging your code from there.
if model.status == GRB.OPTIMAL:
print( omega_1.X, omega_2.X )You can also export the model in readable form to an LP file, with model.write("model.lp") immediately before calling model.optimize(). Open the LP file with a text editor and check if the model is built as per your expectations.
Best regards,
Simran0
Please sign in to leave a comment.
Comments
5 comments