Skip to content

Commit

Permalink
allow setting stdout=False in solver to disable stdout even for large…
Browse files Browse the repository at this point in the history
…r models
  • Loading branch information
maurerle committed Nov 7, 2024
1 parent f785046 commit 303585a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
24 changes: 16 additions & 8 deletions linopy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,13 @@ def integers_to_file(
f.writelines(batch)


def to_lp_file(m: Model, fn: Path, integer_label: str, slice_size: int = 10_000_000):
log = m._xCounter > 10_000

def to_lp_file(
m: Model,
fn: Path,
integer_label: str,
slice_size: int = 10_000_000,
log: bool = True,
) -> None:
batch_size = 5000

with open(fn, mode="w") as f:
Expand Down Expand Up @@ -559,9 +563,9 @@ def constraints_to_file_polars(m, f, log=False, lazy=False, slice_size=2_000_000
# formatted.sink_csv(f, **kwargs)


def to_lp_file_polars(m, fn, integer_label="general", slice_size=2_000_000):
log = m._xCounter > 10_000

def to_lp_file_polars(
m, fn, integer_label="general", slice_size=2_000_000, log: bool = True
):
with open(fn, mode="wb") as f:
start = time.time()

Expand All @@ -583,6 +587,7 @@ def to_file(
io_api: str | None = None,
integer_label: str = "general",
slice_size: int = 2_000_000,
log: bool | None = None,
) -> Path:
"""
Write out a model to a lp or mps file.
Expand All @@ -597,10 +602,13 @@ def to_file(
if io_api is None:
io_api = fn.suffix[1:]

if log is None:
log = m._xCounter > 10_000

if io_api == "lp":
to_lp_file(m, fn, integer_label, slice_size=slice_size)
to_lp_file(m, fn, integer_label, slice_size=slice_size, log=log)
elif io_api == "lp-polars":
to_lp_file_polars(m, fn, integer_label, slice_size=slice_size)
to_lp_file_polars(m, fn, integer_label, slice_size=slice_size, log=log)

elif io_api == "mps":
if "highs" not in solvers.available_solvers:
Expand Down
7 changes: 6 additions & 1 deletion linopy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ def solve(
sanitize_infinities: bool = True,
slice_size: int = 2_000_000,
remote: None = None,
log: bool | None = None,
**solver_options,
) -> tuple[str, str]:
"""
Expand Down Expand Up @@ -1027,6 +1028,10 @@ def solve(
Remote handler to use for solving model on a server. Note that when
solving on a rSee
linopy.remote.RemoteHandler for more details.
log : bool, optional
Whether to show a progress bar of writing the lp file. The default is
None, which means that the progress bar is shown if the model has more
than 10000 variables and constraints.
**solver_options : kwargs
Options passed to the solver.
Expand Down Expand Up @@ -1124,7 +1129,7 @@ def solve(
env=env,
)
else:
problem_fn = self.to_file(to_path(problem_fn), io_api)
problem_fn = self.to_file(to_path(problem_fn), io_api, log=log)
result = solver.solve_problem_from_file(
problem_fn=to_path(problem_fn),
solution_fn=to_path(solution_fn),
Expand Down

0 comments on commit 303585a

Please sign in to comment.