Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirection of solver output is no longer needed #1575

Open
ksbeattie opened this issue Feb 20, 2025 · 1 comment
Open

Redirection of solver output is no longer needed #1575

ksbeattie opened this issue Feb 20, 2025 · 1 comment
Assignees
Labels
Priority:Normal Normal Priority Issue or PR

Comments

@ksbeattie
Copy link
Member

This functionality is no longer needed within idaes-pse, as Pyomo can now do this.

@ksbeattie ksbeattie added the Priority:Normal Normal Priority Issue or PR label Feb 20, 2025
@mrmundt
Copy link
Contributor

mrmundt commented Feb 20, 2025

Longer details:

In the new solver interfaces, we finally introduced a handy-dandy capability to not just turn tee on or off in the config:


        self.tee: List[TextIO] = self.declare(
            'tee',
            ConfigValue(
                domain=TextIO_or_Logger,
                default=False,
                description="""``tee`` accepts :py:class:`bool`,
                :py:class:`io.TextIOBase`, or :py:class:`logging.Logger`
                (or a list of these types).  ``True`` is mapped to
                ``sys.stdout``.  The solver log will be printed to each of
                these streams / destinations.""",
            ),
        )

It can now be passed a bool, an io.TextIOBase-type-object, or a logger. For example:

import pyomo.environ as pyo
import io, logging

log_stream = io.StringIO()
logging.basicConfig(stream=log_stream, level=logging.INFO)
logger = logging.getLogger('contrib.solver.config.test.1')

m = pyo.ConcreteModel()
m.x = pyo.Var([1, 2], initialize=1, bounds=(0, None))
m.eq = pyo.Constraint(expr=m.x[1] * m.x[2] ** 1.5 == 3)
m.obj = pyo.Objective(expr=m.x[1] ** 2 + m.x[2] ** 2)

solver = pyo.SolverFactory("ipopt_v2")
solver.solve(m, tee=logger, timelimit=5)

log_contents = log_stream.getvalue()
print(log_contents)

The output from solver.solve gets added to the logger (INFO level).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority:Normal Normal Priority Issue or PR
Projects
None yet
Development

No branches or pull requests

2 participants