在使用CPLEX的python中,在纸浆中使用timelimit是否存在错误?

2024-05-15 12:51:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如果您需要该模型来回答我的问题或更好地理解我的问题,我可以将其包括在内

当我以一定的时间限制(本例中为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'.

Tags: andto模型fortimeonsecsolver
1条回答
网友
1楼 · 发布于 2024-05-15 12:51:09

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

代码显示了当前在纸浆中使用的逻辑,您必须检查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

相关问题 更多 >