使用CPLEX Python API后如何获得经过时间的值 [英] how to get value of elapsed time after using CPLEX Python API

查看:238
本文介绍了使用CPLEX Python API后如何获得经过时间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using CPLEX python API to solve an optimization problem. The model is named DOT. When I run DOT.solve(), many information occur in the python console:

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

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

My question is that is there an easy way to get value of the elapsed time (i.e., 0.48 in my case)? And not just for the network optimizer, but for dual method, barrier method too.

我对python和CPLEX还是很陌生,我非常感谢

I'm very new to python and CPLEX, and I'm very grateful for any help.

推荐答案

要获取总求解时间(以挂钟时间为单位),可以使用 get_time 方法。要获取日志输出中显示的网络时间值,您必须解析日志输出。下面的示例演示了这两个示例:

To get the total solve time (in wall clock time), you can use the get_time method. To get the value for "Network time", as displayed in the log output, you'll have to parse the log output. Here's an example that demonstrates both of these:

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()

这应该使您了解如何从日志中解析出其他信息(这并不是一个复杂的解析器的示例)。

That should give you an idea of how to parse out other pieces of information from the log (it is not meant to be an example of a sophisticated parser).

您可能对 get_dettime 也是如此;可以使用与 get_time 相同的方法(如上所述),但是不受计算机负载的影响。

You may be interested in get_dettime as well; it can be used the same way as get_time (as above), but is not susceptible to the load on your machine.

这篇关于使用CPLEX Python API后如何获得经过时间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆