-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
59 lines (49 loc) · 1.42 KB
/
main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
% Written by Spencer Kraisler
%
% This is an implementation of the algorithm from the paper Distributed
% Consensus on Manifolds using the Riemannian Center of Mass.
%
% Aug. 24, 2023
%
clear; close all; clc
%% Init constraints
N = 10; % num agents
tol = 1e-6; % tolerance for sim
max_steps = 200; % max steps for sim
n = 5;
M = rotationsfactory(n); % SO(n) manifold object
M.inner = @(x, xi, eta) trace(xi'*eta)/2;
M.norm = @(x, xi) sqrt(M.inner(x, xi, xi));
% Generate random connected undirected graph
p = .4; % probability of 2 nodes being adjacent
G = RandomConnectedGraph(N, p);
% Generate init positions of agents
sigma = pi/2; % st dev of gaussian distr
center = M.rand(); % generate init agent positions around center
x = repmat({}, 1, N);
for i = 1:N
v_i = sigma*rand*M.randvec(center);
x_i = M.exp(center, v_i);
x{i} = x_i;
end
%% run sim
consensus_error_hist = [];
for t = 1:max_steps
% compute consensus error
consensus_error = ConsensusError(x, M, G);
if consensus_error < tol % halt sim if tolerance achieved
break;
end
consensus_error_hist = [consensus_error_hist consensus_error];
i = mod(t, N) + 1;
% compute RCM of nbrhood of agent i
N_i = neighbors(G, i)';
x{i} = RiemannianCenterOfMass(x(N_i), M);
end
%% plot error over time
hold on
grid on
set(gca, 'YScale', 'log'); % log y-scale
xlabel('time step')
ylabel('consensus error')
plot(consensus_error_hist)