46.2 kB
jdoug631's picture
% HMGWO Routing Protocol for Heterogeneous WSNs % Based on the paper "Routing Protocol for Heterogeneous Wireless Sensor % Networks Based on a Modified Grey Wolf Optimizer" % Simulation Parameters numNodes = 100; % Total number of nodes areaSize = 100; % Square area (meters) numRounds = 100; % Number of simulation rounds packetSize = 4000; % Bits per data packet initialEnergy_normal = 0.5; % Joules initialEnergy_advanced = 1.0; % Joules (β=2 ratio) a1 = 0.2; % Weight for fitness function F1 a2 = 0.2; % Weight for fitness function F2 a3 = 0.2; % Weight for objective function F3 p = 0.1; % Portion of cluster heads % Energy Model Parameters Eelec = 50e-9; % Joules/bit (50 nJ) EDA = 5e-9; % Joules/bit (5 nJ) eps_fs = 10e-12; % pJ/bit/m^2 eps_mp = 0.0013e-12; % pJ/bit/m^4 dcrossover = sqrt(eps_fs/eps_mp); % Threshold distance % Node Deployment % Randomly distribute nodes in the area nodePositions = areaSize * rand(numNodes, 2); % Assign node types (normal/advanced) nodeTypes = ones(numNodes, 1); % 1: normal, 2: advanced advancedIndices = randperm(numNodes, round(0.1 * numNodes)); % 10% advanced nodes nodeTypes(advancedIndices) = 2; % Initialize energy levels nodeEnergy = zeros(numNodes, 1); nodeEnergy(nodeTypes == 1) = initialEnergy_normal; nodeEnergy(advancedIndices) = initialEnergy_advanced; % Base Station Position BS = [areaSize/2, areaSize/2]; % Initialize simulation variables aliveNodes = ones(numRounds, 1) * numNodes; residualEnergy = zeros(numRounds, numNodes); throughput = zeros(numRounds, 1); % Main simulation loop for round = 1:numRounds % 1. Initial Cluster Selection (Section 4.3.1) % Calculate F1 fitness values distToBS = sqrt((nodePositions(:,1) - BS(1)).^2 + (nodePositions(:,2) - BS(2)).^2); dMAXBS = max(distToBS); dMINBS = min(distToBS); F1 = zeros(numNodes, 1); normalNodes = (nodeTypes == 1) & (nodeEnergy > 0); advancedNodes = (nodeTypes == 2) & (nodeEnergy > 0); F1(normalNodes) = a1 * (nodeEnergy(normalNodes) ./ initialEnergy_normal) + ... (1 - a1) * (dMAXBS - distToBS(normalNodes)) / (dMAXBS - dMINBS); F1(advancedNodes) = (1 - a1) * (nodeEnergy(advancedNodes) ./ initialEnergy_advanced) + ... a1 * (dMAXBS - distToBS(advancedNodes)) / (dMAXBS - dMINBS); % Sort nodes by fitness [F1_sorted, nodeIndices] = sort(F1, 'descend'); % Select initial CHs numCH = round(p * numNodes); CH_indices = zeros(numCH, 1); clusterMembers = cell(numCH, 1); % Select top nodes as initial CHs for i = 1:numCH CH_indices(i) = nodeIndices(i); end % 2. Modified Grey Wolf Optimizer (Section 4.3.2) % Initialize GWO parameters numWolves = 10; wolfPositions = nodePositions(CH_indices(1:numWolves), :); wolfEnergy = nodeEnergy(CH_indices(1:numWolves)); alphaPos = wolfPositions(1, :); betaPos = wolfPositions(1, :); deltaPos = wolfPositions(1, :); % GWO optimization loop maxIter = 50; for iter = 1:maxIter % Calculate fitness values for wolves distToPrey = sqrt((wolfPositions(:,1) - BS(1)).^2 + (wolfPositions(:,2) - BS(2)).^2); F2 = a2 * (max(wolfEnergy) - wolfEnergy) + ... (1 - a2) * (max(distToPrey) - distToPrey) / (max(distToPrey) - min(distToPrey)); % Update alpha, beta, delta [~, sortedIndices] = sort(F2); alphaPos = wolfPositions(sortedIndices(1), :); betaPos = wolfPositions(sortedIndices(2), :); deltaPos = wolfPositions(sortedIndices(3), :); % Update positions using GWO equations a = 2 - iter*(2/maxIter); for i = 1:numWolves A1 = 2*a*rand() - a; C1 = 2*rand(); D_alpha = abs(C1*alphaPos - wolfPositions(i,:)); X1 = alphaPos - A1*D_alpha; A2 = 2*a*rand() - a; C2 = 2*rand(); D_beta = abs(C2*betaPos - wolfPositions(i,:)); X2 = betaPos - A2*D_beta; A3 = 2*a*rand() - a; C3 = 2*rand(); D_delta = abs(C3*deltaPos - wolfPositions(i,:)); X3 = deltaPos - A3*D_delta; wolfPositions(i,:) = (X1 + X2 + X3)/3; end % Ensure positions stay within area bounds wolfPositions = max(min(wolfPositions, areaSize), 0); end % 3. Final CH Selection % Calculate final fitness F3 distToPrey = sqrt((wolfPositions(:,1) - BS(1)).^2 + (wolfPositions(:,2) - BS(2)).^2); F3 = a3 * mean(distToPrey) + (1 - a3) * distToPrey; % Select best wolf as final CH [minF3, bestCH] = min(F3); finalCH = wolfPositions(bestCH, :); % 4. Cluster Formation % Assign nodes to nearest CH distToCH = sqrt((nodePositions(:,1) - finalCH(1)).^2 + ... (nodePositions(:,2) - finalCH(2)).^2); % Update node energy based on communication % Energy consumption for data transmission for i = 1:numNodes if ~ismember(i, bestCH) % Calculate transmission distance d = distToCH(i); if d <= dcrossover E_tx = packetSize * Eelec + packetSize * eps_fs * d^2; else E_tx = packetSize * Eelec + packetSize * eps_mp * d^4; end nodeEnergy(i) = max(nodeEnergy(i) - E_tx, 0); end end % Energy consumption for CH operations E_CH = packetSize * Eelec + packetSize * EDA + ... (packetSize * Eelec + packetSize * eps_mp * (distToPrey(bestCH))^4); nodeEnergy(CH_indices(bestCH)) = max(nodeEnergy(CH_indices(bestCH)) - E_CH, 0); % Update simulation metrics aliveNodes(round) = sum(nodeEnergy > 0); residualEnergy(round, :) = nodeEnergy'; throughput(round) = numNodes - sum(nodeEnergy == 0); % Stop simulation if all nodes are dead if aliveNodes(round) == 0 break; end end % Plot network lifetime figure; plot(1:round, aliveNodes(1:round), 'b', 'LineWidth', 2); title('Network Lifetime Analysis'); xlabel('Rounds'); ylabel('Number of Alive Nodes'); grid on; % Plot throughput figure; plot(1:round, throughput(1:round), 'r', 'LineWidth', 2); title('Throughput Analysis'); xlabel('Rounds'); ylabel('Number of Packets Received by BS'); grid on; % Plot energy consumption figure; plot(1:round, mean(residualEnergy(1:round,:), 2), 'g', 'LineWidth', 2); title('Average Residual Energy'); xlabel('Rounds'); ylabel('Average Energy (J)'); grid on; - Initial Deployment
6610607 verified