Skip to content

Commit

Permalink
test_initial_guess updated
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreMartinon committed Jul 2, 2024
1 parent e04c087 commit 1b5bad7
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions test/test_initial_guess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ maxiter = 0

# test functions
function check_xf(sol, xf)
#println(xf)
#println(sol.state(sol.times[end]))
return xf == sol.state(sol.times[end])
end
function check_uf(sol, uf)
#println(uf)
#println(sol.control(sol.times[end]))
return uf == sol.control(sol.times[end])
end
function check_v(sol, v)
#println(v)
#println(sol.variable)
return v == sol.variable
end

Expand Down Expand Up @@ -49,9 +43,6 @@ x_func = t->[t^2, sqrt(t)]
u_func = t->(cos(10*t)+1)*0.5

# interpolated initial gues
v = 1 #tf
t_vec = [0, .1, v]
t_matrix = [0 .1 v]
x_vec = [[0, 0], [1, 2], [5, -1]]
x_matrix = [0 0; 1 2; 5 -1]
u_vec = [0, 0.3, .1]
Expand Down Expand Up @@ -93,21 +84,21 @@ end

sol = solve(ocp, print_level=0, init=(state=x_const, control=u_const), max_iter=maxiter)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Constant x,u; default v", sol.objective, sol.iterations)
@printf("%-56s %.3f at %d iterations\n", "Constant x,u; default v", sol.objective, sol.iterations)
else
println(check_xf(sol, x_const) && check_uf(sol, u_const))
end

sol = solve(ocp, print_level=0, init=(state=x_const, variable=v_const), max_iter=maxiter)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Constant x,v; default u)", sol.objective, sol.iterations)
@printf("%-56s %.3f at %d iterations\n", "Constant x,v; default u", sol.objective, sol.iterations)
else
println(check_xf(sol, x_const) && check_v(sol, v_const))
end

sol = solve(ocp, print_level=0, init=(control=u_const, variable=v_const), max_iter=maxiter)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Constant u,v; default x)", sol.objective, sol.iterations)
@printf("%-56s %.3f at %d iterations\n", "Constant u,v; default x", sol.objective, sol.iterations)
else
println(check_uf(sol, u_const) && check_v(sol, v_const))
end
Expand Down Expand Up @@ -141,17 +132,37 @@ else
println(check_xf(sol, x_func(sol.times[end])) && check_uf(sol, u_func(sol.times[end])))
end

# +++1.d interpolated initial guess
# 1.d interpolated initial guess
t_vec = [0, .1, v_const]
sol = solve(ocp, print_level=0, init=(time=t_vec, state=x_vec, control=u_vec, variable=v_const), max_iter=maxiter)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Vector t,x,u; constant v", sol.objective, sol.iterations)
else
println(check_xf(sol, x_vec[end]) && check_uf(sol, u_vec[end]) && check_v(sol, v_const))
end

# 1.e mixed initial guess
sol = solve(ocp, print_level=0, init=(state=x_func, control=u_const), max_iter=maxiter)
t_matrix = [0 .1 v_const]
sol = solve(ocp, print_level=0, init=(time=t_matrix, state=x_vec, control=u_vec, variable=v_const), max_iter=maxiter)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Matrix t; vector x,u; constant v", sol.objective, sol.iterations)
else
println(check_xf(sol, x_vec[end]) && check_uf(sol, u_vec[end]) && check_v(sol, v_const))
end

sol = solve(ocp, print_level=0, init=(time=t_vec, state=x_matrix, control=u_vec, variable=v_const), max_iter=maxiter)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Functional x; constant u; default v", sol.objective, sol.iterations)
@printf("%-56s %.3f at %d iterations\n", "Matrix x; vector t,u; constant v", sol.objective, sol.iterations)
else
println(check_xf(sol, x_func(sol.times[end])) && check_uf(sol, u_const))
println(check_xf(sol, x_vec[end]) && check_uf(sol, u_vec[end]) && check_v(sol, v_const))
end

#+++ some mixes with interp
# 1.e mixed initial guess
sol = solve(ocp, print_level=0, init=(time=t_vec, state=x_vec, control=u_func, variable=v_const), max_iter=maxiter)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Mixed: vector t,x; functional u; constant v", sol.objective, sol.iterations)
else
println(check_xf(sol, x_vec[end]) && check_uf(sol, u_func(sol.times[end])) && check_v(sol, v_const))
end

# 1.f warm start
sol = solve(ocp, print_level=0, init=sol0, max_iter=maxiter)
Expand All @@ -166,13 +177,13 @@ end
println("\n2. Setting the initial guess at the DOCP level")
docp = directTranscription(ocp)
# mixed init
setInitialGuess(docp, (state=x_func, control=u_const))
setInitialGuess(docp, (time=t_vec, state=x_vec, control=u_func, variable=v_const))
dsol = solve(docp, print_level=0, max_iter=maxiter)
sol = OCPSolutionFromDOCP(docp, dsol)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Mixed initial guess set in DOCP", sol.objective, sol.iterations)
else
println(check_xf(sol, x_func(sol.times[end])) && check_uf(sol, u_const))
println(check_xf(sol, x_vec[end]) && check_uf(sol, u_func(sol.times[end])) && check_v(sol, v_const))
end

# warm start
Expand All @@ -190,12 +201,12 @@ end
println("\n3. Passing the initial guess to solve call")
setInitialGuess(docp, ()) # reset init in docp
# mixed init
dsol = solve(docp, init=(state=x_func, control=u_const), print_level=0, max_iter=maxiter)
dsol = solve(docp, init=(time=t_vec, state=x_vec, control=u_func, variable=v_const), print_level=0, max_iter=maxiter)
sol = OCPSolutionFromDOCP(docp, dsol)
if maxiter > 0
@printf("%-56s %.3f at %d iterations\n", "Mixed initial guess passed to solve", sol.objective, sol.iterations)
else
println(check_xf(sol, x_func(sol.times[end])) && check_uf(sol, u_const))
println(check_xf(sol, x_vec[end]) && check_uf(sol, u_func(sol.times[end])) && check_v(sol, v_const))
end

# warm start
Expand Down

0 comments on commit 1b5bad7

Please sign in to comment.