-
Notifications
You must be signed in to change notification settings - Fork 79
fix(sos): SOS constraints on masked variables (#688) #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c14e8e5
test(sos): regression matrix for masked SOS variables (#688)
FBumann 5cf2efb
fix(sos): resolve linopy labels to solver column positions in direct …
FBumann 2669e08
fix(sos): filter masked SOS members + robust set id in LP writer
FBumann afc1dc3
test(pwl): convert masked-sos2 NaN-padding test to positive assertion
FBumann 2c54196
test(sos): move masked-reformulation test to test_sos_reformulation
FBumann d76fa65
refactor(sos): extract _iter_sos_sets to deduplicate direct-API plumbing
FBumann 72c441b
test(pwl): parametrize masked-sos2 NaN-padding across solver × io_api
FBumann 7c76473
refactor(sos): numpy-only _iter_sos_sets, inline _sos_set_positions
FBumann 281b47b
fix(sos): pass Python lists to Xpress addSOS
FBumann 6c0837f
fix(sos): convert numpy args to lists at Gurobi addSOS boundary
FBumann f4d5d38
fix(test): annotate SOS fixture vars to satisfy mypy
FBumann cc6b0dc
test(sos): cover empty-slice skip in LP writer
FBumann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -424,16 +424,26 @@ def sos_to_file( | |
|
|
||
| for name in names: | ||
| var = m.variables[name] | ||
| sos_type = var.attrs[SOS_TYPE_ATTR] | ||
| sos_dim = var.attrs[SOS_DIM_ATTR] | ||
| sos_type = int(var.attrs[SOS_TYPE_ATTR]) # type: ignore[call-overload] | ||
| sos_dim = str(var.attrs[SOS_DIM_ATTR]) | ||
|
|
||
| other_dims = [dim for dim in var.labels.dims if dim != sos_dim] | ||
| for var_slice in var.iterate_slices(slice_size, other_dims): | ||
| ds = var_slice.labels.to_dataset() | ||
| ds["sos_labels"] = ds["labels"].isel({sos_dim: 0}) | ||
| # Per-set id: max of labels along the SOS dim. Real labels are | ||
| # non-negative and globally unique, so max yields a valid, | ||
| # unique-per-set id whenever the set has any unmasked slot. | ||
| # Fully-masked sets get id -1 and are filtered out below. | ||
| ds["sos_labels"] = ds["labels"].max(sos_dim) | ||
| ds["weights"] = ds.coords[sos_dim] | ||
| df = to_polars(ds) | ||
|
|
||
| # Drop masked member rows so the LP file never emits `x-1`, and | ||
| # drop any rows belonging to a fully-masked set (sos_labels == -1). | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove that
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made it shorter. We can drop fully if you prefer |
||
| df = df.filter((pl.col("labels") != -1) & (pl.col("sos_labels") != -1)) | ||
| if df.is_empty(): | ||
| continue | ||
|
|
||
| df = df.group_by("sos_labels").agg( | ||
| pl.concat_str( | ||
| *print_variable(pl.col("labels")), pl.lit(":"), pl.col("weights") | ||
|
|
@@ -593,8 +603,6 @@ def to_file( | |
| """ | ||
| Write out a model to a lp or mps file. | ||
| """ | ||
| m._check_sos_unmasked() | ||
|
|
||
| if fn is None: | ||
| fn = Path(m.get_problem_file()) | ||
| if isinstance(fn, str): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.