To simulate the Clusterhead Gateway Switch Routing (CGSR) in MATLAB environment that is a hierarchical routing protocol modeled for mobile ad hoc networks (MANETs). In CGSR, nodes are gathered into clusters, and every single cluster has a clusterhead, which handles interaction in the cluster and sends information among the clusters via gateway nodes. The CGSR protocol offers scalability and efficiency within dynamic networks such as MANETs.
To replicate the CGSR using MATLAB that has simple steps normally include describing a network topology with nodes, choosing clusterheads, sustaining a cluster structure, and executing a routing mechanism via gateways and clusterheads.
Steps to Simulate CGSR Protocol in MATLAB
- Define the Network Topology
- Describe a collection of nodes, its locations, and their communication range. Nodes will be gathered into clusters along with one node within each cluster perfoming as the clusterhead.
Example of defining the network and nodes:
numNodes = 20; % Number of nodes
networkArea = 100; % Size of the network area (100×100 meters)
nodePositions = rand(numNodes, 2) * networkArea; % Random positions for nodes
communicationRange = 30; % Define communication range for each node
% Adjacency matrix for connectivity between nodes based on communication range
adjacencyMatrix = inf(numNodes);
for i = 1:numNodes
for j = i+1:numNodes
distance = norm(nodePositions(i,:) – nodePositions(j,:));
if distance <= communicationRange
adjacencyMatrix(i,j) = distance;
adjacencyMatrix(j,i) = distance;
end
end
end
- Select Clusterheads
- Choose clusterheads by using cluster formation algorithm. One simple method is to allocate the node including the highest degree (the most neighbors) as the clusterhead.
Example of selecting clusterheads:
function clusterheads = selectClusterheads(adjacencyMatrix, numNodes, threshold)
% Select nodes as clusterheads based on their connectivity (degree)
degree = sum(adjacencyMatrix < inf, 2); % Calculate node degrees
clusterheads = find(degree >= threshold); % Nodes with degree above the threshold
end
threshold = 3; % Minimum number of neighbors to be a clusterhead
clusterheads = selectClusterheads(adjacencyMatrix, numNodes, threshold);
disp([‘Selected Clusterheads: ‘, num2str(clusterheads)]);
- Form Clusters Around Clusterheads
- After choosing the clusterheads then we can allocate each non-clusterhead node to the nearby clusterhead. It will make clusters of nodes, with one clusterhead handling every single cluster.
Example of forming clusters:
function clusters = formClusters(nodePositions, clusterheads, numNodes)
clusters = cell(length(clusterheads), 1);
for i = 1:numNodes
% Find the closest clusterhead to this node
distances = vecnorm(nodePositions(i,:) – nodePositions(clusterheads,:), 2, 2);
[~, closestClusterheadIdx] = min(distances);
clusters{closestClusterheadIdx} = [clusters{closestClusterheadIdx}, i];
end
end
clusters = formClusters(nodePositions, clusterheads, numNodes);
disp(‘Clusters formed around clusterheads:’);
for i = 1:length(clusters)
disp([‘Cluster ‘, num2str(i), ‘: ‘, num2str(clusters{i})]);
end
- Implement Intra-Cluster Routing
- For interaction within the cluster, we need to utilize a basic direct communication mechanism, as nodes are predictable to be within interaction range of its clusterhead.
Example of intra-cluster routing:
function route = intraClusterRouting(adjacencyMatrix, src, dst, clusters)
% Intra-cluster routing assumes direct communication
for i = 1:length(clusters)
if ismember(src, clusters{i}) && ismember(dst, clusters{i})
if adjacencyMatrix(src, dst) < inf
route = [src, dst];
else
route = []; % No direct path
end
return;
end
end
route = []; % Nodes are not in the same cluster
end
- Implement Inter-Cluster Routing Through Gateways
- Inter-cluster communication needs to transmit information from one clusterhead to another, which probably through gateway nodes (nodes that are in the communication range of several clusterheads).
Example of inter-cluster routing through gateways:
function route = interClusterRouting(adjacencyMatrix, src, dst, clusterheads, clusters)
% Find the source and destination clusterheads
srcClusterhead = findClusterhead(src, clusters, clusterheads);
dstClusterhead = findClusterhead(dst, clusters, clusterheads);
% If the source and destination are in different clusters, find gateway nodes
if srcClusterhead ~= dstClusterhead
% Find gateway nodes that connect the two clusters
gateways = findGateways(srcClusterhead, dstClusterhead, adjacencyMatrix, clusters);
if ~isempty(gateways)
route = [src, gateways(1), dst];
else
route = []; % No gateway found
end
else
% If source and destination are in the same cluster, use intra-cluster routing
route = intraClusterRouting(adjacencyMatrix, src, dst, clusters);
end
end
function clusterhead = findClusterhead(node, clusters, clusterheads)
for i = 1:length(clusters)
if ismember(node, clusters{i})
clusterhead = clusterheads(i);
return;
end
end
end
function gateways = findGateways(srcClusterhead, dstClusterhead, adjacencyMatrix, clusters)
% Find nodes that can communicate between the two clusterheads
srcClusterNodes = clusters{srcClusterhead};
dstClusterNodes = clusters{dstClusterhead};
gateways = [];
for i = srcClusterNodes
for j = dstClusterNodes
if adjacencyMatrix(i, j) < inf
gateways = [gateways, i, j];
end
end
end
end
- Simulate Data Transmission Using CGSR
- After creating clusters and describing the routing mechanisms then we can replicate the broadcast of data from a source to a destination, either within the similar cluster or across clusters.
Example of simulating data transmission:
function simulateDataTransmission(adjacencyMatrix, src, dst, clusterheads, clusters)
if ismember(src, clusterheads) && ismember(dst, clusterheads)
% Direct communication between clusterheads
route = [src, dst];
else
% Use intra- or inter-cluster routing
route = interClusterRouting(adjacencyMatrix, src, dst, clusterheads, clusters);
end
if isempty(route)
disp(‘No valid route found.’);
else
disp([‘Data transmitted from ‘, num2str(src), ‘ to ‘, num2str(dst), ‘ via route: ‘, num2str(route)]);
end
end
% Example: Transmit data from node 1 to node 15
srcNode = 1;
dstNode = 15;
simulateDataTransmission(adjacencyMatrix, srcNode, dstNode, clusterheads, clusters);
- Evaluate CGSR Performance
- Estimate their performance with the help of parameters like average route length, number of hops, packet delivery ratio, or routing overhead, after replicating the CGSR.
Example of calculating average route length:
totalRouteLength = 0;
routeCount = 0;
for i = 1:numNodes
for j = i+1:numNodes
route = interClusterRouting(adjacencyMatrix, i, j, clusterheads, clusters);
if ~isempty(route)
totalRouteLength = totalRouteLength + length(route);
routeCount = routeCount + 1;
end
end
end
avgRouteLength = totalRouteLength / routeCount;
disp([‘Average route length: ‘, num2str(avgRouteLength)]);
- Visualize Network Topology and Clusters
- Envision the network, clusters, and the routing paths by using MATLAB’s plotting functions.
Example of visualizing the network:
figure;
hold on;
plot(nodePositions(:,1), nodePositions(:,2), ‘bo’); % Plot node positions
% Highlight clusterheads
plot(nodePositions(clusterheads,1), nodePositions(clusterheads,2), ‘ro’, ‘MarkerSize’, 10, ‘LineWidth’, 2);
% Draw links between connected nodes
for i = 1:numNodes
for j = i+1:numNodes
if adjacencyMatrix(i,j) < inf
plot([nodePositions(i,1), nodePositions(j,1)], …
[nodePositions(i,2), nodePositions(j,2)], ‘k-‘);
end
end
end
title(‘CGSR Network Topology with Clusters and Clusterheads’);
hold off;
In this setup, Clusterhead Gateway Switch Routing (CGSR) protocol projects simulations, carried out using MATLAB environment through the above simulation approach with example coding, is thorough and we are equipped to offer additional information.
At phdprime.com, we are here to help you achieve outstanding results in simulating CGSR Protocol Projects using MATLAB. We provide innovative project ideas and conduct performance analyses tailored to your specific interests