feat: Add relax() and unrelax() to Variable and Variables#634
Merged
feat: Add relax() and unrelax() to Variable and Variables#634
Conversation
Add methods to relax integrality of binary/integer variables to continuous, enabling LP relaxation of MILP models. Supports partial relaxation of individual variables or filtered views (e.g. m.variables.integers.relax()). Semi-continuous variables raise NotImplementedError since their relaxation requires bound changes. Refactors fix(relax=True) to delegate to relax(), removing the relax parameter from fix() to avoid redundancy. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
Author
|
@FabianHofmann sorry for the noisy notebook! The rest should be easy to review. |
Collaborator
Author
|
I'll need to adjust this before merge. |
- Fix docstring: overwrite default is True, not False - Cache attrs lookups in relax() to avoid redundant property calls - Guard fix() when no solution is available (clear ValueError) - Decouple unfix() from unrelax() — they are independent operations - Use `self` instead of `1 * self` in add_constraints - Clean up unnecessary f-string prefixes - Add no-op docstring note for continuous variables in relax() - Add tests for fix-without-solution and independent relax/unfix Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Store the attr name ("binary"/"integer") directly in the registry and
use it as the key for both clearing and restoring, eliminating
conditional branches.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Consistent with .binaries, .integers, .continuous — enables chaining like m.variables.relaxed.unrelax() and m.variables.fixed.unfix(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- m.variables.relaxed.unrelax() chain - m.variables.fixed.unfix() chain - Double relax preserves original type in registry Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
Author
|
@FabianHofmann ready |
Collaborator
The code does not show this. can you check if that is intended? it seems independent from each other |
Collaborator
Author
|
@FabianHofmann Thats was a stale statement from the PR description! |
FabianHofmann
approved these changes
Mar 26, 2026
Collaborator
FabianHofmann
left a comment
There was a problem hiding this comment.
Feel free to merge
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow up on #625
Summary
relax()/unrelax()/relaxedtoVariableandVariables, enabling LP relaxation of MILP modelsm.variables.integers.relax(),m.variables.binaries.relax(), or individualm.variables["x"].relax()NotImplementedError(their LP relaxation requires bound changes, not just a flag flip)relaxparameter fromfix()— the canonical workflow is nowfix()thenrelax()as separate, composable operationsUsage
This is sort of a refactor of #625 which works fine as not released yet!
Test plan
Variable.relax()/unrelax()on binary, integer, continuous, and semi-continuousVariables.relax()/unrelax()on full and filtered containersm.type)test_fix_relax.pypass🤖 Generated with Claude Code