How to Simulate Greedy Perimeter Stateless Routing MATLAB

To simulate the Greedy Perimeter Stateless Routing (GPSR) using MATLAB, it is a routing protocol generally utilized within wireless ad hoc and sensor networks. It aggregates greedy forwarding with perimeter routing. In GPSR, a packet is first sent to the node nearest to the destination that is greedy mode. If greedy sending fails such as when there’s no closer neighbor, the protocol changes to perimeter mode that sends the packet along the edges of the communication graph until it can continue greedy forwarding.

This guide shows on how we can simulate a simple GPSR protocol in MATLAB.

Steps to Simulate GPSR in MATLAB

  1. Define the Network Topology and Node Positions:
    • Signify node positions within a 2D plane using a matrix.
    • Describe the communication range thus each node can only interact directly with nodes in that range.

% Parameters

numNodes = 10;          % Number of nodes

areaSize = 100;         % Size of the area (100×100 units)

communicationRange = 30; % Communication range of each node

% Randomly place nodes within the area

nodePositions = rand(numNodes, 2) * areaSize;

% Display node positions for reference

disp(‘Node Positions:’);

disp(nodePositions);

  1. Create an Adjacency Matrix Based on Communication Range:
    • Compute the distances among nodes and then describe an adjacency matrix in which each entry shows whether two nodes are within each communication range of other.

% Initialize adjacency matrix based on communication range

adjacencyMatrix = zeros(numNodes);

for i = 1:numNodes

for j = i+1:numNodes

distance = norm(nodePositions(i, 🙂 – nodePositions(j, :));

if distance <= communicationRange

adjacencyMatrix(i, j) = 1;

adjacencyMatrix(j, i) = 1;

end

end

end

% Display adjacency matrix

disp(‘Adjacency Matrix:’);

disp(adjacencyMatrix);

  1. Define Greedy Forwarding Function:
    • Greedy forwarding transmits the packet to the neighbor nearest to the destination. If no such neighbor occurs then greedy forwarding fails, and the protocol need change to perimeter mode.

function [nextHop, success] = greedyForward(currentNode, destinationNode, nodePositions, adjacencyMatrix)

success = false;

nextHop = -1;

minDistance = norm(nodePositions(currentNode, 🙂 – nodePositions(destinationNode, :));

% Search for the closest neighbor within communication range

neighbors = find(adjacencyMatrix(currentNode, 🙂 == 1);

for i = 1:length(neighbors)

neighbor = neighbors(i);

distanceToDestination = norm(nodePositions(neighbor, 🙂 – nodePositions(destinationNode, :));

if distanceToDestination < minDistance

minDistance = distanceToDestination;

nextHop = neighbor;

success = true;

end

end

end

  1. Implement Perimeter Mode Using Right-Hand Rule:
    • Once greedy forwarding flops then GPSR changes to perimeter mode in which the packet is sent around the “perimeter” of the empty area utilizing the right-hand rule until it can continue greedy forwarding.

function [nextHop, success] = perimeterForward(currentNode, previousNode, destinationNode, nodePositions, adjacencyMatrix)

success = false;

nextHop = -1;

% Use the right-hand rule: select the next hop in counterclockwise order

neighbors = find(adjacencyMatrix(currentNode, 🙂 == 1 & (1:numNodes) ~= previousNode);

if isempty(neighbors)

return;

end

% Calculate angles to determine counterclockwise order

angles = zeros(1, length(neighbors));

for i = 1:length(neighbors)

neighbor = neighbors(i);

vector1 = nodePositions(previousNode, 🙂 – nodePositions(currentNode, :);

vector2 = nodePositions(neighbor, 🙂 – nodePositions(currentNode, :);

angles(i) = atan2(vector2(2), vector2(1)) – atan2(vector1(2), vector1(1));

if angles(i) < 0

angles(i) = angles(i) + 2 * pi;

end

end

% Find the next neighbor in counterclockwise order

[~, idx] = min(angles);

nextHop = neighbors(idx);

success = true;

end

  1. Simulate the GPSR Protocol from Source to Destination:
    • Initialize the source and destination, and attempt to forward the packet using greedy mode. If greedy mode fails then change to perimeter mode and resume until the packet attains the destination.

% Set source and destination nodes

sourceNode = 1;

destinationNode = 10;

% Initialize variables for simulation

currentNode = sourceNode;

previousNode = -1;

path = currentNode;

mode = “greedy”; % Start with greedy mode

disp([‘Starting GPSR from Node ‘, num2str(sourceNode), ‘ to Node ‘, num2str(destinationNode)]);

while currentNode ~= destinationNode

if strcmp(mode, “greedy”)

[nextHop, success] = greedyForward(currentNode, destinationNode, nodePositions, adjacencyMatrix);

if success

previousNode = currentNode;

currentNode = nextHop;

path = [path, currentNode];

else

mode = “perimeter”;

disp(‘Switching to perimeter mode.’);

end

elseif strcmp(mode, “perimeter”)

[nextHop, success] = perimeterForward(currentNode, previousNode, destinationNode, nodePositions, adjacencyMatrix);

if success

previousNode = currentNode;

currentNode = nextHop;

path = [path, currentNode];

% Check if we can resume greedy mode

if norm(nodePositions(currentNode, 🙂 – nodePositions(destinationNode, :)) < …

norm(nodePositions(previousNode, 🙂 – nodePositions(destinationNode, :))

mode = “greedy”;

disp(‘Resuming greedy mode.’);

end

else

error(‘Packet is stuck in a loop. No viable path to the destination.’);

end

end

end

disp([‘Path found: ‘, num2str(path)]);

  1. Visualize the Network Topology and Routing Path:
    • Envision the nodes, communication links, and the path taken by the packet using MATLAB’s plotting functions.

figure;

hold on;

% Plot nodes and communication links

for i = 1:numNodes

for j = i+1:numNodes

if adjacencyMatrix(i, j) == 1

plot([nodePositions(i, 1), nodePositions(j, 1)], [nodePositions(i, 2), nodePositions(j, 2)], ‘k–‘);

end

end

plot(nodePositions(i, 1), nodePositions(i, 2), ‘bo’); % Nodes

text(nodePositions(i, 1), nodePositions(i, 2), num2str(i), ‘VerticalAlignment’, ‘bottom’, ‘HorizontalAlignment’, ‘center’);

end

% Highlight the source and destination nodes

plot(nodePositions(sourceNode, 1), nodePositions(sourceNode, 2), ‘go’, ‘MarkerSize’, 10, ‘MarkerFaceColor’, ‘g’);

plot(nodePositions(destinationNode, 1), nodePositions(destinationNode, 2), ‘ro’, ‘MarkerSize’, 10, ‘MarkerFaceColor’, ‘r’);

% Plot the path

for i = 1:length(path)-1

plot([nodePositions(path(i), 1), nodePositions(path(i+1), 1)], [nodePositions(path(i), 2), nodePositions(path(i+1), 2)], ‘b-‘, ‘LineWidth’, 2);

end

title(‘GPSR Routing Path’);

hold off;

Explanation of Key Components

  • Greedy Forwarding: In every single step, the packet is sent to the node nearest to the destination in the communication range.
  • Perimeter Mode: If no nearer neighbor occurs then perimeter mode is utilized. The packet tracks the perimeter of the network area with the help of the right-hand rule to traverse around voids.
  • Mode Switching: Once perimeter mode discovers a node nearer to the destination, the protocol continues greedy forwarding.
  • Visualization: The network topology and routing path are envisioned to know the operation of GPSR.

Possible Extensions

  • Dynamic Topology Changes: Replicate the node mobility by modernizing node positions and monitor how GPSR manages it.
  • Obstacle Handling: Set obstacles within the area to make “voids,” which challenging the protocol to determine alternate routes.
  • Performance Metrics: Assess performance parameters such as hop count, path length, and time spent in each mode.

We kindly invite you to send us your information via email, and in return, you will receive exceptional guidance. For personalized support with your Greedy Perimeter Stateless Routing Project, please connect with phdprime.com. Our researchers are equipped to offer you high-quality project ideas and topics related to Greedy Perimeter Stateless Routing, and we are here to assist you in evaluating network performance for your project. Furthermore, we provide assistance with MATLAB simulation results, and our team specializes in the GPSR protocol within MATLAB.

Opening Time

9:00am

Lunch Time

12:30pm

Break Time

4:00pm

Closing Time

6:30pm

  • award1
  • award2