|
| 1 | +using Test |
| 2 | +using LinearAlgebra |
| 3 | +using LinearMPC |
| 4 | +using RobustAndOptimalControl |
| 5 | + |
| 6 | +# Test basic LQGProblem conversion (from docstring example) |
| 7 | +Ts = 0.1 |
| 8 | +sys = ss([1 Ts; 0 1], [0; Ts], [1 0], 0, Ts) |
| 9 | + |
| 10 | +# Create LQGProblem: Q1=state cost, Q2=input cost, R1=process noise, R2=measurement noise |
| 11 | +lqg = LQGProblem(sys, I(2), I(1), I(2), 0.01*I(1)) |
| 12 | + |
| 13 | +# Convert to LinearMPC with constraints |
| 14 | +mpc = LinearMPC.MPC(lqg; N=20, umin=[-0.3], umax=[0.3]) |
| 15 | + |
| 16 | +# Check basic properties |
| 17 | +@test mpc.model.Ts == Ts |
| 18 | +@test mpc.model.F == sys.A |
| 19 | +@test mpc.model.G == sys.B |
| 20 | +@test mpc.settings.Np == 20 |
| 21 | + |
| 22 | +# Simulate and verify it works |
| 23 | +sim = LinearMPC.Simulation(mpc; N=100, r=[1.0, 0]) |
| 24 | + |
| 25 | +# Check that simulation ran |
| 26 | +@test size(sim.xs, 2) == 101 # 100 steps + initial state |
| 27 | +@test size(sim.us, 2) == 100 |
| 28 | + |
| 29 | +# Check constraint satisfaction |
| 30 | +@test all(sim.us .>= -0.3 - 1e-6) |
| 31 | +@test all(sim.us .<= 0.3 + 1e-6) |
| 32 | + |
| 33 | +# Check that output converges toward reference |
| 34 | +final_output = (sys.C * sim.xs[:, end])[1] |
| 35 | +@test abs(final_output - 1.0) < 0.1 # Should be close to reference |
| 36 | + |
| 37 | +@testset "LQGProblem with Q3 (input rate penalty)" begin |
| 38 | + # Test with input rate cost |
| 39 | + Q3 = 0.5 * I(1) |
| 40 | + mpc_q3 = LinearMPC.MPC(lqg; N=15, Q3, umin=[-0.5], umax=[0.5]) |
| 41 | + |
| 42 | + sim_q3 = LinearMPC.Simulation(mpc_q3; N=50, r=[0.5, 0]) |
| 43 | + |
| 44 | + # With Q3, control should be smoother (smaller rate of change) |
| 45 | + du = diff(sim_q3.us, dims=2) |
| 46 | + max_rate = maximum(abs.(du)) |
| 47 | + @test max_rate < 0.3 # Rate should be limited due to Q3 penalty |
| 48 | +end |
| 49 | + |
| 50 | +@testset "LQGProblem with terminal cost Qf" begin |
| 51 | + # Test with terminal cost |
| 52 | + Qf = 10.0 * I(2) # Higher terminal cost |
| 53 | + mpc_qf = LinearMPC.MPC(lqg; N=10, Qf, umin=[-1.0], umax=[1.0]) |
| 54 | + |
| 55 | + sim_qf = LinearMPC.Simulation(mpc_qf; N=30, r=[0.3, 0]) |
| 56 | + |
| 57 | + # Should still work and converge |
| 58 | + final_output = (sys.C * sim_qf.xs[:, end])[1] |
| 59 | + @test abs(final_output - 0.3) < 0.1 |
| 60 | +end |
| 61 | + |
| 62 | +@testset "LQGProblem MIMO system" begin |
| 63 | + # Test with 2-input 2-output system |
| 64 | + A = [0.9 0.1; 0.05 0.95] |
| 65 | + B = [1.0 0.0; 0.0 1.0] |
| 66 | + C = [1.0 0.0; 0.0 1.0] |
| 67 | + D = zeros(2, 2) |
| 68 | + sys_mimo = ss(A, B, C, D, Ts) |
| 69 | + |
| 70 | + lqg_mimo = LQGProblem(sys_mimo, I(2), I(2), I(2), 0.01*I(2)) |
| 71 | + mpc_mimo = LinearMPC.MPC(lqg_mimo; N=15, |
| 72 | + umin=[-0.5, -0.5], umax=[0.5, 0.5]) |
| 73 | + |
| 74 | + sim_mimo = LinearMPC.Simulation(mpc_mimo; N=50, r=[0.3, -0.2]) |
| 75 | + |
| 76 | + # Check dimensions |
| 77 | + @test size(sim_mimo.us, 1) == 2 |
| 78 | + @test size(sim_mimo.xs, 1) == 2 |
| 79 | + |
| 80 | + # Check constraints on both inputs |
| 81 | + @test all(sim_mimo.us[1, :] .>= -0.5 - 1e-6) |
| 82 | + @test all(sim_mimo.us[1, :] .<= 0.5 + 1e-6) |
| 83 | + @test all(sim_mimo.us[2, :] .>= -0.5 - 1e-6) |
| 84 | + @test all(sim_mimo.us[2, :] .<= 0.5 + 1e-6) |
| 85 | +end |
| 86 | + |
| 87 | +@testset "LQGProblem continuous-time error" begin |
| 88 | + # Test that continuous-time systems throw an error |
| 89 | + sys_cont = ss([0 1; -1 -1], [0; 1], [1 0], 0) # Continuous-time |
| 90 | + lqg_cont = LQGProblem(sys_cont, I(2), I(1), I(2), 0.01*I(1)) |
| 91 | + |
| 92 | + @test_throws ErrorException LinearMPC.MPC(lqg_cont; N=10) |
| 93 | +end |
0 commit comments