@@ -97,8 +97,8 @@ function comparison(; max_iter, test_name)
9797 ε_rel_control = 1e-1
9898 ε_abs_control = 1e-6
9999
100- # options for solvers
101- Options = Dict(
100+ # options_ipopt for solvers
101+ options_ipopt = Dict(
102102 :print_level => 0 ,
103103 :tol => TOL,
104104 :mu_strategy => MU_STRATEGY,
@@ -107,6 +107,16 @@ function comparison(; max_iter, test_name)
107107 :max_wall_time => MAX_WALL_TIME,
108108 )
109109
110+ options_madnlp = Dict(
111+ :print_level => MadNLP. ERROR,
112+ :tol => TOL,
113+ # :mu_strategy => MU_STRATEGY,
114+ # :sb => SB,
115+ :max_iter => max_iter,
116+ :max_wall_time => MAX_WALL_TIME,
117+ :linear_solver => MumpsSolver,
118+ )
119+
110120 # we loop over the problems
111121 for f in LIST_OF_PROBLEMS
112122 N = metadata[f][:N] # get default N
@@ -119,30 +129,16 @@ function comparison(; max_iter, test_name)
119129 DEBUG && println(" \n ┌─ " , string(f), " (" , string(test_name), " )" )
120130 DEBUG && println(" │" )
121131
122- # ######### OptimalControl ##########
123- docp = OptimalControlProblems. eval(f)(OptimalControlBackend(); N= N)
124- nlp_oc = nlp_model(docp)
125- nlp_sol = NLPModelsIpopt. ipopt(nlp_oc; Options... )
126- sol = build_ocp_solution(docp, nlp_sol)
127- sol_oc = deepcopy(sol) # for plotting
128-
129- t_oc = time_grid(sol)
130- x_oc = state(sol). (t_oc)
131- u_oc = control(sol). (t_oc)
132- o_oc = objective(sol)
133- i_oc = iterations(sol)
134- v_oc = variable(sol)
135-
136132 # ############## JuMP ###############
137133 nlp_jp = OptimalControlProblems. eval(f)(JuMPBackend(); N= N)
138134 set_optimizer(nlp_jp, Ipopt. Optimizer)
139135 set_silent(nlp_jp)
140- set_optimizer_attribute(nlp_jp, " tol" , Options [:tol])
141- set_optimizer_attribute(nlp_jp, " max_iter" , Options [:max_iter])
142- set_optimizer_attribute(nlp_jp, " mu_strategy" , Options [:mu_strategy])
136+ set_optimizer_attribute(nlp_jp, " tol" , options_ipopt [:tol])
137+ set_optimizer_attribute(nlp_jp, " max_iter" , options_ipopt [:max_iter])
138+ set_optimizer_attribute(nlp_jp, " mu_strategy" , options_ipopt [:mu_strategy])
143139 set_optimizer_attribute(nlp_jp, " linear_solver" , " mumps" )
144- set_optimizer_attribute(nlp_jp, " max_wall_time" , Options [:max_wall_time])
145- set_optimizer_attribute(nlp_jp, " sb" , Options [:sb])
140+ set_optimizer_attribute(nlp_jp, " max_wall_time" , options_ipopt [:max_wall_time])
141+ set_optimizer_attribute(nlp_jp, " sb" , options_ipopt [:sb])
146142 optimize!(nlp_jp)
147143
148144 t_jp = time_grid(f, nlp_jp)
@@ -153,8 +149,34 @@ function comparison(; max_iter, test_name)
153149 v_jp = variable(f, nlp_jp)
154150 p_jp = costate(f, nlp_jp). (t_jp)
155151
152+ # ######### OptimalControl ##########
153+ docp = OptimalControlProblems. eval(f)(OptimalControlBackend(); N= N)
154+ nlp_oc = nlp_model(docp)
155+ nlp_sol = NLPModelsIpopt. ipopt(nlp_oc; options_ipopt... )
156+ sol_oc = build_ocp_solution(docp, nlp_sol)
157+
158+ t_oc = time_grid(sol_oc)
159+ x_oc = state(sol_oc). (t_oc)
160+ u_oc = control(sol_oc). (t_oc)
161+ o_oc = objective(sol_oc)
162+ i_oc = iterations(sol_oc)
163+ v_oc = variable(sol_oc)
164+
165+ # ######### OptimalControl_s ##########
166+ docp = OptimalControlProblems. eval(Symbol(f, :_s))(OptimalControlBackend(), :madnlp, :exa; N= N)
167+ nlp_os = nlp_model(docp)
168+ nlp_sol = madnlp(nlp_os; options_madnlp... )
169+ sol_os = build_ocp_solution(docp, nlp_sol)
170+
171+ t_os = time_grid(sol_os)
172+ x_os = state(sol_os). (t_os)
173+ u_os = control(sol_os). (t_os)
174+ o_os = objective(sol_os)
175+ i_os = iterations(sol_os)
176+ v_os = variable(sol_os)
177+
156178 # ######### Iterations ##########
157- DEBUG && println(" ├─ Iterations → OC: " , i_oc, " , JP : " , i_jp )
179+ DEBUG && println(" ├─ Iterations → JP: " , i_jp, " , OC: " , i_oc, " , OS : " , i_os )
158180
159181 keep_problem = true
160182
@@ -303,18 +325,43 @@ function comparison(; max_iter, test_name)
303325 @assert(length(p_vars)== n)
304326
305327 # OptimalControl
328+ color = 1
306329 labelOC = if (test_name == :solution)
307330 " OptimalControl: " * string(i_oc) * " it"
308331 else
309332 " OptimalControl"
310333 end
311334 plt = plot(
312335 sol_oc;
313- state_style= (color= 1 ,),
314- costate_style= (color= 1 , legend= :none),
315- control_style= (color= 1 , legend= :none),
316- path_style= (color= 1 , legend= :none),
317- dual_style= (color= 1 , legend= :none),
336+ state_style= (color= color,),
337+ costate_style= (color= color, legend= :none),
338+ control_style= (color= color, legend= :none),
339+ path_style= (color= color, legend= :none),
340+ dual_style= (color= color, legend= :none),
341+ size= (900 , 220 * (n+ m)),
342+ label= labelOC,
343+ leftmargin= 20 mm,
344+ )
345+ for i in 2 : n
346+ plot!(plt[i]; legend= :none)
347+ end
348+
349+ # OptimalControl_s
350+ color = 2
351+ labelOC = if (test_name == :solution)
352+ " OptimalControl_s: " * string(i_oc) * " it"
353+ else
354+ " OptimalControl_s"
355+ end
356+ plot!(
357+ plt,
358+ sol_os;
359+ linestyle= :dot,
360+ state_style= (color= color,),
361+ costate_style= (color= color, legend= :none),
362+ control_style= (color= color, legend= :none),
363+ path_style= (color= color, legend= :none),
364+ dual_style= (color= color, legend= :none),
318365 size= (900 , 220 * (n+ m)),
319366 label= labelOC,
320367 leftmargin= 20 mm,
@@ -324,21 +371,22 @@ function comparison(; max_iter, test_name)
324371 end
325372
326373 # JuMP
374+ color = 3
327375 labelJP = (test_name == :solution) ? " JuMP: " * string(i_jp) * " it" : " JuMP"
328376 for i in eachindex(x_vars) # state
329377 xi_jp = [x_jp[k][i] for k in eachindex(t_jp)]
330378 label = i == 1 ? labelJP : :none
331- plot!(plt[i], t_jp, xi_jp; color= 2 , linestyle= :dash, label= label)
379+ plot!(plt[i], t_jp, xi_jp; color= color , linestyle= :dash, label= label)
332380 end
333381
334382 for i in eachindex(p_vars) # costate
335383 pi_jp = [p_jp[k][i] for k in eachindex(t_jp)]
336- plot!(plt[n + i], t_jp, - pi_jp; color= 2 , linestyle= :dash, label= :none)
384+ plot!(plt[n + i], t_jp, - pi_jp; color= color , linestyle= :dash, label= :none)
337385 end
338386
339387 for i in eachindex(u_vars) # control
340388 ui_jp = [u_jp[k][i] for k in eachindex(t_jp)]
341- plot!(plt[2 n + i], t_jp, ui_jp; color= 2 , linestyle= :dash, label= :none)
389+ plot!(plt[2 n + i], t_jp, ui_jp; color= color , linestyle= :dash, label= :none)
342390 end
343391
344392 # save figure
0 commit comments