feat: support optimal control problems with zero-dimensional control#246
Merged
feat: support optimal control problems with zero-dimensional control#246
Conversation
- 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.
- 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.
Member
Author
|
@jbcaillau Please review and merge. |
Contributor
18 tasks
Update version number for release with zero control dimension support.
Member
Author
|
This Line 1426 in 4bd9932 may be problematic. Here is an error I have: ExpGrowth: Error During Test at /Users/ocots/Research/logiciels/dev/control-toolbox/OptimalControl/test/suite/solve/test_canonical.jl:221
Got exception outside of a @test
FieldError: type Nothing has no field `offset`; Nothing has no fields at all.
Stacktrace:
[1] getproperty
@ ./Base_compiler.jl:54 [inlined]
[2] solution(result::SolverCore.GenericExecutionStats{Float64, Vector{Float64}, Vector{Float64}, Any}, x::Nothing)
@ ExaModels ~/.julia/packages/ExaModels/IFdue/src/nlp.jl:1273
[3] (::Main.TestCanonical.TestProblems.var"#ExponentialGrowth##32#ExponentialGrowth##33"{Main.TestCanonical.TestProblems.var"#ExponentialGrowth##34#ExponentialGrowth##35"{Int64, Type{Float64}, Vector{ExaModels.Constraint}, ExaModels.Variable{Tuple{Int64, UnitRange{Int64}}, Int64}, ExaModels.Variable{Tuple{Int64}, Int64}}})(sol::SolverCore.GenericExecutionStats{Float64, Vector{Float64}, Vector{Float64}, Any}; val::Symbol)
@ Main.TestCanonical.TestProblems ~/.julia/packages/CTParser/4zcBf/src/onepass.jl:1426
[4] build_OCP_solution(docp::CTDirect.DOCP{CTDirect.Midpoint, CTModels.OCP.Model{CTModels.OCP.NonAutonomous, CTModels.OCP.TimesModel{CTModels.OCP.FixedTimeModel{Int64}, CTModels.OCP.FixedTimeModel{Int64}}, CTModels.OCP.StateModel, CTModels.OCP.EmptyControlModel, CTModels.OCP.VariableModel, CTModels.OCP.var"#dyn!#__build_dynamics_from_parts##0"{Vector{Tuple{UnitRange{Int64}, Function}}}, CTModels.OCP.LagrangeObjectiveModel{Main.TestCanonical.TestProblems.var"#fun##1088#ExponentialGrowth##3"{Main.TestCanonical.TestProblems.var"#data#ExponentialGrowth##0"}}, CTModels.OCP.ConstraintsModel{Tuple{Vector{Float64}, CTModels.OCP.var"#path_cons_nl!#build##1"{Tuple{}, Vector{Int64}, Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Main.TestCanonical.TestProblems.var"#fun##1076#ExponentialGrowth##1", Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}}, Main.TestCanonical.TestProblems.var"#ExponentialGrowth##4#ExponentialGrowth##5"{Main.TestCanonical.TestProblems.var"#ExponentialGrowth##6#ExponentialGrowth##7"{Main.TestCanonical.TestProblems.var"#data#ExponentialGrowth##0"}}}}, nlp_solution::SolverCore.GenericExecutionStats{Float64, Vector{Float64}, Vector{Float64}, Any}, T::Vector{Float64}, objective::Float64, iterations::Int64, constraints_violation::Float64, message::String, status::Symbol, successful::Bool; exa_getter::Function)
@ CTDirect ~/.julia/packages/CTDirect/HXHev/src/DOCP_data.jl:544
[5] (::CTDirect.var"#build_exa_solution#build_exa_solution##0"{CTDirect.DOCP{CTDirect.Midpoint, CTModels.OCP.Model{CTModels.OCP.NonAutonomous, CTModels.OCP.TimesModel{CTModels.OCP.FixedTimeModel{Int64}, CTModels.OCP.FixedTimeModel{Int64}}, CTModels.OCP.StateModel, CTModels.OCP.EmptyControlModel, CTModels.OCP.VariableModel, CTModels.OCP.var"#dyn!#__build_dynamics_from_parts##0"{Vector{Tuple{UnitRange{Int64}, Function}}}, CTModels.OCP.LagrangeObjectiveModel{Main.TestCanonical.TestProblems.var"#fun##1088#ExponentialGrowth##3"{Main.TestCanonical.TestProblems.var"#data#ExponentialGrowth##0"}}, CTModels.OCP.ConstraintsModel{Tuple{Vector{Float64}, CTModels.OCP.var"#path_cons_nl!#build##1"{Tuple{}, Vector{Int64}, Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Main.TestCanonical.TestProblems.var"#fun##1076#ExponentialGrowth##1", Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}}, Main.TestCanonical.TestProblems.var"#ExponentialGrowth##4#ExponentialGrowth##5"{Main.TestCanonical.TestProblems.var"#ExponentialGrowth##6#ExponentialGrowth##7"{Main.TestCanonical.TestProblems.var"#data#ExponentialGrowth##0"}}}}})(nlp_solution::SolverCore.GenericExecutionStats{Float64, Vector{Float64}, Vector{Float64}, Any})
@ CTDirect ~/.julia/packages/CTDirect/HXHev/src/collocation.jl:235with this problem # observed data (analytical solution)
data(t) = 2.0 * exp(0.5 * t)
@def ocp begin
p ∈ R, variable # growth rate to estimate
t ∈ [0, 10], time
x ∈ R, state
x(0) == 2.0
ẋ(t) == p * x(t)
∫((x(t) - data(t))^2) → min # fit to observed data
endWe need to test this. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #246 +/- ##
==========================================
- Coverage 98.21% 98.20% -0.01%
==========================================
Files 4 4
Lines 1062 1060 -2
==========================================
- Hits 1043 1041 -2
Misses 19 19 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
Author
Should be solved by next commit. |
- Add isnothing checks in onepass.jl getter for control, control_l, control_u - Add comprehensive test for getter function without control - Prevent FieldError when dim_u is nothing - Bump version to 0.8.12-beta
Member
Author
|
Désolé @jbcaillau je merge et fais une nouvelle release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📖 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
p_dynamics!,p_dynamics_coord!,p_lagrange!, andp_bolza!functionsp_control!to prevent declaring control after dynamics/cost are definedTechnical Improvements
replace_callfunction to handleVector{Union{Nothing, Symbol}}efficientlyNothingsymbols in replacement loops for better performanceComprehensive Testing
🔄 Behavior Changes
✅ Now Supported
❌ Still Prevented
🧪 Testing
📈 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.