Skip to content

Commit c37d432

Browse files
authored
Merge pull request #240 from control-toolbox/239-add-non-unicode-keywords-for-derivative-and-integral
Non unicode keywords
2 parents 3981b34 + c7b1093 commit c37d432

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

src/checking.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Throw ```UnauthorizedCall``` exception if the state of an ocp is not set.
1818
1919
"""
2020
function __check_state_set(ocp::OptimalControlModel)
21-
__is_state_not_set(ocp) && throw(UnauthorizedCall("the state dimension has to be set before. Use state!."))
21+
__is_state_not_set(ocp) && throw(UnauthorizedCall("the state dimension has to be set before."))
2222
end
2323

2424
"""
@@ -28,7 +28,7 @@ Throw ```UnauthorizedCall``` exception if the control of an ocp is not set.
2828
2929
"""
3030
function __check_control_set(ocp::OptimalControlModel)
31-
__is_control_not_set(ocp) && throw(UnauthorizedCall("the control dimension has to be set before. Use control!."))
31+
__is_control_not_set(ocp) && throw(UnauthorizedCall("the control dimension has to be set before."))
3232
end
3333

3434
"""
@@ -38,7 +38,7 @@ Throw ```UnauthorizedCall``` exception if the time of an ocp is not set.
3838
3939
"""
4040
function __check_is_time_set(ocp::OptimalControlModel)
41-
__is_time_not_set(ocp) && throw(UnauthorizedCall("the time dimension has to be set before. Use time!."))
41+
__is_time_not_set(ocp) && throw(UnauthorizedCall("the time dimension has to be set before."))
4242
end
4343

4444
"""
@@ -48,7 +48,7 @@ Throw ```UnauthorizedCall``` exception if the variable of an ocp is not set.
4848
4949
"""
5050
function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence, NonFixed})
51-
__is_variable_not_set(ocp) && throw(UnauthorizedCall("the variable dimension has to be set before. Use variable!."))
51+
__is_variable_not_set(ocp) && throw(UnauthorizedCall("the variable dimension has to be set before."))
5252
end
5353

5454
"""

src/model.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function variable!(
174174

175175
# checkings
176176
is_fixed(ocp) && throw(UnauthorizedCall("the ocp has no variable, you cannot use variable! function."))
177-
__is_variable_set(ocp) && throw(UnauthorizedCall("the variable has already been set. Use variable! once."))
177+
__is_variable_set(ocp) && throw(UnauthorizedCall("the variable has already been set."))
178178
(q > 1) && (size(components_names, 1) q) && throw(IncorrectArgument("the number of variable names must be equal to the variable dimension"))
179179

180180
ocp.variable_dimension = q
@@ -256,7 +256,7 @@ function state!(
256256
components_names::Vector{String}=__state_components_names(n, name))
257257

258258
# checkings
259-
__is_state_set(ocp) && throw(UnauthorizedCall("the state has already been set. Use state! once."))
259+
__is_state_set(ocp) && throw(UnauthorizedCall("the state has already been set."))
260260
(n > 1) && (size(components_names, 1) n) && throw(IncorrectArgument("the number of state names must be equal to the state dimension"))
261261

262262
ocp.state_dimension = n
@@ -342,7 +342,7 @@ function control!(
342342
components_names::Vector{String}=__control_components_names(m, name))
343343

344344
# checkings
345-
__is_control_set(ocp) && throw(UnauthorizedCall("the control has already been set. Use control! once."))
345+
__is_control_set(ocp) && throw(UnauthorizedCall("the control has already been set."))
346346
(m > 1) && (size(components_names, 1) m) && throw(IncorrectArgument("the number of control names must be equal to the control dimension"))
347347

348348
ocp.control_dimension = m
@@ -425,7 +425,7 @@ function time!(
425425
!isnothing(indf) && !(1 indf q) && throw(IncorrectArgument("the index of tf variable must be contained in 1:$q"))
426426

427427
# check if the function has been already called
428-
__is_time_set(ocp) && throw(UnauthorizedCall("the time has already been set. Use time! once."))
428+
__is_time_set(ocp) && throw(UnauthorizedCall("the time has already been set."))
429429

430430
# check consistency
431431
!isnothing(t0) && !isnothing(ind0) && throw(IncorrectArgument("Providing t0 and ind0 has no sense. The initial time cannot be fixed and free."))
@@ -673,7 +673,7 @@ function dynamics!(ocp::OptimalControlModel{T, V}, f::Function) where {T <: Time
673673

674674
# we check if the dimensions and times have been set
675675
__check_all_set(ocp)
676-
__is_dynamics_set(ocp) && throw(UnauthorizedCall("the dynamics has already been set. Use dynamics! once."))
676+
__is_dynamics_set(ocp) && throw(UnauthorizedCall("the dynamics has already been set."))
677677

678678
ocp.dynamics = Dynamics(f, T, V)
679679

@@ -710,7 +710,7 @@ function objective!(ocp::OptimalControlModel{T, V}, type::Symbol, f::Function,
710710

711711
# we check if the dimensions and times have been set
712712
__check_all_set(ocp)
713-
__is_objective_set(ocp) && throw(UnauthorizedCall("the objective has already been set. Use objective! once."))
713+
__is_objective_set(ocp) && throw(UnauthorizedCall("the objective has already been set."))
714714

715715
# check the validity of the criterion
716716
!__is_criterion_valid(criterion) && throw(IncorrectArgument("the following criterion is not valid: " * String(criterion) *
@@ -755,7 +755,7 @@ function objective!(ocp::OptimalControlModel{T, V}, type::Symbol, g::Function, f
755755

756756
# we check if the dimensions and times have been set
757757
__check_all_set(ocp)
758-
__is_objective_set(ocp) && throw(UnauthorizedCall("the objective has already been set. Use objective! once."))
758+
__is_objective_set(ocp) && throw(UnauthorizedCall("the objective has already been set."))
759759

760760
# check the validity of the criterion
761761
!__is_criterion_valid(criterion) && throw(IncorrectArgument("the following criterion is not valid: " * String(criterion) *

src/onepass.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ $(TYPEDEF)
3030
t_dep::Bool=false
3131
end
3232

33-
__init_aliases() = begin
33+
__init_aliases(;max_dim=20) = begin
3434
al = OrderedDict{Symbol, Union{Real, Symbol, Expr}}()
35-
for i 1:9 al[Symbol(:R, ctupperscripts(i))] = :( R^$i ) end
35+
for i 1:max_dim al[Symbol(:R, ctupperscripts(i))] = :( R^$i ) end
36+
al[:<=] = :
37+
al[:>=] = :
38+
al[:derivative] = :∂
39+
al[:integral] = :∫
3640
al
3741
end
3842

test/test_onepass.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,65 @@ end
23892389
( u(t)^2 ) / 2 min # forbidden
23902390
end
23912391

2392+
end
2393+
2394+
# ---------------------------------------------------------------
2395+
# ---------------------------------------------------------------
2396+
@testset "non unicode keywords" begin
2397+
println("non unicode keywords testset...")
23922398

2399+
# --------------------------------
2400+
# min
2401+
t0 = 0
2402+
tf = 1
2403+
@def o begin
2404+
t [ t0, tf ], time
2405+
x R^2, state
2406+
u R, control
2407+
x(t0) == [ -1, 0 ], (1)
2408+
x(tf) == [ 0, 0 ]
2409+
derivative(x)(t) == A * x(t) + B * u(t)
2410+
integral( 0.5u(t)^2 ) min
2411+
end
2412+
x = [ 1, 2 ]
2413+
x0 = 2 * x
2414+
xf = 3 * x
2415+
u = -1
2416+
A = [ 0 1
2417+
0 0 ]
2418+
B = [ 0
2419+
1 ]
2420+
@test constraint(o, :eq1)(x0, xf) == x0
2421+
@test o.dynamics(x, u) == A * x + B * u
2422+
@test o.lagrange(x, u) == 0.5u^2
2423+
@test o.criterion == :min
2424+
2425+
@def o begin
2426+
z R, variable
2427+
t [ 0, 1 ], time
2428+
x R², state
2429+
u R, control
2430+
r = x₁
2431+
v = x₂
2432+
0 <= r(0) - z <= 1, (1)
2433+
0 <= v(1)^2 <= 1, (2)
2434+
[ 0, 0 ] <= x(0) <= [ 1, 1 ], (♡)
2435+
z >= 0, (3)
2436+
(t) == [ v(t), r(t)^2 + z ]
2437+
( u(t)^2 + z * x₁(t) ) min
2438+
end
2439+
x0 = [ 2, 3 ]
2440+
xf = [ 4, 5 ]
2441+
x = [ 1, 2 ]
2442+
u = 3
2443+
z = 4
2444+
@test constraint(o, :eq1)(x0, xf, z) == x0[1] - z
2445+
@test constraint(o, :eq2)(x0, xf, z) == xf[2]^2
2446+
@test constraint(o, Symbol(""))(x0, xf, z) == x0
2447+
@test constraint(o, :eq3)(z) == z
2448+
@test o.dynamics(x, u, z) == [ x[2], x[1]^2 + z ]
2449+
@test o.lagrange(x, u, z) == u^2 + z * x[1]
2450+
23932451
end
23942452

23952453
end

0 commit comments

Comments
 (0)