|
| 1 | +using AlgebraicOptimization |
| 2 | +using AlgebraicControl |
| 3 | +using Convex |
| 4 | +using LinearAlgebra |
| 5 | +using ProximalAlgorithms |
| 6 | + |
| 7 | +# Set up each agent's dynamics: x' = Ax + Bu |
| 8 | +dt = 0.1 # Discretization step size |
| 9 | +A = [1 dt 0 0; 0 1 0 0; 0 0 1 dt; 0 0 0 1] |
| 10 | +B = [0 0; dt 0; 0 0; 0 dt] |
| 11 | +C = [1.0 0 0 0; 0 0 1.0 0] # Output agent's position |
| 12 | +Q = Matrix{Float64}(I(4)) |
| 13 | +Q[1, 1] = 0.0 |
| 14 | +Q[3, 3] = 0.0 # No objective on positions, just make velocities go to 0 |
| 15 | +R = Matrix{Float64}(I(2)) |
| 16 | + |
| 17 | +sys = LinearSystem(A, B) |
| 18 | + |
| 19 | +# Stage cost function: minimize control effort and deviation from origin |
| 20 | +stage_cost(u, x) = quadform(x, Q) + quadform(u, R) |
| 21 | +stage_constraints = Function[] |
| 22 | +# Create a multi-stage program for a horizon of N steps |
| 23 | +N = 10 |
| 24 | +mpc_program = multi_stage_program(stage_cost, stage_constraints, sys, N) |
| 25 | + |
| 26 | +# Create a proxable MPC program |
| 27 | +agent1_obj, agent2_obj, agent3_obj = ProxableMPCProgram(mpc_program), ProxableMPCProgram(mpc_program), ProxableMPCProgram(mpc_program) |
| 28 | + |
| 29 | +set_x0!(agent1_obj, rand(4)) |
| 30 | +set_x0!(agent2_obj, rand(4)) |
| 31 | +set_x0!(agent3_obj, rand(4)) |
| 32 | + |
| 33 | +s = @cellular_sheaf C begin |
| 34 | + x::Stalk{4}, y::Stalk{4}, z::Stalk{4} |
| 35 | + |
| 36 | + C(x) == C(y) |
| 37 | + C(x) == C(z) |
| 38 | + C(y) == C(z) |
| 39 | +end |
| 40 | + |
| 41 | + |
| 42 | +p = HomologicalProgram([agent1_obj, agent2_obj, agent3_obj], s) |
| 43 | + |
| 44 | +solve(p, ProximalAlgorithms.DouglasRachford(maxit=10)) |
| 45 | + |
| 46 | +sim_length = 100 |
| 47 | + |
| 48 | +for i in 1:sim_length |
| 49 | + solve(p, ProximalAlgorithms.DouglasRachford(maxit=10)) |
| 50 | + x1_curr = evaluate(agent1_obj.input_var) |
| 51 | + x2_curr = evaluate(agent2_obj.input_var) |
| 52 | + x3_curr = evaluate(agent3_obj.input_var) |
| 53 | + |
| 54 | + u1_curr = evaluate(agent1_obj.control_vars[1]) |
| 55 | + u2_curr = evaluate(agent2_obj.control_vars[1]) |
| 56 | + u3_curr = evaluate(agent3_obj.control_vars[1]) |
| 57 | + |
| 58 | + x1_next = sys(x1_curr, u1_curr) |
| 59 | + x2_next = sys(x2_curr, u2_curr) |
| 60 | + x3_next = sys(x3_curr, u3_curr) |
| 61 | + |
| 62 | + set_x0!(agent1_obj, x1_next) |
| 63 | + set_x0!(agent2_obj, x2_next) |
| 64 | + set_x0!(agent3_obj, x3_next) |
| 65 | +end |
| 66 | + |
| 67 | +x1_f = evaluate(agent1_obj.input_var) |
| 68 | +x2_f = evaluate(agent2_obj.input_var) |
| 69 | +x3_f = evaluate(agent3_obj.input_var) |
| 70 | + |
| 71 | + |
| 72 | + |
0 commit comments