feat: PiecewiseFormulation return type, model repr, rename to add_piecewise_formulation#642
Conversation
7782b04 to
648bf24
Compare
e953159 to
8de5277
Compare
…iecewise_formulation - Add PiecewiseFormulation dataclass grouping all auxiliary variables and constraints created by a piecewise formulation - Add _groups registry on Model to track grouped artifacts - Model repr hides grouped items from Variables/Constraints sections and shows them in a new "Groups" section - Rename add_piecewise_constraints -> add_piecewise_formulation - Export PiecewiseFormulation from linopy Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Reorder cells so add_piecewise_formulation is the last statement, letting Jupyter display the PiecewiseFormulation repr automatically. Add print(m) cell to show the grouped model repr. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Split tangent_lines cell so its LinearExpression repr is displayed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…roups PiecewiseFormulation now shows full dims (including internal) for each variable and constraint. Model groups section shows "over (dim1, dim2)" for user-facing dims only. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Match style of Variables/Constraints containers which don't show counts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…name Replace generic "Groups" with "Piecewise Formulations" in Model repr. Rename internal registry and helper to match. 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>
Match Constraint repr pattern: `name` instead of 'name'. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Match Constraint repr style: `name` [dim: size, ...] — method Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove jetTransient metadata and normalize cell format. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
PiecewiseFormulation now stores variable/constraint names as strings with a model reference. Properties return live Views on access. This makes serialization trivial — persist as JSON in netcdf attrs, reconstruct on load. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
dd60a4a to
0b482ee
Compare
- PWL_X_LINK_SUFFIX/_Y_LINK_SUFFIX → PWL_LINK_SUFFIX (N-var, single link)
- PWL_BINARY_SUFFIX → PWL_SEGMENT_BINARY_SUFFIX (disjunctive segment selection)
- PWL_INC_BINARY_SUFFIX → PWL_ORDER_BINARY_SUFFIX (incremental ordering)
- PWL_INC_LINK_SUFFIX → PWL_DELTA_BOUND_SUFFIX (δ ≤ binary)
- PWL_INC_ORDER_SUFFIX → PWL_BINARY_ORDER_SUFFIX (binary_{i+1} ≤ δ_i)
- PWL_FILL_SUFFIX → PWL_FILL_ORDER_SUFFIX (δ_{i+1} ≤ δ_i)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
e2e1a63 to
18af4d7
Compare
for more information, see https://pre-commit.ci
|
More thoughts about the Formulation concept On the Formulation conceptA This is intentional. Solvers don't speak "formulations" — they speak variables, constraints, bounds. Even Gurobi's It also means formulations are composable: piecewise with Key design point: Formulation classes are never constructed by the user. They are result views returned by This pattern could generalize naturally to other |
I thought a bit about user ergonomics after creation for the piecewise stuff. I would like to keep the improvements surgical and only for inspecting, not for storage or behaviour. I decided that adding some sort of filtering in
__repr__methods and a basic registry onModelis probably the most surgical solution. I also added a dataclassPiecewiseFormulationas a return time for the renamedadd_piecewise_formulation(), which makes it more prominent whats actually happening: Adding a complex formulation, not a regular constraint!.This allows the user to inspect the created variables and constraints through a higher level object/view. This also feels quite usable for other Formulation concepts, like the existing internal SOS-Reformulations or others to come. One issue i see is that we categorize variables/constraints into 2 levels - the user defined one and the one created internally through Formulations. I like the concept and dont think there is anything wrong with it, but i think its an important sidenote.
Happy to hear your thoughts on it!
Summary
PiecewiseFormulationclass as return type ofadd_piecewise_formulation()(renamed fromadd_piecewise_constraints)variables/constraintsproperties return live views_piecewise_formulationsregistry onModelwith IO persistence (netcdf round-trip)add_piecewise_formulation)PiecewiseFormulationfromlinopyPiecewiseFormulation repr:
Model repr:
Access:
Suffix renames
Internal PWL suffix constants renamed for clarity:
_x_link/_y_link_link_binary_segment_binary_inc_binary_order_binary_inc_link_delta_bound_inc_order_binary_order_fill_fill_orderUnchanged:
_lambda,_convex,_delta,_select,_active_boundTest plan
pytest test/test_piecewise_constraints.py— 108 passedpytest test/test_model.py— 18 passedmypy . --exclude build— cleanruff check— clean🤖 Generated with Claude Code