How to Start LEACH Routing Projects Using MATLAB

To create the LEACH (Low-Energy Adaptive Clustering Hierarchy) is a famous routing protocol used in wireless sensor networks (WSNs). It is model for decrease the energy usage through organizing the sensor nodes in clusters and chooses a Cluster Head (CH) we perform a communication by the base station or sink, rather than have an overall nodes transmit the data directly. This supports to balancing the energy usage with the network.

Starting a LEACH routing project in MATLAB essential for the breaking down of LEACH protocol in the main components for apply them. Here’s a step-by-step guide to help you get started:

Steps to Start LEACH Routing Projects Using MATLAB

Key Components of LEACH Protocol:

  1. Cluster Formation: Nodes are structure into the clusters. Every cluster has the leader called as the Cluster Head (CH).
  2. Cluster Head Rotation: LEACH periodically rotates the CHs we assure the energy balancing with the network.
  3. Data Transmission: Non-CH nodes transmit their data to the CH. The CH aggregates the data and sending the base station.
  4. Energy Management: Nodes are consuming the energy according to their transmission distance, and CHs are selected in a path which reduces the energy usage.

Steps to Implement LEACH Protocol in MATLAB:

  1. Define Network and Node Positions

Start, we require to describe the network’s size, the number of nodes, and their positions. This can be complete through creating arbitrary positions for sensor nodes in a 2D sector.

Example:

% Define the network area (e.g., 100×100 m)

area = [0, 100, 0, 100];  % x_min, x_max, y_min, y_max

 

% Number of nodes in the network

numNodes = 100;

% Randomly generate node positions within the area

nodePositions = [rand(numNodes, 1) * (area(2) – area(1)) + area(1), …

rand(numNodes, 1) * (area(4) – area(3)) + area(3)];

% Display node positions

figure;

plot(nodePositions(:,1), nodePositions(:,2), ‘o’);

xlabel(‘X Position (m)’);

ylabel(‘Y Position (m)’);

title(‘Node Positions in LEACH Network’);

grid on;

  1. Cluster Head (CH) Selection

In LEACH, the CH collection is complete for probabilistically. Every node has a sure for the probability for developing a CH in every round, and this probability is influenced through on how many rounds the node have been a CH in the past.

  • CH Selection Criteria: A node’s probability for develop a CH can be setting, and nodes that have not been CHs recently have a higher probability.
  • Rotation: These assure which energy is balanced with the network.

CH Selection Algorithm:

function [CHs, clusterAssignments] = selectClusterHeads(nodePositions, numNodes, P, round)

% P: Probability of becoming a CH in each round

% round: Current round number to account for cluster head rotation

 

% Initialize

CHs = [];

clusterAssignments = zeros(numNodes, 1);  % Assign clusters

for i = 1:numNodes

% Node’s probability to become a CH

randVal = rand;

if randVal <= P

% If the random value is less than the probability, select this node as CH

CHs = [CHs, i];

clusterAssignments(i) = i;  % This node is a CH

end

end

% Ensure that there are always some CHs selected

if isempty(CHs)

CHs = randi(numNodes, [1, 1]);  % Select at least one CH if none selected

end

end

This perform the probabilistically choose a node to be CHs according to the probability P. In a real LEACH protocol, the probability variations over time, nevertheless this simple for apply and we choose the nodes according to a fixed probability P.

  1. Cluster Formation and Member Assignment

After CHs are choosing, we want to allocate other nodes to the near CH to form clusters. This can be complete through measuring the distance from every node to each CH and allocating the node to the near one.

Cluster Assignment Algorithm:

function clusterAssignments = assignNodesToClusters(nodePositions, CHs, numNodes)

clusterAssignments = zeros(numNodes, 1);

for i = 1:numNodes

minDistance = inf;

for j = 1:length(CHs)

% Calculate the distance from node to each CH

distance = sqrt((nodePositions(i, 1) – nodePositions(CHs(j), 1))^2 + …

(nodePositions(i, 2) – nodePositions(CHs(j), 2))^2);

if distance < minDistance

minDistance = distance;

clusterAssignments(i) = CHs(j);  % Assign node to the closest CH

end

end

end

end

This function allocates every node to the nearby CH according to the Euclidean distance.

  1. Data Transmission and Energy Consumption

In LEACH, nodes forward the data for corresponding CH, and the CH transmit the aggregated data for the base station. We require accounting for energy usage terms on the distance for a node or CH transmits.

Energy Model (Simplified):

  • Energy usage for transmission is typically modeled as: Etx=E0⋅dαE_{tx} = E_0 \cdot d^{\alpha}Etx​=E0​⋅dα where:
    • E0E_0E0​ is the energy need for sending a 1 bit at distance 1 meter.
    • ddd is the transmission distance.
    • α\alphaα is the path loss exponent (typically 2 to 4).

Energy Consumption Function:

function energy = computeTransmissionEnergy(distance, E0, alpha)

energy = E0 * (distance^alpha);

end

We can use this perform to calculate the energy used through every node or CH when transmitting data.

  1. Simulation and Results

Now that we have the basic LEACH components, we can replicate the several rounds of the protocol. For every round:

  • Choose the cluster head.
  • Allocate the nodes to clusters.
  • Replicate the data transmission and measure the energy usage.
  • Rotate the CHs after each round to balance energy consumption.

Example of running the simulation for multiple rounds:

numRounds = 10;  % Number of rounds for simulation

P = 0.1;  % Probability of becoming a CH

E0 = 50e-9;  % Transmission energy constant

alpha = 2;  % Path loss exponent

% Initialize energy values (assume all nodes have the same initial energy)

initialEnergy = 1000;  % Energy in mJ

energies = repmat(initialEnergy, [numNodes, 1]);

for round = 1:numRounds

% Select CHs

[CHs, clusterAssignments] = selectClusterHeads(nodePositions, numNodes, P, round);

 

% Assign nodes to clusters

clusterAssignments = assignNodesToClusters(nodePositions, CHs, numNodes);

% Simulate data transmission and calculate energy consumption

for i = 1:numNodes

% If node is not a CH, compute energy consumption

if clusterAssignments(i) ~= i

% Calculate distance to its CH

chPosition = nodePositions(clusterAssignments(i), :);

distance = sqrt((nodePositions(i, 1) – chPosition(1))^2 + (nodePositions(i, 2) – chPosition(2))^2);

% Compute energy for transmission to CH

energyUsed = computeTransmissionEnergy(distance, E0, alpha);

energies(i) = energies(i) – energyUsed;  % Decrease node’s energy

end

end

% Display energy consumption

disp([‘Round ‘, num2str(round), ‘ – Node Energies:’]);

disp(energies);

end

This will process for the LEACH protocol in several rounds and monitor the energy usage of every node. Next different rounds, nodes will general process the out of energy, and the network will have fewer active nodes.

  1. Visualizing the Network

Finally, we can visualize the network through CHs and the cluster formation at every round.

Visualization Example:

figure;

hold on;

plot(nodePositions(:,1), nodePositions(:,2), ‘bo’);  % Plot all nodes

plot(nodePositions(CHs, 1), nodePositions(CHs, 2), ‘ro’);  % Plot CHs in red

xlabel(‘X Position (m)’);

ylabel(‘Y Position (m)’);

title(‘LEACH Cluster Formation’);

legend(‘Sensor Nodes’, ‘Cluster Heads’);

hold off;

This basic execution will give you a foundation for replicating and experimenting with the LEACH protocol in MATLAB. You can expand it further to include features like network topology changes, performance parameter metrics for sample throughput, lifetime, and more sophisticated energy models.

In the conclusion, we can gain the valuable insights about how the LEACH routing example projects will implement in diverse scenarios by using the MATLAB tool. We also deliver the additional information regarding the LEACH Routing.