Skip to content

Commit 191f44e

Browse files
committed
rename argument
1 parent 8e06f72 commit 191f44e

File tree

5 files changed

+253
-141
lines changed

5 files changed

+253
-141
lines changed

linopy/io.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ def print_coord(coord):
6969
return coord
7070

7171

72-
def get_printers(m: Model, for_polars: bool = False, with_names: bool = False):
73-
if with_names:
72+
def get_printers(
73+
m: Model, for_polars: bool = False, explicit_coordinate_names: bool = False
74+
):
75+
if explicit_coordinate_names:
7476

7577
def print_variable(var):
7678
name, coord = m.variables.get_label_position(var)
@@ -460,7 +462,7 @@ def to_lp_file(
460462
)
461463
f.write("end\n")
462464

463-
logger.info(f" Writing time: {round(time.time()-start, 2)}s")
465+
logger.info(f" Writing time: {round(time.time() - start, 2)}s")
464466

465467

466468
def objective_write_linear_terms_polars(f, df, print_variable):
@@ -722,7 +724,7 @@ def to_lp_file_polars(
722724
)
723725
f.write(b"end\n")
724726

725-
logger.info(f" Writing time: {round(time.time()-start, 2)}s")
727+
logger.info(f" Writing time: {round(time.time() - start, 2)}s")
726728

727729

728730
def to_file(
@@ -732,7 +734,7 @@ def to_file(
732734
integer_label: str = "general",
733735
slice_size: int = 2_000_000,
734736
progress: bool | None = None,
735-
with_names: bool = True,
737+
explicit_coordinate_names: bool = True,
736738
) -> Path:
737739
"""
738740
Write out a model to a lp or mps file.
@@ -750,7 +752,11 @@ def to_file(
750752
if progress is None:
751753
progress = m._xCounter > 10_000
752754

753-
printers = get_printers(m, for_polars=io_api == "lp-polars", with_names=with_names)
755+
printers = get_printers(
756+
m,
757+
for_polars=io_api == "lp-polars",
758+
explicit_coordinate_names=explicit_coordinate_names,
759+
)
754760

755761
if io_api == "lp":
756762
to_lp_file(
@@ -779,7 +785,7 @@ def to_file(
779785

780786
# Use very fast highspy implementation
781787
# Might be replaced by custom writer, however needs C/Rust bindings for performance
782-
h = m.to_highspy(with_names=with_names)
788+
h = m.to_highspy(explicit_coordinate_names=explicit_coordinate_names)
783789
h.writeModel(str(fn))
784790
else:
785791
raise ValueError(
@@ -789,7 +795,9 @@ def to_file(
789795
return fn
790796

791797

792-
def to_mosek(m: Model, task: Any | None = None, with_names: bool = False) -> Any:
798+
def to_mosek(
799+
m: Model, task: Any | None = None, explicit_coordinate_names: bool = False
800+
) -> Any:
793801
"""
794802
Export model to MOSEK.
795803
@@ -807,7 +815,9 @@ def to_mosek(m: Model, task: Any | None = None, with_names: bool = False) -> Any
807815

808816
import mosek
809817

810-
print_variable, print_constraint = get_printers(m, with_names=with_names)
818+
print_variable, print_constraint = get_printers(
819+
m, explicit_coordinate_names=explicit_coordinate_names
820+
)
811821

812822
if task is None:
813823
task = mosek.Task()
@@ -897,7 +907,9 @@ def to_mosek(m: Model, task: Any | None = None, with_names: bool = False) -> Any
897907
return task
898908

899909

900-
def to_gurobipy(m: Model, env: Any | None = None, with_names: bool = False) -> Any:
910+
def to_gurobipy(
911+
m: Model, env: Any | None = None, explicit_coordinate_names: bool = False
912+
) -> Any:
901913
"""
902914
Export the model to gurobipy.
903915
@@ -916,7 +928,9 @@ def to_gurobipy(m: Model, env: Any | None = None, with_names: bool = False) -> A
916928
"""
917929
import gurobipy
918930

919-
print_variable, print_constraint = get_printers(m, with_names=with_names)
931+
print_variable, print_constraint = get_printers(
932+
m, explicit_coordinate_names=explicit_coordinate_names
933+
)
920934

921935
m.constraints.sanitize_missings()
922936
model = gurobipy.Model(env=env)
@@ -946,7 +960,7 @@ def to_gurobipy(m: Model, env: Any | None = None, with_names: bool = False) -> A
946960
return model
947961

948962

949-
def to_highspy(m: Model, with_names: bool = False) -> Highs:
963+
def to_highspy(m: Model, explicit_coordinate_names: bool = False) -> Highs:
950964
"""
951965
Export the model to highspy.
952966
@@ -965,7 +979,9 @@ def to_highspy(m: Model, with_names: bool = False) -> Highs:
965979
"""
966980
import highspy
967981

968-
print_variable, print_constraint = get_printers(m, with_names=with_names)
982+
print_variable, print_constraint = get_printers(
983+
m, explicit_coordinate_names=explicit_coordinate_names
984+
)
969985

970986
M = m.matrices
971987
h = highspy.Highs()

linopy/model.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ def solve(
958958
self,
959959
solver_name: str | None = None,
960960
io_api: str | None = None,
961-
with_names: bool = False,
961+
explicit_coordinate_names: bool = False,
962962
problem_fn: str | Path | None = None,
963963
solution_fn: str | Path | None = None,
964964
log_fn: str | Path | None = None,
@@ -991,7 +991,7 @@ def solve(
991991
'direct' the problem is communicated to the solver via the solver
992992
specific API, e.g. gurobipy. This may lead to faster run times.
993993
The default is set to 'lp' if available.
994-
with_names : bool, optional
994+
explicit_coordinate_names : bool, optional
995995
If the Api to use for communicating with the solver is based on 'lp',
996996
this option allows to keep the variable and constraint names in the
997997
lp file. This may lead to slower run times.
@@ -1133,18 +1133,18 @@ def solve(
11331133
warmstart_fn=to_path(warmstart_fn),
11341134
basis_fn=to_path(basis_fn),
11351135
env=env,
1136-
with_names=with_names,
1136+
explicit_coordinate_names=explicit_coordinate_names,
11371137
)
11381138
else:
1139-
if solver_name in ["glpk", "cbc"] and with_names:
1139+
if solver_name in ["glpk", "cbc"] and explicit_coordinate_names:
11401140
logger.warning(
11411141
f"{solver_name} does not support writing names to lp files, disabling it."
11421142
)
1143-
with_names = False
1143+
explicit_coordinate_names = False
11441144
problem_fn = self.to_file(
11451145
to_path(problem_fn),
11461146
io_api=io_api,
1147-
with_names=with_names,
1147+
explicit_coordinate_names=explicit_coordinate_names,
11481148
slice_size=slice_size,
11491149
progress=progress,
11501150
)

linopy/solvers.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def solve_problem_from_model(
240240
warmstart_fn: Path | None = None,
241241
basis_fn: Path | None = None,
242242
env: None = None,
243-
with_names: bool = False,
243+
explicit_coordinate_names: bool = False,
244244
) -> Result:
245245
"""
246246
Abstract method to solve a linear problem from a model.
@@ -279,7 +279,7 @@ def solve_problem(
279279
warmstart_fn: Path | None = None,
280280
basis_fn: Path | None = None,
281281
env: None = None,
282-
with_names: bool = False,
282+
explicit_coordinate_names: bool = False,
283283
) -> Result:
284284
"""
285285
Solve a linear problem either from a model or a problem file.
@@ -299,7 +299,7 @@ def solve_problem(
299299
warmstart_fn=warmstart_fn,
300300
basis_fn=basis_fn,
301301
env=env,
302-
with_names=with_names,
302+
explicit_coordinate_names=explicit_coordinate_names,
303303
)
304304
elif problem_fn is not None:
305305
return self.solve_problem_from_file(
@@ -343,7 +343,7 @@ def solve_problem_from_model(
343343
warmstart_fn: Path | None = None,
344344
basis_fn: Path | None = None,
345345
env: None = None,
346-
with_names: bool = False,
346+
explicit_coordinate_names: bool = False,
347347
):
348348
msg = "Direct API not implemented for CBC"
349349
raise NotImplementedError(msg)
@@ -501,7 +501,7 @@ def solve_problem_from_model(
501501
warmstart_fn: Path | None = None,
502502
basis_fn: Path | None = None,
503503
env: None = None,
504-
with_names: bool = False,
504+
explicit_coordinate_names: bool = False,
505505
) -> Result:
506506
msg = "Direct API not implemented for GLPK"
507507
raise NotImplementedError(msg)
@@ -684,7 +684,7 @@ def solve_problem_from_model(
684684
warmstart_fn: Path | None = None,
685685
basis_fn: Path | None = None,
686686
env: None = None,
687-
with_names: bool = False,
687+
explicit_coordinate_names: bool = False,
688688
) -> Result:
689689
"""
690690
Solve a linear problem directly from a linopy model using the Highs solver.
@@ -706,7 +706,7 @@ def solve_problem_from_model(
706706
Path to the basis file.
707707
env : None, optional
708708
Environment for the solver
709-
with_names : bool, optional
709+
explicit_coordinate_names : bool, optional
710710
Transfer variable and constraint names to the solver (default: False)
711711
712712
Returns
@@ -727,7 +727,7 @@ def solve_problem_from_model(
727727
"Drop the solver option or use 'choose' to enable quadratic terms / integrality."
728728
)
729729

730-
h = model.to_highspy(with_names=with_names)
730+
h = model.to_highspy(explicit_coordinate_names=explicit_coordinate_names)
731731

732732
if log_fn is None and model is not None:
733733
log_fn = model.solver_dir / "highs.log"
@@ -904,7 +904,7 @@ def solve_problem_from_model(
904904
warmstart_fn: Path | None = None,
905905
basis_fn: Path | None = None,
906906
env: None = None,
907-
with_names: bool = False,
907+
explicit_coordinate_names: bool = False,
908908
) -> Result:
909909
"""
910910
Solve a linear problem directly from a linopy model using the Gurobi solver.
@@ -925,7 +925,7 @@ def solve_problem_from_model(
925925
Path to the basis file.
926926
env : None, optional
927927
Gurobi environment for the solver
928-
with_names : bool, optional
928+
explicit_coordinate_names : bool, optional
929929
Transfer variable and constraint names to the solver (default: False)
930930
931931
Returns
@@ -938,7 +938,9 @@ def solve_problem_from_model(
938938
else:
939939
env_ = env
940940

941-
m = model.to_gurobipy(env=env_, with_names=with_names)
941+
m = model.to_gurobipy(
942+
env=env_, explicit_coordinate_names=explicit_coordinate_names
943+
)
942944

943945
return self._solve(
944946
m,
@@ -1139,7 +1141,7 @@ def solve_problem_from_model(
11391141
warmstart_fn: Path | None = None,
11401142
basis_fn: Path | None = None,
11411143
env: None = None,
1142-
with_names: bool = False,
1144+
explicit_coordinate_names: bool = False,
11431145
) -> Result:
11441146
msg = "Direct API not implemented for Cplex"
11451147
raise NotImplementedError(msg)
@@ -1282,7 +1284,7 @@ def solve_problem_from_model(
12821284
warmstart_fn: Path | None = None,
12831285
basis_fn: Path | None = None,
12841286
env: None = None,
1285-
with_names: bool = False,
1287+
explicit_coordinate_names: bool = False,
12861288
) -> Result:
12871289
msg = "Direct API not implemented for SCIP"
12881290
raise NotImplementedError(msg)
@@ -1424,7 +1426,7 @@ def solve_problem_from_model(
14241426
warmstart_fn: Path | None = None,
14251427
basis_fn: Path | None = None,
14261428
env: None = None,
1427-
with_names: bool = False,
1429+
explicit_coordinate_names: bool = False,
14281430
) -> Result:
14291431
msg = "Direct API not implemented for Xpress"
14301432
raise NotImplementedError(msg)
@@ -1570,7 +1572,7 @@ def solve_problem_from_model(
15701572
warmstart_fn: Path | None = None,
15711573
basis_fn: Path | None = None,
15721574
env: None = None,
1573-
with_names: bool = False,
1575+
explicit_coordinate_names: bool = False,
15741576
) -> Result:
15751577
"""
15761578
Solve a linear problem directly from a linopy model using the MOSEK solver.
@@ -1589,7 +1591,7 @@ def solve_problem_from_model(
15891591
Path to the basis file.
15901592
env : None, optional
15911593
Mosek environment for the solver
1592-
with_names : bool, optional
1594+
explicit_coordinate_names : bool, optional
15931595
Transfer variable and constraint names to the solver (default: False)
15941596
15951597
Returns
@@ -1601,7 +1603,9 @@ def solve_problem_from_model(
16011603
env_ = stack.enter_context(mosek.Env())
16021604

16031605
with env_.Task() as m:
1604-
m = model.to_mosek(m, with_names=with_names)
1606+
m = model.to_mosek(
1607+
m, explicit_coordinate_names=explicit_coordinate_names
1608+
)
16051609

16061610
return self._solve(
16071611
m,
@@ -1895,7 +1899,7 @@ def solve_problem_from_model(
18951899
warmstart_fn: Path | None = None,
18961900
basis_fn: Path | None = None,
18971901
env: None = None,
1898-
with_names: bool = False,
1902+
explicit_coordinate_names: bool = False,
18991903
) -> Result:
19001904
msg = "Direct API not implemented for COPT"
19011905
raise NotImplementedError(msg)
@@ -2038,7 +2042,7 @@ def solve_problem_from_model(
20382042
warmstart_fn: Path | None = None,
20392043
basis_fn: Path | None = None,
20402044
env: None = None,
2041-
with_names: bool = False,
2045+
explicit_coordinate_names: bool = False,
20422046
) -> Result:
20432047
msg = "Direct API not implemented for MindOpt"
20442048
raise NotImplementedError(msg)

test/test_io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ def test_to_file_lp(model, tmp_path):
127127

128128

129129
@pytest.mark.skipif("gurobi" not in available_solvers, reason="Gurobipy not installed")
130-
def test_to_file_lp_with_names(model, tmp_path):
130+
def test_to_file_lp_explicit_coordinate_names(model, tmp_path):
131131
import gurobipy
132132

133133
fn = tmp_path / "test.lp"
134-
model.to_file(fn, io_api="lp", with_names=True)
134+
model.to_file(fn, io_api="lp", explicit_coordinate_names=True)
135135

136136
gurobipy.read(str(fn))
137137

0 commit comments

Comments
 (0)