Skip to content

Commit

Permalink
Merge pull request #240 from control-toolbox/239-add-non-unicode-keyw…
Browse files Browse the repository at this point in the history
…ords-for-derivative-and-integral

Non unicode keywords
  • Loading branch information
jbcaillau authored Aug 12, 2024
2 parents 3981b34 + c7b1093 commit c37d432
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/checking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Throw ```UnauthorizedCall``` exception if the state of an ocp is not set.
"""
function __check_state_set(ocp::OptimalControlModel)
__is_state_not_set(ocp) && throw(UnauthorizedCall("the state dimension has to be set before. Use state!."))
__is_state_not_set(ocp) && throw(UnauthorizedCall("the state dimension has to be set before."))
end

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

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

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

"""
Expand Down
14 changes: 7 additions & 7 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function variable!(

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

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

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

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

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

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

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

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

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

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

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

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

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

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

# check the validity of the criterion
!__is_criterion_valid(criterion) && throw(IncorrectArgument("the following criterion is not valid: " * String(criterion) *
Expand Down
8 changes: 6 additions & 2 deletions src/onepass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ $(TYPEDEF)
t_dep::Bool=false
end

__init_aliases() = begin
__init_aliases(;max_dim=20) = begin
al = OrderedDict{Symbol, Union{Real, Symbol, Expr}}()
for i 1:9 al[Symbol(:R, ctupperscripts(i))] = :( R^$i ) end
for i 1:max_dim al[Symbol(:R, ctupperscripts(i))] = :( R^$i ) end
al[:<=] = :
al[:>=] = :
al[:derivative] = :∂
al[:integral] = :∫
al
end

Expand Down
58 changes: 58 additions & 0 deletions test/test_onepass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,65 @@ end
( u(t)^2 ) / 2 min # forbidden
end

end

# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "non unicode keywords" begin
println("non unicode keywords testset...")

# --------------------------------
# min
t0 = 0
tf = 1
@def o begin
t [ t0, tf ], time
x R^2, state
u R, control
x(t0) == [ -1, 0 ], (1)
x(tf) == [ 0, 0 ]
derivative(x)(t) == A * x(t) + B * u(t)
integral( 0.5u(t)^2 ) min
end
x = [ 1, 2 ]
x0 = 2 * x
xf = 3 * x
u = -1
A = [ 0 1
0 0 ]
B = [ 0
1 ]
@test constraint(o, :eq1)(x0, xf) == x0
@test o.dynamics(x, u) == A * x + B * u
@test o.lagrange(x, u) == 0.5u^2
@test o.criterion == :min

@def o begin
z R, variable
t [ 0, 1 ], time
x R², state
u R, control
r = x₁
v = x₂
0 <= r(0) - z <= 1, (1)
0 <= v(1)^2 <= 1, (2)
[ 0, 0 ] <= x(0) <= [ 1, 1 ], (♡)
z >= 0, (3)
(t) == [ v(t), r(t)^2 + z ]
( u(t)^2 + z * x₁(t) ) min
end
x0 = [ 2, 3 ]
xf = [ 4, 5 ]
x = [ 1, 2 ]
u = 3
z = 4
@test constraint(o, :eq1)(x0, xf, z) == x0[1] - z
@test constraint(o, :eq2)(x0, xf, z) == xf[2]^2
@test constraint(o, Symbol(""))(x0, xf, z) == x0
@test constraint(o, :eq3)(z) == z
@test o.dynamics(x, u, z) == [ x[2], x[1]^2 + z ]
@test o.lagrange(x, u, z) == u^2 + z * x[1]

end

end

0 comments on commit c37d432

Please sign in to comment.