You are on page 1of 7

Simular en Matlab los modelos: Random Walk Model, Random WayPoint Model

y Gauss Markov Model. Considerar un nodo desplazandose en un espacio en 2D


(mnimo) y un tiempo definido por ustedes en caso necesario.

Random Walk Model

A continuacin se presenta el cdigo empleado para simular el modelo de movimiento
aleatorio Random Walk.

function random_walk_2d_plot ( step_num )
walk_num = 0;
step_num = 100;
clf
hold on
plot ( 0.0, 0.0, 'ko', 'MarkerSize', 10 )
grid on
while ( 1 )
walk_num = walk_num + 1;
x = zeros(step_num+1,1);
y = zeros(step_num+1,1);
for step = 2 : step_num + 1
destination = [ x(step-1) + 1.0, y(step-1); ...
x(step-1) - 1.0, y(step-1); ...
x(step-1), y(step-1) + 1.0; ...
x(step-1), y(step-1) - 1.0 ];
k = ceil ( 4.0 * rand );
x(step) = destination(k,1);
y(step) = destination(k,2);
plot ( [ x(step-1), x(step) ], [ y(step-1), y(step) ], 'r-',
'LineWidth', 1 );
end
plot ( x(step_num+1), y(step_num+1), 'r*', 'MarkerSize', 10 )
title_string = sprintf ( '2D Random Walk - %d walks, %d steps',
walk_num, step_num );
title ( title_string );
xlabel ( 'X' )
ylabel ( 'Y' );
c = input ( 'RETURN for another, or Q to quit.', 's' );
if ( c == 'q' | c == 'Q' )
break
end
for step = 2 : step_num
plot ( [ x(step-1), x(step) ], [ y(step-1), y(step) ], 'b-',
'LineWidth', 1 );
end
plot ( x(step_num+1), y(step_num+1), 'k*', 'MarkerSize', 10 )
end
hold off
return
end



Random WayPoint Model

A continuacin se presenta el cdigo empleado para simular el modelo de movimiento
aleatorio Random WayPoint.

clear all;clc;close all;
s_input = struct('V_POSITION_X_INTERVAL',[10 30],...%(m)
'V_POSITION_Y_INTERVAL',[10 30],...%(m)
'V_SPEED_INTERVAL',[0.2 2.2],...%(m/s)
'V_PAUSE_INTERVAL',[0 1],...%pause time (s)
'V_WALK_INTERVAL',[2.00 6.00],...%walk time (s)
'V_DIRECTION_INTERVAL',[-180 180],...%(degrees)
'SIMULATION_TIME',100,...%(s)
'NB_NODES',20);
s_mobility = Generate_Mobility(s_input);
timeStep = 0.1;%(s)
test_Animate(s_mobility,s_input,timeStep);





Gauss Markov Model

A continuacin se presenta el cdigo empleado para simular el modelo de movimiento
aleatorio Random WayPoint.

X=150;
Y=150;
n=1;
a = 0.75;
interval = 1;
bordaX = X*0.15;
bordaY = Y*0.15;
borda = bordaY;
if ~(a>=0 && a<=1)
error ('A variavel a deve ser entre 0 e 1');
end
MAXSPEED = 5;
time=250;
quadrantes = 2*pi;
folder =
['movimento_gauss_markov_resulados_n_',int2str(n),'X_',int2str(X),'Y_',i
nt2str(Y),'MaxSpeed_',int2str(MAXSPEED),'a_',int2str(MAXSPEED),'/'];
mkdir(folder)
qtd_data = 6;
nodes_ = zeros(n,qtd_data);
nodes = zeros(n,qtd_data);
hold on;
filename = [folder,'inicio'];
save (filename,'n','X','Y','-ASCII')
for i=1:n
x=X/2;
y=Y/2;
direction=quadrantes*rand;
speed = rand*MAXSPEED;
s_ = 3;
d_ = pi/2;
nodes_(i,:) = [x y speed direction s_ d_];
nodes(i,:) = [x y speed direction s_ d_];
plot(x,y,'og');
end
filename = [folder,'time',int2str(1-1)];
save (filename,'nodes','-ASCII');
limX = 0:1:X;
limY = 0:1:Y;
text(-0.5,-0.5,'(0,0)');
text(-0.5,Y+0.5,['(0,',int2str(Y),')']);
text(X,-0.5,['(',int2str(X),',0)']);
text(X,Y+0.5,['(',int2str(X),',',int2str(Y),')']);
plot(limX,0,'-k','LineWidth',2);
plot(0,limY,'-k','LineWidth',2);
plot(X,limY,'-k','LineWidth',2);
plot(limX,Y,'-k','LineWidth',2);
plot(limX,0+borda,':c','LineWidth',2);
plot(0+borda,limY,':c','LineWidth',2);
plot(X-borda,limY,':c','LineWidth',2);
plot(limX,Y-borda,':c','LineWidth',2);
axis([-5 X+5 -5 Y+5]);
nodes_anterior = nodes;
for t=2:time
for i=1:n
node = nodes_anterior(i,:);
x = node(1);
y = node(2);
if x > 0 && x < borda && y > 0 && y < borda
d_ = pi/4;
elseif x > borda && x < X - borda && x > 0 && y < borda
d_ = pi/2;
elseif x > X - borda && x < X && y > 0 && y < borda
d_ = 3*pi/4;
elseif x > X - borda && x < X && y > borda && y < Y - borda
d_ = pi;
elseif x > X - borda && x < X && y > Y - borda && y < Y
d_ = 5*pi/4;
elseif x > borda && x < X - borda && y > Y - borda && y < Y
d_ = 3*pi/2;
elseif x > 0 && x < borda && y > Y - borda && y < Y
d_ = 7*pi/4;
elseif x > 0 && x < borda && y > borda && y< Y - borda
d_ = 0;
else
d_ = node(6);
end
nodes_anterior(i,:) = [node(1:5) d_ ] ;
oldnode = nodes_anterior(i,:);
oldspeed = oldnode(3) ;
olddirection = oldnode(4);
s_ = oldnode(5);
d_ = oldnode(6);
newspeed = a*oldspeed + (1-a)*s_ + sqrt(1-a^2)*normrnd(0,1);
newdirection =a*olddirection + (1-a)*d_ + sqrt(1-
a^2)*normrnd(0,1);
newnode = [oldnode(1:2) newspeed newdirection oldnode(5:6)];
nodes(i,:) = newnode;
oldnode = nodes_anterior(i,:);
node = nodes(i,:);
x_old = oldnode(1);
y_old = oldnode(2);
s_old = oldnode(3);
d_old = oldnode(4);
x_new = x_old + s_old*cos(d_old);
y_new = y_old + s_old*sin(d_old);
newnode = [x_new y_new node(3:6)];
nodes(i,:) = newnode;
plot([x_old x_new],[y_old y_new],'.k:');
end
nodes_anterior = nodes;
pause(0.1);
end
plot(x_new,y_new,'og');

You might also like