How to make the IPOPT solver silent in with Opti in Python

3,493 views
Skip to first unread message

Shima Nazari

unread,
Sep 10, 2020, 8:13:55 PM9/10/20
to CasADi
Hi, 
I am using casadi.Opti in python with IPOPT solver for an MPC problem. 
I am using the following piece of code to assign a solver and change printing level:

opts = dict(print_time=False, verbose=False)
opti.solver("ipopt",opts)

However, still the solver prints too much information about its iterations. When I try print_iteration=False, or print_iter=False, I get an error of "unknown option". 
Does any body know how to make the solver completely silent in python?
Thank you
Shima

Aaron Ray

unread,
Sep 11, 2020, 1:33:54 PM9/11/20
to CasADi
I believe you need something like:

p_opts = dict(print_time=False, verbose=False)
s_opts = dict(print_level=0)
opti.solver("ipopt", p_opts, s_opts)

The two sets of options are discussed here: https://web.casadi.org/docs/#problem-specification
(I've only worked with the Casadi NlpSolver interfance and not Opti, but I think the ipopt options are the same).

Chris Mower

unread,
Sep 14, 2020, 6:53:13 AM9/14/20
to CasADi
Hi,

To completely silence the solver you can set 

opts = {'ipopt.print_level': 0, 'print_time': 0, 'ipopt.sb': 'yes'}
opti.solver('ipopt', opts)

However, I find having access to the solver output is useful when debugging and instead I re-direct the stdout to a file before calling opti.solve(). You can do this in the following way:

import sys
import traceback
import casadi as ca

opti = ca.Opti()

# Setup opti problem ...

with open('solver_output.txt', 'w') as output_file:

    # Retrieve default stdout
    stdout_old = sys.stdout

    # Re-direct standard output
    sys.stdout = output_file

    try:
        opti.solve()
     except:
         # If error thrown -> print exception to old stdout
         traceback.print_exc(file=stdout_old)
     finally:
         # Re-direct stdout to default
         sys.stdout = stdout_old

Hope this helps!

Best,

Chris
Reply all
Reply to author
Forward
0 new messages