简体   繁体   English

Gurobi:获取优化运行时

[英]Gurobi: Get optimization runtime

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.我想访问从 python 运行 gurobi 中的数学优化问题时找到模型m的最佳解决方案所需的时间

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.不幸的是,返回的运行时间始终为 0.0,与求解模型所花费的时间无关,并且在达到任何时间限制之前。

    m.setParam("TimeLimit", timeLimit)

How can the actual runtime be accessed in gurobi via gurobipy?如何通过 gurobipy 在 gurobi 中访问实际运行时? I have read the Gurobi reference manual but without success.我已阅读Gurobi 参考手册但没有成功。

I just tried adding the following line to the assignment.py example file, and it seemed to print out the runtime just fine.我只是尝试将以下行添加到 assignment.py 示例文件中,它似乎可以很好地打印出运行时。

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?你确定你是在 m.optimize() 之后调用它但在调用 m.update() 或其他任何重置模型运行时间的东西之前调用它吗? Try printing the run time immediately after m.optimize().尝试在 m.optimize() 之后立即打印运行时间。

EDIT: I just realized that the assignment.py was one of mine, not the example problem.编辑:我刚刚意识到 assignment.py 是我的一个,而不是示例问题。

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

You may use model attribute Runtime as mentioned in Gurobi Docs . 您可以使用Gurobi Docs中提到的模型属性Runtime You may check how to use attributes on Gurobi Attribute Example . 您可以在Gurobi Attribute Example上检查如何使用属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM