# How to display results from a solver during solving in Pyomo

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?

• are you working in a notebook? Feb 23, 2021 at 16:23
• try tee=True as a parameter for solve. Feb 23, 2021 at 16:25
• 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. Feb 23, 2021 at 16:37
• @Steven01123581321: I use Spyder for Python and not Jupyter Notebook Feb 23, 2021 at 16:38
• @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 Feb 24, 2021 at 8:14

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.

• 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 %? Feb 23, 2021 at 17:36
• 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) Feb 23, 2021 at 17:41
• 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" Feb 23, 2021 at 17:44
• @PeterBe The answer is edited. Feb 23, 2021 at 18:01
• 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) Feb 24, 2021 at 8:16