【问题标题】:Is there a bug with using timelimit within pulp in python using CPLEX?使用 CPLEX 在 python 中使用 timelimit 是否存在错误?
【发布时间】:2020-11-06 09:32:17
【问题描述】:

我目前正在写我的论文,我想在其中实现一个算法,以使用大型 OR 模型收敛到一个好的解决方案。

如果您需要它来回答我的问题或更好地理解我的问题,我可以包含该模型。

当我以特定的时间限制(在本例中为 60 秒)运行模型时,模型会忽略它并求解到最优。看代码,这里我求解模型prob

path_to_cplex = r'C:\Program Files\IBM\ILOG\CPLEX_Studio1210\cplex\bin\x64_win64\cplex.exe'
solver = pulp.CPLEX_CMD(path=path_to_cplex,timelimit=timelimitnumber)

print('The timelimit on the solver is: '+str(solver.timelimit))
print('Start Solving')

prob.solve(solver)
status =  pulp.LpStatus[prob.status]
ObjectiveValue = prob.objective.value()
solutiontime = prob.solutionTime

print('The solution time was: '+str(solutiontime))

求解这个模型的输出是:

The timelimit on the solver is: 60
Start Solving
The solution time was: 225.6275095000001

我在这里做错了吗?我希望你能帮忙。

更新:

CPLEX 日志:

Log started (V12.10.0.0) Fri Nov  6 14:46:47 2020


Problem 'Plant_Allocation_Problem-pulp.lp' read.
Read time = 3.30 sec. (185.85 ticks)
New value for time limit in seconds: 60
Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae
CPXPARAM_TimeLimit                               60
Found incumbent of value 6.8557690e+08 after 0.42 sec. (345.18 ticks)
Tried aggregator 1 time.
MIP Presolve eliminated 75703 rows and 75650 columns.
MIP Presolve modified 2500 coefficients.
Reduced MIP has 77062 rows, 3655050 columns, and 7312500 nonzeros.
Reduced MIP has 50 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 3.44 sec. (2580.68 ticks)
Tried aggregator 1 time.
Detecting symmetries...
Elapsed time for symmetry detection = 6.13 sec. (10026.07 ticks)
Elapsed time for symmetry detection = 11.86 sec. (20030.20 ticks)
Elapsed time for symmetry detection = 17.83 sec. (30034.36 ticks)
Elapsed time for symmetry detection = 23.98 sec. (40038.62 ticks)
Elapsed time for symmetry detection = 29.72 sec. (50042.91 ticks)
Elapsed time for symmetry detection = 35.19 sec. (60047.07 ticks)
Elapsed time for symmetry detection = 40.64 sec. (70051.21 ticks)
Elapsed time for symmetry detection = 46.19 sec. (80055.38 ticks)
Presolve time = 54.67 sec. (89646.54 ticks)

Root node processing (before b&c):
  Real time             =   60.16 sec. (93475.49 ticks)
Parallel b&c, 12 threads:
  Real time             =    0.00 sec. (0.00 ticks)
  Sync time (average)   =    0.00 sec.
  Wait time (average)   =    0.00 sec.
                          ------------
Total (root+branch&cut) =   60.16 sec. (93475.49 ticks)

Solution pool: 1 solution saved.

MIP - Time limit exceeded, integer feasible:  Objective =  6.8557690211e+08
Current MIP best bound =  0.0000000000e+00 (gap = 6.85577e+08, 100.00%)
Solution time =   60.17 sec.  Iterations = 0  Nodes = 0
Deterministic time = 93486.16 ticks  (1553.65 ticks/sec)

MILP problem relaxed to LP with fixed integer variables using
incumbent solution.
Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae
CPXPARAM_TimeLimit                               60
Parallel mode: deterministic, using up to 12 threads for concurrent optimization:
 * Starting dual Simplex on 1 thread...
 * Starting Barrier on 9 threads...
 * Starting primal Simplex on 1 thread...
 * Starting Sifting on 1 thread...
Tried aggregator 1 time.
LP Presolve eliminated 152765 rows and 3730700 columns.
All rows and columns eliminated.
Presolve time = 2.09 sec. (1210.10 ticks)

Dual simplex solved model.


Dual simplex - Optimal:  Objective =  2.1151566247e+08
Solution time =    4.14 sec.  Iterations = 0 (0)
Deterministic time = 1929.76 ticks  (466.01 ticks/sec)


Solution written to file 'Plant_Allocation_Problem-pulp.sol'.

【问题讨论】:

  • 尝试为求解器添加 msg=True 参数。然后,您可以查看 CPLEX 日志并检查发生了什么以及参数是否正确传递。
  • 嗨,这可能是一个愚蠢的问题。但是这条信息应该在哪里呢?我在我的文件夹中查看了 CPLEX.log 所在的位置,但在求解器完成后它会自行删除。我飞快地查看文件,在日志中看到以下内容:
  • 而且在这个日志中,我可以看到下面一行(最大字符不允许我发布整个日志):CPXPARAM_TimeLimit 60 所以它一定是别的东西?
  • 您使用的是哪个版本的 CPLEX?另外,您能否将求解器打印的日志添加到您的问题中?
  • 我现在已将 CPLEX 日志添加到我的问题中。根据日志 - 也许是 CPLEX V.12.10.0.0?我一个月前刚下载了 CPLEX,所以我应该更新一下。

标签: python python-3.x cplex pulp time-limiting


【解决方案1】:

我认为问题在于prob.solutionTime 属性,您假设(正确或错误地)以秒为单位。我不确定它有什么单位。

代码显示了当前在pull中使用的逻辑,您必须查看python的时间库以获取更多信息。

try:
    from time import process_time as clock
except ImportError:
    from time import clock

# (...)
self.solutionTime = -clock()
status = solver.actualSolve(self, **kwargs)
self.solutionTime += clock()
#(...)

如果要保留日志文件,可以执行以下操作:

solver = pulp.CPLEX_CMD(path=path_to_cplex,timelimit=timelimitnumber, logPath='PATH_TO_LOG.log')

有关 CPLEX_CMD 可能参数的更多信息,请访问:https://coin-or.github.io/pulp/technical/solvers.html#pulp.apis.CPLEX_CMD

【讨论】:

  • 嗨@pchtsp,我已在stackoverflow 中将我的模型的CPLEX 日志添加到我的问题中。它仍然会在约 210 秒后返回。我也在我的手机上计时以验证它。根据 CPLEX 日志,求解时间应该是 4.14 秒 + 60.17 秒吧?但纸浆在约 210 秒之前不会返回溶液。我在这里想念什么?根据所有测量,所有属性都必须以秒为单位,对吧?
  • 您需要分析您的代码以检查其花费时间的位置。 CPLEX 的 60 年代只考虑解决部分本身......也许纸浆在其他地方需要时间。
  • 我想我知道,它花时间在哪里。我已经在它开始解决和结束解决之间计时。这大约需要 210 秒。在这些秒内发生 CPLEX 日志。这意味着,求解器有大约 150 秒的时间来做某事,而我对此一无所知。没有日志,什么都没有。它会在大约 60 秒后返回 CPLEX 提供的解决方案。所以我认为我的问题在于弄清楚求解器在剩余的 150 秒内做什么。但对我来说,它是一个黑匣子。可以得到某种关于纸浆求解器内部发生的事情的日志吗?
  • 您可以尝试分析代码,正如我所说:stackoverflow.com/questions/582336/… 这会告诉您哪个函数需要时间。
  • 啊,对不起。不知道,分析代码是什么意思。我一定会尝试的。感谢您的帮助。
猜你喜欢
  • 2014-09-21
  • 2021-01-22
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 2019-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多