diff --git a/src/model.jl b/src/model.jl index 30bbbc45..07170cfc 100644 --- a/src/model.jl +++ b/src/model.jl @@ -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 diff --git a/src/onepass.jl b/src/onepass.jl index 81d4d094..5273c06e 100644 --- a/src/onepass.jl +++ b/src/onepass.jl @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/test/test_onepass.jl b/test/test_onepass.jl index 8bf4bb81..9b5e0ec1 100644 --- a/test/test_onepass.jl +++ b/test/test_onepass.jl @@ -5,6 +5,7 @@ function test_onepass() # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "aliases" begin + println("aliases testset...") @def o begin x = (y, z) ∈ R², state @@ -142,6 +143,7 @@ end # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "variable" begin + println("variable testset...") @def o begin λ ∈ R^2, variable @@ -236,6 +238,7 @@ end # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "time" begin + println("time testset...") t0 = 0 @def o t ∈ [ t0, t0 + 4 ], time @@ -275,6 +278,7 @@ end # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "state / control" begin + println("state / control testset...") @def o begin x ∈ R, state @@ -461,6 +465,7 @@ end # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "dynamics" begin + println("dynamics testset...") @def o begin t ∈ [ 0, 1 ], time @@ -544,6 +549,7 @@ end # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "constraints" begin + println("constraints testset...") @def o begin tf ∈ R, variable @@ -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) @@ -1381,6 +1482,7 @@ end # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "Lagrange cost" begin + println("lagrange testset...") # -------------------------------- # min @@ -1723,6 +1825,7 @@ end # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "Bolza cost" begin + println("Bolza testset...") # ------------------------------- # min @@ -2035,6 +2138,7 @@ end # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "Mayer cost" begin + println("Mayer testset...") @def o begin s ∈ [ 0, 1 ], time @@ -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