Skip to main content

Defining Constraints using a matrix decision variable

Answered

Comments

2 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?.
  • Mario Ruthmair
    • Gurobi Staff

    Hi Adil,

    You could use matrix variables for modeling the TSP, but using usual 2D variable arrays might be more intuitive, e.g., as sketched in the following piece of code, modeling the objective and the first 3 sets of constraints:

    x = m.addVars(*D.shape, vtype=GRB.BINARY, name="x")
    m.setObjective((gp.quicksum(D[i, j] * x[i, j] for i in range(N) for j in range(N))), GRB.MINIMIZE)
    m.addConstrs((x[i, i] == 0 for i in range(N)), name="no-self")
    m.addConstrs((x.sum(i, "*") == 1 for i in range(N)), name="outgoing")
    m.addConstrs((x.sum("*", i) == 1 for i in range(N)), name="incoming")

    If you insist on matrix variables, there are two examples shipped with the Gurobi package located in

    $GUROBI_HOME/examples/python/matrix1.py
    $GUROBI_HOME/examples/python/matrix2.py

    that also demonstrate matrix slicing which is necessary to build linear constraints.

    Best regards,
    Mario

    0

Post is closed for comments.