From b8ee2fac790a82c02e9ca781f60dc587e3613390 Mon Sep 17 00:00:00 2001 From: Pierre Martinon Date: Mon, 2 Sep 2024 14:34:30 +0200 Subject: [PATCH] better call to get_optim_variable --- src/problem.jl | 5 ++++- src/solution.jl | 9 ++++++--- src/trapeze.jl | 4 +++- src/utils.jl | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/problem.jl b/src/problem.jl index 352a291a..1bf30d1d 100644 --- a/src/problem.jl +++ b/src/problem.jl @@ -282,12 +282,15 @@ function DOCP_objective(xu, docp::DOCP) N = docp.dim_NLP_steps ocp = docp.ocp + # optimization variables + v = Float64[] + docp.has_variable && (v = get_optim_variable(xu, docp)) + # final state is always needed since lagrange cost is there xf, uf, xlf = get_variables_at_time_step(xu, docp, N) # mayer cost if docp.has_mayer - v = get_optim_variable(xu, docp) x0, u0, xl0 = get_variables_at_time_step(xu, docp, 0) obj = obj + ocp.mayer(x0, xf, v) end diff --git a/src/solution.jl b/src/solution.jl index 4eaadfcc..8ce71eac 100644 --- a/src/solution.jl +++ b/src/solution.jl @@ -100,6 +100,7 @@ function parse_DOCP_solution_primal(docp, solution; mult_LB = nothing, mult_UB = N = docp.dim_NLP_steps X = zeros(N + 1, docp.dim_NLP_x) U = zeros(N + 1, docp.dim_NLP_u) + v = Float64[] # multipliers for box constraints if isnothing(mult_LB) || length(mult_LB) == 0 @@ -116,9 +117,11 @@ function parse_DOCP_solution_primal(docp, solution; mult_LB = nothing, mult_UB = mult_variable_box_upper = zeros(N + 1, docp.dim_NLP_v) # retrieve optimization variables - v = get_optim_variable(solution, docp) - mult_variable_box_lower = get_optim_variable(mult_LB, docp) - mult_variable_box_upper = get_optim_variable(mult_UB, docp) + if docp.has_variable + v = get_optim_variable(solution, docp) + mult_variable_box_lower = get_optim_variable(mult_LB, docp) + mult_variable_box_upper = get_optim_variable(mult_UB, docp) + end # loop over time steps for i = 1:(N + 1) diff --git a/src/trapeze.jl b/src/trapeze.jl index 123acfc9..ab5d4bec 100644 --- a/src/trapeze.jl +++ b/src/trapeze.jl @@ -120,7 +120,9 @@ struct ArgsAtTimeStep_Trapeze end # +++multiple dispatch here seems to cause more allocations ! function initArgs(xu, docp::DOCP{TrapezeTag}, time_grid) - v = get_optim_variable(xu, docp) + # optimization variables + v = Float64[] + docp.has_variable && (v = get_optim_variable(xu, docp)) args_i = ArgsAtTimeStep_Trapeze(xu, docp, v, time_grid, 0) args_ip1 = ArgsAtTimeStep_Trapeze(xu, docp, v, time_grid, 1) return (args_i, args_ip1), v diff --git a/src/utils.jl b/src/utils.jl index 8664beec..e2844322 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -20,7 +20,7 @@ function get_optim_variable(xu, docp) return xu[(end - docp.dim_NLP_v + 1):end] end else - return similar(xu, 0) + error("Problem is not variable dependent") end end