Skip to main content

fix variables to convert NLP to LP

Answered

Comments

4 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three 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.
  • Jaromił Najman
    • Gurobi Staff

    It is not possible to directly construct nonlinear expressions in Gurobi such as the \(x\cdot y^2\). This is because the QuadExpr object allows for at most quadratic terms and the operation QuadExpr * GRBVar is not defined.

    For your case, it would be best to have 2 models, a "fixed x model" and a "fixed y model". For the particular example you presented, this would be similar to

    from gurobipy import*

    mxfixed = Model("x_fixed")
    mxfixed.setParam("NonConvex",2)
    x_xfixed = [2]*10
    y_xfixed = mxfixed.addVars(10)

    c_xfixed = mxfixed.addConstr(quicksum(x_xfixed[i] *y_xfixed[i] *y_xfixed[i] for i in range(10)) >=10)

    mxfixed.setObjective(sum(x_xfixed) + y_xfixed.sum())
    mxfixed.optimize()

    myfixed = Model("y_fixed")

    x_myfixed = myfixed.addVars(10)
    y_myfixed = [3]*10

    c = myfixed.addConstr(quicksum(x_myfixed[i] *y_myfixed[i] *y_myfixed[i] for i in range(10)) >=10)

    myfixed.setObjective(x_myfixed.sum() + sum(y_myfixed))
    myfixed.optimize()

    Best regards, 
    Jaromił

    0
  • ce jekl
    • Gurobi-versary
    • Collaborator
    • Investigator

    I use an iterative framework, which first fixes x, then y, then x again.

    And I have a concern that in my previous post: https://support.gurobi.com/hc/en-us/community/posts/4974386567697/comments/6031986401041 you told me that constructing new expressions can be way faster than removing variables from expression.

    But in this situation, is changing the expression coefficient faster than constructing new expressions?

    0
  • Jaromił Najman
    • Gurobi Staff

    If I understand your idea correctly, you will have to use the chgCoeff method to change particular coefficient depicting your fixed x and y values in the corresponding "fixed x" or "fixed y" model. In most cases, it should be the fastest. Still, I think it is best if you test both, the chgCoeff method vs constructing a new constraint and removing the old one.

    0

Post is closed for comments.