使用 CPLEX 在 python 中的 pulp 中使用时间限制是否存在错误?

Is there a bug with using timelimit within pulp in python using CPLEX?

我目前正在写我的论文,我想在其中实现一种算法,以使用大型 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'.

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

该代码显示了 pulp 中使用的当前逻辑,您需要查看 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