PULP - 如何获取 CPLEX 求解器状态而不是 LpStatus 状态?

PULP - How to get the CPLEX solver status instead of the LpStatus stauts?

我在 Python 中通过 PULP 使用 CPLEX 求解器。当我解决有时间限制的问题时,CPLEX 会在屏幕上打印代码 107 which means "Time limit exceeded, but integer solution exists". However, if I print the status of pulp.LpStatus[problem.status] what I get back is the value 1 which according to pulp's documentation 表示已找到最佳解决方案,这实际上是错误的。

如何访问 CPLEX 状态代码而不是 PULP 的?

您可以直接访问 CPLEX 状态代码和状态字符串。考虑以下示例:

>>> import pulp 
>>> prob = pulp.LpProblem("example", pulp.LpMinimize)
>>> x = pulp.LpVariable('x', lowBound=0, upBound=1)
>>> prob+= x <= -1
  • 示例 1 - 超出时间限制

    >>> solver = pulp.CPLEX_PY(msg=0, timeLimit=0)
    >>> prob.setSolver(solver)
    >>> prob.solve()
    -3
    >>> solver.solverModel.solution.get_status()
    108
    >>> solver.solverModel.solution.get_status_string()
    'time limit exceeded, no integer solution'
    
  • 示例 2 - 不可行

    >>> solver = pulp.CPLEX_PY(msg=0)
    >>> prob.setSolver(solver)
    >>> prob.solve()
    -1
    >>> solver.solverModel.solution.get_status()
    103
    >>> solver.solverModel.solution.get_status_string()
    'integer infeasible'