簡體   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