Skip to content

Commit cc4bfae

Browse files
ocotsgithub-actions[bot]
authored andcommitted
🤖 Format .jl files
1 parent 508601c commit cc4bfae

File tree

17 files changed

+174
-129
lines changed

17 files changed

+174
-129
lines changed

ext/CTDirectExtADNLP.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ function CTDirect.build_nlp!(
2929
c! = (c, x) -> CTDirect.DOCP_constraints!(c, x, docp)
3030

3131
# unused backends (option excluded_backend = [:jprod_backend, :jtprod_backend, :hprod_backend, :ghjvprod_backend] does not seem to work)
32-
unused_backends = (hprod_backend=ADNLPModels.EmptyADbackend,
33-
jtprod_backend=ADNLPModels.EmptyADbackend,
34-
jprod_backend=ADNLPModels.EmptyADbackend,
35-
ghjvprod_backend=ADNLPModels.EmptyADbackend)
32+
unused_backends = (
33+
hprod_backend=ADNLPModels.EmptyADbackend,
34+
jtprod_backend=ADNLPModels.EmptyADbackend,
35+
jprod_backend=ADNLPModels.EmptyADbackend,
36+
ghjvprod_backend=ADNLPModels.EmptyADbackend,
37+
)
3638

3739
# call NLP problem constructor
3840
if adnlp_backend == :manual
@@ -52,9 +54,11 @@ function CTDirect.build_nlp!(
5254
c!,
5355
CTDirect.DOCP_Hessian_pattern(docp),
5456
)
55-
backend_options = (gradient_backend=ADNLPModels.ReverseDiffADGradient,
56-
jacobian_backend=J_backend,
57-
hessian_backend=H_backend)
57+
backend_options = (
58+
gradient_backend=ADNLPModels.ReverseDiffADGradient,
59+
jacobian_backend=J_backend,
60+
hessian_backend=H_backend,
61+
)
5862

5963
else
6064
# use backend preset
@@ -70,10 +74,10 @@ function CTDirect.build_nlp!(
7074
c!,
7175
docp.bounds.con_l,
7276
docp.bounds.con_u;
73-
minimize = !docp.flags.max,
77+
minimize=(!docp.flags.max),
7478
backend_options...,
7579
unused_backends...,
76-
show_time = show_time,
80+
show_time=show_time,
7781
)
7882

7983
# set NLP in DOCP

ext/CTDirectExtExa.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ function CTDirect.build_nlp!(
6060
return nothing
6161
end
6262

63-
6463
function CTDirect.get_time_grid_exa(docp_solution, docp)
6564
grid = zeros(docp.time.steps+1)
6665
ocp = docp.ocp
6766

6867
if docp.flags.freet0 || docp.flags.freetf
69-
v = docp.exa_getter(docp_solution, val=:variable)
68+
v = docp.exa_getter(docp_solution; val=:variable)
7069
end
7170

7271
if docp.flags.freet0

ext/CTDirectExtMadNLP.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ function CTDirect.solve_docp(
3838
nlp = CTDirect.nlp_model(docp)
3939

4040
# preallocate solver (NB. need to pass printlevel here)
41-
solver = MadNLPSolver(nlp; print_level=print_level, tol=tol, max_iter=max_iter, kwargs...
41+
solver = MadNLPSolver(
42+
nlp; print_level=print_level, tol=tol, max_iter=max_iter, kwargs...
4243
)
4344

4445
# solve discretized problem with NLP solver
@@ -48,9 +49,7 @@ function CTDirect.solve_docp(
4849
return docp_solution
4950
end
5051

51-
5252
function CTDirect.SolverInfos(nlp_solution::MadNLP.MadNLPExecutionStats)
53-
5453
objective = nlp_solution.objective # NB sign is incorrect for max problems !
5554
iterations = nlp_solution.iter
5655
constraints_violation = nlp_solution.primal_feas

src/default.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,3 @@ $(TYPEDSIGNATURES)
6969
Default maximum of iterations: `1000`
7070
"""
7171
__max_iterations() = 1000
72-
73-

src/disc/common.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#= Common parts for the discretization =#
23

34
"""

src/disc/euler.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ $(TYPEDSIGNATURES)
8686
Set work array for all dynamics and lagrange cost evaluations
8787
"""
8888
function setWorkArray(docp::DOCP{Euler}, xu, time_grid, v)
89-
9089
ocp = ocp_model(docp)
9190
disc = disc_model(docp)
9291
dims = docp.dims
@@ -124,7 +123,6 @@ $(TYPEDSIGNATURES)
124123
Compute the running cost
125124
"""
126125
function runningCost(docp::DOCP{Euler}, xu, v, time_grid)
127-
128126
ocp = ocp_model(docp)
129127
disc = disc_model(docp)
130128
dims = docp.dims
@@ -156,9 +154,8 @@ Set the constraints corresponding to the state equation
156154
Convention: 1 <= i <= dim_NLP_steps+1
157155
"""
158156
function setStepConstraints!(docp::DOCP{Euler}, c, xu, v, time_grid, i, work)
159-
160157
disc = disc_model(docp)
161-
dims = docp.dims
158+
dims = docp.dims
162159

163160
# offset for previous steps
164161
offset = (i-1)*(disc._state_stage_eqs_block + disc._step_pathcons_block)
@@ -283,7 +280,9 @@ function DOCP_Jacobian_pattern(docp::DOCP{Euler})
283280
add_nonzero_block!(Is, Js, c_offset+1, c_offset+c_block, v_start, v_end)
284281

285282
# 3. boundary constraints (x0, xf, v)
286-
c_offset = docp.time.steps * (disc._state_stage_eqs_block + disc._step_pathcons_block) + disc._step_pathcons_block
283+
c_offset =
284+
docp.time.steps * (disc._state_stage_eqs_block + disc._step_pathcons_block) +
285+
disc._step_pathcons_block
287286
c_block = dims.boundary_cons
288287
x0_start = 1
289288
x0_end = dims.OCP_x

src/disc/irk.jl

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@ struct Gauss_Legendre_1 <: GenericIRK
2828
_step_pathcons_block::Int
2929
_final_control::Bool
3030

31-
function Gauss_Legendre_1(dim_NLP_steps, dim_NLP_x, dim_NLP_u, dim_NLP_v, dim_path_cons, dim_boundary_cons)
31+
function Gauss_Legendre_1(
32+
dim_NLP_steps, dim_NLP_x, dim_NLP_u, dim_NLP_v, dim_path_cons, dim_boundary_cons
33+
)
3234
stage = 1
3335

34-
step_variables_block, state_stage_eqs_block, step_pathcons_block, dim_NLP_variables, dim_NLP_constraints = IRK_dims(dim_NLP_steps, dim_NLP_x, dim_NLP_u, dim_NLP_v, dim_path_cons, dim_boundary_cons, stage)
36+
step_variables_block, state_stage_eqs_block, step_pathcons_block, dim_NLP_variables, dim_NLP_constraints = IRK_dims(
37+
dim_NLP_steps,
38+
dim_NLP_x,
39+
dim_NLP_u,
40+
dim_NLP_v,
41+
dim_path_cons,
42+
dim_boundary_cons,
43+
stage,
44+
)
3545

3646
disc = new(
3747
"[test only] Implicit Midpoint aka Gauss-Legendre collocation for s=1, 2nd order, symplectic, A-stable",
@@ -152,7 +162,9 @@ $(TYPEDSIGNATURES)
152162
153163
Return the dimension of the NLP variables and constraints for a generic IRK discretizion, with the control taken constant per step (ie not distinct controls at time stages)
154164
"""
155-
function IRK_dims(dim_NLP_steps, dim_NLP_x, dim_NLP_u, dim_NLP_v, dim_path_cons, dim_boundary_cons, stage)
165+
function IRK_dims(
166+
dim_NLP_steps, dim_NLP_x, dim_NLP_u, dim_NLP_v, dim_path_cons, dim_boundary_cons, stage
167+
)
156168

157169
# size of variables block for one step: x, u, k
158170
step_variables_block = dim_NLP_x + dim_NLP_u + dim_NLP_x * stage
@@ -167,9 +179,14 @@ function IRK_dims(dim_NLP_steps, dim_NLP_x, dim_NLP_u, dim_NLP_v, dim_path_cons,
167179
dim_NLP_variables = dim_NLP_steps * step_variables_block + dim_NLP_x + dim_NLP_v
168180

169181
# NLP constraints size ([dynamics, stage, path]_1..N, final path, boundary, variable)
170-
dim_NLP_constraints = dim_NLP_steps * (state_stage_eqs_block + step_pathcons_block) + step_pathcons_block + dim_boundary_cons
171-
172-
return step_variables_block, state_stage_eqs_block, step_pathcons_block, dim_NLP_variables, dim_NLP_constraints
182+
dim_NLP_constraints =
183+
dim_NLP_steps * (state_stage_eqs_block + step_pathcons_block) +
184+
step_pathcons_block +
185+
dim_boundary_cons
186+
187+
return step_variables_block,
188+
state_stage_eqs_block, step_pathcons_block, dim_NLP_variables,
189+
dim_NLP_constraints
173190
end
174191

175192
"""
@@ -179,7 +196,7 @@ Set work array for all dynamics and lagrange cost evaluations
179196
"""
180197
function setWorkArray(docp::DOCP{<: GenericIRK}, xu, time_grid, v)
181198
# work array layout: [x_ij ; sum_bk]
182-
dims = docp.dims
199+
dims = docp.dims
183200
work = similar(xu, dims.OCP_x + dims.NLP_x)
184201
return work
185202
end
@@ -190,9 +207,8 @@ $(TYPEDSIGNATURES)
190207
Compute the running cost
191208
"""
192209
function runningCost(docp::DOCP{<: GenericIRK}, xu, v, time_grid)
193-
194210
disc = disc_model(docp)
195-
dims = docp.dims
211+
dims = docp.dims
196212
obj_lagrange = 0.0
197213

198214
# work array layout: [x_ij ; sum_bk]
@@ -219,21 +235,18 @@ function runningCost(docp::DOCP{<: GenericIRK}, xu, v, time_grid)
219235
for l in 1:disc.stage
220236
kil = get_stagevars_at_time_step(xu, docp, i, l)
221237
@views @. work_xij[1:dims.OCP_x] =
222-
work_xij[1:dims.OCP_x] +
223-
hi * disc.butcher_a[j, l] * kil[1:dims.OCP_x]
238+
work_xij[1:dims.OCP_x] + hi * disc.butcher_a[j, l] * kil[1:dims.OCP_x]
224239
end
225240

226241
# update sum b_j k_i^j (lagrange term)
227242
# split to avoid dual tag ordering error in AD
228243
if j == 1
229244
work_sumbk[1] =
230-
disc.butcher_b[j] *
231-
CTModels.lagrange(docp.ocp)(tij, work_xij, ui, v)
245+
disc.butcher_b[j] * CTModels.lagrange(docp.ocp)(tij, work_xij, ui, v)
232246
else
233247
work_sumbk[1] =
234248
work_sumbk[1] +
235-
disc.butcher_b[j] *
236-
CTModels.lagrange(docp.ocp)(tij, work_xij, ui, v)
249+
disc.butcher_b[j] * CTModels.lagrange(docp.ocp)(tij, work_xij, ui, v)
237250
end
238251
end
239252

@@ -251,11 +264,10 @@ Set the constraints corresponding to the state equation
251264
Convention: 1 <= i <= dim_NLP_steps (+1)
252265
"""
253266
function setStepConstraints!(docp::DOCP{<: GenericIRK}, c, xu, v, time_grid, i, work)
254-
255267
ocp = ocp_model(docp)
256268
disc = disc_model(docp)
257269
dims = docp.dims
258-
270+
259271
# work array layout: [x_ij ; sum_bk]
260272
work_xij = @view work[1:dims.OCP_x]
261273
work_sumbk = @view work[(dims.OCP_x + 1):(dims.OCP_x + dims.NLP_x)]
@@ -289,12 +301,10 @@ function setStepConstraints!(docp::DOCP{<: GenericIRK}, c, xu, v, time_grid, i,
289301
# update sum b_j k_i^j (w/ lagrange term) for state equation after loop
290302
# split to avoid dual tag ordering error in AD
291303
if j == 1
292-
@views @. work_sumbk[1:dims.NLP_x] =
293-
disc.butcher_b[j] * kij[1:dims.NLP_x]
304+
@views @. work_sumbk[1:dims.NLP_x] = disc.butcher_b[j] * kij[1:dims.NLP_x]
294305
else
295306
@views @. work_sumbk[1:dims.NLP_x] =
296-
work_sumbk[1:dims.NLP_x] +
297-
disc.butcher_b[j] * kij[1:dims.NLP_x]
307+
work_sumbk[1:dims.NLP_x] + disc.butcher_b[j] * kij[1:dims.NLP_x]
298308
end
299309

300310
# state at stage: x_i^j = x_i + h_i sum a_jl k_i^l
@@ -303,8 +313,7 @@ function setStepConstraints!(docp::DOCP{<: GenericIRK}, c, xu, v, time_grid, i,
303313
for l in 1:disc.stage
304314
kil = get_stagevars_at_time_step(xu, docp, i, l)
305315
@views @. work_xij[1:dims.OCP_x] =
306-
work_xij[1:dims.OCP_x] +
307-
hi * disc.butcher_a[j, l] * kil[1:dims.OCP_x]
316+
work_xij[1:dims.OCP_x] + hi * disc.butcher_a[j, l] * kil[1:dims.OCP_x]
308317
end
309318

310319
# stage equations k_i^j = f(t_i^j, x_i^j, u_i, v) as c[] = k - f
@@ -332,10 +341,8 @@ function setStepConstraints!(docp::DOCP{<: GenericIRK}, c, xu, v, time_grid, i,
332341
xip1 - (xi + hi * work_sumbk[1:dims.OCP_x])
333342
if docp.flags.lagrange && docp.flags.lagrange_to_mayer
334343
c[offset + dims.NLP_x] =
335-
get_lagrange_state_at_time_step(xu, docp, i+1) - (
336-
get_lagrange_state_at_time_step(xu, docp, i) +
337-
hi * work_sumbk[dims.NLP_x]
338-
)
344+
get_lagrange_state_at_time_step(xu, docp, i+1) -
345+
(get_lagrange_state_at_time_step(xu, docp, i) + hi * work_sumbk[dims.NLP_x])
339346
end
340347

341348
# update offset for stage and state equations
@@ -356,7 +363,6 @@ $(TYPEDSIGNATURES)
356363
Build sparsity pattern for Jacobian of constraints
357364
"""
358365
function DOCP_Jacobian_pattern(docp::DOCP{<: GenericIRK})
359-
360366
disc = disc_model(docp)
361367
dims = docp.dims
362368

@@ -443,7 +449,9 @@ function DOCP_Jacobian_pattern(docp::DOCP{<: GenericIRK})
443449
add_nonzero_block!(Is, Js, c_offset+1, c_offset+c_block, v_start, v_end)
444450

445451
# 3. boundary constraints (x0, xf, v)
446-
c_offset = docp.time.steps * (disc._state_stage_eqs_block + disc._step_pathcons_block) + disc._step_pathcons_block
452+
c_offset =
453+
docp.time.steps * (disc._state_stage_eqs_block + disc._step_pathcons_block) +
454+
disc._step_pathcons_block
447455
c_block = dims.boundary_cons
448456
x0_start = 1
449457
x0_end = dims.OCP_x
@@ -467,7 +475,6 @@ $(TYPEDSIGNATURES)
467475
Build sparsity pattern for Hessian of Lagrangian
468476
"""
469477
function DOCP_Hessian_pattern(docp::DOCP{<: GenericIRK})
470-
471478
disc = disc_model(docp)
472479
dims = docp.dims
473480

0 commit comments

Comments
 (0)