Skip to content

Allow disabling print()s #7

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions pyaugmecon/helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time
from multiprocessing import Lock, Value

from pyaugmecon.options import Options


class Helper:
"""A class of helper functions."""
Expand Down Expand Up @@ -55,11 +57,12 @@ def get(self):
class ProgressBar:
"""A class for displaying a progress bar."""

def __init__(self, counter: Counter, total: int, init_message: str = ""):
def __init__(self, counter: Counter, total: int, opts: Options):
"""Initializes the progress bar with a counter and a total number of iterations."""
self.opts = opts
self.counter = counter
self.total = total
self.message = init_message
self.message = ""
self.bar = ""

def set_message(self, message):
Expand All @@ -70,6 +73,9 @@ def set_message(self, message):

def print(self, force):
"""Prints the progress bar."""
if self.opts.disable_output:
return

bar_len = 40

progress = self.counter.value() / float(self.total)
Expand Down
2 changes: 1 addition & 1 deletion pyaugmecon/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, model: pyo.ConcreteModel, opts: Options):

# Setup progress bar
self.to_solve = opts.gp ** (self.n_obj - 1) + self.n_obj**2
self.progress = ProgressBar(Counter(), self.to_solve)
self.progress = ProgressBar(Counter(), self.to_solve, opts)

self.models_solved = Counter()
self.infeasibilities = Counter()
Expand Down
1 change: 1 addition & 0 deletions pyaugmecon/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, opts: dict, solver_opts: dict):
self.output_excel = opts.get("output_excel", True) # Whether to output to Excel
self.process_logging = opts.get("process_logging", False) # Whether to enable process logging
self.process_timeout = opts.get("process_timeout", None) # Timeout for processes
self.disable_output = opts.get("disable_output", False) # Whether to disable printing to console
self.solver_name = opts.get("solver_name", "gurobi") # Name of solver
self.solver_io = opts.get("solver_io", "python") # IO mode of solver

Expand Down
15 changes: 8 additions & 7 deletions pyaugmecon/pyaugmecon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


class PyAugmecon:
def __init__(self, model: PyomoModel, opts: Options, solver_opts={}):
def __init__(self, model: PyomoModel, opts: dict, solver_opts: dict = {}):
"""
Initialize a PyAugmecon object.

Expand Down Expand Up @@ -222,12 +222,13 @@ def solve(self):

# Compute the total runtime and print a summary of the results
self.runtime = round(self.runtime.get(), 2)
Helper.clear_line()
print(
f"Solved {self.model.models_solved.value()} models for "
f"{self.num_unique_pareto_sols} unique Pareto solutions in "
f"{self.runtime} seconds"
)
if not self.opts.disable_output:
Helper.clear_line()
print(
f"Solved {self.model.models_solved.value()} models for "
f"{self.num_unique_pareto_sols} unique Pareto solutions in "
f"{self.runtime} seconds"
)

# Log a summary of the results
self.logger.info(Helper.separator())
Expand Down