diff --git a/src/print.jl b/src/print.jl index 63dfff74..da0697b9 100644 --- a/src/print.jl +++ b/src/print.jl @@ -29,17 +29,23 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::OptimalControlModel{<: TimeD @assert hasproperty(ocp.model_expression, :head) # - #println(io) + println(io) + printstyled(io, "The ", bold=true) + is_time_dependent(ocp) ? printstyled(io, "(non autonomous) ", bold=true) : printstyled(io, "(autonomous) ", bold=true) + printstyled(io, "optimal control problem is given by:\n", bold=true) + println(io) # print the code - tab = 0 + tab = 4 code = striplines(ocp.model_expression) @match code.head begin :block => [__print(code.args[i], io, tab) for i ∈ eachindex(code.args)] _ => __print(code, io, tab) end - - elseif __is_complete(ocp) # print the model if is is complete + + end + + if __is_complete(ocp) # print the model if is is complete # dimensions x_dim = ocp.state_dimension @@ -68,8 +74,11 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::OptimalControlModel{<: TimeD control_args_names = t_ * u_name * "(" * t_name * ")" * _v # - printstyled(io, "Optimal control problem of the form:\n") - println(io, "") + println(io) + printstyled(io, "The ", bold=true) + is_time_dependent(ocp) ? printstyled(io, "(non autonomous) ", bold=true) : printstyled(io, "(autonomous) ", bold=true) + printstyled(io, "optimal control problem is of the form:\n", bold=true) + println(io) # J printstyled(io, " minimize ", color=:blue); print(io, "J(" * x_name * ", " * u_name * _v * ") = ") @@ -178,14 +187,22 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::OptimalControlModel{<: TimeD end + # + println(io) + printstyled(io, "Declarations ", bold=true) + printstyled(io, "(* required):\n", bold=false) + #println(io) + # print table of settings - header = [ "times", "state", "control"] - is_variable_dependent(ocp) && push!(header, "variable") - push!(header, "dynamics", "objective", "constraints") + header = [ "times*", "state*", "control*"] + #is_variable_dependent(ocp) && push!(header, "variable") + push!(header, "variable") + push!(header, "dynamics*", "objective*", "constraints") data = hcat(__is_time_not_set(ocp) ? "❌" : "✅", __is_state_not_set(ocp) ? "❌" : "✅", __is_control_not_set(ocp) ? "❌" : "✅") - is_variable_dependent(ocp) && begin + #is_variable_dependent(ocp) && + begin (data = hcat(data, __is_variable_not_set(ocp) ? "❌" : "✅")) end diff --git a/test/test_model.jl b/test/test_model.jl index 744431d1..0c491d87 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -7,8 +7,8 @@ function test_model() # 30 55 185 @test_throws UnauthorizedCall variable!(ocp, 1) @test_throws UnauthorizedCall constraint!(ocp, :variable, 2:3, [ 0, 3 ]) @test_throws UnauthorizedCall constraint!(ocp, :variable, 0, 1) # the variable here is of dimension 1 - @test_throws UnauthorizedCall constraint!(ocp, :variable, 1:2, [ 0, 0 ], [ 1, 2 ]) - @test_throws UnauthorizedCall constraint!(ocp, :variable, [ 3, 0, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :variable, 1:2, [ 0, 0 ], [ 1, 2 ]) + @test_throws UnauthorizedCall constraint!(ocp, :variable, [ 3, 0, 1 ]) ocp = Model(variable=true) variable!(ocp, 1) @@ -59,26 +59,26 @@ end i == 7 && begin state!(ocp, 2); control!(ocp, 1) end # constraint! 1 - @test_throws UnauthorizedCall constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ]) - @test_throws UnauthorizedCall constraint!(ocp, :initial, 2:3, [ 0, 0 ]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, 2:3, [ 0, 0 ]) @test_throws UnauthorizedCall constraint!(ocp, :final, Index(2), 0) # constraint! 2 - @test_throws UnauthorizedCall constraint!(ocp, :initial, [ 0, 0 ]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, [ 0, 0 ]) @test_throws UnauthorizedCall constraint!(ocp, :final, 2) # if the state is of dimension 1 # constraint! 3 - @test_throws UnauthorizedCall constraint!(ocp, :initial, 2:3, [ 0, 0 ], [ 1, 2 ]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, 2:3, [ 0, 0 ], [ 1, 2 ]) @test_throws UnauthorizedCall constraint!(ocp, :final, Index(1), 0, 2) @test_throws UnauthorizedCall constraint!(ocp, :control, Index(1), 0, 2) - @test_throws UnauthorizedCall constraint!(ocp, :state, 2:3, [ 0, 0 ], [ 1, 2 ]) - @test_throws UnauthorizedCall constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ], [ 1, 2, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, 2:3, [ 0, 0 ], [ 1, 2 ]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ], [ 1, 2, 1 ]) # constraint! 4 - @test_throws UnauthorizedCall constraint!(ocp, :initial, [ 0, 0, 0 ], [ 1, 2, 1 ]) - @test_throws UnauthorizedCall constraint!(ocp, :final, [ 0, 0, 0 ], [ 1, 2, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, [ 0, 0, 0 ], [ 1, 2, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :final, [ 0, 0, 0 ], [ 1, 2, 1 ]) @test_throws UnauthorizedCall constraint!(ocp, :control, [ 0, 0 ], [ 2, 3 ]) - @test_throws UnauthorizedCall constraint!(ocp, :state, [ 0, 0, 0 ], [ 1, 2, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, [ 0, 0, 0 ], [ 1, 2, 1 ]) # constraint! 5 # variable independent ocp @@ -89,22 +89,22 @@ end # time independent and variable independent ocp @test_throws UnauthorizedCall constraint!(ocp, :control, u -> 2u, 0, 1) - @test_throws UnauthorizedCall constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ], [ 1, 2, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ], [ 1, 2, 1 ]) @test_throws UnauthorizedCall constraint!(ocp, :mixed, (x, u) -> x[1]-u, 0, 1) # time dependent and variable independent ocp @test_throws UnauthorizedCall constraint!(ocp, :control, (t, u) -> 2u, 0, 1) - @test_throws UnauthorizedCall constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ], [ 1, 2, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ], [ 1, 2, 1 ]) @test_throws UnauthorizedCall constraint!(ocp, :mixed, (t, x, u) -> x[1]-u, 0, 1) # time independent and variable dependent ocp @test_throws UnauthorizedCall constraint!(ocp, :control, (u, v) -> 2u*v[1], 0, 1) - @test_throws UnauthorizedCall constraint!(ocp, :state, (x, v) -> x-v[1], [ 0, 0, 0 ], [ 1, 2, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, (x, v) -> x-v[1], [ 0, 0, 0 ], [ 1, 2, 1 ]) @test_throws UnauthorizedCall constraint!(ocp, :mixed, (x, u, v) -> x[1]-v[2]*u, 0, 1) # time dependent and variable dependent ocp @test_throws UnauthorizedCall constraint!(ocp, :control, (t, u, v) -> 2u+v[2], 0, 1) - @test_throws UnauthorizedCall constraint!(ocp, :state, (t, x, v) -> x-t*v[1], [ 0, 0, 0 ], [ 1, 2, 1 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, (t, x, v) -> x-t*v[1], [ 0, 0, 0 ], [ 1, 2, 1 ]) @test_throws UnauthorizedCall constraint!(ocp, :mixed, (t, x, u, v) -> x[1]*v[2]-u, 0, 1) # constraint! 6 @@ -116,22 +116,22 @@ end # time independent and variable independent ocp @test_throws UnauthorizedCall constraint!(ocp, :control, u -> 2u, 1) - @test_throws UnauthorizedCall constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ]) @test_throws UnauthorizedCall constraint!(ocp, :mixed, (x, u) -> x[1]-u, 0) # time dependent and variable independent ocp @test_throws UnauthorizedCall constraint!(ocp, :control, (t, u) -> 2u, 1) - @test_throws UnauthorizedCall constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ]) @test_throws UnauthorizedCall constraint!(ocp, :mixed, (t, x, u) -> x[1]-u, 0) # time independent and variable dependent ocp @test_throws UnauthorizedCall constraint!(ocp, :control, (u, v) -> 2u*v[1], 1) - @test_throws UnauthorizedCall constraint!(ocp, :state, (x, v) -> x-v[2], [ 0, 0, 0 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, (x, v) -> x-v[2], [ 0, 0, 0 ]) @test_throws UnauthorizedCall constraint!(ocp, :mixed, (x, u) -> x[1]-u+v[1], 0) # time dependent and variable dependent ocp @test_throws UnauthorizedCall constraint!(ocp, :control, (t, u, v) -> 2u-t*v[2], 1) - @test_throws UnauthorizedCall constraint!(ocp, :state, (t, x, v) -> x-t+v[1], [ 0, 0, 0 ]) + @test_throws UnauthorizedCall constraint!(ocp, :state, (t, x, v) -> x-t+v[1], [ 0, 0, 0 ]) @test_throws UnauthorizedCall constraint!(ocp, :mixed, (t, x, u, v) -> x[1]-u*v[1], 0) end @@ -716,7 +716,7 @@ end variable!(ocp, 4) state!(ocp, 1) control!(ocp, 1) - constraint!(ocp, :variable, [ 0, 0, 0, 0 ], [ 1, 1, 1, 1 ], :eq1) + constraint!(ocp, :variable, [ 0, 0, 0, 0 ], [ 1, 1, 1, 1 ], :eq1) constraint!(ocp, :variable, Index(1), 0, 1, :eq2) constraint!(ocp, :variable, 1:2, [ 0, 0 ], [ 1, 2 ], :eq3) constraint!(ocp, :variable, 1:2:4, [ 0, 0 ], [ -1, 1 ], :eq4) @@ -745,7 +745,7 @@ end constraint!(ocp, :control, (u, v) -> u + v[1], 0, 1, :cu) constraint!(ocp, :state, (x, v) -> x + v[1], 0, 1, :cs) constraint!(ocp, :mixed, (x, u, v) -> x + u + v[1], 1, 1, :cm) - constraint!(ocp, :variable, [ 0, 0, 0, 0 ], :eq1) + constraint!(ocp, :variable, [ 0, 0, 0, 0 ], :eq1) constraint!(ocp, :variable, Index(1), 0, :eq2) constraint!(ocp, :variable, 1:2, [ 0, 0 ], :eq3) constraint!(ocp, :variable, 1:2:4, [ 0, 0 ], :eq4) @@ -912,7 +912,7 @@ end constraint!(ocp, :control, (u, v) -> u+v[2], 0, 1, :cuu) constraint!(ocp, :state, (x, v) -> x+v[1:2], [0, 1], [1, 2], :css) constraint!(ocp, :mixed, (x, u, v) -> x[1]+u+v[2], 1, 1, :cm) - constraint!(ocp, :variable, [ 0, 0, 0, 0 ], [ 5, 5, 5, 5 ], :cv1) + constraint!(ocp, :variable, [ 0, 0, 0, 0 ], [ 5, 5, 5, 5 ], :cv1) constraint!(ocp, :variable, 1:2, [ 1, 2 ], [ 3, 4 ], :cv2) constraint!(ocp, :variable, Index(3), 2, 3, :cv3) constraint!(ocp, :variable, v -> v[3]^2, 0, 1, :cv4)