문제

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