メインコンテンツへスキップ

Using the VarHint in a pyomo model

回答済み

コメント

4件のコメント

  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    Hi Anibal,

    Setting variable attributes can be done with Gurobi persistent solver, see GurobiPersistent — Pyomo 6.7.1 documentation

    Here is a small Python example

    import pyomo.environ as pyo

    # Define your Pyomo model here
    model = pyo.ConcreteModel()
    model.u = pyo.Var(domain=pyo.Binary)

    solver = pyo.SolverFactory('gurobi_persistent', solver_io='python')
    solver.set_instance(model)

    # set attributes
    solver.set_var_attr(model.u, 'VarHintVal', 1)
    solver.set_var_attr(model.u, 'VarHintPri', 1)

    solution = solver.solve(tee=True)

    Best regards,
    Marika

    0
  • Anibal Baradei
    First Comment
    First Question

    I can't thank you enough Ms. Karbstein. I will try to implement this in my code and see how it goes.

    Thank you very much again for your time.

    Best regards,

    Anibal.

    0
  • Anibal Baradei
    First Comment
    First Question

    Dear Dr. Karbstein,

    I implemented the code you showed into a simplified model with only 1 binary variable to test. The new lines of code are as follows:

    opt = pyo.SolverFactory('gurobi_persistent', solver_io='python')

    opt.set_instance(model)

    opt.set_var_attr(model.u[13], 'VarHintVal', 0)
    opt.set_var_attr(model.u[13], 'VarHintPri', 1)

    results = opt.solve(tee=True)

    The variable u in this case is unique (subindex 13), but in a bigger model it could have more (e.g. u[2] and/or u[22], etc.), I don't know if this is relevant or not but I'll just clarify it.

    The model ran successfully, only a little bit slower than when I used the old 'gurobi' solver (again I'm not an expert so I don't know if this is expected or was just a coincidence or maybe the PC was running other things at that time that made it a bit slower). I checked the output log to see if there was any indication of the "hint" being used but could not find anything, could you perhaps take a quick look? (I cut some parts for space sake)

    Gurobi Optimizer version 10.0.0 build v10.0.0rc2 (win64)

    CPU model: 12th Gen Intel(R) Core(TM) i7-12700H, instruction set [SSE2|AVX|AVX2]
    Thread count: 14 physical cores, 20 logical processors, using up to 20 threads

    Optimize a model with 9785307 rows, 7182378 columns and 18223899 nonzeros
    Model fingerprint: 0xe4d718c7
    Variable types: 7182377 continuous, 1 integer (1 binary)
    Coefficient statistics:
      Matrix range     [3e-06, 4e+05]
      Objective range  [1e-02, 2e+08]
      Bounds range     [1e+00, 2e+07]
      RHS range        [5e-01, 2e+07]
    Presolve removed 7451234 rows and 5823584 columns (presolve time = 5s) ...
    Presolve removed 7868940 rows and 6043202 columns (presolve time = 35s) ...
    Presolve removed 7868942 rows and 6043204 columns
    Presolve time: 38.13s
    Presolved: 1916365 rows, 1139174 columns, 5637099 nonzeros
    Variable types: 1139173 continuous, 1 integer (1 binary)

    Deterministic concurrent LP optimizer: primal simplex, dual simplex, and barrier
    Showing barrier log only...

    Root barrier log...

    Elapsed ordering time = 5s
    Ordering time: 12.30s

    Barrier statistics:
     Dense cols : 265
     AA' NZ     : 7.643e+06
     Factor NZ  : 6.448e+07 (roughly 1.8 GB of memory)
     Factor Ops : 1.892e+10 (less than 1 second per iteration)
     Threads    : 12

                      Objective                Residual
    Iter       Primal          Dual         Primal    Dual     Compl     Time
       0   1.51278428e+14 -8.29180876e+16  3.57e+06 1.34e+05  2.75e+11    65s
       1   1.44522645e+14 -7.35841733e+16  3.18e+06 4.69e+05  2.33e+11    66s
     137   1.36015705e+10  1.36015704e+10  2.19e-06 1.80e-08  3.01e-05   299s

    Barrier solved model in 137 iterations and 298.75 seconds (256.08 work units)
    Optimal objective 1.36015705e+10


    Root crossover log...

      404720 DPushes remaining with DInf 4.6509246e-03               300s
           0 DPushes remaining with DInf 0.0000000e+00               606s

      197689 PPushes remaining with PInf 4.1970535e-01               607s
           0 PPushes remaining with PInf 0.0000000e+00               650s

      Push phase complete: Pinf 0.0000000e+00, Dinf 3.5714695e+05    650s


    Root simplex log...

    Iteration    Objective       Primal Inf.    Dual Inf.      Time
      448645    1.3601570e+10   0.000000e+00   3.571470e+05    651s
      449205    1.3601570e+10   0.000000e+00   3.651955e+01    655s
      449317    1.3601570e+10   0.000000e+00   0.000000e+00    657s
      449317    1.3601570e+10   0.000000e+00   0.000000e+00    658s
    Concurrent spin time: 111.69s (can be avoided by choosing Method=3)

    Solved with barrier

    Root relaxation: objective 1.360157e+10, 449317 iterations, 724.37 seconds (1221.31 work units)

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

         0     0 1.3602e+10    0    1          - 1.3602e+10      -     -  770s
         0     0 1.3602e+10    0    1          - 1.3602e+10      -     -  773s
         0     2 1.3602e+10    0    1          - 1.3602e+10      -     -  776s
    *    1     0               1    1.360243e+10 1.3602e+10  0.01% 145225 2825s

    Explored 3 nodes (871122 simplex iterations) in 2827.10 seconds (8104.98 work units)
    Thread count was 20 (of 20 available processors)

    Solution count 2: 1.36024e+10 1.36024e+10

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

    The final time calculated using python was 3133s.

    The model is expected to get even bigger so any other comments or recommendations to tweak on the solver side are more than welcome.

    0
  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    Hi Anibal,

    Unfortunately, there is no output for the variable hint with Gurobi version 10. But if you can upgrade to version 11.0, you will see \(\texttt{Using variable hints}\) in the log output.

    I am not sure about your idea of using a variable hint. Your current model only has one binary variable and probably does not benefit from variable hints. You should check for your bigger models whether there is an advantage when using variable hints. If you know values for variables that imply a feasible solution, using MIP start could be a better option, see How do I use MIP starts?

    You could also test whether a different parameter setting improves performance. For some guidance, see Parameter Guidelines, and especially the section MIP Models.

    Best regards,
    Marika

    0

サインインしてコメントを残してください。