Skip to content

Commit 40c7388

Browse files
committed
v0.2.0
1 parent 9bddc31 commit 40c7388

24 files changed

+275
-277
lines changed

Project.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
name = "IterativeLQR"
22
uuid = "605048dd-e178-462b-beb9-98a09398ef27"
33
authors = ["thowell <thowell@stanford.edu>"]
4-
version = "0.1.3"
4+
version = "0.2.0"
55

66
[deps]
7-
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
87
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
98
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
109
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
1110
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1211

1312
[compat]
14-
BenchmarkTools = "1.0"
1513
JLD2 = "0.4"
1614
Scratch = "1.0"
1715
Symbolics = "0.1.29 - 0.1.29"

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ using LinearAlgebra
3131
T = 11
3232

3333
# particle
34-
nx = 2
35-
nu = 1
34+
num_state = 2
35+
num_action = 1
3636

3737
function particle(x, u, w)
3838
A = [1.0 1.0; 0.0 1.0]
@@ -41,27 +41,27 @@ function particle(x, u, w)
4141
end
4242

4343
# model
44-
dyn = Dynamics(particle, nx, nu)
44+
dyn = Dynamics(particle, num_state, num_action)
4545
model = [dyn for t = 1:T-1]
4646

4747
# initialization
4848
x1 = [0.0; 0.0]
4949
xT = [1.0; 0.0]
50-
= [1.0e-1 * randn(nu) for t = 1:T-1]
50+
= [1.0e-1 * randn(num_action) for t = 1:T-1]
5151
= rollout(model, x1, ū)
5252

5353
# objective
5454
ot = (x, u, w) -> 0.1 * dot(x, x) + 0.1 * dot(u, u)
5555
oT = (x, u, w) -> 0.1 * dot(x, x)
56-
ct = Cost(ot, nx, nu)
57-
cT = Cost(oT, nx, 0)
56+
ct = Cost(ot, num_state, num_action)
57+
cT = Cost(oT, num_state, 0)
5858
obj = [[ct for t = 1:T-1]..., cT]
5959

6060
# constraints
6161
goal(x, u, w) = x - xT
6262

6363
cont = Constraint()
64-
conT = Constraint(goal, nx, 0)
64+
conT = Constraint(goal, num_state, 0)
6565
cons = [[cont for t = 1:T-1]..., conT]
6666

6767
# problem

examples/acrobot.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ using Plots
1212
T = 101
1313

1414
# ## acrobot
15-
nx = 4
16-
nu = 1
17-
nw = 0
15+
num_state = 4
16+
num_action = 1
17+
num_parameter = 0
1818

1919
function acrobot(x, u, w)
2020
mass1 = 1.0
@@ -89,28 +89,28 @@ function midpoint_explicit(x, u, w)
8989
end
9090

9191
# ## model
92-
dyn = Dynamics(midpoint_explicit, nx, nu, nw)
92+
dyn = Dynamics(midpoint_explicit, num_state, num_action, num_parameter)
9393
model = [dyn for t = 1:T-1]
9494

9595
# ## initialization
9696
x1 = [0.0; 0.0; 0.0; 0.0]
9797
xT = [0.0; π; 0.0; 0.0]
98-
= [1.0 * randn(nu) for t = 1:T-1]
99-
w = [zeros(nw) for t = 1:T]
98+
= [1.0 * randn(num_action) for t = 1:T-1]
99+
w = [zeros(num_parameter) for t = 1:T]
100100
= rollout(model, x1, ū, w)
101101

102102
# ## objective
103103
ot = (x, u, w) -> 0.1 * dot(x[3:4], x[3:4]) + 0.1 * dot(u, u)
104104
oT = (x, u, w) -> 0.1 * dot(x[3:4], x[3:4])
105-
ct = Cost(ot, nx, nu, nw)
106-
cT = Cost(oT, nx, 0, nw)
105+
ct = Cost(ot, num_state, num_action, num_parameter)
106+
cT = Cost(oT, num_state, 0, num_parameter)
107107
obj = [[ct for t = 1:T-1]..., cT]
108108

109109
# ## constraints
110110
goal(x, u, w) = x - xT
111111

112112
cont = Constraint()
113-
conT = Constraint(goal, nx, 0)
113+
conT = Constraint(goal, num_state, 0)
114114
cons = [[cont for t = 1:T-1]..., conT]
115115

116116
# ## problem

examples/car.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ using Plots
1212
T = 51
1313

1414
# ## car
15-
nx = 3
16-
nu = 2
17-
nw = 0
15+
num_state = 3
16+
num_action = 2
17+
num_parameter = 0
1818

1919
function car(x, u, w)
2020
[u[1] * cos(x[3]); u[1] * sin(x[3]); u[2]]
@@ -26,7 +26,7 @@ function midpoint_explicit(x, u, w)
2626
end
2727

2828
# ## model
29-
dyn = Dynamics(midpoint_explicit, nx, nu, nw)
29+
dyn = Dynamics(midpoint_explicit, num_state, num_action, num_parameter)
3030
model = [dyn for t = 1:T-1]
3131

3232
# ## initialization
@@ -35,19 +35,19 @@ xT = [1.0; 1.0; 0.0]
3535

3636
# ## rollout
3737
= [1.0e-2 * [1.0; 0.1] for t = 1:T-1]
38-
w = [zeros(nw) for t = 1:T]
38+
w = [zeros(num_parameter) for t = 1:T]
3939
= rollout(model, x1, ū, w)
4040

4141
# ## objective
4242
ot = (x, u, w) -> 1.0 * dot(x - xT, x - xT) + 1.0e-2 * dot(u, u)
4343
oT = (x, u, w) -> 1000.0 * dot(x - xT, x - xT)
44-
ct = Cost(ot, nx, nu, nw)
45-
cT = Cost(oT, nx, 0, nw)
44+
ct = Cost(ot, num_state, num_action, num_parameter)
45+
cT = Cost(oT, num_state, 0, num_parameter)
4646
obj = [[ct for t = 1:T-1]..., cT]
4747

4848
# ## constraints
49-
ul = -5.0 * ones(nu)
50-
uu = 5.0 * ones(nu)
49+
ul = -5.0 * ones(num_action)
50+
uu = 5.0 * ones(num_action)
5151

5252
p_obs = [0.5; 0.5]
5353
r_obs = 0.1
@@ -69,8 +69,8 @@ function terminal_con(x, u, w)
6969
]
7070
end
7171

72-
cont = Constraint(stage_con, nx, nu, indices_inequality=collect(1:5))
73-
conT = Constraint(terminal_con, nx, nu, indices_inequality=collect(3 .+ (1:1)))
72+
cont = Constraint(stage_con, num_state, num_action, indices_inequality=collect(1:5))
73+
conT = Constraint(terminal_con, num_state, num_action, indices_inequality=collect(3 .+ (1:1)))
7474
cons = [[cont for t = 1:T-1]..., conT]
7575

7676
# ## problem

examples/particle.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ using LinearAlgebra
1111
T = 11
1212

1313
# ## acrobot
14-
nx = 2
15-
nu = 1
14+
num_state = 2
15+
num_action = 1
1616

1717
function particle(x, u, w)
1818
A = [1.0 1.0; 0.0 1.0]
@@ -21,27 +21,27 @@ function particle(x, u, w)
2121
end
2222

2323
# ## model
24-
dyn = Dynamics(particle, nx, nu)
24+
dyn = Dynamics(particle, num_state, num_action)
2525
model = [dyn for t = 1:T-1]
2626

2727
# ## initialization
2828
x1 = [0.0; 0.0]
2929
xT = [1.0; 0.0]
30-
= [1.0e-1 * randn(nu) for t = 1:T-1]
30+
= [1.0e-1 * randn(num_action) for t = 1:T-1]
3131
= rollout(model, x1, ū)
3232

3333
# ## objective
3434
ot = (x, u, w) -> 0.1 * dot(x, x) + 0.1 * dot(u, u)
3535
oT = (x, u, w) -> 0.1 * dot(x, x)
36-
ct = Cost(ot, nx, nu)
37-
cT = Cost(oT, nx, 0)
36+
ct = Cost(ot, num_state, num_action)
37+
cT = Cost(oT, num_state, 0)
3838
obj = [[ct for t = 1:T-1]..., cT]
3939

4040
# ## constraints
4141
goal(x, u, w) = x - xT
4242

4343
cont = Constraint()
44-
conT = Constraint(goal, nx, 0)
44+
conT = Constraint(goal, num_state, 0)
4545
cons = [[cont for t = 1:T-1]..., conT]
4646

4747
# ## problem

src/augmented_lagrangian.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ function augmented_lagrangian(model::Model{T}, costs::Objective{T}, constraints:
1414
# horizon
1515
H = length(model) + 1
1616
# penalty
17-
constraint_penalty = [ones(c.nc) for c in constraints]
18-
constraint_penalty_matrix = [Diagonal(ones(c.nc)) for c in constraints]
17+
constraint_penalty = [ones(c.num_constraint) for c in constraints]
18+
constraint_penalty_matrix = [Diagonal(ones(c.num_constraint)) for c in constraints]
1919
# duals
20-
constraint_dual = [zeros(c.nc) for c in constraints]
20+
constraint_dual = [zeros(c.num_constraint) for c in constraints]
2121
# active set
22-
active_set = [ones(Int, c.nc) for c in constraints]
22+
active_set = [ones(Int, c.num_constraint) for c in constraints]
2323
# pre-allocated memory
24-
constraint_tmp = [zeros(c.nc) for c in constraints]
25-
constraint_jacobian_state_tmp = [zeros(c.nc, t < H ? model[t].nx : model[H-1].ny) for (t, c) in enumerate(constraints)]
26-
constraint_jacobian_action_tmp = [zeros(c.nc, t < H ? model[t].nu : 0) for (t, c) in enumerate(constraints)]
24+
constraint_tmp = [zeros(c.num_constraint) for c in constraints]
25+
constraint_jacobian_state_tmp = [zeros(c.num_constraint, t < H ? model[t].num_state : model[H-1].num_next_state) for (t, c) in enumerate(constraints)]
26+
constraint_jacobian_action_tmp = [zeros(c.num_constraint, t < H ? model[t].num_action : 0) for (t, c) in enumerate(constraints)]
2727
data = constraint_data(model, constraints)
2828
AugmentedLagrangianCosts(costs,
2929
data,
@@ -54,8 +54,8 @@ function cost(obj::AugmentedLagrangianCosts, states, actions, parameters)
5454

5555
for t = 1:H
5656
J += λ[t]' * c[t]
57-
nc = obj.constraint_data.constraints[t].nc
58-
for i = 1:nc
57+
num_constraint = obj.constraint_data.constraints[t].num_constraint
58+
for i = 1:num_constraint
5959
if a[t][i] == 1
6060
J += 0.5 * ρ[t][i] * c[t][i]^2.0
6161
end
@@ -98,8 +98,8 @@ function augmented_lagrangian_update!(obj::AugmentedLagrangianCosts;
9898
H = length(c)
9999

100100
for t = 1:H
101-
nc = cons[t].nc
102-
for i = 1:nc
101+
num_constraint = cons[t].num_constraint
102+
for i = 1:num_constraint
103103
λ[t][i] += ρ[t][i] * c[t][i]
104104
if i in cons[t].indices_inequality
105105
λ[t][i] = max(0.0, λ[t][i])

src/backward_pass.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ function backward_pass!(policy::PolicyData, problem::ProblemData;
3636
Qux = policy.action_value.hessian_action_state
3737

3838
# terminal value function
39-
P[T] .= gxx[T]
40-
p[T] .= gx[T]
39+
P[H] .= gxx[H]
40+
p[H] .= gx[H]
4141

4242
for t = H-1:-1:1
4343
# Qx[t] .= gx[t] + fx[t]' * p[t+1]

src/constraints.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ struct Constraint{T}
22
val
33
jacobian_state
44
jacobian_action
5-
nc::Int
6-
nx::Int
7-
nu::Int
8-
nw::Int
5+
num_constraint::Int
6+
num_state::Int
7+
num_action::Int
8+
num_parameter::Int
99
val_cache::Vector{T}
1010
jacobian_state_cache::Matrix{T}
1111
jacobian_action_cache::Matrix{T}
@@ -14,12 +14,12 @@ end
1414

1515
Constraints{T} = Vector{Constraint{T}} where T
1616

17-
function Constraint(f::Function, nx::Int, nu::Int;
17+
function Constraint(f::Function, num_state::Int, num_action::Int;
1818
indices_inequality::Vector{Int}=collect(1:0),
19-
nw::Int=0)
19+
num_parameter::Int=0)
2020

2121
#TODO: option to load/save methods
22-
@variables x[1:nx], u[1:nu], w[1:nw]
22+
@variables x[1:num_state], u[1:num_action], w[1:num_parameter]
2323

2424
val = f(x, u, w)
2525
jacobian_state = Symbolics.jacobian(val, x)
@@ -29,13 +29,13 @@ function Constraint(f::Function, nx::Int, nu::Int;
2929
jacobian_state_func = eval(Symbolics.build_function(jacobian_state, x, u, w)[2])
3030
jacobian_action_func = eval(Symbolics.build_function(jacobian_action, x, u, w)[2])
3131

32-
nc = length(val)
32+
num_constraint = length(val)
3333

3434
return Constraint(
3535
val_func,
3636
jacobian_state_func, jacobian_action_func,
37-
nc, nx, nu, nw,
38-
zeros(nc), zeros(nc, nx), zeros(nc, nu),
37+
num_constraint, num_state, num_action, num_parameter,
38+
zeros(num_constraint), zeros(num_constraint, num_state), zeros(num_constraint, num_action),
3939
indices_inequality)
4040
end
4141

@@ -50,7 +50,7 @@ end
5050

5151
function constraints!(violations, constraints::Constraints{T}, states, actions, parameters) where T
5252
for (t, con) in enumerate(constraints)
53-
con.nc == 0 && continue
53+
con.num_constraint == 0 && continue
5454
con.val(con.val_cache, states[t], actions[t], parameters[t])
5555
@views violations[t] .= con.val_cache
5656
fill!(con.val_cache, 0.0) # TODO: confirm this is necessary
@@ -60,7 +60,7 @@ end
6060
function jacobian!(jacobian_states, jacobian_actions, constraints::Constraints{T}, states, actions, parameters) where T
6161
H = length(constraints)
6262
for (t, con) in enumerate(constraints)
63-
con.nc == 0 && continue
63+
con.num_constraint == 0 && continue
6464
con.jacobian_state(con.jacobian_state_cache, states[t], actions[t], parameters[t])
6565
@views jacobian_states[t] .= con.jacobian_state_cache
6666
fill!(con.jacobian_state_cache, 0.0) # TODO: confirm this is necessary

src/costs.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ struct Cost{T}
1414
hessian_action_state_cache::Matrix{T}
1515
end
1616

17-
function Cost(f::Function, nx::Int, nu::Int; nw::Int=0)
17+
function Cost(f::Function, num_state::Int, num_action::Int; num_parameter::Int=0)
1818
#TODO: option to load/save methods
19-
@variables x[1:nx], u[1:nu], w[1:nw]
19+
@variables x[1:num_state], u[1:num_action], w[1:num_parameter]
2020

2121
val = f(x, u, w)
2222
gradient_state = Symbolics.gradient(val, x)
@@ -36,8 +36,8 @@ function Cost(f::Function, nx::Int, nu::Int; nw::Int=0)
3636
gradient_state_func, gradient_action_func,
3737
hessian_state_state_func, hessian_action_action_func, hessian_action_state_func,
3838
zeros(1),
39-
zeros(nx), zeros(nu),
40-
zeros(nx, nx), zeros(nu, nu), zeros(nu, nx))
39+
zeros(num_state), zeros(num_action),
40+
zeros(num_state, num_state), zeros(num_action, num_action), zeros(num_action, num_state))
4141
end
4242

4343
Objective{T} = Vector{Cost{T}} where T

src/data/constraints.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ end
1010

1111
function constraint_data(model::Model, cons::Constraints)
1212
H = length(cons)
13-
c = [zeros(cons[t].nc) for t = 1:H]
14-
cx = [zeros(cons[t].nc, t < H ? model[t].nx : model[H-1].ny) for t = 1:H]
15-
cu = [zeros(cons[t].nc, model[t].nu) for t = 1:H-1]
13+
c = [zeros(cons[t].num_constraint) for t = 1:H]
14+
cx = [zeros(cons[t].num_constraint, t < H ? model[t].num_state : model[H-1].num_next_state) for t = 1:H]
15+
cu = [zeros(cons[t].num_constraint, model[t].num_action) for t = 1:H-1]
1616
ConstraintsData(cons, c, cx, cu)
1717
end
1818

@@ -27,9 +27,9 @@ function constraint_violation(constraint_data::ConstraintsData;
2727
H = length(constraints)
2828
max_violation = 0.0
2929
for t = 1:H
30-
nc = constraints[t].nc
30+
num_constraint = constraints[t].num_constraint
3131
ineq = constraints[t].indices_inequality
32-
for i = 1:nc
32+
for i = 1:num_constraint
3333
c = constraint_data.violations[t][i]
3434
cti = (i in ineq) ? max(0.0, c) : abs(c)
3535
max_violation = max(max_violation, cti)

src/data/model.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ struct ModelData{T,X,U,W}
1010
end
1111

1212
function model_data(dynamics::Vector{Dynamics{T}}) where T
13-
jacobian_state = [zeros(d.ny, d.nx) for d in dynamics]
14-
jacobian_action = [zeros(d.ny, d.nu) for d in dynamics]
15-
jacobian_parameter = [zeros(d.ny, d.nw) for d in dynamics]
16-
ModelData(model, jacobian_state, jacobian_action, jacobian_parameter)
13+
jacobian_state = [zeros(d.num_next_state, d.num_state) for d in dynamics]
14+
jacobian_action = [zeros(d.num_next_state, d.num_action) for d in dynamics]
15+
jacobian_parameter = [zeros(d.num_next_state, d.num_parameter) for d in dynamics]
16+
ModelData(dynamics, jacobian_state, jacobian_action, jacobian_parameter)
1717
end
1818

1919
function reset!(data::ModelData)

0 commit comments

Comments
 (0)