@@ -87,12 +87,12 @@ def objective_write_quad_terms(
8787
8888
8989def objective_to_file (
90- m : Model , f : TextIOWrapper , log : bool = False , batch_size : int = 10000
90+ m : Model , f : TextIOWrapper , progress : bool = False , batch_size : int = 10000
9191) -> None :
9292 """
9393 Write out the objective of a model to a lp file.
9494 """
95- if log :
95+ if progress :
9696 logger .info ("Writing objective." )
9797
9898 sense = m .objective .sense
@@ -129,7 +129,7 @@ def objective_to_file(
129129def constraints_to_file (
130130 m : Model ,
131131 f : TextIOWrapper ,
132- log : bool = False ,
132+ progress : bool = False ,
133133 batch_size : int = 50_000 ,
134134 slice_size : int = 100_000 ,
135135) -> None :
@@ -138,7 +138,7 @@ def constraints_to_file(
138138
139139 f .write ("\n \n s.t.\n \n " )
140140 names : Iterable = m .constraints
141- if log :
141+ if progress :
142142 names = tqdm (
143143 list (names ),
144144 desc = "Writing constraints." ,
@@ -198,7 +198,7 @@ def constraints_to_file(
198198def bounds_to_file (
199199 m : Model ,
200200 f : TextIOWrapper ,
201- log : bool = False ,
201+ progress : bool = False ,
202202 batch_size : int = 10000 ,
203203 slice_size : int = 100_000 ,
204204) -> None :
@@ -210,7 +210,7 @@ def bounds_to_file(
210210 return
211211
212212 f .write ("\n \n bounds\n \n " )
213- if log :
213+ if progress :
214214 names = tqdm (
215215 list (names ),
216216 desc = "Writing continuous variables." ,
@@ -241,7 +241,7 @@ def bounds_to_file(
241241def binaries_to_file (
242242 m : Model ,
243243 f : TextIOWrapper ,
244- log : bool = False ,
244+ progress : bool = False ,
245245 batch_size : int = 1000 ,
246246 slice_size : int = 100_000 ,
247247) -> None :
@@ -253,7 +253,7 @@ def binaries_to_file(
253253 return
254254
255255 f .write ("\n \n binary\n \n " )
256- if log :
256+ if progress :
257257 names = tqdm (
258258 list (names ),
259259 desc = "Writing binary variables." ,
@@ -277,7 +277,7 @@ def binaries_to_file(
277277def integers_to_file (
278278 m : Model ,
279279 f : TextIOWrapper ,
280- log : bool = False ,
280+ progress : bool = False ,
281281 batch_size : int = 1000 ,
282282 slice_size : int = 100_000 ,
283283 integer_label : str = "general" ,
@@ -290,7 +290,7 @@ def integers_to_file(
290290 return
291291
292292 f .write (f"\n \n { integer_label } \n \n " )
293- if log :
293+ if progress :
294294 names = tqdm (
295295 list (names ),
296296 desc = "Writing integer variables." ,
@@ -311,9 +311,13 @@ def integers_to_file(
311311 f .writelines (batch )
312312
313313
314- def to_lp_file (m : Model , fn : Path , integer_label : str , slice_size : int = 10_000_000 ):
315- log = m ._xCounter > 10_000
316-
314+ def to_lp_file (
315+ m : Model ,
316+ fn : Path ,
317+ integer_label : str ,
318+ slice_size : int = 10_000_000 ,
319+ progress : bool = True ,
320+ ) -> None :
317321 batch_size = 5000
318322
319323 with open (fn , mode = "w" ) as f :
@@ -322,17 +326,17 @@ def to_lp_file(m: Model, fn: Path, integer_label: str, slice_size: int = 10_000_
322326 if isinstance (f , int ):
323327 raise ValueError ("File not found." )
324328
325- objective_to_file (m , f , log = log )
329+ objective_to_file (m , f , progress = progress )
326330 constraints_to_file (
327- m , f = f , log = log , batch_size = batch_size , slice_size = slice_size
331+ m , f = f , progress = progress , batch_size = batch_size , slice_size = slice_size
328332 )
329- bounds_to_file (m , f = f , log = log , batch_size = batch_size , slice_size = slice_size )
330- binaries_to_file (m , f = f , log = log , batch_size = batch_size , slice_size = slice_size )
333+ bounds_to_file (m , f = f , progress = progress , batch_size = batch_size , slice_size = slice_size )
334+ binaries_to_file (m , f = f , progress = progress , batch_size = batch_size , slice_size = slice_size )
331335 integers_to_file (
332336 m ,
333337 integer_label = integer_label ,
334338 f = f ,
335- log = log ,
339+ progress = progress ,
336340 batch_size = batch_size ,
337341 slice_size = slice_size ,
338342 )
@@ -371,11 +375,11 @@ def objective_write_quadratic_terms_polars(f, df):
371375 f .write (b"] / 2\n " )
372376
373377
374- def objective_to_file_polars (m , f , log = False ):
378+ def objective_to_file_polars (m , f , progress = False ):
375379 """
376380 Write out the objective of a model to a lp file.
377381 """
378- if log :
382+ if progress :
379383 logger .info ("Writing objective." )
380384
381385 sense = m .objective .sense
@@ -399,7 +403,7 @@ def objective_to_file_polars(m, f, log=False):
399403 objective_write_quadratic_terms_polars (f , quads )
400404
401405
402- def bounds_to_file_polars (m , f , log = False , slice_size = 2_000_000 ):
406+ def bounds_to_file_polars (m , f , progress = False , slice_size = 2_000_000 ):
403407 """
404408 Write out variables of a model to a lp file.
405409 """
@@ -408,7 +412,7 @@ def bounds_to_file_polars(m, f, log=False, slice_size=2_000_000):
408412 return
409413
410414 f .write (b"\n \n bounds\n \n " )
411- if log :
415+ if progress :
412416 names = tqdm (
413417 list (names ),
414418 desc = "Writing continuous variables." ,
@@ -437,7 +441,7 @@ def bounds_to_file_polars(m, f, log=False, slice_size=2_000_000):
437441 formatted .write_csv (f , ** kwargs )
438442
439443
440- def binaries_to_file_polars (m , f , log = False , slice_size = 2_000_000 ):
444+ def binaries_to_file_polars (m , f , progress = False , slice_size = 2_000_000 ):
441445 """
442446 Write out binaries of a model to a lp file.
443447 """
@@ -446,7 +450,7 @@ def binaries_to_file_polars(m, f, log=False, slice_size=2_000_000):
446450 return
447451
448452 f .write (b"\n \n binary\n \n " )
449- if log :
453+ if progress :
450454 names = tqdm (
451455 list (names ),
452456 desc = "Writing binary variables." ,
@@ -471,7 +475,7 @@ def binaries_to_file_polars(m, f, log=False, slice_size=2_000_000):
471475
472476
473477def integers_to_file_polars (
474- m , f , log = False , integer_label = "general" , slice_size = 2_000_000
478+ m , f , progress = False , integer_label = "general" , slice_size = 2_000_000
475479):
476480 """
477481 Write out integers of a model to a lp file.
@@ -481,7 +485,7 @@ def integers_to_file_polars(
481485 return
482486
483487 f .write (f"\n \n { integer_label } \n \n " .encode ())
484- if log :
488+ if progress :
485489 names = tqdm (
486490 list (names ),
487491 desc = "Writing integer variables." ,
@@ -505,13 +509,13 @@ def integers_to_file_polars(
505509 formatted .write_csv (f , ** kwargs )
506510
507511
508- def constraints_to_file_polars (m , f , log = False , lazy = False , slice_size = 2_000_000 ):
512+ def constraints_to_file_polars (m , f , progress = False , lazy = False , slice_size = 2_000_000 ):
509513 if not len (m .constraints ):
510514 return
511515
512516 f .write (b"\n \n s.t.\n \n " )
513517 names = m .constraints
514- if log :
518+ if progress :
515519 names = tqdm (
516520 list (names ),
517521 desc = "Writing constraints." ,
@@ -559,18 +563,18 @@ def constraints_to_file_polars(m, f, log=False, lazy=False, slice_size=2_000_000
559563 # formatted.sink_csv(f, **kwargs)
560564
561565
562- def to_lp_file_polars (m , fn , integer_label = "general" , slice_size = 2_000_000 ):
563- log = m . _xCounter > 10_000
564-
566+ def to_lp_file_polars (
567+ m , fn , integer_label = "general" , slice_size = 2_000_000 , progress : bool = True
568+ ):
565569 with open (fn , mode = "wb" ) as f :
566570 start = time .time ()
567571
568- objective_to_file_polars (m , f , log = log )
569- constraints_to_file_polars (m , f = f , log = log , slice_size = slice_size )
570- bounds_to_file_polars (m , f = f , log = log , slice_size = slice_size )
571- binaries_to_file_polars (m , f = f , log = log , slice_size = slice_size )
572+ objective_to_file_polars (m , f , progress = progress )
573+ constraints_to_file_polars (m , f = f , progress = progress , slice_size = slice_size )
574+ bounds_to_file_polars (m , f = f , progress = progress , slice_size = slice_size )
575+ binaries_to_file_polars (m , f = f , progress = progress , slice_size = slice_size )
572576 integers_to_file_polars (
573- m , integer_label = integer_label , f = f , log = log , slice_size = slice_size
577+ m , integer_label = integer_label , f = f , progress = progress , slice_size = slice_size
574578 )
575579 f .write (b"end\n " )
576580
@@ -583,6 +587,7 @@ def to_file(
583587 io_api : str | None = None ,
584588 integer_label : str = "general" ,
585589 slice_size : int = 2_000_000 ,
590+ progress : bool | None = None ,
586591) -> Path :
587592 """
588593 Write out a model to a lp or mps file.
@@ -597,10 +602,13 @@ def to_file(
597602 if io_api is None :
598603 io_api = fn .suffix [1 :]
599604
605+ if progress is None :
606+ progress = m ._xCounter > 10_000
607+
600608 if io_api == "lp" :
601- to_lp_file (m , fn , integer_label , slice_size = slice_size )
609+ to_lp_file (m , fn , integer_label , slice_size = slice_size , progress = progress )
602610 elif io_api == "lp-polars" :
603- to_lp_file_polars (m , fn , integer_label , slice_size = slice_size )
611+ to_lp_file_polars (m , fn , integer_label , slice_size = slice_size , progress = progress )
604612
605613 elif io_api == "mps" :
606614 if "highs" not in solvers .available_solvers :
0 commit comments