Skip to content

feat: support optimal control problems with zero-dimensional control#246

Open
ocots wants to merge 6 commits intomainfrom
dim-zero-control
Open

feat: support optimal control problems with zero-dimensional control#246
ocots wants to merge 6 commits intomainfrom
dim-zero-control

Conversation

@ocots
Copy link
Member

@ocots ocots commented Mar 21, 2026

📖 Summary

This PR introduces support for non-controlled optimal control problems (zero-dimensional control) by removing the artificial requirement that control variables must be declared before dynamics and cost criteria. The change enables mathematically valid problems without control variables while maintaining logical declaration order consistency.

🎯 Key Changes

Parser Flexibility

  • Remove mandatory control validation in p_dynamics!, p_dynamics_coord!, p_lagrange!, and p_bolza! functions
  • Add reverse validation in p_control! to prevent declaring control after dynamics/cost are defined
  • Maintain declaration order logic while allowing autonomous problems

Technical Improvements

  • Enhanced replace_call function to handle Vector{Union{Nothing, Symbol}} efficiently
  • Skip Nothing symbols in replacement loops for better performance
  • Improved type stability for variable substitution in autonomous systems

Comprehensive Testing

  • Complete test suite for zero control dimension scenarios
  • Error handling validation for invalid control initialization attempts
  • Integration tests for solution building and serialization without control
  • Module isolation improvements in test runner configuration

🔄 Behavior Changes

Now Supported

# Autonomous optimal control problems
CTParser.@def begin
    t  [0, 1], time
    x  R², state
    (t) = [x₂(t), -x₁(t)]  # No control required
    x₁(1)^2  min
end

Still Prevented

# Invalid declaration order
CTParser.@def begin
    t  [0, 1], time
    x  R², state
    (t) = [x₂(t), u(t)]     # Dynamics first
    u  R, control            # Control after dynamics ❌
    x₁(1)^2  min
end

🧪 Testing

  • 27 test cases covering all zero-control scenarios
  • Error validation for improper control usage
  • Integration verification with solution building pipeline
  • Backwards compatibility maintained for existing control problems

📈 Impact

This change expands CTParser.jl's capability to handle the full spectrum of optimal control problems, from fully controlled to completely autonomous systems, while preserving the logical structure that prevents confusing mixed declaration patterns.

- Remove mandatory control declaration requirement in parser
- Fix replace_call method signature for Vector{Union{Nothing, Symbol}} compatibility
- Add comprehensive test suite for zero control dimension cases
- Update test runner configuration for better module isolation

This enables definition of autonomous optimal control problems without control
variables, which was previously blocked by parser validation logic.
ocots added 5 commits March 21, 2026 14:12
- Add check to avoid unnecessary has/subs calls on Nothing symbols
- Use eachindex(x) instead of 1:length(x) for better performance
- Skip processing of Nothing symbols in variable replacement loop

This optimization improves performance when handling control variables
that may be Nothing in autonomous optimal control problems.
- Add validation in p_control! to ensure control is declared before dynamics and cost
- Control cannot be declared after dynamics (is_global_dyn or is_coord_dyn)
- Control cannot be declared after cost criterion (criterion is set)
- Maintains consistency while allowing autonomous problems (dynamics without control)

This prevents mixed declaration patterns while preserving flexibility for zero-control problems.
- Remove duplicate test for initial_control with nothing
- The functionality is already covered in the 'initial_guess without control' test
- Streamline test suite by eliminating redundant test cases
- Add declaration order validation tests
- Add coordinate dynamics without control tests
- Add advanced cost criteria tests (Lagrange, Bolza)
- Add constraints without control tests
- All 24 tests now pass successfully

This provides comprehensive coverage for autonomous optimal control problems
and validates the new flexible declaration order logic.
- Add test for state initialization without control
- Add test for variable initialization with zero control
- Update get_model function to support variable parameter
- All 27 tests now pass successfully

Enhances test coverage for initialization scenarios in autonomous
optimal control problems.
@ocots
Copy link
Member Author

ocots commented Mar 21, 2026

@jbcaillau Please review and merge.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 21, 2026

Breakage test results
Date: 2026-03-21 14:01:02

Name Latest Stable
OptimalControl compat: v0.8.10-beta compat: v0.8.10-beta

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.

2 participants