# How to implement PSO algorithm for sizing of supercapacitor and battery in HESS using Matlab

16 views (last 30 days)
Sri Hari R on 1 Apr 2022
Answered: Anshuman on 6 Feb 2024
The PSO Code for HESS Sizing of Ultracapacitors and Battery in Electric Vehicle
Sam Chak on 5 Feb 2023
Edited: Sam Chak on 5 Feb 2023
Hi Marko, I don't have the HESS code. The PSO algorithm is available in the Optimization Toolbox.
I think the general idea is that the size of the battery affects the performance of the system. Thus, I think there should be 3 components for an unconstrained problem:
1. The dynamics of system.
2. The objective function that measures the performance (maybe power and energy dissipated). Can be a quadratic function.
3. The optimizer, e.g., PSO, GA, ACO, ABC, fminunc, fmincon, fminsearch.
Marko Stokic on 6 Feb 2023
@Sam Chak, thank you very much for the explanation!

Anshuman on 6 Feb 2024
PSO is a heuristic algorithm, and the actual implementation details can vary widely depending on the specific problem being solved. Below is a simplified example of a MATLAB code snippet that uses PSO to size hybrid energy storage systems (HESS) consisting of ultracapacitors and batteries for an electric vehicle (EV).
% PSO parameters
numParticles = 30; % Number of particles in the swarm
maxIterations = 100; % Maximum number of iterations
inertiaWeight = 0.7; % Inertia weight
cognitiveComponent = 1.5; % Cognitive component constant
socialComponent = 1.5; % Social component constant
% Problem-specific parameters
% Define the ranges for ultracapacitor size and battery size
ucapRange = [10, 1000]; % Range of ultracapacitor size in Farads
battRange = [1, 100]; % Range of battery size in kWh
% Initialize the swarm
position = zeros(numParticles, 2); % Each particle has a position [ucapSize, battSize]
velocity = zeros(numParticles, 2); % Velocity of the particles
pBest = zeros(numParticles, 2); % Best position of each particle
pBestCost = inf(numParticles, 1); % Best cost for each particle
gBest = zeros(1, 2); % Global best position
gBestCost = inf; % Global best cost
% Initialize positions and velocities
for i = 1:numParticles
position(i, :) = [rand*(ucapRange(2)-ucapRange(1))+ucapRange(1), rand*(battRange(2)-battRange(1))+battRange(1)];
end
% PSO main loop
for iter = 1:maxIterations
for i = 1:numParticles
% Evaluate the cost of the current position
cost = objectiveFunction(position(i, 1), position(i, 2));
% Update personal best
if cost < pBestCost(i)
pBest(i, :) = position(i, :);
pBestCost(i) = cost;
end
% Update global best
if cost < gBestCost
gBest = position(i, :);
gBestCost = cost;
end
end
% Update velocity and position of particles
for i = 1:numParticles
velocity(i, :) = inertiaWeight * velocity(i, :) ...
+ cognitiveComponent * rand * (pBest(i, :) - position(i, :)) ...
+ socialComponent * rand * (gBest - position(i, :));
position(i, :) = position(i, :) + velocity(i, :);
% Ensure the particles stay within the defined ranges
position(i, 1) = max(min(position(i, 1), ucapRange(2)), ucapRange(1));
position(i, 2) = max(min(position(i, 2), battRange(2)), battRange(1));
end
% Display iteration information
disp(['Iteration ' num2str(iter) ': Best Cost = ' num2str(gBestCost)]);
end
% Objective function (to be defined according to the problem)
function cost = objectiveFunction(ucapSize, battSize)
% Define the cost function based on the HESS sizing criteria
% This is a placeholder function; you should define your own cost function
% based on energy, power requirements, cost, weight, etc.
cost = (ucapSize * 0.5) + (battSize * 200); % Example cost function
end