# Hybrid Model of Platoon Formation

In course of a examination project for EECI course “Analysis and Design of Hybrid Control Systems”, this project has been conducted. The formation of platoons into a hybrid system is modelled as a hybrid system. You can find the full project report here:

Hybrid Model of Platoon Formation

Below, you can find the simulation code for the multi agent case.

function [value discrete] = C(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file
%
% Description: Flow set
% Return 0 if outside of C, and 1 if inside C
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
K = round(length(x)/3);

d = x(1:K);
u = x(K+1:2*K);
p = x(2*K+1:3*K);

value = 0;
for i = 1:K-1
if d(i+1) = d(i)
if p(i+1) ~= p(i)
inside = 1;
end
end
end

end

function inside = D(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file
%
% Description: Jump set
% Return 0 if outside of D, and 1 if inside D
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
K = round(length(x)/3);

d = x(1:K);
u = x(K+1:2*K);
p = x(2*K+1:3*K);

inside = 0;

for i = 1:K-1
if d(i+1) >= d(i)
if p(i+1) ~= p(i)
inside = 1;
end
end
end
end

function xdot = f(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file
%
% Description: Flow map
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% state
K = round(length(x)/3);

d = x(1:K);
u = x(K+1:2*K);
p = x(2*K+1:3*K);

% differential equations
xdot = [u;zeros(2*K,1)];
end

function xplus = g(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file
%
% Description: Jump map
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% state
K = round(length(x)/3);

d = x(1:K);
u = x(K+1:2*K);
p = x(2*K+1:3*K);

u_min = 10;
u_max = 20;

% decide whether u_min or u_max will be used in the set valued case
r = rand(1);
if r &gt; 0.5
bar_u = u_max;
else
bar_u = u_min;
end

% search for the platoons that caused the jump
for i = 1:K-1
if d(i+1) &gt;= d(i)
if p(i+1) ~= p(i)
break;
end
end
end
% find all agents in these platoons
N = find(p == p(i+1) | p == p(i));
p(N) = p(i);
first_agent = sum(N == 1);
last_agent = sum(N == K);
if first_agent &amp;&amp; last_agent || ~first_agent &amp;&amp; ~last_agent % we're in the middel or one platoon
u(N) = bar_u;
elseif first_agent &amp;&amp; ~last_agent % that's the first platoon
u(N) = u_min;
elseif ~first_agent &amp;&amp; last_agent % that's the las tplatoon
u(N) = u_max;
end
% the adjustment in order to end up in the flow set after the jump
d(N) = d(N(1));
xplus = [d;u;p];
end

clear all

% function run

u_min = 10;
u_max = 20;

K = 9;

% initial conditions
d = cumsum(rand(1,K))*5;
d = d(K:-1:1);
d = d.';
u(1) = u_min;
des = rand(1,K-2);
u(2:K-1) = u_min*(des &gt; 0.5) + u_max*(des  priority for jumps
% rule = 2 -&gt; priority for flows
rule = 1;

options = odeset('RelTol',1e-6,'MaxStep',.1);

maxStepCoefficient = .1;  % set the maximum step length. At each run of the
% integrator the option 'MaxStep' is set to
% (time length of last integration)*maxStepCoefficient.
%  Default value = 0.1

% simulate
[t x j] = HyEQsolver( @f,@g,@C,@D,x0,TSPAN,JSPAN,rule,options,maxStepCoefficient);

% plot solution
figure;
subplot(2,1,1)
plot(t,x(:,1:K));
grid on;
% legend('d'interpreter','tex');
ylabel('\$d_i\$','interpreter','latex');
xlabel('time');
subplot(2,1,2);
plot(t,x(:,2*K+1:end));
xlabel('time');
ylabel('\$p_i\$','interpreter','latex');
ylim([0,K+1]);