要获得总解决时间(挂钟时间),可以使用get_time方法。要获得“网络时间”的值,如日志输出中所示,您必须解析日志输出。以下是一个演示这两个例子:这应该给你如何从日志中解析出其它信息的想法(这并不意味着是一个复杂的解析器的例子)
from __future__ import print_function
import sys
import cplex
class OutputProcessor(object):
"""File-like object that processes CPLEX output."""
def __init__(self):
self.network_time = None
def write(self, line):
if line.find("Network time =") >= 0:
tokens = line.split()
try:
# Expecting the time to be the fourth token. E.g.,
# "Network", "time", "=", "0.48", "sec.", ...
self.network_time = float(tokens[3])
except ValueError:
print("WARNING: Failed to parse network time!")
print(line, end='')
def flush(self):
sys.stdout.flush()
def main():
c = cplex.Cplex()
outproc = OutputProcessor()
# Intercept the results stream with our output processor.
c.set_results_stream(outproc)
# Read in a model file (required command line argument). This is
# purely an example, thus no error handling.
c.read(sys.argv[1])
c.parameters.lpmethod.set(c.parameters.lpmethod.values.network)
start_time = c.get_time()
c.solve()
end_time = c.get_time()
print("Total solve time (sec.):", end_time - start_time)
print("Network time (sec.):", outproc.network_time)
if __name__ == "__main__":
main()
。
您可能也有兴趣get_dettime以及;它可以与get_time
(如上所述)相同的方式使用,但不易受到机器负载的影响。
我修改了我的代码根据你的答案,它的工作,谢谢!但是我还有一个问题:总时间似乎比网络时间长得多(例如1.72秒比0.56秒),那为什么(在“总时间”期间和“网络时间”期间它在做什么? ) – user12345
Python API是Callable C库的一个包装。当您在Python API中调用solve时,会有额外的代码来决定调用哪个优化例程(例如'CPXXmipopt','CPXXlpopt'等),并且优化结束时会检查状态代码确定没有发生错误,等等。这个额外的处理可能是你看到的差异。 “网络时间”是Callable C Library自身报告的时间。你报告的差异比我看到的还要大,但是不知道更多关于你的模型/程序的内容,我不能说其他的东西。 – rkersh
现在我有一个草图的想法。谢谢。 – user12345