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

APPSI: disable sending output to logger by default #3521

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pyomo/contrib/appsi/solvers/cbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(
self.executable = Executable('cbc')
self.filename = None
self.keepfiles = False
self.solver_output_logger = logger
self.solver_output_logger = None
self.log_level = logging.INFO


Expand Down Expand Up @@ -389,11 +389,13 @@ def _check_and_escape_options():
cmd.extend(['-solve'])
cmd.extend(['-solu', self._filename + '.soln'])

ostreams = [
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
ostreams = []
if self.config.solver_output_logger is not None:
ostreams.append(
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
)
)
]
if self.config.stream_solver:
ostreams.append(sys.stdout)

Expand Down
29 changes: 19 additions & 10 deletions pyomo/contrib/appsi/solvers/cplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(

self.filename = None
self.keepfiles = False
self.solver_output_logger = logger
self.solver_output_logger = None
self.log_level = logging.INFO


Expand Down Expand Up @@ -253,18 +253,27 @@ def _apply_solver(self, timer: HierarchicalTimer):
cplex_model.read(self._filename + '.lp')
timer.stop('cplex read lp')

log_stream = LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
)
if config.stream_solver:
if config.solver_output_logger is not None:
log_stream = LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
)
else:
log_stream = None

def _process_stream(arg):
sys.stdout.write(arg)
return arg
def _process_stream(arg):
sys.stdout.write(arg)
return arg

cplex_model.set_results_stream(log_stream, _process_stream)
if config.stream_solver:
if log_stream is not None:
cplex_model.set_results_stream(log_stream, _process_stream)
else:
cplex_model.set_results_stream(sys.stdout)
else:
cplex_model.set_results_stream(log_stream)
if log_stream is not None:
cplex_model.set_results_stream(log_stream)
else:
cplex_model.set_results_stream(None)

for key, option in self.cplex_options.items():
opt_cmd = cplex_model.parameters
Expand Down
12 changes: 7 additions & 5 deletions pyomo/contrib/appsi/solvers/gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(
self.declare('log_level', ConfigValue(domain=NonNegativeInt))

self.logfile = ''
self.solver_output_logger = logger
self.solver_output_logger = None
self.log_level = logging.INFO


Expand Down Expand Up @@ -359,11 +359,13 @@ def symbol_map(self):
return self._symbol_map

def _solve(self, timer: HierarchicalTimer):
ostreams = [
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
ostreams = []
if self.config.solver_output_logger is not None:
ostreams.append(
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
)
)
]
if self.config.stream_solver:
ostreams.append(sys.stdout)

Expand Down
13 changes: 7 additions & 6 deletions pyomo/contrib/appsi/solvers/highs.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
self.declare('log_level', ConfigValue(domain=NonNegativeInt))

self.logfile = ''
self.solver_output_logger = logger
self.solver_output_logger = None
self.log_level = logging.INFO


Expand Down Expand Up @@ -248,11 +248,13 @@ def _solve(self, timer: HierarchicalTimer):
config = self.config
options = self.highs_options

ostreams = [
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
ostreams = []
if config.solver_output_logger is not None:
ostreams.append(
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
)
)
]
if self.config.stream_solver:
ostreams.append(sys.stdout)

Expand All @@ -272,7 +274,6 @@ def _solve(self, timer: HierarchicalTimer):
if config.warmstart:
self._warm_start()
timer.start('optimize')
ostreams[-1].write("RUN!\n")
if self.version()[:2] >= (1, 8):
self._solver_model.HandleKeyboardInterrupt = True
self._solver_model.run()
Expand Down
12 changes: 7 additions & 5 deletions pyomo/contrib/appsi/solvers/ipopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(
self.executable = Executable('ipopt')
self.filename = None
self.keepfiles = False
self.solver_output_logger = logger
self.solver_output_logger = None
self.log_level = logging.INFO


Expand Down Expand Up @@ -440,11 +440,13 @@ def _apply_solver(self, timer: HierarchicalTimer):
else:
timeout = None

ostreams = [
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
ostreams = []
if self.config.solver_output_logger is not None:
ostreams.append(
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
)
)
]
if self.config.stream_solver:
ostreams.append(sys.stdout)

Expand Down
12 changes: 7 additions & 5 deletions pyomo/contrib/appsi/solvers/maingo.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __init__(
),
)
self.declare("logfile", ConfigValue(domain=str, default=""))
self.declare("solver_output_logger", ConfigValue(default=logger))
self.declare("solver_output_logger", ConfigValue(default=None))
self.declare(
"log_level", ConfigValue(domain=NonNegativeInt, default=logging.INFO)
)
Expand Down Expand Up @@ -209,11 +209,13 @@ def symbol_map(self):
return self._symbol_map

def _solve(self, timer: HierarchicalTimer):
ostreams = [
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
ostreams = []
if self.config.solver_output_logger is not None:
ostreams.append(
LogStream(
level=self.config.log_level, logger=self.config.solver_output_logger
)
)
]
if self.config.stream_solver:
ostreams.append(sys.stdout)

Expand Down
Loading