2626from linopy import solvers
2727from linopy .constants import CONCAT_DIM
2828from linopy .objective import Objective
29- from linopy .variables import Variables
3029
3130if TYPE_CHECKING :
3231 from highspy .highs import Highs
@@ -69,30 +68,34 @@ def print_coord(coord):
6968 coord = print_coord (coord ).translate (coord_sanitizer )
7069 return coord
7170
71+
7272def get_print (m : Model , anonymously : bool = True ):
7373 if anonymously :
74+
7475 def print_variable_anonymous (var ):
7576 return f"x{ var } "
77+
7678 def print_constraint_anonymous (cons ):
7779 return f"c{ cons } "
80+
7881 return print_variable_anonymous , print_constraint_anonynous
7982 else :
83+
8084 def print_variable (var ):
8185 name , coord = m .variables .get_label_position (var )
8286 name = clean_name (name )
8387 return f"{ name } { print_coord (coord )} "
88+
8489 def print_constraint (constraints , cons ):
8590 name , coord = m .constraints .get_label_position (cons )
8691 name = clean_name (name )
8792 return f"{ name } { print_coord (coord )} "
93+
8894 return print_variable , print_constraint
89-
95+
96+
9097def objective_write_linear_terms (
91- df : DataFrame ,
92- f : TextIOWrapper ,
93- batch : list [Any ],
94- batch_size : int ,
95- print_variable
98+ df : DataFrame , f : TextIOWrapper , batch : list [Any ], batch_size : int , print_variable
9699) -> list [str | Any ]:
97100 """
98101 Write the linear terms of the objective to a file.
@@ -113,7 +116,7 @@ def objective_write_quad_terms(
113116 f : TextIOWrapper ,
114117 batch : list [str ],
115118 batch_size : int ,
116- print_variable
119+ print_variable ,
117120) -> list [str ]:
118121 """
119122 Write the cross terms of the objective to a file.
@@ -133,14 +136,18 @@ def objective_write_quad_terms(
133136
134137
135138def objective_to_file (
136- m : Model , f : TextIOWrapper , progress : bool = False , batch_size : int = 10000 , printers = None
139+ m : Model ,
140+ f : TextIOWrapper ,
141+ progress : bool = False ,
142+ batch_size : int = 10000 ,
143+ printers = None ,
137144) -> None :
138145 """
139146 Write out the objective of a model to a lp file.
140147 """
141148 if progress :
142149 logger .info ("Writing objective." )
143-
150+
144151 print_variable , _ = printers
145152 sense = m .objective .sense
146153 f .write (f"{ sense } \n \n obj:\n \n " )
@@ -181,11 +188,11 @@ def constraints_to_file(
181188 progress : bool = False ,
182189 batch_size : int = 50_000 ,
183190 slice_size : int = 100_000 ,
184- printers = None
191+ printers = None ,
185192) -> None :
186193 if not len (m .constraints ):
187194 return
188-
195+
189196 print_variable , print_constraint = printers
190197
191198 f .write ("\n \n s.t.\n \n " )
@@ -257,15 +264,15 @@ def bounds_to_file(
257264 progress : bool = False ,
258265 batch_size : int = 10000 ,
259266 slice_size : int = 100_000 ,
260- printers = None
267+ printers = None ,
261268) -> None :
262269 """
263270 Write out variables of a model to a lp file.
264271 """
265272 names : Iterable = list (m .variables .continuous ) + list (m .variables .integers )
266273 if not len (list (names )):
267274 return
268-
275+
269276 print_variable , _ = printers
270277
271278 f .write ("\n \n bounds\n \n " )
@@ -304,15 +311,15 @@ def binaries_to_file(
304311 progress : bool = False ,
305312 batch_size : int = 1000 ,
306313 slice_size : int = 100_000 ,
307- printers = None
314+ printers = None ,
308315) -> None :
309316 """
310317 Write out binaries of a model to a lp file.
311318 """
312319 names : Iterable = m .variables .binaries
313320 if not len (list (names )):
314321 return
315-
322+
316323 print_variable , _ = printers
317324
318325 f .write ("\n \n binary\n \n " )
@@ -345,15 +352,15 @@ def integers_to_file(
345352 batch_size : int = 1000 ,
346353 slice_size : int = 100_000 ,
347354 integer_label : str = "general" ,
348- printers = None
355+ printers = None ,
349356) -> None :
350357 """
351358 Write out integers of a model to a lp file.
352359 """
353360 names : Iterable = m .variables .integers
354361 if not len (list (names )):
355362 return
356-
363+
357364 print_variable , _ = printers
358365
359366 f .write (f"\n \n { integer_label } \n \n " )
@@ -388,7 +395,7 @@ def to_lp_file(
388395 anonymously : bool = True ,
389396) -> None :
390397 batch_size = 5000
391-
398+
392399 printers = get_print (m , anonymously )
393400
394401 with open (fn , mode = "w" ) as f :
@@ -399,13 +406,28 @@ def to_lp_file(
399406
400407 objective_to_file (m , f , progress = progress , printers = printers )
401408 constraints_to_file (
402- m , f = f , progress = progress , batch_size = batch_size , slice_size = slice_size , printers = printers
409+ m ,
410+ f = f ,
411+ progress = progress ,
412+ batch_size = batch_size ,
413+ slice_size = slice_size ,
414+ printers = printers ,
403415 )
404416 bounds_to_file (
405- m , f = f , progress = progress , batch_size = batch_size , slice_size = slice_size , printers = printers
417+ m ,
418+ f = f ,
419+ progress = progress ,
420+ batch_size = batch_size ,
421+ slice_size = slice_size ,
422+ printers = printers ,
406423 )
407424 binaries_to_file (
408- m , f = f , progress = progress , batch_size = batch_size , slice_size = slice_size , printers = printers
425+ m ,
426+ f = f ,
427+ progress = progress ,
428+ batch_size = batch_size ,
429+ slice_size = slice_size ,
430+ printers = printers ,
409431 )
410432 integers_to_file (
411433 m ,
0 commit comments