Skip to content

Hide the generated labels of unnamed constraints in plots (#526) - #534

Merged
ocots merged 2 commits into
mainfrom
fix/526-unnamed-labels-plot
Nov 22, 2026
Merged

ocots merged 2 commits into
mainfrom
fix/526-unnamed-labels-plot

Conversation

@ocots

@ocots ocots commented Nov 22, 2026

Copy link
Copy Markdown
Member

Closes #526.

Problem

A constraint written without a label in @def gets a generated label, and the plot shows it: u (label##288). CTParser generates label##N, outside the ##unnamed convention of CTModels, and the plot wrote the label as is.

Changes

  • CTModels.Models.is_unnamed_label(label): one predicate for "generated label". A generated label comes from gensym, so its name contains ##. That covers both ##unnamed#N (CTModels) and label##N (CTParser), without waiting for a CTParser change. A label given by the user is a Julia identifier and never contains ##.

  • Plot (PlotCase, shared by Plots and Makie): an unnamed constraint is named after what is known of it.

    constraint left cell dual alone
    box labelled :x2_box x₂ (x2_box) dual x2_box
    unnamed box on x₂ box x₂ dual box x₂
    path labelled :c1 c1 dual c1
    unnamed path, row 2 path 2 dual path 2

    A generic name holds a space, so it can never be mistaken for a label; parentheses stay reserved for real labels.

  • Model signature: _label_strings uses the same predicate. A solution of a model with unnamed constraints written by CTParser could not be imported against a new evaluation of @def (the generated labels differ): it now can. The labels read from the file are normalized too, so files written before this change are accepted as well.

Checked, unchanged

  • The display of a model does not show constraint labels.
  • The messages of dual(sol, ocp, label) show the label passed by the caller.

Tests

  • test/suite/models/test_unnamed_labels.jl (new): the predicate (generated / user labels, @inferred), _label_strings, and the signature of two instances of a model with generated labels (non-regression).
  • test/suite/plotting/test_plot_rows.jl: the naming helpers; a figure with a labelled path constraint, a two-row unnamed path constraint, an unnamed state box and a control box labelled as CTParser does. No ## in any ylabel, and the duals plotted alone are named too.
  • test/suite/serialization/test_import_model_mismatch.jl: import validation is silent across instances with generated labels, including an old file holding label##288.

Follow-up

On CTParser, have an unnamed constraint take CTModels' default label, so that one convention holds. Not required for this fix.

Conventions: ct-conventions v0.14.0, ct-agents v0.5.0.

🤖 Generated with Claude Code

A constraint written without a label gets a generated one (`##unnamed#N`
from CTModels, `label##N` from CTParser), and its plot row read
`u (label##288)`. `Models.is_unnamed_label` now recognises any generated
label (a `gensym`, whose name contains `##`), and the plot names such a
row after what is known: `box u`, `path 2`, `dual box u`, `dual path 2`.

The model signature uses the same predicate, so a file of a model with
unnamed constraints written by CTParser can be imported against a new
instance of the model, including files written before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ocots

ocots commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Verification

  • Full suite: 5369 / 5369 pass (Julia 1.12).
  • Docs build: clean on the second pass. The first pass on a fresh checkout cannot resolve the @extref self-links to the new symbols (is_unnamed_label, _box_name, …) because the published inventory doesn't have them yet; that is the usual InterLinks bootstrap.
  • typos src test docs/src ext: clean. JuliaFormatter v2 (blue style): no change outside the touched files.
  • Not on the hot path (plotting and import validation only), so no JET or allocation guard. is_unnamed_label is checked with @inferred.

Design note: generic names

The generic names come from what is known about the constraint: its kind, plus the component it bounds or its row in the stacked path constraint. They contain a space, which a label (a Julia identifier) cannot, and parentheses stay reserved for real labels. So box u can never be confused with a label, while u (u_box) keeps its meaning.

…526)

The messages raised when the duals given by label to `build_solution`, or
their limits at the switching times, are invalid showed the generated label
of an unnamed constraint (`:label##288`), and suggested
`constraint_rows(model, :label##288)`, which is no valid Julia.

`Models._describe_constraint` names a constraint for a message: its kind and
label (`path constraint :c1`), or, for an unnamed one, what is known of it
(`unnamed path constraint (rows 2:3)`, `unnamed control box on u`). The
switching model, which knows no model, says `unnamed constraint dual`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ocots

ocots commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Second phase: the messages of build_solution

While checking the other places where labels are shown, I found more leaks than the first comment said. The model and solution displays show no label, and the messages of dual(sol, ocp, label) / constraint_rows(model, label) repeat the label the caller typed. But the messages of build_solution(...; duals=Dict(label => ...)) are a different case. There, the labels come from the producer (CTDirect walks every label of the model, generated ones included), so an invalid dual showed:

dual size mismatch for the constraint :label##288
Hint  ... `CTModels.Models.constraint_rows(model, :label##288)` gives its rows.

That message is aimed at the developer of the producer, but the user is the one who reads it at solve time, and the suggested call is not valid Julia.

Change

Models._describe_constraint(model, label[, kind, rows]) names a constraint in a message, with the same logic as the plot:

constraint description
labelled path constraint :c1, state box :x2_box
unnamed nonlinear unnamed path constraint (rows 2:3), unnamed boundary constraint (row 1)
unnamed box unnamed control box on u, unnamed variable box on v₁, v₂

It is used in every message of the labelled duals (_checked_timed_dual, _checked_static_dual, _ensure_dual_size, _claim_components!) and in the placement of the limits at the switching times (_place_dual_limits). The constraint_rows(model, :label) hint is kept for a real label only. SwitchingModel, built before any model is known, says unnamed constraint dual.

Example:

IncorrectArgument → _ensure_dual_size
│  dual size mismatch for the unnamed path constraint (rows 2:3)
│  Got       number of columns = 1
│  Expected  2 (the number of rows of the unnamed path constraint (rows 2:3))
│  Hint      Give one entry per row of the unnamed path constraint (rows 2:3).

A labelled constraint now also gets its kind in the message (dual size mismatch for the path constraint :g instead of for the constraint :g).

Tests

test/suite/models/test_unnamed_labels.jl now has a fixture with unnamed constraints of every kind (path on two rows, boundary, aliased state boxes, CTParser-style control box, variable box on two components). It tests _describe_constraint (@inferred, unknown label) and checks that every message above contains no ##.

Verification

Full suite 5407 / 5407. Docs clean on the second pass (new symbol, the usual InterLinks bootstrap). typos clean, JuliaFormatter v2 clean.

@ocots
ocots marked this pull request as ready for review November 22, 2026 17:43
@ocots
ocots merged commit c847fa4 into main Nov 22, 2026
3 checks passed
@ocots
ocots deleted the fix/526-unnamed-labels-plot branch November 22, 2026 17:43
@github-actions github-actions Bot mentioned this pull request Nov 25, 2026
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.

Plot shows generated labels of unnamed constraints ("u (label##288)")

1 participant