Skip to content

Conversation

jbaayen
Copy link
Collaborator

@jbaayen jbaayen commented Jul 4, 2025

Initial constraints are only required when an incomplete set of initial states is provided. If the set of initial states is either complete or "sufficiently" complete (e.g., values of all differentiated states are provided), then initial constraints may or may not be required depending on the use case.

This change introduces an option that controls explicitly whether or not the initial constraints are created.

…l constraints

Initial constraints are only required when an incomplete set of initial states is provided.
If the set of initial states is either complete or "sufficiently" complete (e.g., values
of all differentiated states are provided), then initial constraints may or may not be
required depending on the use case.

This change introduces an option that controls explicitly whether or not the initial
constraints are created.
@jbaayen jbaayen requested review from jackvreeken and jarsarasty July 4, 2025 18:32
@jarsarasty jarsarasty added the enhancement New feature or request label Jul 8, 2025
@jarsarasty jarsarasty added this to the 2.8 milestone Jul 8, 2025
@jarsarasty jarsarasty requested a review from Ailbhemit July 15, 2025 07:33
@jarsarasty
Copy link
Contributor

@jbaayen
It appears that a few conflicts need to be resolved in this branch.

Copy link
Contributor

@jarsarasty jarsarasty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jbaayen ,

Thank you for this pull request, which extends the flexibility of the model specification in RTC-Tools.

Please consider adding validation to ensure that when add_initial_constraints=False, all initial states must be fully specified.
When add_initial_constraints=True and all initial states are specified, a warning could be issue to inform the user that the system may be overdetermined.

Please note that some conflicts in the test_modelica_mixin file need to be resolved.

:cvar check_collocation_linearity:
If ``True``, check whether collocation constraints are linear. Default is ``True``.
:cvar add_initial_constraints:
If ``True``, add constraints for initial conditions. Default is ``True``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be a bit more concise:

If True (default), add constraints for initial conditions

any model equations that are collocated must be linear. By default, all
model equations are collocated, and linearity of the model equations is
verified. Working with non-linear models is possible, but discouraged.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may sound redundant, but adding a note like this one—or one that's a bit shorter—might be helpful:
When the initial states are fully specified, you can disable the initial constraints by setting the class variable add_initial_constraints to False when constructing the problem. Only disable initial constraints if all initial state variables have been fully and explicitly specified as part of the problem setup (e.g., through initial conditions or history). If any initial state is missing and initial constraints are disabled, the problem may become underdetermined, and the optimization may produce incorrect results.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds an option to disable initial constraints in the CollocatedIntegratedOptimizationProblem class. This allows users to control whether initial constraints are created, which is useful when a complete set of initial states is provided and initial constraints may lead to an overdetermined system.

  • Add add_initial_constraints class variable to control initial constraint generation
  • Conditionally execute initial constraint-related code based on this flag
  • Add comprehensive tests to verify both enabled and disabled behavior

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/rtctools/optimization/collocated_integrated_optimization_problem.py Implements the core functionality by adding the add_initial_constraints flag and wrapping constraint generation logic with conditional checks
tests/optimization/test_modelica_mixin.py Adds test cases to verify the new functionality works correctly with and without initial constraints
Comments suppressed due to low confidence (1)

src/rtctools/optimization/collocated_integrated_optimization_problem.py:1

  • The comment reformatting appears to be unintentional. The original single-line comment on line 614 should be kept instead of being split and duplicated across lines 615-616.
import itertools

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +627 to +629
init_der = (h.values[-1] - h.values[-2]) / (
h.times[-1] - h.times[-2]
)
Copy link
Preview

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line break formatting change appears unrelated to the PR's purpose. The original single-line format should be preserved to maintain consistency with the existing codebase.

Suggested change
init_der = (h.values[-1] - h.values[-2]) / (
h.times[-1] - h.times[-2]
)
init_der = (h.values[-1] - h.values[-2]) / (h.times[-1] - h.times[-2])

Copilot uses AI. Check for mistakes.

:cvar check_collocation_linearity:
If ``True``, check whether collocation constraints are linear. Default is ``True``.
:cvar add_initial_constraints:
If ``True``, add constraints for initial conditions. Default is ``True``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it also be made explicit to which variables this refers to: both constant and controlled inputs?

Copy link
Contributor

@Ailbhemit Ailbhemit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and useful feature. One small request to make clear in documentation that all initial conditions will not be picked up and added as constraints.

@FaridAlavi
Copy link
Contributor

FaridAlavi commented Sep 4, 2025

After resolving the conflict and addressing the previous comments, this merge can be completed.

@jarsarasty
Copy link
Contributor

Currently SonarCloud Quality Gate failes due to test coverage. It seems that SonarCloud needs to read the actual coverage report file when it runs the scan. If the file is not present during the SonarCloud scan job, it will default to 0% coverage, regardless of what the logs show.
We may need to update the workflow to make the coverage report available to the SonarQube scan. Interestingly, we haven't seen this kind of issue in other pull requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants