Defining Constraints using a matrix decision variable
回答済みHi,
I have trying to define constraints in the travelling sales man problem, but I couldn't find any syntax on how to use the decision variable matrix.
This is the problem description
and here is my implementation
you can see all the syntaxes i have been trying (referencing numpy arrays)

ct: dataframe of the following, (x and y are the coordinates)

can you help me out with how to write the constraints?
Thank you
-
正式なコメント
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?. -
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.pythat also demonstrate matrix slicing which is necessary to build linear constraints.
Best regards,
Mario0
投稿コメントは受け付けていません。
コメント
2件のコメント