Clear Filters
Clear Filters

particle swarm optimization code to minimize cost function

28 views (last 30 days)
Help me form an objective function and possible constraints to minimize the cost function using Particle Swarm Optimization(PSO).
I have to minimize the electricity cost in a microgrid with fixed load demand for each hour (i.e. 24 hr) using PSO. The microgrid have solar generation , wind generation, Microturbine and an energy storage system. the microgrid is operating in grid connected mode so we need to consider the amount of energy bought from or sold to main grid also for this data of cost of the electricity per hr is given. the following data is available:
solar_power_available_per_hr = [0 0 0 0 0 0 0 0.2 3.75 7.525 10.45 11.95 23.9 21.05 0.315 7.875 0.55 0 0 0 0 0 0 0];
wind_power_available_per_hr = [1.785 1.785 1.785 1.785 1.785 0.915 1.785 1.305 1.785 3.09 8.775 10.41 3.915 2.37 1.785 1.305 1.785 1.785 1.305 1.785 1.305 1.305 0.915 0.615];
power_demand_from_loads_per_hr = [52 50 50 51 56 53 70 75 76 80 78 74 72 72 76 80 85 88 90 87 78 71 65 56];
electricity_price_per_kWh_during_each_hr = [0.23 0.19 0.14 0.12 0.12 0.20 0.23 0.38 1.50 4 4 4 1.5 4 2 1.95 0.6 0.41 0.35 0.43 1.17 0.54 0.30 0.26];
% P_solar: min = 0, max = 25kW;
% P_wind: min = 0, max = 15kW;
% P_microturbine: min = 6kW, max = 30kW;
% battery power: min = -30kW, max = +30kW;
% grid power: min = -30kW, max = +30kW;
battery_efficiency_considerd = 90/100 % (i.e. eta(charge or discharge)=0.9).
Below data is obtained using fuzzy logic based battery scheduling
initial_soc_considered = 86/100 % percent
Pbatt(t) = Pbatt(t-1) + 0.9*Pchar*del(t) - (1/0.9)*Pdis*del(t);
power_of_battery_charged(+)_or_discharge_per_hr = [-18.1072, 0, 0, 0, 0, 0, -18.1072, -18.4010, -18.1072, -10.3106, -10.6032, -9.6518, -9.4837, -8.2505, -9.5903, -8.9892, 19.4399, -18.8184, 12.6071, -19.0777, -16.9413, 16.9189, -17.1563, 16.4767];
Battery_SoC = [86, 65.88, 65.88, 65.88, 65.88, 65.88, 65.88, 45.76, 25.32, 5.19, 5.19, 5.19, 5.19, 5.19, 5.19, 5.19, 5.19, 26.79, 5.88, 19.89, 19.89, 1.07, 19.87, 0.81];
I have scheduled the battery so that minimum amount of power should be bought from the main grid in order to reduce the cost. and excess power can be sold to the grid when demand is low or renewable generation is high.
parameters: Bid price($/kWh) for differnt sources:
battery = 0.38;
solar = 2.584;
wind = 1.073;
microturbine = 0.457;
microturbine_startup_and_shutdown_cost = 0.96;
I am an absolute beginner in PSO. so, if you could provide the basic syntax of PSO for the above problem along with the objective function and constraints it will be really helpful.
Thanks in advance!
  9 Comments
REENA
REENA on 22 Nov 2023
var is the keyword i am using to introduce new variable P_Grid......
so that;
P_Grid= var(nHours, 1) will give 24*1 matrix (nHours=24 )
i want to print the values of f (cost function) for all the 24 hrs and verify in which hr the cost is minimum(f_min) or gbest value.
Walter Roberson
Walter Roberson on 22 Nov 2023
the particleswarm function expects a function handle as the first parameter, and the number of variables as the second parameter. The first output for it is a vector of values that minimizes the function.
https://www.mathworks.com/help/gads/particleswarm.html

Sign in to comment.

Answers (1)

Sam Chak
Sam Chak on 21 Nov 2023
Regarding the particleswarm() solver, I believe you can define constraints using the Problem-Based Optimization approach. However, I am accustomed to defining constraints for a collection of vectors in a single function call. If I recall correctly, only the patternsearch() and ga() solvers allow direct calling the nonlinear constraint function in their syntax.
%% Pattern Search method
x0 = [0.25, 0.6, 12.2];
patopt = optimoptions('patternsearch', 'UseCompletePoll', true, 'UseVectorized', true);
[x fval] = patternsearch(@costfun, x0, [], [], [], [], [], [], @nonlcon, patopt)
Optimization finished: mesh size less than options.MeshTolerance and constraint violation is less than options.ConstraintTolerance.
x = 1×3
0.3028 0.5418 12.1906
fval = -148.9967
%% Genetic Algorithm method
gaopt = optimoptions('ga', 'UseVectorized', true);
lb = [0.1 0.5 12.0];
ub = [0.3 0.7 12.2];
[x fval] = ga(@costfun, 3, [], [], [], [], lb, ub, @nonlcon, gaopt)
Optimization finished: average change in the fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
x = 1×3
0.2889 0.5678 12.1905
fval = -149.0134
%% multivariate Cost function to minimized
function y = costfun(x)
y = - x(:,1).^2 - x(:,2).^2 - x(:,3).^2;
end
Nonlinear constraints:
%% subject to Nonlinear constraints
function [c ceq] = nonlcon(x)
c(:,1) = x(:,1).^2/4 + x(:,2).^2/9 + x(:,3).^2/25 - 6; % inequality constraint #1
c(:,2) = cosh(x(:,1) + x(:,2)) - x(:,3); % inequality constraint #2
ceq = x(:,1).*x(:,2).*x(:,3) - 2; % equality constraint #1
end
  3 Comments
Sam Chak
Sam Chak on 22 Nov 2023
Upon reviewing your code, I noticed the absence of MATLAB's built-in PSO optimizer (particleswarm). Since I didn't devise the PSO algorithm myself, I am unable to discern the correctness of specific lines in your user-defined algorithm.
Furthermore, I find it challenging to comprehend your constraints without annotations. They lack a mathematical representation that would facilitate understanding among scientists, engineers, and mathematicians. This is why I advocate for defining nonlinear constraints for a vector collection in a singular function call (@nonlcon), as demonstrated above.
Regardless, let's decompose this into three learning outcomes:
  • Outcome 1: Minimize the cost of the battery optimization problem over the 24-hour period.
  • Outcome 2: Attain Outcome #1 through the application of the PSO method.
  • Outcome 3: Realize Outcome #2 by implementing a user-defined PSO algorithm.
For Outcome #1, attempt to solve the constrained optimization problem utilizing the patternsearch() and ga() solvers. Subsequently, compare the obtained results with those derived from the PSO solver.
For Outcome #2, address the optimization problem using the built-in particleswarm() solver, by transforming the constrained problem into an unconstrained one. If it yields results reasonably consistent with those obtained from patternsearch() and ga(), it suggests you are on the correct path. Additionally, consider refining the lower and upper bounds to enhance the result further.
Achieving Learning Outcome #3 poses a moderate challenge as you need to ensure the flawlessness or precise functionality of your user-defined PSO algorithm. If you attempt to directly pursue Outcome #3 without first addressing the first two learning outcomes, and even in the event that the user-defined PSO algorithm produces outputs, the ability to validate whether the algorithm genuinely identifies the minimum cost remains uncertain. Therefore, it is advisable to subject the user-defined PSO algorithm to testing, examining its optimization capabilities on both the Sphere function and the Rosenbrock function.
REENA
REENA on 26 Nov 2023
i tried the changes u have suggested but still getting error. can you please help me with atleast one of the variable. and can u suggest how to get MATLAB's built-in PSO optimizer (particleswarm)?

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!