Skip to content

Commit c442b2d

Browse files
committed
Working tests
1 parent f004279 commit c442b2d

File tree

8 files changed

+134
-33
lines changed

8 files changed

+134
-33
lines changed

Project.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ version = "0.1.0"
77
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
88
Convex = "f65535da-76fb-5f13-bab9-19810c17039a"
99
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
10-
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
11-
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
1210
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
13-
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
1411
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
1512
SCS = "c946c3f1-0d1f-5ce8-9dea-7daa1f7e2d13"
1613
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

examples/ConstrainedLQR.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using AlgebraicControl.CMPC
22
#using AlgebraicControl.ParaConvCat
33
using SCS
4-
#using Convex
4+
using Convex
55

66
A = [0 1; .01 0]
77
B = [0; 1]

src/CMPC.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ module CMPC
22

33
export one_step_bifunction, MPC_bifunction
44

5-
using JuMP
6-
using Ipopt
75
using LinearAlgebra
86
using Convex
97
using ..ParaConvCat
108
using ..Categories
119

12-
13-
1410
function one_step_bifunction(f::Function,g::Function,A,B)
1511
n = size(A)[1]
1612
n_B = size(B)[1]

src/ParaConvCat.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ function to_problem(F::ConvexBifunction)::Problem
1919
end
2020
end
2121

22-
function to_cvx(F::OpenParaConvexBifunction, args...)
23-
bf = F(args...)
24-
return to_problem(bf)
25-
end
2622

2723
struct OpenParaConvexBifunction
2824
dom::Int
@@ -54,22 +50,32 @@ end
5450
(F::OpenParaConvexBifunction)(ps::Vector{Variable}, x::Variable, y::Variable) =
5551
F.impl(ps, x, y)
5652

53+
54+
function to_cvx(F::OpenParaConvexBifunction, args...)
55+
bf = F(args...)
56+
return to_problem(bf)
57+
end
58+
5759
struct ParaConv <: Category{Int, OpenParaConvexBifunction} end
5860

5961
dom(::ParaConv, F::OpenParaConvexBifunction) = F.dom
6062
codom(::ParaConv, F::OpenParaConvexBifunction) = F.codom
6163
id(::ParaConv, X::Int) = begin
6264
impl = (_, x1, x2) ->
6365
ConvexBifunction(Constant(0), [x1 == x2])
64-
return OpenParaConvexBifunction(X, X, 0, impl)
66+
return OpenParaConvexBifunction(X, X, Variable[], impl)
6567
end
6668
compose(::ParaConv, F::OpenParaConvexBifunction, G::OpenParaConvexBifunction) = begin
6769
@assert codom(ParaConv(), F) == dom(ParaConv(), G)
6870
y = Variable(codom(ParaConv(), F))
6971
F_nparams = length(F.para)
7072
G_nparams = length(G.para)
7173
impl = (ps, x, z) -> begin
72-
FCB = F(ps[1:F_nparams], x, y)
74+
if F_nparams > 0
75+
FCB = F(ps[1:F_nparams], x, y)
76+
else
77+
FCB = F(Variable[], x, y)
78+
end
7379
GCB = G(ps[F_nparams+1:end], y, z)
7480
return ConvexBifunction(FCB.obj + GCB.obj, vcat(FCB.cons, GCB.cons))
7581
end

src/ParaDynam.jl

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/CMPC.jl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Test
2+
using AlgebraicControl.CMPC
3+
using AlgebraicControl.ParaConvCat
4+
using Convex
5+
using SCS
6+
7+
A = [0 1; .01 0]
8+
B = [0; 1]
9+
Q = 5*[1.0 0; 0 1.0]
10+
R = 3.0
11+
x₀ = [3, 1]
12+
N = 10
13+
dim_x = 2
14+
dim_u = 1
15+
ϵ = 0.0001
16+
17+
cost(uₖ,xₖ) = quadform(xₖ,Q) + R*square(uₖ)
18+
dynamics(uₖ,xₖ) = A*xₖ + B*uₖ
19+
set_constraints(uₖ,xₖ) = [
20+
uₖ <= 1, uₖ >= -1,
21+
xₖ[1] <= 3, xₖ[1] >= -3,
22+
xₖ[2] <= 2, xₖ[2] >= -2
23+
]
24+
25+
one_step = one_step_bifunction(dim_x,dim_u,cost, set_constraints, dynamics)
26+
MPC_bifunc = MPC_bifunction(one_step, N)
27+
28+
us = [Variable(1) for i in 1:N-1]
29+
x_N = Variable(2)
30+
31+
MPC_prob = to_cvx(MPC_bifunc, us, x₀, x_N)
32+
33+
solve!(MPC_prob, SCS.Optimizer; silent_solver=true)
34+
35+
us_sol = evaluate.(us)
36+
37+
function simulate(A, B, x₀, us, N)
38+
x = x₀
39+
for i in 1:N
40+
x = A*x + B*us[i]
41+
end
42+
return x
43+
end
44+
45+
# Test that simulate comes close to x_N
46+
res = simulate(A, B, x₀, us_sol, N-1)
47+
@test norm(res - evaluate(x_N)) < ϵ
48+
49+
### Compare to hand written implementation
50+
xs = Variable(2, N)
51+
us = Variable(1, N-1)
52+
53+
constraints = Constraint[
54+
xs[:, i+1] == A*xs[:,i] + B*us[:,i] for i in 1:N-1
55+
]
56+
57+
for i in 1:N-1
58+
push!(constraints, xs[:,i][1] <= 3)
59+
push!(constraints, xs[:,i][1] >= -3)
60+
push!(constraints, xs[:,i][2] <= 2)
61+
push!(constraints, xs[:,i][2] >= -2)
62+
push!(constraints, us[:,i] <= 1)
63+
push!(constraints, us[:,i] >= -1)
64+
end
65+
66+
push!(constraints, xs[:,1] == x₀)
67+
68+
objective = sum([quadform(xs[:,i], Q) + R*square(us[:,i]) for i in 1:N-1])
69+
70+
prob = minimize(objective, constraints)
71+
72+
solve!(prob, SCS.Optimizer; silent_solver=true)
73+
74+
true_us_sol = evaluate.(us)
75+
76+
# Test that hand written implementation gets same solution
77+
# as automated implementation.
78+
@test norm(true_us_sol - us_sol) < ϵ

test/ParaConvCat.jl

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using AlgebraicControl.ParaConvCat
22
using Test
33
using Convex
4-
using Ipopt
54
using LinearAlgebra
65
using AlgebraicControl.Categories
76
using SCS
@@ -31,9 +30,33 @@ u1 = Variable(8)
3130
x1 = Variable(10)
3231
x2 = Variable(10)
3332
FCB = F([u1], x1, x2)
33+
prob = to_problem(FCB)
34+
solve!(prob, SCS.Optimizer; silent_solver=true)
35+
o1 = prob.optval
36+
37+
# Test unitality
38+
F_id = compose(ParaConv(), F, id(ParaConv(), 10))
39+
u1 = Variable(8)
40+
x1 = Variable(10)
41+
x2 = Variable(10)
42+
FCB = F_id([u1], x1, x2)
43+
prob = to_problem(FCB)
44+
solve!(prob, SCS.Optimizer; silent_solver=true)
45+
o2 = prob.optval
46+
@test o1 o2
3447

48+
id_F = compose(ParaConv(), id(ParaConv(), 10), F)
49+
u1 = Variable(8)
50+
x1 = Variable(10)
51+
x2 = Variable(10)
52+
FCB = id_F([u1], x1, x2)
3553
prob = to_problem(FCB)
36-
solve!(prob, SCS.Optimizer)
54+
solve!(prob, SCS.Optimizer; silent_solver=true)
55+
o2 = prob.optval
56+
@test o1 o2
57+
58+
59+
3760

3861
FF = compose(ParaConv(), F, F)
3962

@@ -48,7 +71,7 @@ prob = to_problem(FFCB)
4871
fix!(x1, repeat([5],10))
4972
fix!(x3, zeros(10))
5073

51-
solve!(prob, SCS.Optimizer)
74+
solve!(prob, SCS.Optimizer; silent_solver=true)
5275
o1 = prob.optval
5376

5477
x1_val = evaluate(x1)
@@ -70,7 +93,7 @@ true_prob = minimize(
7093
fix!(tx1, repeat([5],10))
7194
fix!(tx3, zeros(10))
7295

73-
solve!(true_prob, SCS.Optimizer)
96+
solve!(true_prob, SCS.Optimizer, silent_solver=true)
7497
o2 = true_prob.optval
7598

7699
tx1_val = evaluate(tx1)
@@ -80,6 +103,12 @@ tu2_val = evaluate(tu2)
80103
tx3_val = A*tx2_val + B*tu2_val
81104

82105
@test o1 == o2
106+
@test x1_val == tx1_val
107+
@test x2_val == tx2_val
108+
@test x3_val == tx3_val
109+
@test u1_val == tu1_val
110+
@test u2_val == tu2_val
111+
83112

84113

85114

test/runtests.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
using Test
22

3-
@testset "Optimization Algorithms" begin
4-
include("Optimizers.jl")
3+
@testset "ParaConv" begin
4+
include("ParaConvCat.jl")
5+
end
6+
7+
@testset "Convex Model Predictive Control" begin
8+
include("CMPC.jl")
59
end
610

11+
#=@testset "Optimization Algorithms" begin
12+
include("Optimizers.jl")
13+
end=#
14+
715
#=@testset "Linear Quadratic Regulators" begin
816
include("LQR.jl")
917
end=#

0 commit comments

Comments
 (0)