How to deal with the error when using Gurobi with cvxpy :Unable to retrieve attribute 'BarIterCount'

712 Views Asked by At

How to deal with the error when using Gurobi with cvxpy :AttributeError: Unable to retrieve attribute 'BarIterCount'.

I have an Integer programming problem, using cvxpy and set gurobi as a solver. When the number of variables is small, the result is ok. After the number of variables reaches a level of like 43*13*6, then the error occurred. I suppose it may be caused by the scale of the problem, in which the gurobi solver can not estimate the BarIterCount, which is the max Iterations needed.

Thus, I wonder, is there any way to manually set the BarItercount attribute of gurobi through the interface of the CVX? Or whether there exists another way to solve this problem?

Thanks for any suggestions you may provide for me.

The trace log is as follows:

If my model is small, like I set a number which indicates the scale of model as 3, then the program is ok. The trace is :

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%

If the number is 6, then error occurs:

-------------------------------------------------------
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'

Hopefully this can provide more hint for solution.

1

There are 1 best solutions below

5
On

BarIterCount is the number of barrier iterations performed to solve an LP. This is not a limit on the number of iterations and it should only be queried when the current optimization process has been finished. You cannot set this attribute either, of course.

To actually limit the number of iterations the barrier algorithm is allowed to take, you can use the parameter BarIterLimit.

Please inspect your log file for further information about the solver's behavior.