Skip to content

Commit daade0c

Browse files
authored
Merge pull request #738 from control-toolbox/auto-juliaformatter-pr
[AUTO] JuliaFormatter.jl run
2 parents 0073ea1 + b945f5f commit daade0c

9 files changed

Lines changed: 217 additions & 130 deletions

File tree

test/helpers/print_utils.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,11 @@ function print_test_line(
291291

292292
# Error: scientific notation with 2 decimal places
293293
err_str = rel_error === missing ? "N/A" : @sprintf("%.2e", rel_error / 100) # Convert to fraction then scientific format
294-
err_color = rel_error === missing ? :white : (rel_error < 1 ? :green : (rel_error < 5 ? :yellow : :red))
294+
err_color = if rel_error === missing
295+
:white
296+
else
297+
(rel_error < 1 ? :green : (rel_error < 5 ? :yellow : :red))
298+
end
295299
printstyled(lpad(err_str, 7); color=err_color)
296300

297301
# Memory: optional, right-aligned, 10 characters

test/problems/double_integrator.jl

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ function DoubleIntegratorTime()
3333
xf = [0.0, 0.0]
3434
u_max = 1.0
3535
u_min = -1.0
36-
36+
3737
@def ocp begin
3838
tf R, variable
3939
t [0, tf], time
4040
x = (q, v) R², state
4141
u R, control
42-
42+
4343
-1 u(t) 1
44-
44+
4545
q(0) == -1
4646
v(0) == 0
4747
q(tf) == 0
4848
v(tf) == 0
49-
49+
5050
(t) == [v(t), u(t)]
51-
51+
5252
tf min
5353
end
54-
54+
5555
return (
5656
ocp=ocp,
5757
obj=2.0,
@@ -62,7 +62,7 @@ function DoubleIntegratorTime()
6262
t0=t0,
6363
tf=2.0,
6464
u_max=u_max,
65-
u_min=u_min
65+
u_min=u_min,
6666
)
6767
end
6868

@@ -93,22 +93,31 @@ function DoubleIntegratorEnergy()
9393
tf = 1.0
9494
x0 = [-1.0, 0.0]
9595
xf = [0.0, 0.0]
96-
96+
9797
@def ocp begin
9898
t [0, 1], time
9999
x = (q, v) R², state
100100
u R, control
101-
101+
102102
x(0) == [-1, 0]
103103
x(1) == [0, 0]
104-
104+
105105
(q)(t) == v(t)
106106
(v)(t) == u(t)
107-
107+
108108
0.5(u(t)^2) min
109109
end
110-
111-
return (ocp=ocp, obj=6.0, name="double_integrator_energy", init=nothing, x0=x0, xf=xf, t0=t0, tf=tf)
110+
111+
return (
112+
ocp=ocp,
113+
obj=6.0,
114+
name="double_integrator_energy",
115+
init=nothing,
116+
x0=x0,
117+
xf=xf,
118+
t0=t0,
119+
tf=tf,
120+
)
112121
end
113122

114123
"""
@@ -141,23 +150,23 @@ function DoubleIntegratorEnergyConstrained()
141150
x0 = [-1.0, 0.0]
142151
xf = [0.0, 0.0]
143152
v_max = 1.2
144-
153+
145154
@def ocp begin
146155
t [0, 1], time
147156
x = (q, v) R², state
148157
u R, control
149-
158+
150159
v(t) 1.2
151-
160+
152161
x(0) == [-1, 0]
153162
x(1) == [0, 0]
154-
163+
155164
(q)(t) == v(t)
156165
(v)(t) == u(t)
157-
166+
158167
0.5(u(t)^2) min
159168
end
160-
169+
161170
return (
162171
ocp=ocp,
163172
obj=7.680030,
@@ -167,6 +176,6 @@ function DoubleIntegratorEnergyConstrained()
167176
xf=xf,
168177
t0=t0,
169178
tf=tf,
170-
v_max=v_max
179+
v_max=v_max,
171180
)
172181
end

test/problems/quadrotor.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# - name :: a short problem name
77
# - init :: NamedTuple of components for CTSolvers.initial_guess
88
function Quadrotor(; T=1, g=9.8, r=0.1)
9-
109
ocp = @def begin
1110
t [0, T], time
1211
x R⁹, state
@@ -46,9 +45,9 @@ function Quadrotor(; T=1, g=9.8, r=0.1)
4645
end
4746

4847
init = @init ocp begin
49-
x(t) := 0.1 * ones(9)
50-
u(t) := 0.1 * ones(4)
48+
x(t) := 0.1 * ones(9)
49+
u(t) := 0.1 * ones(4)
5150
end
5251

5352
return (ocp=ocp, obj=4.2679623758, name="quadrotor", init=init)
54-
end
53+
end

test/problems/transfer.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ function F3(x)
5353
end
5454

5555
function Transfer(; Tmax=60)
56-
57-
cTmax = 3600^2 / 1e6; T = Tmax * cTmax # Conversion from Newtons to kg x Mm / h²
56+
cTmax = 3600^2 / 1e6;
57+
T = Tmax * cTmax # Conversion from Newtons to kg x Mm / h²
5858
mass0 = 1500 # Initial mass of the spacecraft
5959
β = 1.42e-02 # Engine specific impulsion
6060
P0 = 11.625 # Initial semilatus rectum
@@ -67,7 +67,7 @@ function Transfer(; Tmax=60)
6767
Lf = 3π # Estimation of final longitude
6868
x0 = [P0, ex0, ey0, hx0, hy0, L0] # Initial state
6969
xf = [Pf, exf, eyf, hxf, hyf, Lf] # Final state
70-
70+
7171
ocp = @def begin
7272
tf R, variable
7373
t [0, tf], time
@@ -76,17 +76,18 @@ function Transfer(; Tmax=60)
7676
x(0) == x0
7777
x[1:5](tf) == xf[1:5]
7878
mass = mass0 - β * T * t
79-
(t) == F0(x(t)) + T / mass * (u₁(t) * F1(x(t)) + u₂(t) * F2(x(t)) + u₃(t) * F3(x(t)))
79+
(t) ==
80+
F0(x(t)) + T / mass * (u₁(t) * F1(x(t)) + u₂(t) * F2(x(t)) + u₃(t) * F3(x(t)))
8081
u₁(t)^2 + u₂(t)^2 + u₃(t)^2 1
8182
tf min
8283
end
83-
84-
init = @init ocp begin
84+
85+
init = @init ocp begin
8586
tf_i = 15
8687
x(t) := x0 + (xf - x0) * t / tf_i # Linear interpolation
87-
u(t) := [0.1, 0.5, 0.] # Initial guess for the control
88+
u(t) := [0.1, 0.5, 0.0] # Initial guess for the control
8889
tf := tf_i # Initial guess for final time
8990
end
90-
91+
9192
return (ocp=ocp, obj=14.79643132, name="transfer", init=init)
92-
end
93+
end

test/suite/indirect/test_double_integrator_energy.jl

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
module TestDoubleIntegratorEnergy
88

9-
import Test
10-
import OptimalControl
9+
using Test: Test
10+
using OptimalControl: OptimalControl
1111
import NonlinearSolve: NonlinearProblem, solve
1212
import LinearAlgebra: norm
1313
import OrdinaryDiffEq: OrdinaryDiffEq
@@ -21,11 +21,11 @@ const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING :
2121

2222
function test_double_integrator_energy()
2323
Test.@testset "Double Integrator Energy Minimization" verbose=VERBOSE showtiming=SHOWTIMING begin
24-
24+
2525
# ====================================================================
2626
# INTEGRATION TEST - Unconstrained Energy Minimization
2727
# ====================================================================
28-
28+
2929
Test.@testset "Unconstrained singular control" begin
3030
# Get problem from TestProblems
3131
prob_data = TestProblems.DoubleIntegratorEnergy()
@@ -35,36 +35,36 @@ function test_double_integrator_energy()
3535
t0 = prob_data.t0
3636
tf = prob_data.tf
3737
obj_ref = prob_data.obj
38-
38+
3939
# Singular control: u(x, p) = p₂
4040
u(x, p) = p[2]
41-
41+
4242
# Hamiltonian flow
4343
f = OptimalControl.Flow(ocp, u)
44-
44+
4545
# State projection
4646
π((x, p)) = x
47-
47+
4848
# Shooting function
4949
S(p0) = π(f(t0, x0, p0, tf)) - xf
50-
50+
5151
# Known solution (from documentation)
5252
p0_ref = [12.0, 6.0]
53-
53+
5454
# Test shooting function with known solution
5555
s = S(p0_ref)
56-
56+
5757
# Verify solution (should be close to zero)
5858
Test.@test norm(s) < 1e-6
59-
59+
6060
# Note: We don't test the objective value directly here since
6161
# we're testing the shooting method, not the full solution
6262
end
63-
63+
6464
# ====================================================================
6565
# INTEGRATION TEST - Constrained Energy Minimization
6666
# ====================================================================
67-
67+
6868
Test.@testset "Constrained three-arc structure" begin
6969
# Get problem from TestProblems
7070
prob_data = TestProblems.DoubleIntegratorEnergyConstrained()
@@ -74,18 +74,20 @@ function test_double_integrator_energy()
7474
t0 = prob_data.t0
7575
tf = prob_data.tf
7676
v_max = prob_data.v_max
77-
77+
7878
# Flow for unconstrained extremals (singular control u = p₂)
7979
f_interior = OptimalControl.Flow(ocp, (x, p) -> p[2])
80-
80+
8181
# Boundary control and constraint
8282
ub = 0.0 # boundary control
8383
g(x) = v_max - x[2] # constraint: g(x) ≥ 0
8484
μ(p) = p[1] # dual variable
85-
85+
8686
# Flow for boundary extremals
87-
f_boundary = OptimalControl.Flow(ocp, (x, p) -> ub, (x, u) -> g(x), (x, p) -> μ(p))
88-
87+
f_boundary = OptimalControl.Flow(
88+
ocp, (x, p) -> ub, (x, u) -> g(x), (x, p) -> μ(p)
89+
)
90+
8991
# Shooting function
9092
function shoot!(s, p0, t1, t2)
9193
x_t0, p_t0 = x0, p0
@@ -96,19 +98,19 @@ function test_double_integrator_energy()
9698
s[3] = g(x_t1) # constraint activation at entry
9799
s[4] = p_t1[2] # switching condition
98100
end
99-
101+
100102
# Known solution (from documentation)
101103
p0_ref = [38.4, 9.6]
102104
t1_ref = 0.25
103105
t2_ref = 0.75
104-
106+
105107
# Test shooting function with known solution
106108
s = zeros(4)
107109
shoot!(s, p0_ref, t1_ref, t2_ref)
108-
110+
109111
# Verify solution (should be close to zero)
110112
Test.@test norm(s) < 1e-6
111-
113+
112114
# Note: obj_ref is nothing for this problem (no reference value available)
113115
end
114116
end

test/suite/indirect/test_double_integrator_time.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
module TestDoubleIntegratorTime
88

9-
import Test
10-
import OptimalControl
9+
using Test: Test
10+
using OptimalControl: OptimalControl
1111
import NonlinearSolve: NonlinearProblem, solve
1212
import LinearAlgebra: norm
1313
import OrdinaryDiffEq: OrdinaryDiffEq
@@ -21,11 +21,11 @@ const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING :
2121

2222
function test_double_integrator_time()
2323
Test.@testset "Double Integrator Time Minimization" verbose=VERBOSE showtiming=SHOWTIMING begin
24-
24+
2525
# ====================================================================
2626
# INTEGRATION TEST - Bang-Bang Shooting Method
2727
# ====================================================================
28-
28+
2929
Test.@testset "Bang-bang shooting with switching time" begin
3030
# Get problem from TestProblems
3131
prob_data = TestProblems.DoubleIntegratorTime()
@@ -36,14 +36,14 @@ function test_double_integrator_time()
3636
u_max = prob_data.u_max
3737
u_min = prob_data.u_min
3838
obj_ref = prob_data.obj
39-
39+
4040
# Pseudo-Hamiltonian: H(x, p, u) = p₁v + p₂u - 1
4141
H(x, p, u) = p[1] * x[2] + p[2] * u - 1
42-
42+
4343
# Hamiltonian flows for bang-bang control
4444
f_max = OptimalControl.Flow(ocp, (x, p, tf) -> u_max)
4545
f_min = OptimalControl.Flow(ocp, (x, p, tf) -> u_min)
46-
46+
4747
# Shooting function
4848
function shoot!(s, p0, t1, tf)
4949
x_t0, p_t0 = x0, p0
@@ -53,19 +53,19 @@ function test_double_integrator_time()
5353
s[3] = p_t1[2] # switching condition
5454
s[4] = H(x_tf, p_tf, u_min) # free final time
5555
end
56-
56+
5757
# Known solution (from documentation)
5858
p0_ref = [1.0, 1.0]
5959
t1_ref = 1.0
6060
tf_ref = 2.0
61-
61+
6262
# Test shooting function with known solution
6363
s = zeros(4)
6464
shoot!(s, p0_ref, t1_ref, tf_ref)
65-
65+
6666
# Verify solution (should be close to zero)
6767
Test.@test norm(s) < 1e-6
68-
68+
6969
# Verify objective value
7070
Test.@test tf_ref obj_ref atol=1e-6
7171
end

0 commit comments

Comments
 (0)