Skip to content

A merge in steps nests the brackets of the objective #797

Description

@FBumann

Prompt: I think we should do an issue about the objective parenthesis...?

Note

The following content was generated by AI.

merge puts each objective in brackets. A merge in steps therefore nests the brackets: ((a) + (b)) + (c), where one list gives (a) + (b) + (c). The canonical text is the same, so only the written file differs.

Reproduction, proposed change, scope

Reproduction. Found while testing merges in steps for #763, on feat/adds-to at 09cb6f2. main builds the objective the same way, in _summed_objective. The fixtures are in tests/test_terms.py, and dem and obj each add one objective:

merge([merge([BALANCE, dem, FLEET]), obj]).objective.expression
# ((sum(shed)) + (sum(gen_p))) + (sum(store_p))
merge([BALANCE, dem, FLEET, obj]).objective.expression
# (sum(shed)) + (sum(gen_p)) + (sum(store_p))

The two specs give the same canonical_yaml.

Proposed change. Join the objectives with a plain +, as #763 does for the terms of a sum. + and - are the loosest operators in the arithmetic grammar (_build_grammar in _expression_parser.py), so the join never changes how a term groups, and a term such as -x parses as a + -x.

What the change touches. Taken with a throwaway edit on 09cb6f2:

  • _summed_objective: one line.
  • Two tests fail and need the new expected text: test_the_objectives_are_summed_each_term_parenthesised and test_several_files_page.py::test_every_python_step_prints_what_the_page_shows.
  • Prose that says "each term in parentheses": docs/howto/compose.md (two places), docs/examples/library/composed.md, and the module docstring and _summed_objective's docstring in composition.py.

Every merged objective prints without brackets, including a one-list merge. That is a change a reader of the changelog can see, so it gets its own PR and changelog line, not a line in #763.

Not checked. Whether a generated page prints a merged objective. The suite passed with the edit, except for the two tests above.

Version. main at v0.2.0-23-gabe99679.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JiNwuVCdEkSLKNxiCNaNAv

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compositionAssembling a model from files, and fixing a decision

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions