I would like to see the current results (e.g. MIP gaps) of a solver in pyomo (as in GAMS) while solving the problem. I read that I should use the option "stream-solver" (see for instance here https://static1.squarespace.com/static/5492d7f4e4b00040889988bd/t/57bd0f93d482e927298cc9da/1472008085561/3_PyomoFundamentals.pdf). But I do not know how to apply it. When I put it as a parameter I get an error message. So here you can see what I tried:

opt = pyo.SolverFactory('glpk')
results = opt.solve(model, stream-solver=true)

Do you know how I can do that?

  • $\begingroup$ are you working in a notebook? $\endgroup$ Commented Feb 23, 2021 at 16:23
  • 2
    $\begingroup$ try tee=True as a parameter for solve. $\endgroup$ Commented Feb 23, 2021 at 16:25
  • $\begingroup$ Thanks Oguz for your answer. Yes, basically it works. Do you know how I can specify the MIP gap? Further, do you know if the difference between the glpk solver and CPLEX is so extremely high that CPLEX solves my modle (written in GAMS) within 0,00 seconds with a gap of 0,1% whereas the GLPK-solver still has a MIP-Gap of 1,5 % after 10 minutes. $\endgroup$
    – PeterBe
    Commented Feb 23, 2021 at 16:37
  • $\begingroup$ @Steven01123581321: I use Spyder for Python and not Jupyter Notebook $\endgroup$
    – PeterBe
    Commented Feb 23, 2021 at 16:38
  • 1
    $\begingroup$ @OguzToragay: Thanks for your comment. Just for your information: I checked the pyomo model and it is correct and identical to the GAMS model that I solve with CPLEX. In fact the difference between the two solvers can be extremely high. For one model configuration they have almost the same solving time (1 second) and yield the same result. However, for my specific model configuration (more realistic one) CPLEX solves it in under 1 second (mip-gap of 0.1 %) while the GLPK solver still has a MIP-Gap of 1,7 % after 20 minutes $\endgroup$
    – PeterBe
    Commented Feb 24, 2021 at 8:14

1 Answer 1


You can use tee = True as a parameter for .solve in Pyomo. Moreover, to access the optimality gap you can use the following code in Pyomo:

msolver = SolverFactory('glpk')
solution = msolver.solve(m, tee=True)
data = solution.Problem._list

Then you have a list of detailed information about the problem's solution. For instance

LB = data[0].lower_bound
UB = data[0].upper_bound

will give you the LB and UB from which you can calculate the gap.

Edit: Try model.solve(GLPK(options=['--mipgap', '0.02'])) to change the gap value for the solver. Or try msolver.options['mipgap'] = 0.02.

  • $\begingroup$ Thanks Oguz for your answer. How can I set the MIP gap of the solver meaning e.g. that the solver should stop after having a MIP gap of 1 %? $\endgroup$
    – PeterBe
    Commented Feb 23, 2021 at 17:36
  • $\begingroup$ It depends on the solver, if you want to set any options or attribute or parameter for the solver you can do it in pyomo by: solver.options['MIPGap'] = 0.01 (my solver is Gurobi) $\endgroup$ Commented Feb 23, 2021 at 17:41
  • $\begingroup$ Thanks Oguz for your answer and effort. I really appreciate it. So I used the following code (; indicating a new line, which is somehow not possible in StackExchange comment): "opt = pyo.SolverFactory('glpk'); opt.options['MIPGap'] = 0.02 ; results = opt.solve(model, tee=True)"; and I get an error message " "Solver (%s) did not exit normally" % self.name) ApplicationError: Solver (glpk) did not exit normally" $\endgroup$
    – PeterBe
    Commented Feb 23, 2021 at 17:44
  • $\begingroup$ @PeterBe The answer is edited. $\endgroup$ Commented Feb 23, 2021 at 18:01
  • 1
    $\begingroup$ Thanks Oguz for your answer. It works. I accepted and upvoted your answer (and I also added a comment above about the difference between the GLPK and CPLEX for my model configuration) $\endgroup$
    – PeterBe
    Commented Feb 24, 2021 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.