Skip to content

Commit ded66c5

Browse files
authored
Merge pull request #21 from control-toolbox/profiling
update constraints perf
2 parents e6fa881 + fd6549b commit ded66c5

File tree

11 files changed

+216
-181
lines changed

11 files changed

+216
-181
lines changed

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
99
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
1010
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
1111
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
12-
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1312

1413
[compat]
1514
CTBase = "0.14.0"
1615
DocStringExtensions = "0.9.3"
1716
MLStyle = "0.4.17"
1817
MacroTools = "0.5.13"
1918
Parameters = "0.12.3"
20-
StaticArrays = "1.9.8"
2119
julia = "1.11"

profiling/Project.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3+
CTModels = "34c4fa32-2049-4079-8329-de33c2a22e2d"
4+
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
5+
PProf = "e4faabce-9ead-11e9-39d9-4379958e3056"
6+
Profile = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"
7+
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"

profiling/boundary.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
begin
2+
using Revise
3+
using CTModels
4+
5+
using JET
6+
using BenchmarkTools
7+
using Profile
8+
9+
# define problem with new model: simple integrator
10+
function simple_integrator_model()
11+
pre_ocp = CTModels.PreModel()
12+
CTModels.state!(pre_ocp, 1)
13+
CTModels.control!(pre_ocp, 2)
14+
CTModels.time!(pre_ocp, t0=0.0, tf=1.0)
15+
f!(r, t, x, u, v) = r .= .- x[1] .- u[1] .+ u[2]
16+
CTModels.dynamics!(pre_ocp, f!)
17+
l(t, x, u, v) = (u[1] .+ u[2]).^2
18+
CTModels.objective!(pre_ocp, :min, lagrange=l)
19+
function bc!(r, x0, xf, v)
20+
r[1] = x0[1]
21+
r[2] = xf[1]
22+
return nothing
23+
end
24+
#bc!(r, x0, xf, v) = r .= [x0[1], xf[1]]
25+
CTModels.constraint!(pre_ocp, :boundary, f=bc!, lb=[-1, 0], ub=[-1, 0], label=:boundary1)
26+
CTModels.constraint!(pre_ocp, :boundary, f=bc!, lb=[-1, 0], ub=[-1, 0], label=:boundary2)
27+
CTModels.constraint!(pre_ocp, :boundary, f=bc!, lb=[-1, 0], ub=[-1, 0], label=:boundary3)
28+
CTModels.constraint!(pre_ocp, :control, rg=1:2, lb=[0, 0], ub=[Inf, Inf], label=:control_rg)
29+
CTModels.definition!(pre_ocp, Expr(:simple_integrator_min_energy))
30+
ocp = CTModels.build_model(pre_ocp)
31+
return ocp
32+
end
33+
34+
ocp = simple_integrator_model()
35+
36+
x0 = [1.0]
37+
xf = [0.0]
38+
v = Float64[]
39+
r = zeros(Float64, 6)
40+
bc_constraint = CTModels.boundary_constraints_nl(ocp)
41+
boundary! = bc_constraint[2]
42+
boundary!(r, x0, xf, v)
43+
r
44+
45+
function bc!(r, x0, xf, v)
46+
r[1] = x0[1]
47+
r[2] = xf[1]
48+
return nothing
49+
end
50+
end
51+
52+
let
53+
println("--------------------------------")
54+
println("Boundary constraint")
55+
@code_warntype bc!(r, x0, xf, v)
56+
57+
println("--------------------------------")
58+
println("Boundary constraint from model")
59+
@code_warntype boundary!(r, x0, xf, v)
60+
end
61+
62+
let
63+
println("Boundary constraint")
64+
@btime bc!(r, x0, xf, v)
65+
println("Boundary constraint from model")
66+
@btime boundary!(r, x0, xf, v)
67+
end
68+
69+
let
70+
println("--------------------------------")
71+
println("Boundary constraint")
72+
@code_native debuginfo=:none dump_module=false bc!(r, x0, xf, v)
73+
74+
println("--------------------------------")
75+
println("Boundary constraint from model")
76+
@code_native debuginfo=:none dump_module=false boundary!(r, x0, xf, v)
77+
end

src/constraints.jl

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ function __constraint!(
106106

107107
(::Nothing, ::Function, ::ctVector, ::ctVector) => begin
108108
# set the constraint
109-
if type [:boundary, :variable, :path]
109+
if type [:boundary, :path]
110110
ocp_constraints[label] = (type, f, lb, ub)
111111
else
112112
throw(
113113
CTBase.IncorrectArgument(
114114
"the following type of constraint is not valid: " *
115115
String(type) *
116-
". Please choose in [ :boundary, :variable, :path ] or check the arguments of the constraint! method.",
116+
". Please choose in [ :boundary, :path ] or check the arguments of the constraint! method.",
117117
),
118118
)
119119
end
@@ -172,7 +172,7 @@ constraint(ocp::Model, label::Symbol)::Tuple = ocp.constraints.dict[label]
172172

173173
(
174174
path_constraints_nl(
175-
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TV1,TB,TS,TC,TV2,ConstraintsDictType}}
175+
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TB,TS,TC,TV,ConstraintsDictType}}
176176
)::TP
177177
) where {
178178
T<:TimesModel,
@@ -182,35 +182,15 @@ constraint(ocp::Model, label::Symbol)::Tuple = ocp.constraints.dict[label]
182182
D<:Function,
183183
O<:AbstractObjectiveModel,
184184
TP,
185-
TV1,
186185
TB,
187186
TS,
188187
TC,
189-
TV2,
188+
TV,
190189
} = ocp.constraints.path_nl
191190

192-
(
193-
variable_constraints_nl(
194-
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TV1,TB,TS,TC,TV2,ConstraintsDictType}}
195-
)::TV1
196-
) where {
197-
T<:TimesModel,
198-
S<:AbstractStateModel,
199-
C<:AbstractControlModel,
200-
V<:AbstractVariableModel,
201-
D<:Function,
202-
O<:AbstractObjectiveModel,
203-
TP,
204-
TV1,
205-
TB,
206-
TS,
207-
TC,
208-
TV2,
209-
} = ocp.constraints.variable_nl
210-
211191
(
212192
boundary_constraints_nl(
213-
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TV1,TB,TS,TC,TV2,ConstraintsDictType}}
193+
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TB,TS,TC,TV,ConstraintsDictType}}
214194
)::TB
215195
) where {
216196
T<:TimesModel,
@@ -220,16 +200,15 @@ constraint(ocp::Model, label::Symbol)::Tuple = ocp.constraints.dict[label]
220200
D<:Function,
221201
O<:AbstractObjectiveModel,
222202
TP,
223-
TV1,
224203
TB,
225204
TS,
226205
TC,
227-
TV2,
206+
TV,
228207
} = ocp.constraints.boundary_nl
229208

230209
(
231210
state_constraints_box(
232-
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TV1,TB,TS,TC,TV2,ConstraintsDictType}}
211+
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TB,TS,TC,TV,ConstraintsDictType}}
233212
)::TS
234213
) where {
235214
T<:TimesModel,
@@ -239,16 +218,15 @@ constraint(ocp::Model, label::Symbol)::Tuple = ocp.constraints.dict[label]
239218
D<:Function,
240219
O<:AbstractObjectiveModel,
241220
TP,
242-
TV1,
243221
TB,
244222
TS,
245223
TC,
246-
TV2,
224+
TV,
247225
} = ocp.constraints.state_box
248226

249227
(
250228
control_constraints_box(
251-
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TV1,TB,TS,TC,TV2,ConstraintsDictType}}
229+
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TB,TS,TC,TV,ConstraintsDictType}}
252230
)::TC
253231
) where {
254232
T<:TimesModel,
@@ -258,17 +236,16 @@ constraint(ocp::Model, label::Symbol)::Tuple = ocp.constraints.dict[label]
258236
D<:Function,
259237
O<:AbstractObjectiveModel,
260238
TP,
261-
TV1,
262239
TB,
263240
TS,
264241
TC,
265-
TV2,
242+
TV,
266243
} = ocp.constraints.control_box
267244

268245
(
269246
variable_constraints_box(
270-
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TV1,TB,TS,TC,TV2,ConstraintsDictType}}
271-
)::TV2
247+
ocp::Model{T,S,C,V,D,O,ConstraintsModel{TP,TB,TS,TC,TV,ConstraintsDictType}}
248+
)::TV
272249
) where {
273250
T<:TimesModel,
274251
S<:AbstractStateModel,
@@ -277,51 +254,43 @@ constraint(ocp::Model, label::Symbol)::Tuple = ocp.constraints.dict[label]
277254
D<:Function,
278255
O<:AbstractObjectiveModel,
279256
TP,
280-
TV1,
281257
TB,
282258
TS,
283259
TC,
284-
TV2,
260+
TV,
285261
} = ocp.constraints.variable_box
286262

287263
"""
288264
$(TYPEDSIGNATURES)
289265
290266
Return the dimension of nonlinear path constraints.
291267
"""
292-
dim_path_constraints_nl(ocp::Model)::Int = length(path_constraints_nl(ocp)[1])
268+
dim_path_constraints_nl(ocp::Model)::Dimension = length(path_constraints_nl(ocp)[1])
293269

294270
"""
295271
$(TYPEDSIGNATURES)
296272
297273
Return the dimension of the boundary constraints.
298274
"""
299-
dim_boundary_constraints_nl(ocp::Model)::Int = length(boundary_constraints_nl(ocp)[1])
300-
301-
"""
302-
$(TYPEDSIGNATURES)
303-
304-
Return the dimension of nonlinear variable constraints.
305-
"""
306-
dim_variable_constraints_nl(ocp::Model)::Int = length(variable_constraints_nl(ocp)[1])
275+
dim_boundary_constraints_nl(ocp::Model)::Dimension = length(boundary_constraints_nl(ocp)[1])
307276

308277
"""
309278
$(TYPEDSIGNATURES)
310279
311280
Return the dimension of box constraints on state.
312281
"""
313-
dim_state_constraints_box(ocp::Model)::Int = length(state_constraints_box(ocp)[1])
282+
dim_state_constraints_box(ocp::Model)::Dimension = length(state_constraints_box(ocp)[1])
314283

315284
"""
316285
$(TYPEDSIGNATURES)
317286
318287
Return the dimension of box constraints on control.
319288
"""
320-
dim_control_constraints_box(ocp::Model)::Int = length(control_constraints_box(ocp)[1])
289+
dim_control_constraints_box(ocp::Model)::Dimension = length(control_constraints_box(ocp)[1])
321290

322291
"""
323292
$(TYPEDSIGNATURES)
324293
325294
Return the dimension of box constraints on variable.
326295
"""
327-
dim_variable_constraints_box(ocp::Model)::Int = length(variable_constraints_box(ocp)[1])
296+
dim_variable_constraints_box(ocp::Model)::Dimension = length(variable_constraints_box(ocp)[1])

0 commit comments

Comments
 (0)