Is there a rule to write addvar or addvars?
Answeredmain.py
from gurobipy import Model, GRB, quicksum
n = 20
bus_cap = 5
bus_number = 4
N = [i for i in range(1, n + 2)]
N_f = [i for i in range(2, n + 2)]
K = [k for k in range(1, bus_number + 1)]
A = [(i, j, k) for i in N for j in N for k in K if i != j]
d = {(1, 1): 0.0, (1, 2): 29.5999, (1, 3): 29.9105,..., (21, 18): 3.2028, (21, 19): 4.5491, (21, 20): 5.0024, (21, 21): 0.0}
U = [i for i in range(2, n + 2)]
m = Model("Bitirme_Final")
e = m.addVar(vtype=GRB.CONTINUOUS, name="Epsilon")
x = m.addVars(A, vtype=GRB.BINARY, name="assign")
u = m.addVars(U, vtype=GRB.INTEGER, name="u")
m.setObjective(e, GRB.MINIMIZE)
m.addConstr(e >= 0)
m.addConstrs(quicksum(x[i, j, k] for j in N for i in N_f if i != j) <= bus_cap for k in K)
m.addConstrs(quicksum(x[i, j, k] for i in N for k in K if i != j) == 1 for j in N_f)
m.addConstrs(quicksum(x[i, 1, k] for i in N_f) == 1 for k in K)
m.addConstrs(quicksum(x[i, j, k] for i in N if i != j) == quicksum(x[j, i, k] for i in N if i != j) for j in N for k in K)
m.addConstrs(quicksum(x[i, j, k] * d[i, j] for j in N for i in N_f if i != j) <= e for k in K)
m.addConstrs((u[i]-u[j] + len(N) * quicksum(x[i, j, k] for k in K) <= (len(N) - 1)) for i in N for j in N if i != j and i != 1 and j != 1)
m.addConstrs((u[i] >= 0)for i in U)
m.optimize()
First case GAP -> 4.83% in 60s
e = m.addVar(vtype=GRB.CONTINUOUS, name="Epsilon")
x = m.addVars(A, vtype=GRB.BINARY, name="assign")
u = m.addVars(U, vtype=GRB.INTEGER, name="u")
Second case GAP -> 0% in 59s
x = m.addVars(A, vtype=GRB.BINARY, name="assign")
u = m.addVars(U, vtype=GRB.INTEGER, name="u")
e = m.addVar(vtype=GRB.CONTINUOUS, name="Epsilon")
When I write the variables in the order as in the first case, the program runs for infinite time and it approaches to the optimal point with 4.83% GAP in 60 seconds.
But when I write the variables in the order as in the second case, the program gives the optimal solution in 59 seconds.
Do you have an opinion what creates this difference? Is there a rule for writing addVar and addVars in a specific order?
-
Official comment
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 Oğuz,
Ordering of variables affects Gurobi's heuristics and algorithmic decisions. Thus, it is very well possible that a simple reordering of variables or constraints may affect the optimization path significantly. You can find more details in Is Gurobi Optimizer deterministic?
There is no rule for adding the variables in a specific order.
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments