Skip to content

Commit 984f09c

Browse files
committed
refactor: remove alias :i for initial_guess, DRY with _INITIAL_GUESS_ALIASES/_INITIAL_GUESS_ALIASES_ONLY constants
1 parent cfae130 commit 984f09c

8 files changed

Lines changed: 29 additions & 73 deletions

File tree

src/helpers/component_completion.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using DocStringExtensions
2-
31
"""
42
$(TYPEDSIGNATURES)
53

src/helpers/descriptive_routing.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using DocStringExtensions
2-
31
# ============================================================================
42
# Descriptive mode routing helpers
53
# ============================================================================
@@ -22,6 +20,12 @@ using DocStringExtensions
2220
const _DEFAULT_DISPLAY::Bool = true
2321
const _DEFAULT_INITIAL_GUESS::Nothing = nothing
2422

23+
# Aliases for initial_guess (single source of truth)
24+
# _INITIAL_GUESS_ALIASES_ONLY : used in OptionDefinition (name is separate)
25+
# _INITIAL_GUESS_ALIASES : used in _extract_action_kwarg (includes primary name)
26+
const _INITIAL_GUESS_ALIASES_ONLY::Tuple{Symbol} = (:init,)
27+
const _INITIAL_GUESS_ALIASES::Tuple{Symbol, Symbol} = (:initial_guess, :init)
28+
2529
# Unwrap an OptionValue (from route_all_options) to its raw value.
2630
# Falls back to `fallback` if `opt` is not an OptionValue.
2731
_unwrap_option(opt::CTSolvers.OptionValue, fallback) = opt.value
@@ -102,7 +106,7 @@ function _descriptive_action_defs()::Vector{CTSolvers.OptionDefinition}
102106
return [
103107
CTSolvers.OptionDefinition(
104108
name = :initial_guess,
105-
aliases = (:init, :i),
109+
aliases = _INITIAL_GUESS_ALIASES_ONLY,
106110
type = Any,
107111
default = _DEFAULT_INITIAL_GUESS,
108112
description = "Initial guess for the OCP solution",

src/solve/explicit.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ then completes missing components via the registry before calling Layer 3.
1010
- `ocp`: The optimal control problem to solve
1111
- `registry`: Strategy registry for completing partial components
1212
- `kwargs...`: All keyword arguments. Action options extracted here:
13-
- `initial_guess` (aliases: `init`, `i`): Initial guess, default `nothing`
13+
- `initial_guess` (aliases: `init`): Initial guess, default `nothing`
1414
- `display`: Whether to display configuration information, default `true`
1515
- Typed components: `discretizer`, `modeler`, `solver` (identified by abstract type)
1616
@@ -30,7 +30,7 @@ function solve_explicit(
3030

3131
# Extract action options with alias support
3232
init_raw, kwargs1 = _extract_action_kwarg(
33-
kwargs, (:initial_guess, :init, :i), _DEFAULT_INITIAL_GUESS
33+
kwargs, _INITIAL_GUESS_ALIASES, _DEFAULT_INITIAL_GUESS
3434
)
3535
display_val, _ = _extract_action_kwarg(
3636
kwargs1, (:display,), _DEFAULT_DISPLAY

test/suite/helpers/test_kwarg_extraction.jl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,41 +102,38 @@ function test_kwarg_extraction()
102102
Test.@testset "Action Kwarg Extraction" begin
103103
Test.@testset "Extracts primary name" begin
104104
kw = pairs((; initial_guess=42, display=false))
105-
val, rest = OptimalControl._extract_action_kwarg(kw, (:initial_guess, :init, :i), nothing)
105+
val, rest = OptimalControl._extract_action_kwarg(kw, OptimalControl._INITIAL_GUESS_ALIASES, nothing)
106106
Test.@test val == 42
107107
Test.@test haskey(rest, :display)
108108
Test.@test !haskey(rest, :initial_guess)
109109
end
110110

111111
Test.@testset "Extracts alias 1" begin
112112
kw = pairs((; init=42, display=false))
113-
val, rest = OptimalControl._extract_action_kwarg(kw, (:initial_guess, :init, :i), nothing)
113+
val, rest = OptimalControl._extract_action_kwarg(kw, OptimalControl._INITIAL_GUESS_ALIASES, nothing)
114114
Test.@test val == 42
115115
Test.@test haskey(rest, :display)
116116
Test.@test !haskey(rest, :init)
117117
end
118118

119-
Test.@testset "Extracts alias 2" begin
119+
Test.@testset "No alias 'i' (removed)" begin
120120
kw = pairs((; i=42, display=false))
121-
val, rest = OptimalControl._extract_action_kwarg(kw, (:initial_guess, :init, :i), nothing)
122-
Test.@test val == 42
121+
val, rest = OptimalControl._extract_action_kwarg(kw, OptimalControl._INITIAL_GUESS_ALIASES, nothing)
122+
Test.@test val === nothing # default, since :i is not recognized
123123
Test.@test haskey(rest, :display)
124-
Test.@test !haskey(rest, :i)
124+
Test.@test haskey(rest, :i) # :i remains in remaining kwargs
125125
end
126126

127127
Test.@testset "Returns default when not found" begin
128128
kw = pairs((; display=false))
129-
val, rest = OptimalControl._extract_action_kwarg(kw, (:initial_guess, :init, :i), :my_default)
129+
val, rest = OptimalControl._extract_action_kwarg(kw, OptimalControl._INITIAL_GUESS_ALIASES, :my_default)
130130
Test.@test val === :my_default
131131
Test.@test haskey(rest, :display)
132132
end
133133

134134
Test.@testset "Throws on multiple aliases present" begin
135-
kw = pairs((; init=42, i=43))
136-
Test.@test_throws CTBase.IncorrectArgument OptimalControl._extract_action_kwarg(kw, (:initial_guess, :init, :i), nothing)
137-
138-
kw2 = pairs((; initial_guess=42, init=43, i=44))
139-
Test.@test_throws CTBase.IncorrectArgument OptimalControl._extract_action_kwarg(kw2, (:initial_guess, :init, :i), nothing)
135+
kw = pairs((; initial_guess=42, init=43))
136+
Test.@test_throws CTBase.IncorrectArgument OptimalControl._extract_action_kwarg(kw, OptimalControl._INITIAL_GUESS_ALIASES, nothing)
140137
end
141138
end
142139
end

test/suite/solve/test_descriptive_routing.jl

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function test_descriptive_routing()
169169
Test.@test defs isa Vector{CTSolvers.Options.OptionDefinition}
170170
Test.@test length(defs) == 2
171171
Test.@test defs[1].name == :initial_guess
172-
Test.@test defs[1].aliases == (:init, :i)
172+
Test.@test defs[1].aliases == (:init,)
173173
Test.@test defs[2].name == :display
174174
end
175175

@@ -392,20 +392,6 @@ function test_descriptive_routing()
392392
)
393393
Test.@test sol isa MockSolution2
394394
end
395-
396-
Test.@testset "solve_descriptive - initial_guess alias 'i'" begin
397-
ocp = MockOCP2()
398-
init = MockInit2()
399-
400-
sol = OptimalControl.solve_descriptive(
401-
ocp, :collocation, :adnlp, :ipopt;
402-
i = init,
403-
display = false,
404-
registry = MOCK_REGISTRY,
405-
)
406-
Test.@test sol isa MockSolution2
407-
end
408-
409395
end
410396
end
411397

test/suite/solve/test_dispatch.jl

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,6 @@ function test_solve_dispatch()
126126
Test.@test result isa MockSolution
127127
end
128128

129-
Test.@testset "solve_explicit - alias 'i'" begin
130-
result = OptimalControl.solve_explicit(
131-
ocp;
132-
i=init,
133-
display=false,
134-
registry=registry,
135-
discretizer=disc, modeler=mod, solver=sol
136-
)
137-
Test.@test result isa MockSolution
138-
end
139-
140129
Test.@testset "solve_explicit - partial components (mock registry completes)" begin
141130
result = OptimalControl.solve_explicit(
142131
ocp;
@@ -172,14 +161,16 @@ function test_solve_dispatch()
172161
Test.@test result isa MockSolution
173162
end
174163

175-
Test.@testset "solve_descriptive - alias 'i'" begin
176-
result = OptimalControl.solve_descriptive(
177-
ocp, :collocation, :adnlp, :ipopt;
178-
i=init,
179-
display=false,
180-
registry=registry
181-
)
182-
Test.@test result isa MockSolution
164+
Test.@testset "solve_descriptive - alias 'i' (removed)" begin
165+
# :i is no longer recognized as an alias for initial_guess
166+
Test.@test_throws CTBase.IncorrectArgument begin
167+
OptimalControl.solve_descriptive(
168+
ocp, :collocation, :adnlp, :ipopt;
169+
i=init,
170+
display=false,
171+
registry=registry
172+
)
173+
end
183174
end
184175

185176
Test.@testset "solve_descriptive - empty description dispatches" begin

test/suite/solve/test_orchestration.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,6 @@ function test_orchestration()
141141
Test.@test result isa MockSolution
142142
end
143143

144-
Test.@testset "solve_descriptive - alias 'i'" begin
145-
ocp = MockOCP()
146-
result = CommonSolve.solve(ocp, :collocation, :adnlp, :ipopt;
147-
i=MockInit(), display=false)
148-
Test.@test result isa MockSolution
149-
end
150-
151144
Test.@testset "solve_descriptive - error on unknown option" begin
152145
ocp = MockOCP()
153146
Test.@test_throws CTBase.IncorrectArgument begin

test/suite/solve/test_solve_modes.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,6 @@ function test_solve_modes()
102102
)
103103
Test.@test sol_init isa OptimalControl.AbstractSolution
104104
end
105-
106-
Test.@testset "Alias 'i'" begin
107-
sol_i = OptimalControl.solve(
108-
pb.ocp,
109-
:collocation;
110-
i=pb.init,
111-
grid_size=20,
112-
max_iter=0,
113-
print_level=0,
114-
display=false
115-
)
116-
Test.@test sol_i isa OptimalControl.AbstractSolution
117-
end
118105
end
119106
end
120107
end

0 commit comments

Comments
 (0)