سؤال

I would like to access the time it took to find the optimal solution of model m when running a mathematical optimization problem in gurobi from python.

So far I use

runtime = m.Runtime
print("The run time is %f" % runtime)

Unfortunately, the returned runtime is always 0.0, independent of the time it took to solve the model, and before any timelimit is reached.

    m.setParam("TimeLimit", timeLimit)

How can the actual runtime be accessed in gurobi via gurobipy? I have read the Gurobi reference manual but without success.

هل كانت مفيدة؟

المحلول

I just tried adding the following line to the assignment.py example file, and it seemed to print out the runtime just fine.

print m.Runtime

Are you sure you're calling it after m.optimize() but before calling m.update() or anything else that resets the model run time? Try printing the run time immediately after m.optimize().

EDIT: I just realized that the assignment.py was one of mine, not the example problem.

from gurobipy import *
from numpy import *

numT = 300;
numC = 300;

Assignment = random.random((numT,numC))

m=Model("Assignment")

X = []
for t in range(numT):
    X.append([])
    for c in range(numC):
        X[t].append(m.addVar(vtype=GRB.BINARY,name="X%d%d"% (t, c)))
m.update()
m.modelSense = GRB.MAXIMIZE
constraintT = []
constraintC = []
for t in range(numT):
    constraintT.append(m.addConstr(quicksum(X[t][c] for c in range(numC)) == 1 ,'constraintT%d' % t))

for c in range(numC):
    constraintT.append(m.addConstr(quicksum(X[t][c] for t in range(numT)) == 1 ,'constraintC%d' % t))

m.setObjective(quicksum(quicksum([X[t][c]*Assignment[t][c] for c in range(numC)]) for t in range(numT)))

m.update()
m.optimize()

print 'runtime is',m.Runtime
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top