@@ -87,12 +87,12 @@ def objective_write_quad_terms(
87
87
88
88
89
89
def 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
91
91
) -> None :
92
92
"""
93
93
Write out the objective of a model to a lp file.
94
94
"""
95
- if log :
95
+ if progress :
96
96
logger .info ("Writing objective." )
97
97
98
98
sense = m .objective .sense
@@ -129,7 +129,7 @@ def objective_to_file(
129
129
def constraints_to_file (
130
130
m : Model ,
131
131
f : TextIOWrapper ,
132
- log : bool = False ,
132
+ progress : bool = False ,
133
133
batch_size : int = 50_000 ,
134
134
slice_size : int = 100_000 ,
135
135
) -> None :
@@ -138,7 +138,7 @@ def constraints_to_file(
138
138
139
139
f .write ("\n \n s.t.\n \n " )
140
140
names : Iterable = m .constraints
141
- if log :
141
+ if progress :
142
142
names = tqdm (
143
143
list (names ),
144
144
desc = "Writing constraints." ,
@@ -198,7 +198,7 @@ def constraints_to_file(
198
198
def bounds_to_file (
199
199
m : Model ,
200
200
f : TextIOWrapper ,
201
- log : bool = False ,
201
+ progress : bool = False ,
202
202
batch_size : int = 10000 ,
203
203
slice_size : int = 100_000 ,
204
204
) -> None :
@@ -210,7 +210,7 @@ def bounds_to_file(
210
210
return
211
211
212
212
f .write ("\n \n bounds\n \n " )
213
- if log :
213
+ if progress :
214
214
names = tqdm (
215
215
list (names ),
216
216
desc = "Writing continuous variables." ,
@@ -241,7 +241,7 @@ def bounds_to_file(
241
241
def binaries_to_file (
242
242
m : Model ,
243
243
f : TextIOWrapper ,
244
- log : bool = False ,
244
+ progress : bool = False ,
245
245
batch_size : int = 1000 ,
246
246
slice_size : int = 100_000 ,
247
247
) -> None :
@@ -253,7 +253,7 @@ def binaries_to_file(
253
253
return
254
254
255
255
f .write ("\n \n binary\n \n " )
256
- if log :
256
+ if progress :
257
257
names = tqdm (
258
258
list (names ),
259
259
desc = "Writing binary variables." ,
@@ -277,7 +277,7 @@ def binaries_to_file(
277
277
def integers_to_file (
278
278
m : Model ,
279
279
f : TextIOWrapper ,
280
- log : bool = False ,
280
+ progress : bool = False ,
281
281
batch_size : int = 1000 ,
282
282
slice_size : int = 100_000 ,
283
283
integer_label : str = "general" ,
@@ -290,7 +290,7 @@ def integers_to_file(
290
290
return
291
291
292
292
f .write (f"\n \n { integer_label } \n \n " )
293
- if log :
293
+ if progress :
294
294
names = tqdm (
295
295
list (names ),
296
296
desc = "Writing integer variables." ,
@@ -311,9 +311,13 @@ def integers_to_file(
311
311
f .writelines (batch )
312
312
313
313
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 :
317
321
batch_size = 5000
318
322
319
323
with open (fn , mode = "w" ) as f :
@@ -322,17 +326,21 @@ def to_lp_file(m: Model, fn: Path, integer_label: str, slice_size: int = 10_000_
322
326
if isinstance (f , int ):
323
327
raise ValueError ("File not found." )
324
328
325
- objective_to_file (m , f , log = log )
329
+ objective_to_file (m , f , progress = progress )
326
330
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
332
+ )
333
+ bounds_to_file (
334
+ m , f = f , progress = progress , batch_size = batch_size , slice_size = slice_size
335
+ )
336
+ binaries_to_file (
337
+ m , f = f , progress = progress , batch_size = batch_size , slice_size = slice_size
328
338
)
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 )
331
339
integers_to_file (
332
340
m ,
333
341
integer_label = integer_label ,
334
342
f = f ,
335
- log = log ,
343
+ progress = progress ,
336
344
batch_size = batch_size ,
337
345
slice_size = slice_size ,
338
346
)
@@ -371,11 +379,11 @@ def objective_write_quadratic_terms_polars(f, df):
371
379
f .write (b"] / 2\n " )
372
380
373
381
374
- def objective_to_file_polars (m , f , log = False ):
382
+ def objective_to_file_polars (m , f , progress = False ):
375
383
"""
376
384
Write out the objective of a model to a lp file.
377
385
"""
378
- if log :
386
+ if progress :
379
387
logger .info ("Writing objective." )
380
388
381
389
sense = m .objective .sense
@@ -399,7 +407,7 @@ def objective_to_file_polars(m, f, log=False):
399
407
objective_write_quadratic_terms_polars (f , quads )
400
408
401
409
402
- def bounds_to_file_polars (m , f , log = False , slice_size = 2_000_000 ):
410
+ def bounds_to_file_polars (m , f , progress = False , slice_size = 2_000_000 ):
403
411
"""
404
412
Write out variables of a model to a lp file.
405
413
"""
@@ -408,7 +416,7 @@ def bounds_to_file_polars(m, f, log=False, slice_size=2_000_000):
408
416
return
409
417
410
418
f .write (b"\n \n bounds\n \n " )
411
- if log :
419
+ if progress :
412
420
names = tqdm (
413
421
list (names ),
414
422
desc = "Writing continuous variables." ,
@@ -437,7 +445,7 @@ def bounds_to_file_polars(m, f, log=False, slice_size=2_000_000):
437
445
formatted .write_csv (f , ** kwargs )
438
446
439
447
440
- def binaries_to_file_polars (m , f , log = False , slice_size = 2_000_000 ):
448
+ def binaries_to_file_polars (m , f , progress = False , slice_size = 2_000_000 ):
441
449
"""
442
450
Write out binaries of a model to a lp file.
443
451
"""
@@ -446,7 +454,7 @@ def binaries_to_file_polars(m, f, log=False, slice_size=2_000_000):
446
454
return
447
455
448
456
f .write (b"\n \n binary\n \n " )
449
- if log :
457
+ if progress :
450
458
names = tqdm (
451
459
list (names ),
452
460
desc = "Writing binary variables." ,
@@ -471,7 +479,7 @@ def binaries_to_file_polars(m, f, log=False, slice_size=2_000_000):
471
479
472
480
473
481
def integers_to_file_polars (
474
- m , f , log = False , integer_label = "general" , slice_size = 2_000_000
482
+ m , f , progress = False , integer_label = "general" , slice_size = 2_000_000
475
483
):
476
484
"""
477
485
Write out integers of a model to a lp file.
@@ -481,7 +489,7 @@ def integers_to_file_polars(
481
489
return
482
490
483
491
f .write (f"\n \n { integer_label } \n \n " .encode ())
484
- if log :
492
+ if progress :
485
493
names = tqdm (
486
494
list (names ),
487
495
desc = "Writing integer variables." ,
@@ -505,13 +513,13 @@ def integers_to_file_polars(
505
513
formatted .write_csv (f , ** kwargs )
506
514
507
515
508
- def constraints_to_file_polars (m , f , log = False , lazy = False , slice_size = 2_000_000 ):
516
+ def constraints_to_file_polars (m , f , progress = False , lazy = False , slice_size = 2_000_000 ):
509
517
if not len (m .constraints ):
510
518
return
511
519
512
520
f .write (b"\n \n s.t.\n \n " )
513
521
names = m .constraints
514
- if log :
522
+ if progress :
515
523
names = tqdm (
516
524
list (names ),
517
525
desc = "Writing constraints." ,
@@ -559,18 +567,22 @@ def constraints_to_file_polars(m, f, log=False, lazy=False, slice_size=2_000_000
559
567
# formatted.sink_csv(f, **kwargs)
560
568
561
569
562
- def to_lp_file_polars (m , fn , integer_label = "general" , slice_size = 2_000_000 ):
563
- log = m . _xCounter > 10_000
564
-
570
+ def to_lp_file_polars (
571
+ m , fn , integer_label = "general" , slice_size = 2_000_000 , progress : bool = True
572
+ ):
565
573
with open (fn , mode = "wb" ) as f :
566
574
start = time .time ()
567
575
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 )
576
+ objective_to_file_polars (m , f , progress = progress )
577
+ constraints_to_file_polars (m , f = f , progress = progress , slice_size = slice_size )
578
+ bounds_to_file_polars (m , f = f , progress = progress , slice_size = slice_size )
579
+ binaries_to_file_polars (m , f = f , progress = progress , slice_size = slice_size )
572
580
integers_to_file_polars (
573
- m , integer_label = integer_label , f = f , log = log , slice_size = slice_size
581
+ m ,
582
+ integer_label = integer_label ,
583
+ f = f ,
584
+ progress = progress ,
585
+ slice_size = slice_size ,
574
586
)
575
587
f .write (b"end\n " )
576
588
@@ -583,6 +595,7 @@ def to_file(
583
595
io_api : str | None = None ,
584
596
integer_label : str = "general" ,
585
597
slice_size : int = 2_000_000 ,
598
+ progress : bool | None = None ,
586
599
) -> Path :
587
600
"""
588
601
Write out a model to a lp or mps file.
@@ -597,10 +610,15 @@ def to_file(
597
610
if io_api is None :
598
611
io_api = fn .suffix [1 :]
599
612
613
+ if progress is None :
614
+ progress = m ._xCounter > 10_000
615
+
600
616
if io_api == "lp" :
601
- to_lp_file (m , fn , integer_label , slice_size = slice_size )
617
+ to_lp_file (m , fn , integer_label , slice_size = slice_size , progress = progress )
602
618
elif io_api == "lp-polars" :
603
- to_lp_file_polars (m , fn , integer_label , slice_size = slice_size )
619
+ to_lp_file_polars (
620
+ m , fn , integer_label , slice_size = slice_size , progress = progress
621
+ )
604
622
605
623
elif io_api == "mps" :
606
624
if "highs" not in solvers .available_solvers :
0 commit comments