【问题标题】:How to deal with the error when using Gurobi with cvxpy :Unable to retrieve attribute 'BarIterCount'cvxpy 使用 Gurobi 报错的处理方法:Unable to retrieve attribute 'BarIterCount'
【发布时间】:2020-12-16 12:47:39
【问题描述】:

Gurobi with cvxpy 报错如何处理:AttributeError: Unable to retrieve attribute 'BarIterCount'。

我有一个整数编程问题,使用 cvxpy 并将 gurobi 设置为求解器。 当变量数量较少时,结果还可以。变量个数达到43*13*6 这样的级别后,就发生了错误。我想这可能是由问题的规模引起的,其中 gurobi 求解器无法估计 BarIterCount,这是所需的最大迭代次数。

那么请问有没有办法通过CVX的界面手动设置gurobi的BarItercount属性呢?或者是否有其他方法可以解决这个问题?

感谢您为我提供的任何建议。

trace日志如下:

如果我的模型很小,比如我设置了一个表示模型比例的数字为3,那么程序就可以了。跟踪是:

Using license file D:\software\lib\site-packages\gurobipy\gurobi.lic
Restricted license - for non-production use only - expires 2022-01-13
Parameter OutputFlag unchanged
   Value: 1  Min: 0  Max: 1  Default: 1
D:\software\lib\site-packages\cvxpy\reductions\solvers\solving_chain.py:326: DeprecationWarning: Deprecated, use Model.addMConstr() instead
  solver_opts, problem._solver_cache)
Changed value of parameter QCPDual to 1
   Prev: 0  Min: 0  Max: 1  Default: 0
Gurobi Optimizer version 9.1.0 build v9.1.0rc0 (win64)
Thread count: 16 physical cores, 32 logical processors, using up to 32 threads
Optimize a model with 126 rows, 370 columns and 2689 nonzeros
Model fingerprint: 0x70d49530
Variable types: 0 continuous, 370 integer (369 binary)
Coefficient statistics:
  Matrix range     [1e+00, 7e+00]
  Objective range  [1e+00, 1e+00]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 6e+00]
Found heuristic solution: objective 7.0000000
Presolve removed 4 rows and 90 columns
Presolve time: 0.01s
Presolved: 122 rows, 280 columns, 1882 nonzeros
Variable types: 0 continuous, 280 integer (279 binary)

Root relaxation: objective 4.307692e+00, 216 iterations, 0.00 seconds

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0    4.30769    0   49    7.00000    4.30769  38.5%     -    0s
H    0     0                       6.0000000    4.30769  28.2%     -    0s
     0     0    5.00000    0   35    6.00000    5.00000  16.7%     -    0s
     0     0    5.00000    0   37    6.00000    5.00000  16.7%     -    0s
     0     0    5.00000    0    7    6.00000    5.00000  16.7%     -    0s

Cutting planes:
  Gomory: 4
  Cover: 9
  MIR: 4
  StrongCG: 1
  GUB cover: 9
  Zero half: 1
  RLT: 1

Explored 1 nodes (849 simplex iterations) in 0.12 seconds
Thread count was 32 (of 32 available processors)

Solution count 2: 6 7

Optimal solution found (tolerance 1.00e-04)
Best objective 6.000000000000e+00, best bound 6.000000000000e+00, gap 0.0000%

如果数字为6,则发生错误:

-------------------------------------------------------
Using license file D:\software\lib\site-packages\gurobipy\gurobi.lic
Restricted license - for non-production use only - expires 2022-01-13
Parameter OutputFlag unchanged
   Value: 1  Min: 0  Max: 1  Default: 1
D:\software\lib\site-packages\cvxpy\reductions\solvers\solving_chain.py:326: DeprecationWarning: Deprecated, use Model.addMConstr() instead
  solver_opts, problem._solver_cache)
Changed value of parameter QCPDual to 1
   Prev: 0  Min: 0  Max: 1  Default: 0
Gurobi Optimizer version 9.1.0 build v9.1.0rc0 (win64)
Thread count: 16 physical cores, 32 logical processors, using up to 32 threads
Traceback (most recent call last):
  File "model.py", line 274, in <module>
    problem.solve(solver=cp.GUROBI,verbose=True)
  File "D:\software\lib\site-packages\cvxpy\problems\problem.py", line 396, in solve
    return solve_func(self, *args, **kwargs)
  File "D:\software\lib\site-packages\cvxpy\problems\problem.py", line 754, in _solve
    self.unpack_results(solution, solving_chain, inverse_data)
  File "D:\software\lib\site-packages\cvxpy\problems\problem.py", line 1058, in unpack_results
    solution = chain.invert(solution, inverse_data)
  File "D:\software\lib\site-packages\cvxpy\reductions\chain.py", line 79, in invert
    solution = r.invert(solution, inv)
  File "D:\software\lib\site-packages\cvxpy\reductions\solvers\qp_solvers\gurobi_qpif.py", line 59, in invert
    s.NUM_ITERS:  model.BarIterCount,
  File "src\gurobipy\model.pxi", line 343, in gurobipy.gurobipy.Model.__getattr__
  File "src\gurobipy\model.pxi", line 1842, in gurobipy.gurobipy.Model.getAttr
  File "src\gurobipy\attrutil.pxi", line 100, in gurobipy.gurobipy.__getattr
AttributeError: Unable to retrieve attribute 'BarIterCount'

希望这可以为解决方案提供更多提示。

【问题讨论】:

  • 对于类型错误非常抱歉。上面的“43136”应该是43x13x6。希望它没有误导。

标签: gurobi cvxpy mixed-integer-programming


【解决方案1】:

BarIterCount 是求解 LP 所执行的障碍迭代次数。这不是对迭代次数的限制,只有在当前优化过程完成时才应查询。当然,你也不能设置这个属性。

要实际限制屏障算法允许的迭代次数,您可以使用参数BarIterLimit

请检查您的日志文件以获取有关求解器行为的更多信息。

【讨论】:

  • 你好,马特米尔滕。错误如下: s.NUM_ITERS: model.BarIterCount, File "src\gurobipy\model.pxi", line 343, in gurobipy.gurobipy.Model.getattr File "src\gurobipy\model.pxi", line 1842,在 gurobipy.gurobipy.Model.getAttr 文件“src\gurobipy\attrutil.pxi”中,第 100 行,在 gurobipy.gurobipy.__getattr AttributeError:无法检索属性“BarIterCount”。希望这可以为解决方案提供一些帮助。请注意,我通过 cvxpy 的接口使用 gurobi。
  • 请在日志文件中检查查询障碍迭代次数是否有意义(是否使用障碍算法?求解器是否成功终止?)。您还应该考虑直接使用 Gurobi,即不使用 cvx,以更好地控制求解器。
  • 我已经更新了上面的问题。希望这可以提供有关我的问题的更多详细信息。如果这仍然无法解决,我会考虑您的建议,直接使用 Gurobi 重新构建模型,而不使用 cvx。非常感谢。
  • 好吧,看来,您甚至还没有解决任何问题。所以没有任何要查询的 Barrier 迭代。
  • 我在上面放了更多信息,包括模型规模较小时的详细输出。我想知道,是不是因为模型太大了,然后gurobi知道自己解决不了,打破流程?
猜你喜欢
  • 2017-02-03
  • 1970-01-01
  • 2022-12-27
  • 2021-12-27
  • 2023-03-24
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多