解決時間の合計(壁時計時)を取得するには、get_timeメソッドを使用できます。ログ出力に表示される "Network 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ライブラリ自体で報告された時間です。あなたが報告している違いは、私が見たよりも大きいですが、あなたのモデル/プログラムについてもっと知らないと、私は本当に何も言えません。 – rkersh
今私はスケッチのアイディアを持っています。ありがとう。 – user12345