Skip to main content

Different ways of defining quadratic expressions

Answered

Comments

3 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 why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    Yes, those two expressions are mathematically equivalent. You can test this by adding both expressions to the model in separate constraints, writing out an LP file with Model.write(), then visually inspecting the LP file to compare the two constraints. For example, the following code

    import gurobipy as gp

    m = gp.Model()
    x = m.addMVar(5, name='x')

    # Add constraint in two different ways
    m.addConstr(x @ x == 1, name='approach1')
    m.addConstr(gp.quicksum(var*var for var in x.tolist()) == 1, name='approach2')

    # Write results to LP file
    m.write('foo.lp')

    creates a \( \texttt{foo.lp} \) model file with the exact same constraint repeated twice:

    Minimize

    Subject To
    approach1: [ x[0] ^2 + x[1] ^2 + x[2] ^2 + x[3] ^2 + x[4] ^2 ] = 1
    approach2: [ x[0] ^2 + x[1] ^2 + x[2] ^2 + x[3] ^2 + x[4] ^2 ] = 1
    Bounds
    End
    1
  • Permanently deleted user
    • Gurobi-versary
    • First Comment
    • First Question

    Thanks. I have been looking for a way to transform these expressions to some "canonical form" to compare them but I haven't come up with the idea of exporting the model to file.

    0

Post is closed for comments.