Skip to content

Commit

Permalink
Merge pull request #162 from control-toolbox/fixconstraint
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
ocots authored Jun 20, 2024
2 parents f9f104f + d4584ff commit dc089a7
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ function constraint!(
end

# bounds
(!isnothing(lb) && isnothing(ub)) && (ub = Inf*(size(lb,1) == 1 ? 1 : ones(eltype(ub), size(ub,1))))
(!isnothing(lb) && isnothing(ub)) && (ub = Inf*(size(lb,1) == 1 ? 1 : ones(eltype(lb), size(lb,1))))
( isnothing(lb) && !isnothing(ub)) && (lb = -Inf*(size(ub,1) == 1 ? 1 : ones(eltype(ub), size(ub,1))))

# range
Expand Down
14 changes: 7 additions & 7 deletions src/onepass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end

__init_aliases() = begin
al = OrderedDict{Symbol, Union{Real, Symbol, Expr}}()
for i 1:9 al[Symbol(:R, ctupperscripts(i))] = :( R^$i ) end
for i 1:9 al[Symbol(:R, ctupperscripts(i))] = :( R^$i ) end
al
end

Expand Down Expand Up @@ -88,15 +88,15 @@ parse!(p, ocp, e; log=false) = begin
end
# variable
:( $v R^$q, variable ) => p_variable!(p, ocp, v, q; log)
:( $v R , variable ) => p_variable!(p, ocp, v ; log)
:( $v R , variable ) => p_variable!(p, ocp, v, 1; log)
# time
:( $t [ $t0, $tf ], time ) => p_time!(p, ocp, t, t0, tf; log)
# state
:( $x R^$n, state ) => p_state!(p, ocp, x, n; log)
:( $x R , state ) => p_state!(p, ocp, x ; log)
:( $x R , state ) => p_state!(p, ocp, x, 1; log)
# control
:( $u R^$m, control ) => p_control!(p, ocp, u, m; log)
:( $u R , control ) => p_control!(p, ocp, u ; log)
:( $u R , control ) => p_control!(p, ocp, u, 1; log)
# dynamics
:( ($x)($t) == $e1 ) => p_dynamics!(p, ocp, x, t, e1 ; log)
:( ($x)($t) == $e1, $label ) => p_dynamics!(p, ocp, x, t, e1, label; log)
Expand Down Expand Up @@ -163,7 +163,7 @@ parse!(p, ocp, e; log=false) = begin
end
end

p_variable!(p, ocp, v, q=1; components_names=nothing, log=false) = begin
p_variable!(p, ocp, v, q; components_names=nothing, log=false) = begin
log && println("variable: $v, dim: $q")
v isa Symbol || return __throw("forbidden variable name: $v", p.lnum, p.line)
p.v = v
Expand Down Expand Up @@ -224,7 +224,7 @@ p_time!(p, ocp, t, t0, tf; log=false) = begin
__wrap(code, p.lnum, p.line)
end

p_state!(p, ocp, x, n=1; components_names=nothing, log=false) = begin
p_state!(p, ocp, x, n; components_names=nothing, log=false) = begin
log && println("state: $x, dim: $n")
x isa Symbol || return __throw("forbidden state name: $x", p.lnum, p.line)
p.x = x
Expand All @@ -242,7 +242,7 @@ p_state!(p, ocp, x, n=1; components_names=nothing, log=false) = begin
end
end

p_control!(p, ocp, u, m=1; components_names=nothing, log=false) = begin
p_control!(p, ocp, u, m; components_names=nothing, log=false) = begin
log && println("control: $u, dim: $m")
u isa Symbol || return __throw("forbidden control name: $u", p.lnum, p.line)
p.u = u
Expand Down
109 changes: 107 additions & 2 deletions test/test_onepass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function test_onepass()
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "aliases" begin
println("aliases testset...")

@def o begin
x = (y, z) R², state
Expand Down Expand Up @@ -142,6 +143,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "variable" begin
println("variable testset...")

@def o begin
λ R^2, variable
Expand Down Expand Up @@ -236,6 +238,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "time" begin
println("time testset...")

t0 = 0
@def o t [ t0, t0 + 4 ], time
Expand Down Expand Up @@ -275,6 +278,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "state / control" begin
println("state / control testset...")

@def o begin
x R, state
Expand Down Expand Up @@ -461,6 +465,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "dynamics" begin
println("dynamics testset...")

@def o begin
t [ 0, 1 ], time
Expand Down Expand Up @@ -544,6 +549,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "constraints" begin
println("constraints testset...")

@def o begin
tf R, variable
Expand Down Expand Up @@ -975,8 +981,103 @@ end
@test o.constraints[:eq14][4] == Inf
@test o.constraints[:eq15][4] == Inf

# minimal constraint tests
# remark: constraint are heavily tested in test_ctparser_constraints.jl
@def o begin
v R^2, variable
t [ 0, 1 ], time
x R², state
u R², control
x(0) 0, 0 ]
x(0) [ 0, 0 ], (1)
x(1) [ 0, 0 ]
x(1) [ 0, 0 ], (2)
[ x₁(0)^3, 0 ] [ 0, 0 ]
[ x₁(0)^3, 0 ] [ 0, 0 ], (3)
x(t) [ 0, 0 ]
x(t) [ 0, 0 ], (4)
u(t) [ 0, 0 ]
u(t) [ 0, 0 ], (5)
x₁(t)^3, 0 ] [ 0, 0 ]
x₁(t)^3, 0 ] [ 0, 0 ], (6)
[ u₁(t)^3, 0 ] [ 0, 0 ]
[ u₁(t)^3, 0 ] [ 0, 0 ], (7)
[ u₁(t)^3, x₁(t) ] [ 0, 0 ]
[ u₁(t)^3, x₁(t) ] [ 0, 0 ], (8)
v [ 0, 0 ]
v [ 0, 0 ], (9)
[ v₁^2, 0 ] [ 0, 0 ]
[ v₁^2, 0 ] [ 0, 0 ], (10)
end

@test o.constraints[:eq1 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq2 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq3 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq4 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq5 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq6 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq7 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq8 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq9 ][3] ==-[ Inf, Inf ]
@test o.constraints[:eq10][3] ==-[ Inf, Inf ]
@test o.constraints[:eq1 ][4] == [ 0, 0 ]
@test o.constraints[:eq2 ][4] == [ 0, 0 ]
@test o.constraints[:eq3 ][4] == [ 0, 0 ]
@test o.constraints[:eq4 ][4] == [ 0, 0 ]
@test o.constraints[:eq5 ][4] == [ 0, 0 ]
@test o.constraints[:eq6 ][4] == [ 0, 0 ]
@test o.constraints[:eq7 ][4] == [ 0, 0 ]
@test o.constraints[:eq8 ][4] == [ 0, 0 ]
@test o.constraints[:eq9 ][4] == [ 0, 0 ]
@test o.constraints[:eq10][4] == [ 0, 0 ]

@def o begin
v R^2, variable
t [ 0, 1 ], time
x R², state
u R², control
x(0) 0, 0 ]
x(0) [ 0, 0 ], (1)
x(1) [ 0, 0 ]
x(1) [ 0, 0 ], (2)
[ x₁(0)^3, 0 ] [ 0, 0 ]
[ x₁(0)^3, 0 ] [ 0, 0 ], (3)
x(t) [ 0, 0 ]
x(t) [ 0, 0 ], (4)
u(t) [ 0, 0 ]
u(t) [ 0, 0 ], (5)
x₁(t)^3, 0 ] [ 0, 0 ]
x₁(t)^3, 0 ] [ 0, 0 ], (6)
[ u₁(t)^3, 0 ] [ 0, 0 ]
[ u₁(t)^3, 0 ] [ 0, 0 ], (7)
[ u₁(t)^3, x₁(t) ] [ 0, 0 ]
[ u₁(t)^3, x₁(t) ] [ 0, 0 ], (8)
v [ 0, 0 ]
v [ 0, 0 ], (9)
[ v₁^2, 0 ] [ 0, 0 ]
[ v₁^2, 0 ] [ 0, 0 ], (10)
end

@test o.constraints[:eq1 ][4] == [ Inf, Inf ]
@test o.constraints[:eq2 ][4] == [ Inf, Inf ]
@test o.constraints[:eq3 ][4] == [ Inf, Inf ]
@test o.constraints[:eq4 ][4] == [ Inf, Inf ]
@test o.constraints[:eq5 ][4] == [ Inf, Inf ]
@test o.constraints[:eq6 ][4] == [ Inf, Inf ]
@test o.constraints[:eq7 ][4] == [ Inf, Inf ]
@test o.constraints[:eq8 ][4] == [ Inf, Inf ]
@test o.constraints[:eq9 ][4] == [ Inf, Inf ]
@test o.constraints[:eq10][4] == [ Inf, Inf ]
@test o.constraints[:eq1 ][3] == [ 0, 0 ]
@test o.constraints[:eq2 ][3] == [ 0, 0 ]
@test o.constraints[:eq3 ][3] == [ 0, 0 ]
@test o.constraints[:eq4 ][3] == [ 0, 0 ]
@test o.constraints[:eq5 ][3] == [ 0, 0 ]
@test o.constraints[:eq6 ][3] == [ 0, 0 ]
@test o.constraints[:eq7 ][3] == [ 0, 0 ]
@test o.constraints[:eq8 ][3] == [ 0, 0 ]
@test o.constraints[:eq9 ][3] == [ 0, 0 ]
@test o.constraints[:eq10][3] == [ 0, 0 ]


t0 = 9.0; tf = 9.1
r0 = 1.0; r1 = 2.0
v0 = 2.0; vmax = sqrt(2)
Expand Down Expand Up @@ -1381,6 +1482,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "Lagrange cost" begin
println("lagrange testset...")

# --------------------------------
# min
Expand Down Expand Up @@ -1723,6 +1825,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "Bolza cost" begin
println("Bolza testset...")

# -------------------------------
# min
Expand Down Expand Up @@ -2035,6 +2138,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "Mayer cost" begin
println("Mayer testset...")

@def o begin
s [ 0, 1 ], time
Expand Down Expand Up @@ -2154,6 +2258,7 @@ end
# ---------------------------------------------------------------
# ---------------------------------------------------------------
@testset "closure" begin
println("closure testset...")

a = 1
f(b) = begin # closure of a, local c, and @def in function
Expand Down

0 comments on commit dc089a7

Please sign in to comment.