Skip to content

Commit

Permalink
better call to get_optim_variable
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreMartinon committed Sep 2, 2024
1 parent b863ccd commit b8ee2fa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/trapeze.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b8ee2fa

Please sign in to comment.