Linearizing non-linear constraint

Answered

Comments

3 comments

  • Elisabeth Rodriguez-Heck

    Hi Ana, 

    Testing out your code I get the following error 

    TypeError: unsupported operand type(s) for -: 'GenExpr' and 'GenExpr'

    Note that the functions min_ and max_ are Constraint Helper Functions and should be used in conjunction with overloaded operators and Model.addConstr or Model.addConstrs. 

    A working example would be to replace your last three lines of code by

    mayorU = m.addVar(vtype=GRB.CONTINUOUS)
    menorU = m.addVar(vtype=GRB.CONTINUOUS)

    m.addConstr(mayorU == max_(u))
    m.addConstr(menorU == min_(u))

    m.setObjective(mayorU - menorU, GRB.MAXIMIZE)
     
    Concerning your question about linearity: with this substitution you are creating a model with a linear objective function but note that you are simply moving the non-linearity into the constraints. Perhaps you'll find this article How to linearize max, min and abs functions useful to rewrite your model to be linear.
     
    Best regards,
    Elisabeth
    0
    Comment actions Permalink
  • Ana Guasque Ortega

    Thanks, Elisabeth.

    Yes, you are totally right, I simplified the code but yours is the correct one.

    Thanks for solving the question of non-linearily. Now, I understand that the non-linearity is in the constraints (and not in the objective) and thanks for the link with more information.

    Is Gurobi capable of solving this non-linearity? Because it gives me correct results.

    Thanks again.

    Best,

    Ana

    0
    Comment actions Permalink
  • Elisabeth Rodriguez-Heck

    Hi Ana,

    Happy to read that this answer helped.

    Indeed, Gurobi is capable of handling some types of general constraints, you can find more information on the types of constraints that Gurobi can handle on the Constraints documentation page.

    Best regards,

    Elisabeth

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk