Skip to content

Commit

Permalink
Merge pull request #135 from control-toolbox/print
Browse files Browse the repository at this point in the history
foo
  • Loading branch information
jbcaillau authored May 20, 2024
2 parents 32d0905 + d856b1c commit 9c156ad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
37 changes: 27 additions & 10 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 * ") = ")
Expand Down Expand Up @@ -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
Expand Down
44 changes: 22 additions & 22 deletions test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9c156ad

Please sign in to comment.