繁体   English   中英

如何在使用CPLEX Python API后获取已用时间的值

[英]how to get value of elapsed time after using CPLEX Python API

我正在使用CPLEX python API来解决优化问题。 该模型名为DOT。 当我运行DOT.solve()时,python控制台中会出现许多信息:

Iteration log ...
...
Network - Optimal:  Objective =    1.6997945303e+01
Network time = 0.48 sec. (91.93 ticks)  Iterations = 50424 (8674)
...

我的问题是,是否有一种简单的方法来获得经过时间的价值(即我的情况下为0.48)? 而且不仅仅是针对网络优化器,而是针对双重方法,屏障方法也是如此。

我是python和CPLEX的新手,我非常感谢任何帮助。

要获得总解决时间(在挂钟时间内),可以使用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一样使用(如上所述),但不会受到机器负载的影响。

暂无
暂无

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

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