Skip to content

Commit f004279

Browse files
committed
paper stuff
1 parent 98227aa commit f004279

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

examples/ConstrainedLQR.jl

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,26 @@ Q = 5*[1.0 0; 0 1.0]
99
R = 3.0
1010
x₀ = [3, 1]
1111
N = 10
12+
dim_x = 2
13+
dim_u = 1
1214

1315
cost(uₖ,xₖ) = quadform(xₖ,Q) + R*square(uₖ)
14-
set_constraints(uₖ,xₖ) = [
16+
dynamics(uₖ,xₖ) = A*xₖ + B*uₖ
17+
constraints(uₖ,xₖ) = [
1518
uₖ <= 1, uₖ >= -1,
1619
xₖ[1] <= 3, xₖ[1] >= -3,
1720
xₖ[2] <= 2, xₖ[2] >= -2
1821
]
19-
dynamics(uₖ,xₖ,xₖplus1) = xₖplus1 == A*xₖ + B*uₖ
20-
21-
one_step = one_step_bifunction(cost, set_constraints, A, B)
2222

23+
one_step = one_step_bifunction(dim_x,dim_u,cost, constraints, dynamics)
2324
MPC_bifunc = MPC_bifunction(one_step, N)
2425

25-
us = [Variable(1) for i in 1:N]
26-
#x1 = Variable(2)
27-
#x1 = repeat([1],2)
26+
us = [Variable(1) for i in 1:N-1]
2827
x_N = Variable(2)
29-
#x_N = zeros(2)
3028

3129
MPC_prob = to_cvx(MPC_bifunc, us, x₀, x_N)
3230

33-
#fix!(x1, repeat([1], 2))
3431
solve!(MPC_prob, SCS.Optimizer)
35-
o = MPC_prob.optval
3632

3733
function simulate(A, B, x₀, us, N)
3834
x = x₀
@@ -41,3 +37,28 @@ function simulate(A, B, x₀, us, N)
4137
end
4238
return x
4339
end
40+
41+
### Compare to hand written implementation
42+
xs = Variable(2, N)
43+
us = Variable(1, N-1)
44+
45+
constraints = Constraint[
46+
xs[:, i+1] == A*xs[:,i] + B*us[:,i] for i in 1:N-1
47+
]
48+
49+
for i in 1:N-1
50+
push!(constraints, xs[:,i][1] <= 3)
51+
push!(constraints, xs[:,i][1] >= -3)
52+
push!(constraints, xs[:,i][2] <= 2)
53+
push!(constraints, xs[:,i][2] >= -2)
54+
push!(constraints, us[:,i] <= 1)
55+
push!(constraints, us[:,i] >= -1)
56+
end
57+
58+
push!(constraints, xs[:,1] == x₀)
59+
60+
objective = sum([quadform(xs[:,i], Q) + R*square(us[:,i]) for i in 1:N-1])
61+
62+
prob = minimize(objective, constraints)
63+
64+
solve!(prob, SCS.Optimizer)

src/CMPC.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,22 @@ function one_step_bifunction(f::Function,g::Function,A,B)
2828
return OpenParaConvexBifunction(n,n,[m],impl)
2929
end
3030

31+
function one_step_bifunction(x_dim, u_dim, f::Function,g::Function,h::Function)
32+
impl = (u1,x1,x2) -> ConvexBifunction(
33+
f(u1[1],x1),
34+
vcat(g(u1[1],x1), x2 == h(u1[1],x1))
35+
)
36+
return OpenParaConvexBifunction(x_dim,x_dim,[u_dim],impl)
37+
end
38+
3139
function MPC_bifunction(one_step::OpenParaConvexBifunction, N::Int)
3240
res = one_step
33-
for i in 1:N-1
41+
for i in 1:N-2
3442
res = compose(ParaConv(), res, one_step)
3543
end
3644
return res
3745
end
3846

39-
end
47+
48+
end
49+

0 commit comments

Comments
 (0)