@@ -69,48 +69,45 @@ def print_coord(coord):
6969 return coord
7070
7171
72- def get_printers (m : Model , io_api : str , anonymously : bool = True ):
73- if anonymously :
74- if io_api == "lp" :
75- def print_variable_anonymous (var ):
76- return f"x{ var } "
77-
78- def print_constraint_anonymous (cons ):
79- return f"c{ cons } "
80-
81- elif io_api == "lp-polars" :
82- def print_variable_anonymous (series ):
83- return pl .lit (" x" ).alias ("x" ), series .cast (pl .String )
84-
85- def print_constraint_anonymous (series ):
86- return pl .lit ("c" ).alias ("c" ), series .cast (pl .String )
87- else :
88- return None
89-
90- return print_variable_anonymous , print_constraint_anonymous
91- else :
92- def print_variable_lp (var ):
72+ def get_printers (m : Model , for_polars : bool = False , with_names : bool = False ):
73+ if with_names :
74+ def print_variable (var ):
9375 name , coord = m .variables .get_label_position (var )
9476 name = clean_name (name )
9577 return f"{ name } { print_coord (coord )} #{ var } "
9678
97- def print_constraint_lp (cons ):
79+ def print_constraint (cons ):
9880 name , coord = m .constraints .get_label_position (cons )
9981 name = clean_name (name )
10082 return f"{ name } { print_coord (coord )} #{ cons } "
10183
10284 def print_variable_polars (series ):
103- return pl .lit (" " ), series .map_elements (print_variable_lp , return_dtype = pl .String )
85+ return pl .lit (" " ), series .map_elements (print_variable , return_dtype = pl .String )
86+
87+ def print_constraint_polars (series ):
88+ return pl .lit (None ), series .map_elements (print_constraint , return_dtype = pl .String )
89+
90+ if for_polars :
91+ return print_variable_polars , print_constraint_polars
92+ else :
93+ return print_variable , print_constraint
94+ else :
95+ def print_variable (var ):
96+ return f"x{ var } "
97+
98+ def print_constraint (cons ):
99+ return f"c{ cons } "
100+
101+ def print_variable_polars (series ):
102+ return pl .lit (" x" ).alias ("x" ), series .cast (pl .String )
104103
105104 def print_constraint_polars (series ):
106- return pl .lit (None ) , series .map_elements ( print_constraint_lp , return_dtype = pl .String )
105+ return pl .lit ("c" ). alias ( "c" ) , series .cast ( pl .String )
107106
108- if io_api == "lp" :
109- return print_variable_lp , print_constraint_lp
110- elif io_api == "lp-polars" :
107+ if for_polars :
111108 return print_variable_polars , print_constraint_polars
112109 else :
113- return None
110+ return print_variable , print_constraint
114111
115112
116113def objective_write_linear_terms (
@@ -739,9 +736,7 @@ def to_file(
739736 if progress is None :
740737 progress = m ._xCounter > 10_000
741738
742- printers = get_printers (m , io_api , anonymously = not with_names )
743- if with_names and io_api not in ["lp" , "lp-polars" ]:
744- logger .warning ("Debug names are only supported for LP files." )
739+ printers = get_printers (m , for_polars = io_api == "lp-polars" , with_names = with_names )
745740
746741 if io_api == "lp" :
747742 to_lp_file (m , fn , integer_label , slice_size = slice_size , progress = progress , printers = printers )
@@ -758,7 +753,7 @@ def to_file(
758753
759754 # Use very fast highspy implementation
760755 # Might be replaced by custom writer, however needs C/Rust bindings for performance
761- h = m .to_highspy ()
756+ h = m .to_highspy (with_names = with_names )
762757 h .writeModel (str (fn ))
763758 else :
764759 raise ValueError (
@@ -768,7 +763,7 @@ def to_file(
768763 return fn
769764
770765
771- def to_mosek (m : Model , task : Any | None = None ) -> Any :
766+ def to_mosek (m : Model , task : Any | None = None , with_names : bool = False ) -> Any :
772767 """
773768 Export model to MOSEK.
774769
@@ -786,6 +781,8 @@ def to_mosek(m: Model, task: Any | None = None) -> Any:
786781
787782 import mosek
788783
784+ print_variable , print_constraint = get_printers (m , with_names = with_names )
785+
789786 if task is None :
790787 task = mosek .Task ()
791788
@@ -796,9 +793,9 @@ def to_mosek(m: Model, task: Any | None = None) -> Any:
796793 # for j, n in enumerate(("x" + M.vlabels.astype(str).astype(object))):
797794 # task.putvarname(j, n)
798795
799- labels = M .vlabels . astype ( str ).astype (object )
796+ labels = np . vectorize ( print_variable )( M .vlabels ).astype (object )
800797 task .generatevarnames (
801- np .arange (0 , len (labels )), "x %0" , [len (labels )], None , [0 ], list (labels )
798+ np .arange (0 , len (labels )), "%0" , [len (labels )], None , [0 ], list (labels )
802799 )
803800
804801 ## Variables
@@ -835,7 +832,7 @@ def to_mosek(m: Model, task: Any | None = None) -> Any:
835832 ## Constraints
836833
837834 if len (m .constraints ) > 0 :
838- names = "c" + M .clabels . astype ( str ).astype (object )
835+ names = np . vectorize ( print_constraint )( M .clabels ).astype (object )
839836 for i , n in enumerate (names ):
840837 task .putconname (i , n )
841838 bkc = [
@@ -874,7 +871,7 @@ def to_mosek(m: Model, task: Any | None = None) -> Any:
874871 return task
875872
876873
877- def to_gurobipy (m : Model , env : Any | None = None ) -> Any :
874+ def to_gurobipy (m : Model , env : Any | None = None , with_names : bool = False ) -> Any :
878875 """
879876 Export the model to gurobipy.
880877
@@ -893,12 +890,14 @@ def to_gurobipy(m: Model, env: Any | None = None) -> Any:
893890 """
894891 import gurobipy
895892
893+ print_variable , print_constraint = get_printers (m , with_names = with_names )
894+
896895 m .constraints .sanitize_missings ()
897896 model = gurobipy .Model (env = env )
898897
899898 M = m .matrices
900899
901- names = "x" + M .vlabels . astype ( str ).astype (object )
900+ names = np . vectorize ( print_variable )( M .vlabels ).astype (object )
902901 kwargs = {}
903902 if len (m .binaries .labels ) + len (m .integers .labels ):
904903 kwargs ["vtype" ] = M .vtypes
@@ -913,15 +912,15 @@ def to_gurobipy(m: Model, env: Any | None = None) -> Any:
913912 model .ModelSense = - 1
914913
915914 if len (m .constraints ):
916- names = "c" + M .clabels . astype ( str ).astype (object )
915+ names = np . vectorize ( print_constraint )( M .clabels ).astype (object )
917916 c = model .addMConstr (M .A , x , M .sense , M .b ) # type: ignore
918917 c .setAttr ("ConstrName" , list (names )) # type: ignore
919918
920919 model .update ()
921920 return model
922921
923922
924- def to_highspy (m : Model ) -> Highs :
923+ def to_highspy (m : Model , with_names : bool = False ) -> Highs :
925924 """
926925 Export the model to highspy.
927926
@@ -940,6 +939,8 @@ def to_highspy(m: Model) -> Highs:
940939 """
941940 import highspy
942941
942+ print_variable , print_constraint = get_printers (m , with_names = with_names )
943+
943944 M = m .matrices
944945 h = highspy .Highs ()
945946 h .addVars (len (M .vlabels ), M .lb , M .ub )
@@ -966,9 +967,9 @@ def to_highspy(m: Model) -> Highs:
966967 h .addRows (num_cons , lower , upper , A .nnz , A .indptr , A .indices , A .data )
967968
968969 lp = h .getLp ()
969- lp .col_names_ = "x" + M .vlabels . astype ( str ).astype (object )
970+ lp .col_names_ = np . vectorize ( print_variable )( M .vlabels ).astype (object )
970971 if len (M .clabels ):
971- lp .row_names_ = "c" + M .clabels . astype ( str ).astype (object )
972+ lp .row_names_ = np . vectorize ( print_constraint )( M .clabels ).astype (object )
972973 h .passModel (lp )
973974
974975 # quadrative objective
0 commit comments