Skip to content

Commit b356ddc

Browse files
FabianHofmannFabian Hofmann
authored and
Fabian Hofmann
committed
pips-io: print out rows
1 parent df409ce commit b356ddc

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

linopy/io.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -330,45 +330,54 @@ def to_block_files(m, fn):
330330
is_varblock_0 = varblocks == 0
331331
is_conblock_L = conblocks == N + 1
332332

333-
for key, suffix in zip(["labels", "coeffs", "vars"], ["row", "data", "col"]):
333+
keys = ["labels", "coeffs", "vars"]
334+
335+
def filtered(arr, mask, key):
336+
"""
337+
Set coefficients to zero for mask, keep others unchanged.
338+
339+
PIPS requires this information to set the shape of sub-matrices.
340+
"""
341+
assert key in keys
342+
if key == "coeffs":
343+
return np.where(mask, arr, 0)
344+
return arr
345+
346+
for key, suffix in zip(keys, ["row", "data", "col"]):
334347
arr = cons.ravel(key, "vars", filter_missings=True)
335348
for n in tqdm(range(N + 1), desc=f"Write constraint {key}"):
336349

337350
is_conblock_n = conblocks == n
338351
is_varblock_n = varblocks == n
339352

340-
mask = is_conblock_n & is_varblock_0
341-
arr[mask & is_equality].tofile(path / f"block{n}" / f"A_{suffix}", sep="\n")
342-
arr[mask & ~is_equality].tofile(
353+
mask = is_conblock_n & is_equality
354+
filtered(arr[mask], is_varblock_0[mask], key).tofile(
355+
path / f"block{n}" / f"A_{suffix}", sep="\n"
356+
)
357+
mask = is_conblock_n & ~is_equality
358+
filtered(arr[mask], is_varblock_0[mask], key).tofile(
343359
path / f"block{n}" / f"C_{suffix}", sep="\n"
344360
)
345361

346-
mask = is_conblock_L & is_varblock_n
347-
arr[mask & is_equality].tofile(
362+
mask = is_conblock_L & is_equality
363+
filtered(arr[mask], is_varblock_n[mask], key).tofile(
348364
path / f"block{n}" / f"BL_{suffix}", sep="\n"
349365
)
350-
arr[mask & ~is_equality].tofile(
366+
mask = is_conblock_L & ~is_equality
367+
filtered(arr[mask], is_varblock_n[mask], key).tofile(
351368
path / f"block{n}" / f"DL_{suffix}", sep="\n"
352369
)
353370

354371
if n:
355-
mask = is_conblock_n & is_varblock_n
356-
arr[mask & is_equality].tofile(
372+
mask = is_conblock_n & is_equality
373+
filtered(arr[mask], is_varblock_n[mask], key).tofile(
357374
path / f"block{n}" / f"B_{suffix}", sep="\n"
358375
)
359-
arr[mask & ~is_equality].tofile(
376+
mask = is_conblock_n & ~is_equality
377+
filtered(arr[mask], is_varblock_n[mask], key).tofile(
360378
path / f"block{n}" / f"D_{suffix}", sep="\n"
361379
)
362380

363-
if key == "labels":
364-
mask = is_conblock_n & is_equality
365-
all_rows = np.sort(np.unique(arr[mask]))
366-
all_rows.tofile(path / f"block{n}" / "A-B_rows", sep="\n")
367-
368-
mask = is_conblock_n & ~is_equality
369-
all_rows = np.sort(np.unique(arr[mask]))
370-
all_rows.tofile(path / f"block{n}" / "C-D_rows", sep="\n")
371-
372381

373382
def non_bool_dict(d):
374383
"""

0 commit comments

Comments
 (0)