How to Simulate GPSR Protocol Projects Using MATLAB

To simulate the Greedy Perimeter Stateless Routing (GPSR) protocol using MATLAB necessitates executing a geographic-based routing algorithm in which nodes send packets depends on its locations. GPSR works in two modes they are greedy forwarding, where packets are sent to the nearby node to the destination, and perimeter mode that is utilized once greedy sending fails by reason of local minima. Follow the provided steps of GPSR projects that can be simulated using MATLAB:

Steps to Simulate GPSR Protocol in MATLAB

  1. Define the Network Topology:
  • Make a wireless network in which nodes are shared arbitrarily in a provided area. The node positions denote the geographic coordinates that are utilized within GPSR creating routing decisions.

Example:

% Number of nodes in the network

num_nodes = 20;

% Define the size of the simulation area (e.g., 100×100 units)

area_size = 100;

% Randomly generate positions for the nodes

nodes = area_size * rand(num_nodes, 2); % Random (x, y) coordinates

% Define the communication range (maximum distance between nodes for a link)

comm_range = 30;

% Plot the network topology

figure;

scatter(nodes(:, 1), nodes(:, 2), ‘filled’);

xlabel(‘X coordinate’);

ylabel(‘Y coordinate’);

title(‘Network Topology for GPSR Simulation’);

grid on;

hold on;

  1. Create Adjacency Matrix for Neighboring Nodes:
  • Find out which nodes are in communication range of each other that will create the links within the network.

Example:

% Calculate the pairwise distances between all nodes

distances = pdist2(nodes, nodes);

% Create an adjacency matrix where 1 indicates a link between nodes

adjacency_matrix = distances <= comm_range & distances > 0;

% Plot the links between neighboring nodes

for i = 1:num_nodes

for j = i+1:num_nodes

if adjacency_matrix(i, j)

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

end

end

end

  1. Implement GPSR Greedy Forwarding Mode:
  • In greedy forwarding, packets are sent to the neighbor, which is geographically near to the destination. If there are no neighbors nearby to the destination then the protocol changes to perimeter mode.

Example of Greedy Forwarding:

% Define the source and destination nodes

source_node = 1;

destination_node = num_nodes;

% Get the position of the destination node

destination_pos = nodes(destination_node, :);

% Initialize current node as the source node

current_node = source_node;

% Start packet forwarding

fprintf(‘Starting GPSR from Node %d to Node %d\n’, source_node, destination_node);

while current_node ~= destination_node

% Get the current node’s position

current_pos = nodes(current_node, :);

% Find all neighbors of the current node

neighbors = find(adjacency_matrix(current_node, :));

% If no neighbors, switch to perimeter mode (later step)

if isempty(neighbors)

fprintf(‘No neighbors for Node %d. Switching to perimeter mode.\n’, current_node);

break;

end

% Compute the distances from neighbors to the destination

distances_to_dest = vecnorm(nodes(neighbors, 🙂 – destination_pos, 2, 2);

% Find the closest neighbor to the destination

[min_dist, idx] = min(distances_to_dest);

next_node = neighbors(idx);

% Check if the closest neighbor is closer to the destination

if min_dist < norm(current_pos – destination_pos)

fprintf(‘Forwarding packet from Node %d to Node %d (Greedy Mode)\n’, current_node, next_node);

current_node = next_node;

else

fprintf(‘No closer neighbor found for Node %d. Switching to perimeter mode.\n’, current_node);

break;

end

end

  1. Implement GPSR Perimeter Mode (Optional):
  • Once greedy forwarding be unsuccessful that is no neighbors are closer to the destination then the protocol changes to perimeter mode. In perimeter mode, packets are sent utilizing a right-hand rule to pass through the network until greedy sending can restart.

The right-hand rule can be executed by finding the relative positions of the nodes and then sending the packet along the edges of the network or the boundary.

Example:

% Function to implement perimeter mode using the right-hand rule

function next_hop = perimeter_mode(current_node, adjacency_matrix, nodes)

% In perimeter mode, follow the right-hand rule

% (Find the neighbor that forms the rightmost turn)

neighbors = find(adjacency_matrix(current_node, :));  % Get neighbors

if isempty(neighbors)

next_hop = -1;  % No neighbors in perimeter mode

return;

end

% Placeholder for the perimeter forwarding (you can implement based on geometry)

next_hop = neighbors(1);  % In this example, just select the first neighbor

fprintf(‘Perimeter mode: Forwarding to Node %d\n’, next_hop);

end

% Example usage after greedy mode fails

if current_node ~= destination_node

next_node = perimeter_mode(current_node, adjacency_matrix, nodes);

if next_node ~= -1

current_node = next_node;

else

fprintf(‘No more nodes in perimeter mode. Packet delivery failed.\n’);

end

end

  1. Packet Forwarding Process:
  • Aggregate both greedy forwarding and perimeter mode making the complete GPSR protocol. Packets are send utilizing the greedy mode until local minima are met, after which perimeter mode takes over.

Example:

% Start with greedy forwarding

current_node = source_node;

while current_node ~= destination_node

% Try greedy forwarding first

neighbors = find(adjacency_matrix(current_node, :));

if isempty(neighbors)

fprintf(‘No neighbors for Node %d. Switching to perimeter mode.\n’, current_node);

current_node = perimeter_mode(current_node, adjacency_matrix, nodes);

else

% Greedy forwarding logic

distances_to_dest = vecnorm(nodes(neighbors, 🙂 – destination_pos, 2, 2);

[min_dist, idx] = min(distances_to_dest);

next_node = neighbors(idx);

if min_dist < norm(nodes(current_node, 🙂 – destination_pos)

fprintf(‘Greedy Mode: Forwarding packet from Node %d to Node %d\n’, current_node, next_node);

current_node = next_node;

else

fprintf(‘Switching to perimeter mode from Node %d\n’, current_node);

current_node = perimeter_mode(current_node, adjacency_matrix, nodes);

end

end

% If no valid node in perimeter mode, terminate

if current_node == -1

fprintf(‘Packet delivery failed.\n’);

break;

end

end

  1. Performance Metrics:
  • Calculate the performance parameters like:
    • Packet Delivery Ratio: Ratio of effectively delivered packets to total packets transmitted.
    • End-to-End Delay: Duration for a packet to move from origin to destination.
    • Routing Overhead: The amount of control messages or routing decisions created for the period of the simulation.

Example of calculating end-to-end delay:

% Example packet transmission speed (e.g., 1 unit per second)

transmission_speed = 1;

% Calculate the number of hops for packet transmission

num_hops = 5;  % Example value (based on route)

end_to_end_delay = num_hops / transmission_speed;

fprintf(‘End-to-End Delay: %.2f seconds\n’, end_to_end_delay);

  1. Simulate Multiple Packet Flows (Optional):
  • We can replicate several packet flows by arbitrarily choosing diverse source and destination pairs and implementing the GPSR protocol to each flow.

Example:

num_flows = 10;

for flow = 1:num_flows

% Randomly select source and destination nodes

source_node = randi(num_nodes);

destination_node = randi(num_nodes);

% Run the GPSR protocol for this flow

fprintf(‘Simulating GPSR from Node %d to Node %d\n’, source_node, destination_node);

% Repeat the GPSR logic (greedy and perimeter modes)

end

Conclusion:

This manual guides via replicating the GPSR (Greedy Perimeter Stateless Routing) protocol using MATLAB. GPSR is a geographic-based protocol in which packets are sent according to its position related to the destination. This contains greedy forwarding and perimeter mode for routing around the network voids or obstacles.

Key steps include:

  1. Describing the network topology.
  2. Executing greedy forwarding and perimeter mode.
  3. Sending packets and calculating performance.

We delivered a standard approach for simulating and executing the GPSR (Greedy Perimeter Stateless Routing) protocol projects within MATLAB platform. If you want any information on this topic, we will send it too.

Struggling to get your GPSR Protocol Project simulation done by our team we at phdprime.com will aid you with best project results. So all you need to do is drop your project details by mail we guide you with good results.

Opening Time

9:00am

Lunch Time

12:30pm

Break Time

4:00pm

Closing Time

6:30pm

  • award1
  • award2