2017-09-06 103 views
0

我在Python中使用Gurobi。我正在迭代一组节点,并且在每次迭代时,我都添加了一个约束来解决。解决后,它会产生Gurobi日志如下:将模型信息添加到Gurobi日志中

Optimize a model with 6 rows, 36 columns and 41 nonzeros 
Variable types: 0 continuous, 36 integer (36 binary) 
Coefficient statistics: 
    Matrix range  [1e+00, 1e+00] 
    Objective range [2e+01, 9e+01] 
    Bounds range  [1e+00, 1e+00] 
    RHS range  [2e+00, 2e+00] 

MIP start did not produce a new incumbent solution 
MIP start violates constraint R5 by 2.000000000 

Found heuristic solution: objective 347.281 
Presolve removed 2 rows and 21 columns 
Presolve time: 0.00s 
Presolved: 4 rows, 15 columns, 27 nonzeros 
Found heuristic solution: objective 336.2791955 
Variable types: 0 continuous, 15 integer (15 binary) 

Root relaxation: objective 3.043757e+02, 6 iterations, 0.00 seconds 

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

* 0  0    0  304.3757488 304.37575 0.00%  - 0s 

Explored 0 nodes (6 simplex iterations) in 0.02 seconds 
Thread count was 4 (of 4 available processors) 

Solution count 3: 304.376 336.279 339.43 

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

但是经过一定的迭代,我的答案不是我所期待的。因此,我希望在每次迭代时在Gurobi日志中打印我的所有模型细节(目标函数,约束条件等),我该怎么做?

但model.write()打印我们已编码的目标函数和约束。

Minimize 
    0 x(0,0) + 75.47184905645283 x(0,1) + 57.55866572463264 x(0,2) 
    + 33.97057550292606 x(0,3) + 23.3238075793812 x(0,4) 
    + 40.80441152620633 x(0,5) + 75.47184905645283 x(1,0) + 0 x(1,1) 
    + 32.7566787083184 x(1,2) + 90.60905032059435 x(1,3) 
    + 55.71355310873648 x(1,4) + 40.60788100849391 x(1,5) 
    + 57.55866572463264 x(2,0) + 32.7566787083184 x(2,1) + 0 x(2,2) 
    + 83.36066218546971 x(2,3) + 46.57252408878007 x(2,4) 
    + 41.4004830889689 x(2,5) + 33.97057550292606 x(3,0) 
    + 90.60905032059435 x(3,1) + 83.36066218546971 x(3,2) + 0 x(3,3) 
    + 37.12142238654117 x(3,4) + 50.00999900019995 x(3,5) 
    + 23.3238075793812 x(4,0) + 55.71355310873648 x(4,1) 
    + 46.57252408878007 x(4,2) + 37.12142238654117 x(4,3) + 0 x(4,4) 
    + 17.69180601295413 x(4,5) + 40.80441152620633 x(5,0) 
    + 40.60788100849391 x(5,1) + 41.4004830889689 x(5,2) 
    + 50.00999900019995 x(5,3) + 17.69180601295413 x(5,4) + 0 x(5,5) 
Subject To 
R0: x(0,1) + x(0,2) + x(0,3) + x(0,4) + x(0,5) >= 2 
R1: x(1,0) + x(1,2) + x(1,3) + x(1,4) + x(1,5) >= 2 
R2: x(1,0) + x(1,3) + x(1,4) + x(2,0) + x(2,3) + x(2,4) + x(5,0) + 
    x(5,3)+ x(5,4) >= 2 
R3: x(3,0) + x(3,1) + x(3,2) + x(3,4) + x(3,5) >= 2 
R4: x(0,1) + x(0,2) + x(0,5) + x(3,1) + x(3,2) + x(3,5) + x(4,1) + 
    x(4,2)+ x(4,5) >= 2 
R5: x(0,1) + x(0,2) + x(3,1) + x(3,2) + x(4,1) + x(4,2) + x(5,1) + 
    x(5,2)>= 2 
Bounds 
Binaries 
x(0,0) x(0,1) x(0,2) x(0,3) x(0,4) x(0,5) x(1,0) x(1,1) x(1,2) x(1,3) 
x(1,4) x(1,5) x(2,0) x(2,1) x(2,2) x(2,3) x(2,4) x(2,5) x(3,0) x(3,1) 
x(3,2) x(3,3) x(3,4) x(3,5) x(4,0) x(4,1) x(4,2) x(4,3) x(4,4) x(4,5) 
x(5,0) x(5,1) x(5,2) x(5,3) x(5,4) x(5,5) 
End 

我需要在这知道什么是在每次迭代发生。这是因为一次迭代给了我另一个错误的答案,所以我想在求解时检查是否有任何冗余约束添加到模型中。

换句话说,“Gurobi callbacks”是否允许我们访问模型中可用的所有信息?它会产生什么?

+0

[print constraints Gurobi Python]可能重复(https://stackoverflow.com/questions/45992765/print-constraints-gurobi-python) –

回答

0

换句话说,“Gurobi回调”是否允许我们访问模型中可用的所有 信息?它会产生什么?

不,您不能打印回调函数中生成的约束。

最有可能的问题是下列之一:

  1. 您呼叫的回调内部错误的函数。您可以添加两种约束:懒惰约束和用户剪切。延迟约束对结构是必需的;一个解决方案必须满足所有懒惰的约束。但是,如果数量太多而无法添加到模型中,则会使用延迟约束,并且只希望添加违反约束的约束。用户裁减不是必需的,但它们可以帮助删除小数解,并收紧MIP的LP松弛。就你而言,这听起来像你有懒惰的限制。

  2. 您并未添加所有违反的懒惰约束条件。正如in the documentation所述:“您的回拨应准备切断违反您的懒惰限制的任何 的解决方案,包括已添加的 。”你不应该追踪你是否已经添加了一个懒惰的约束;您必须在每次看到违规时添加它。这是由于Gurobi解算器的并行处理。

相关问题