Skip to content

Piecewise linear constraints: follow-up improvements#602

Open
FabianHofmann wants to merge 10 commits intomasterfrom
piecewise-followup
Open

Piecewise linear constraints: follow-up improvements#602
FabianHofmann wants to merge 10 commits intomasterfrom
piecewise-followup

Conversation

@FabianHofmann
Copy link
Collaborator

@FabianHofmann FabianHofmann commented Mar 6, 2026

Changes proposed in this Pull Request

Strongly needed follow-up improvements to the piecewise linear constraints feature:

  • Introduce piecewise function to create a PiecewiseExpression which can be used in operations like y == piecewise(x, x_pts, y_pts)
  • Make use case breakpoints and segments (new) clearer.
  • Apply convexity checks where needed (LP formulation)
  • Documentation: Expanded doc/piecewise-linear-constraints.rst and updated notebook

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

…ts API, LP formulation for convex/concave cases, and simplify tests
…l formulation, add domain bounds to LP formulation

- Incremental method now uses binary indicator variables with link/order constraints to enforce proper segment filling order (Markowitz & Manne)
- LP method now adds x ∈ [min(xᵢ), max(xᵢ)] domain bound constraints to prevent extrapolation beyond breakpoints
Validate trailing-NaN-only for SOS2 and disjunctive methods to prevent
corrupted adjacency. Fail fast when skip_nan_check=True but breakpoints
actually contain NaN.
Support reversed syntax (y == piecewise(...)) via __le__/__ge__/__eq__
dispatch in BaseExpression and ScalarLinearExpression. Fix LP example
to use power == demand for more illustrative results.
- Add @overload to comparison operators (__le__, __ge__, __eq__) in
  BaseExpression and Variable to distinguish PiecewiseExpression from
  SideLike return types
- Update ConstraintLike type alias to include PiecewiseConstraintDescriptor
- Fix PiecewiseConstraintDescriptor.lhs type from object to LinExprLike
- Fix dict/sequence type mismatches in _dict_to_array, _dict_segments_to_array,
  _segments_list_to_array
- Remove unused type: ignore comments
- Narrow ScalarLinearExpression/ScalarVariable return types to not include
  PiecewiseConstraintDescriptor (impossible at runtime)
@FabianHofmann FabianHofmann requested a review from coroa March 6, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant