Skip to content

Commit eda7872

Browse files
authored
Merge pull request #251 from control-toolbox/250-unicode-fix
No unicode update
2 parents cb2c508 + 254dfc7 commit eda7872

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CTBase"
22
uuid = "54762871-cc72-4466-b8e8-f6c8b58076cd"
33
authors = ["Olivier Cots <[email protected]>", "Jean-Baptiste Caillau <[email protected]>"]
4-
version = "0.12.4"
4+
version = "0.12.5"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

src/onepass.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ __init_aliases(;max_dim=20) = begin
3838
al[:derivative] = :∂
3939
al[:integral] = :∫
4040
al[:( => )] = :
41+
al[:in] = :
4142
al
4243
end
4344

@@ -174,13 +175,14 @@ p_variable!(p, ocp, v, q; components_names=nothing, log=false) = begin
174175
p.v = v
175176
vv = QuoteNode(v)
176177
qq = q isa Integer ? q : 9
177-
for i 1:qq p.aliases[Symbol(v, ctindices(i))] = :( $v[$i] ) end
178-
for i 1:9 p.aliases[Symbol(v, ctupperscripts(i))] = :( $v^$i ) end
178+
for i 1:qq p.aliases[Symbol(v, ctindices(i))] = :( $v[$i] ) end # make: v₁, v₂... if the variable is named v
179+
for i 1:qq p.aliases[Symbol(v, i)] = :( $v[$i] ) end # make: v1, v2... if the variable is named v
180+
for i 1:9 p.aliases[Symbol(v, ctupperscripts(i))] = :( $v^$i ) end # make: v¹, v²... if the variable is named v
179181
if (isnothing(components_names))
180182
__wrap(:( variable!($ocp, $q, $vv) ), p.lnum, p.line)
181183
else
182184
qq==length(components_names.args) || return __throw("the number of variable components must be $qq", p.lnum, p.line)
183-
for i 1:qq p.aliases[components_names.args[i]] = :( $v[$i] ) end
185+
for i 1:qq p.aliases[components_names.args[i]] = :( $v[$i] ) end # aliases from names given by the user
184186
ss = QuoteNode(string.(components_names.args))
185187
__wrap(:( variable!($ocp, $q, $vv, $ss) ), p.lnum, p.line)
186188
end
@@ -236,14 +238,15 @@ p_state!(p, ocp, x, n; components_names=nothing, log=false) = begin
236238
p.x = x
237239
xx = QuoteNode(x)
238240
nn = n isa Integer ? n : 9
239-
for i 1:nn p.aliases[Symbol(x, ctindices(i))] = :( $x[$i] ) end
240-
for i 1:9 p.aliases[Symbol(x, ctupperscripts(i))] = :( $x^$i ) end
241+
for i 1:nn p.aliases[Symbol(x, ctindices(i))] = :( $x[$i] ) end # make: x₁, x₂... if the state is named x
242+
for i 1:nn p.aliases[Symbol(x, i)] = :( $x[$i] ) end # make: x1, x2... if the state is named x
243+
for i 1:9 p.aliases[Symbol(x, ctupperscripts(i))] = :( $x^$i ) end # make: x¹, x²... if the state is named x
241244
p.aliases[Symbol(Unicode.normalize(string(x,"̇")))] = :( ($x) )
242245
if (isnothing(components_names))
243246
__wrap(:( state!($ocp, $n, $xx) ), p.lnum, p.line)
244247
else
245248
nn==length(components_names.args) || return __throw("the number of state components must be $nn", p.lnum, p.line)
246-
for i 1:nn p.aliases[components_names.args[i]] = :( $x[$i] ) end
249+
for i 1:nn p.aliases[components_names.args[i]] = :( $x[$i] ) end # aliases from names given by the user
247250
ss = QuoteNode(string.(components_names.args))
248251
__wrap(:( state!($ocp, $n, $xx, $ss) ), p.lnum, p.line)
249252
end
@@ -255,13 +258,14 @@ p_control!(p, ocp, u, m; components_names=nothing, log=false) = begin
255258
p.u = u
256259
uu = QuoteNode(u)
257260
mm = m isa Integer ? m : 9
258-
for i 1:mm p.aliases[Symbol(u, ctindices(i))] = :( $u[$i] ) end
259-
for i 1:9 p.aliases[Symbol(u, ctupperscripts(i))] = :( $u^$i ) end
261+
for i 1:mm p.aliases[Symbol(u, ctindices(i))] = :( $u[$i] ) end # make: u₁, u₂... if the control is named u
262+
for i 1:mm p.aliases[Symbol(u, i)] = :( $u[$i] ) end # make: u1, u2... if the control is named u
263+
for i 1:9 p.aliases[Symbol(u, ctupperscripts(i))] = :( $u^$i ) end # make: u¹, u²... if the control is named u
260264
if (isnothing(components_names))
261265
__wrap(:( control!($ocp, $m, $uu) ), p.lnum, p.line)
262266
else
263267
mm==length(components_names.args) || return __throw("the number of control components must be $mm", p.lnum, p.line)
264-
for i 1:mm p.aliases[components_names.args[i]] = :( $u[$i] ) end
268+
for i 1:mm p.aliases[components_names.args[i]] = :( $u[$i] ) end # aliases from names given by the user
265269
ss = QuoteNode(string.(components_names.args))
266270
__wrap(:( control!($ocp, $m, $uu, $ss) ), p.lnum, p.line)
267271
end

test/test_onepass.jl

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ end
5252
@testset "aliases" begin
5353
println("aliases testset...")
5454

55+
@def o begin
56+
x = (y, z) in R², state
57+
u = (uu1, uu2, uu3) in R³, control
58+
v = (vv1, vv2) in R², variable
59+
end
60+
@test o.state_components_names == [ "y", "z" ]
61+
@test o.control_components_names == [ "uu1", "uu2", "uu3" ]
62+
@test o.variable_components_names == [ "vv1", "vv2" ]
63+
5564
@def o begin
5665
x = (y, z) R², state
5766
u = (uu1, uu2, uu3) R³, control
@@ -2401,13 +2410,13 @@ end
24012410
t0 = 0
24022411
tf = 1
24032412
@def o begin
2404-
t [ t0, tf ], time
2405-
x R^2, state
2406-
u R, control
2413+
t in [ t0, tf ], time
2414+
x in R^2, state
2415+
u in R, control
24072416
x(t0) == [ -1, 0 ], (1)
24082417
x(tf) == [ 0, 0 ]
24092418
derivative(x)(t) == A * x(t) + B * u(t)
2410-
integral( 0.5u(t)^2 ) min
2419+
integral( 0.5u(t)^2 ) => min
24112420
end
24122421
x = [ 1, 2 ]
24132422
x0 = 2 * x
@@ -2423,18 +2432,18 @@ end
24232432
@test o.criterion == :min
24242433

24252434
@def o begin
2426-
z R, variable
2427-
t [ 0, 1 ], time
2428-
x , state
2429-
u R, control
2430-
r = x
2431-
v = x
2435+
z in R, variable
2436+
t in [ 0, 1 ], time
2437+
x in R^2, state
2438+
u in R, control
2439+
r = x[1]
2440+
v = x[2]
24322441
0 <= r(0) - z <= 1, (1)
24332442
0 <= v(1)^2 <= 1, (2)
24342443
[ 0, 0 ] <= x(0) <= [ 1, 1 ], (♡)
24352444
z >= 0, (3)
2436-
(t) == [ v(t), r(t)^2 + z ]
2437-
( u(t)^2 + z * x(t) ) min
2445+
derivative(x)(t) == [ v(t), r(t)^2 + z ]
2446+
integral( u(t)^2 + z * x[1](t) ) => min
24382447
end
24392448
x0 = [ 2, 3 ]
24402449
xf = [ 4, 5 ]
@@ -2449,18 +2458,18 @@ end
24492458
@test o.lagrange(x, u, z) == u^2 + z * x[1]
24502459

24512460
@def o begin
2452-
z R, variable
2453-
t [ 0, 1 ], time
2454-
x , state
2455-
u R, control
2456-
r = x
2457-
v = x
2461+
z in R, variable
2462+
t in [ 0, 1 ], time
2463+
x in R^2, state
2464+
u in R, control
2465+
r = x[1]
2466+
v = x[2]
24582467
0 <= r(0) - z <= 1, (1)
24592468
0 <= v(1)^2 <= 1, (2)
24602469
[ 0, 0 ] <= x(0) <= [ 1, 1 ], (♡)
24612470
z >= 0, (3)
2462-
(t) == [ v(t), r(t)^2 + z ]
2463-
( u(t)^2 + z * x(t) ) => min
2471+
derivative(x)(t) == [ v(t), r(t)^2 + z ]
2472+
integral( u(t)^2 + z * x[1](t) ) => min
24642473
end
24652474
x0 = [ 2, 3 ]
24662475
xf = [ 4, 5 ]
@@ -2474,6 +2483,34 @@ end
24742483
@test o.dynamics(x, u, z) == [ x[2], x[1]^2 + z ]
24752484
@test o.lagrange(x, u, z) == u^2 + z * x[1]
24762485

2486+
@def o begin
2487+
z in R^2, variable
2488+
t in [ 0, 1 ], time
2489+
x in R^2, state
2490+
u in R^2, control
2491+
r = x1
2492+
v = x2
2493+
0 <= r(0) - z1 <= 1, (1)
2494+
0 <= v(1)^2 <= 1, (2)
2495+
[ 0, 0 ] <= x(0) <= [ 1, 1 ], (♡)
2496+
z1 >= 0, (3)
2497+
z2 == 1
2498+
u2(t) == 0
2499+
derivative(x)(t) == [ v(t), r(t)^2 + z1 ]
2500+
integral( u1(t)^2 + z1 * x1(t) ) => min
2501+
end
2502+
x0 = [ 2, 3 ]
2503+
xf = [ 4, 5 ]
2504+
x = [ 1, 2 ]
2505+
u = [3, 0]
2506+
z = [4, 1]
2507+
@test constraint(o, :eq1)(x0, xf, z) == x0[1] - z[1]
2508+
@test constraint(o, :eq2)(x0, xf, z) == xf[2]^2
2509+
@test constraint(o, Symbol(""))(x0, xf, z) == x0
2510+
@test constraint(o, :eq3)(z) == z[1]
2511+
@test o.dynamics(x, u, z) == [ x[2], x[1]^2 + z[1] ]
2512+
@test o.lagrange(x, u, z) == u[1]^2 + z[1] * x[1]
2513+
24772514
end
24782515

24792516
end

0 commit comments

Comments
 (0)