Skip to main content

issue with Model.addGenConstrMax()

Answered

Comments

6 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support.
  • Simranjit Kaur
    • Gurobi Staff

    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,
    Simran

    0
  • Laaziz Lahlou
    • Gurobi-versary
    • Curious
    • Conversationalist

    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
  • Simranjit Kaur
    • Gurobi Staff

    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,
    Simran
    0
  • Laaziz Lahlou
    • Gurobi-versary
    • Curious
    • Conversationalist

    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
  • Simranjit Kaur
    • Gurobi Staff

    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,
    Simran

    0

Post is closed for comments.