%+++++++++++++++++++ Data base of the power system ++++++++++++++++++++++++
% The following file contains information about the topology of the power
% system such as the bus and line matrix
data_39;
% original matrix and generation buses
bus_o=bus; line_o=line;
slack=find(bus(:,10)==1); % Slack bus
PV=find(bus(:,10)==2); % Generation buses PV
Bgen=vertcat(slack,PV); % Slack and PV buses
PQ=find(bus(:,10)==3); % Load buses
% +++++++++++++++ Parameters of the optimization algorithm ++++++++++++++++
pop = 210; % Size of the population
n_itera = 35; % Number of iterations of the optimization algorithm
Vmin=0.95; % Minimum value of voltage for the generators
Vmax=1.05; % Maximum value of voltage for the generators
mini_tap = 0.95; % Minimum value of the TAP
maxi_tap = 1.05; % Maximum value of the TAP
Smin=-0.5; % Minimum Shunt value
Smax=0.5; % Maximum Sunt value
pos_Shunt = find( bus(:,11) ~= 0); % Positions of Shunts in bus matrix
pos_tap = find( line(:,6) ~= 0); % Positions of TAPs in line matrix
tap_o = line(pos_tap,6); % Original values of TAPs
Shunt_o = bus(pos_Shunt,9); % Original values of Shunts
n_tap = length(pos_tap); % number of TAPs
n_Shunt = length(pos_Shunt); % number of Shunts
n_nodos = length(bus(:,1)); % number of buses of the power system
% ++++++++++++++++ First: Run power flow for the base case ++++++++++++++++
% Store V and theta of the base case
[V_o,Theta_o,~] = PowerFlowClassical(bus_o,line_o);
% +++++++++++++++++++Compute the active power lossess++++++++++++++++++++++
nbranch=length(line_o(:,1));
FromNode=line_o(:,1);
ToNode=line_o(:,2);
for k=1:nbranch
a(k)=line_o(k,6);
if a(k)==0 % in this case, we are analyzing lines
Zpq(k)=line_o(k,3)+1i*line_o(k,4); % impedance of the transmission line
Ypq(k)=Zpq(k)^-1; % admittance of the transmission linee
gpq(k)=real(Ypq(k)); % conductance of the transmission line
% Active power loss of the corresponding line
Llpq(k)=gpq(k)*(V_o(FromNode(k))^2 +V_o(ToNode(k))^2 -2*V_o(FromNode(k))*V_o(ToNode(k))*cos(Theta_o(FromNode(k))-Theta_o(ToNode(k))));
end
end
% Total active power lossess
Plosses=sum(Llpq);
% +++++++++++++++++++++++++++ Optimum power flow ++++++++++++++++++++++++++
% Start the population
for k=1:n_tap % Start the TAP population
x_tap(:,k) = mini_tap +(maxi_tap - mini_tap)*(0.1*floor((10*rand(pop,1))));
end
for k=1:n_Shunt % Start the Shunt population
x_shunt(:,k) = Smin +(Smax - Smin)*(0.1*floor((10*rand(pop,1))));
end
for k=1:length(Bgen) % Start the population of voltage from generators
x_vg(:,k) = Vmin +(Vmax - Vmin)*(0.1*floor((10*rand(pop,1))));
end
% JAYA algorithm
for k=1:n_itera
% with the new values of the TAPs, Shunts and VGs recompute V and Ybus
% Modify line and bus matrix
for p=1:pop
for q=1:n_tap
r=pos_tap(q);
line(r,6)=x_tap(p,q); % modification of line matrix acording to the new TAP values
end; clear r
for qa=1:n_Shunt
r=pos_Shunt(qa);
bus(r,9)=x_shunt(p,qa); % modification of bus matrix according to the new Shunt values
end; clear r
for qb=1:length(Bgen)
r=Bgen(qb);
bus(r,2)=x_vg(p,qb); % modification of bus matrix according to the new VG values
end
% With the new line and bus matrix run power flow
[V_n,Theta_n,~] = PowerFlowClassical(bus,line);
% Objective function
[F,~] = ObjectiveFunction(V_n,line_o,Bgen,Theta_n,nbranch,FromNode,ToNode,PQ);
Ofun=F; Obfun(k,p)=F;
end
% Define the new values of the desition variables: VGs, TAPs and Shunts
[x1,x2,x3] = UpdateDesitionVariables(Obfun(k,:),x_tap,x_shunt,x_vg);
% In this section, correct the particles that are surpassing the
% minimum/ maximum established values
% TAP values
xselect=round(100*x1); %discretize TAP values
x1=xselect/100;
x1a=x1;
for p=1:n_tap
for i=1:pop
if x1(i,p)<mini_tap
x1a(i,p)=mini_tap;
end
if x1(i,p)>maxi_tap
x1a(i,p)=maxi_tap;
end
end
end
% Shunt elements
x2a=x2;
for p=1:n_Shunt
for i=1:pop
if x2(i,p)<Smin
x2a(i,p)=Smin;
end
if x2(i,p)>Smax
x2a(i,p)=Smax;
end
end
end
% Voltages from generators
x3a=x3;
for p=1:length(Bgen)
for i=1:pop
if x3(i,p)<Vmin
x3a(i,p)=Vmin;
end
if x3(i,p)>Vmax
x3a(i,p)=Vmax;
end
end
end
x_tap=x1a; x_shunt=x2a; x_vg=x3a;
% With the corrected updated values, modify bus and line matrix
for p=1:pop
for q=1:n_tap
r=pos_tap(q);
line(r,6)=x_tap(p,q); % modification of line matrix acording to the new corrected TAP values
end; clear r
for qa=1:n_Shunt
r=pos_Shunt(qa);
bus(r,9)=x_shunt(p,qa); % modification of bus matrix according to the new corrected Shunt values
end; clear r
for qb=1:length(Bgen)
r=Bgen(qb);
bus(r,2)=x_vg(p,qb); % modification of bus matrix according to the new corrected VG values
end
% Run Newton Raphson
[V_n,Theta_n,~] = PowerFlowClassical(bus,line);
% Objective function
[Fnew,~] = ObjectiveFunction(V_n,line,Bgen,Theta_n,nbranch,FromNode,ToNode,PQ);
Obfunnew(k,p)=Fnew;
end
% Store values of TAPS, SHUNTS AND VGS for every iteration
XTAP(k,:,:)=x1a; XSHUNT(k,:,:)=x2a; XVG(k,:,:)=x3a;
if k>1
for i=1:pop
if(Obfunnew(k-1,i)<Obfunnew(k,i))
x_tap(i,:)=XTAP(k-1,i,:); x_shunt(i,:)=XSHUNT(k-1,i,:); x_vg(i,:)=XVG(k-1,i,:);
Obfunnew(k,i)=Obfunnew(k-1,i);
% In case that we needed the values of the previos iterations,
% we will have to change the storing matrix XTAP XSHUNT and XVG
XTAP(k,i,:)=x_tap(i,:); XSHUNT(k,i,:)=x_shunt(i,:); XVG(k,i,:)=x_vg(i,:);
end
end
end
% best solution at each iteration
bsof(k)=min(Obfunnew(k,:));
% Find the values of TAPs, Shunts and VGs associated to the best solution
for i=1:pop
if bsof(k)==Obfunnew(k,i)
xitap(k,:)=x_tap(i,:); % TAP values associate to the best solution
xishunt(k,:)=x_shunt(i,:); % Shunt values associate to the best solution
xivg(k,:)=x_vg(i,:); % VG values associate to the best solution
end
end
end
% ++++++++++++++++++++++++++++++++++Solution ++++++++++++++++++++++++++++++
% once the optimization algorithm has sttoped, run power flow with the
% solutions provided
% First, we modify line and bus
for q=1:n_tap
r=pos_tap(q);
line(r,6)=xitap(end,q); % modification of line matrix acording to the new corrected TAP values
end; clear r
for qa=1:n_Shunt
r=pos_Shunt(qa);
bus(r,9)=xishunt(end,qa); % modification of bus matrix according to the new corrected Shunt values
end; clear r
for qb=1:length(Bgen)
r=Bgen(qb);
bus(r,2)=xivg(end,qb); % modification of bus matrix according to the new corrected VG values
end
[Vs,Thetas,~] = PowerFlowClassical(bus,line);
[Fobjective,Pls] = ObjectiveFunction(Vs,line,Bgen,Thetas,nbranch,FromNode,ToNode,PQ);
% ++++++++++++++++++++++++++++++ Print results ++++++++++++++++++++++++++++
disp('OPTIMIZACI覰 MEDIANTE ALGORITMO JAYA')
disp(' ')
disp(' ')
disp(' VOLTAGE ANGLE ')
disp(' ----------------- ---------------- ')
disp(' BUS Orig JAYA Orig JAYA ')
disp(' ')
display_1=[bus(:,1) V_o Vs Theta_o*(180/pi) Thetas*(180/pi)];
disp(display_1)
figure (1)
plot(bsof,'r'); xlabel('Iterations'); ylabel('Function value')
figure (2)
title('Voltage profile')
for k=1:n_nodos
Lim1(k)=0.95;
Lim2(k)=1.05;
end
plot(V_o); hold on; plot(Vs); hold on; plot(Lim1,'--k'); hold on; plot(Lim2,'--k'); xlabel('# bus'); ylabel('Magnitude (pu)')
legend('Voltage base case','Voltage for the optimum solution')
figure (3)
title('Active power lossess')
y=[Plosses,Pls];
c=categorical({'Base case','Optimum solution'});
bar(c,y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)
ylabel('Total active power lossess (pu)')
figure (4)
title('TAP values comparison')
plot(tap_o); hold on; plot(xitap(end,:)); xlabel('# Transformer'); ylabel('TAP value (pu)')
legend('Base case','Optimum solution')
%+++++++++++++++++++ Data base of the power system ++++++++++++++++++++++++
% The following file contains information about the topology of the power
% system such as the bus and line matrix
data_39;
% original matrix and generation buses
bus_o=bus; line_o=line;
slack=find(bus(:,10)==1); % Slack bus
PV=find(bus(:,10)==2); % Generation buses PV
Bgen=vertcat(slack,PV); % Slack and PV buses
PQ=find(bus(:,10)==3); % Load buses
% +++++++++++++++ Parameters of the optimization algorithm ++++++++++++++++
pop = 210; % Size of the population
n_itera = 35; % Number of iterations of the optimization algorithm
Vmin=0.95; % Minimum value of voltage for the generators
Vmax=1.05; % Maximum value of voltage for the generators
mini_tap = 0.95; % Minimum value of the TAP
maxi_tap = 1.05; % Maximum value of the TAP
Smin=-0.5; % Minimum Shunt value
Smax=0.5; % Maximum Sunt value
pos_Shunt = find( bus(:,11) ~= 0); % Positions of Shunts in bus matrix
pos_tap = find( line(:,6) ~= 0); % Positions of TAPs in line matrix
tap_o = line(pos_tap,6); % Original values of TAPs
Shunt_o = bus(pos_Shunt,9); % Original values of Shunts
n_tap = length(pos_tap); % number of TAPs
n_Shunt = length(pos_Shunt); % number of Shunts
n_nodos = length(bus(:,1)); % number of buses of the power system
% ++++++++++++++++ First: Run power flow for the base case ++++++++++++++++