File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -751,6 +751,23 @@ def to_gurobipy(
751751 c = model .addMConstr (M .A , x , M .sense , M .b ) # type: ignore
752752 c .setAttr ("ConstrName" , list (names )) # type: ignore
753753
754+ if m .variables .sos :
755+ for var_name in m .variables .sos :
756+ var = m .variables .sos [var_name ]
757+ sos_type = var .attrs ["sos_type" ]
758+ sos_dim = var .attrs ["sos_dim" ]
759+
760+ def add_sos (s ):
761+ s = s .squeeze ()
762+ model .addSOS (sos_type , x [s ].tolist (), s .coords [sos_dim ].values )
763+
764+ others = tuple (dim for dim in var .labels .dims if dim != sos_dim )
765+ if not others :
766+ add_sos (var .labels )
767+ else :
768+ for _ , s in var .labels .groupby (* others ):
769+ add_sos (s )
770+
754771 model .update ()
755772 return model
756773
Original file line number Diff line number Diff line change @@ -576,10 +576,17 @@ def add_sos_constraints(
576576 raise ValueError (f"sos_dim must name a variable dimension, got { sos_dim } " )
577577
578578 if "sos_type" in variable .attrs or "sos_dim" in variable .attrs :
579- sos_type = variable .attrs .get ("sos_type" )
580- sos_dim = variable .attrs .get ("sos_dim" )
579+ existing_sos_type = variable .attrs .get ("sos_type" )
580+ existing_sos_dim = variable .attrs .get ("sos_dim" )
581581 raise ValueError (
582- "variable already has an sos{sos_type} constraint on {sos_dim}"
582+ f"variable already has an sos{ existing_sos_type } constraint on { existing_sos_dim } "
583+ )
584+
585+ # Validate that sos_dim coordinates are numeric (needed for weights)
586+ if not pd .api .types .is_numeric_dtype (variable .coords [sos_dim ]):
587+ raise ValueError (
588+ f"SOS constraint requires numeric coordinates for dimension '{ sos_dim } ', "
589+ f"but got { variable .coords [sos_dim ].dtype } "
583590 )
584591
585592 variable .attrs .update (sos_type = sos_type , sos_dim = sos_dim )
You can’t perform that action at this time.
0 commit comments