Home > matpower7.1 > extras > syngrid > lib > sg_bus_type.m

sg_bus_type

PURPOSE ^

SG_BUS_TYPE generates a random bus type assignment

SYNOPSIS ^

function [Btypes] = sg_bus_type(link_ids, W)

DESCRIPTION ^

SG_BUS_TYPE  generates a random bus type assignment
   BTYPES = SG_BUS_TYPE(LINK_IDS, W)

   Input
       link_ids - matrix (m by 2) indicating the branch terminal buses
       W - Bus type entropy model (see SG_OPTIONS)

   Output
       Btypes - Bus type assignment vector (N by 1) of G/L/C (1/2/3)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [Btypes] = sg_bus_type(link_ids, W)
0002 %SG_BUS_TYPE  generates a random bus type assignment
0003 %   BTYPES = SG_BUS_TYPE(LINK_IDS, W)
0004 %
0005 %   Input
0006 %       link_ids - matrix (m by 2) indicating the branch terminal buses
0007 %       W - Bus type entropy model (see SG_OPTIONS)
0008 %
0009 %   Output
0010 %       Btypes - Bus type assignment vector (N by 1) of G/L/C (1/2/3)
0011 
0012 %   SynGrid
0013 %   Copyright (c) 2016-2018, Electric Power and Energy Systems (EPES) Research Lab
0014 %   by Zhifang Wang and Seyyed Hamid Elyas, Virginia Commonwealth University
0015 %
0016 %   This file is part of SynGrid.
0017 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0018 
0019 
0020 % global Entopy_W W_star Standard_value
0021 % link_ids=xlsread('RT_nestedSW_129.xlsx');
0022 if W==0
0023     Entopy_W=0;
0024     %     assignin('base','Entopy_W',Entopy_W);
0025 elseif W==1
0026     Entopy_W=1;
0027     %     assignin('base','Entopy_W',Entopy_W);
0028 end
0029 
0030 
0031 
0032 M=length(link_ids(:,1));
0033 N=max(max(link_ids));
0034 
0035 %%%%%%%% bus type ratio setting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0036 if N<2000
0037     G=0.23; L=0.55; C=0.22;    % for IEEE-300
0038 elseif N<10000
0039     G=0.33; L=0.44; C=0.23;   % for NYISO
0040 else
0041     G=0.2; L=0.4; C=0.4;      % for WECC
0042 end
0043 
0044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0045 Iter_PDF=2000;
0046 W_Matrix=[];
0047 History_Bus_Matrix=[];
0048 K=1;
0049 pop=[];
0050 Best=[];
0051 V1=[];
0052 V2=[];
0053 n=20;
0054 B=0.4;
0055 s=100;
0056 Ratio_Bus_Type=[G,L,C];
0057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0058 W_Matrix=PDF_Final(link_ids,Ratio_Bus_Type,History_Bus_Matrix,N,M,Iter_PDF,Entopy_W);    % this function generates bus type entropy for each sample of bus type assignment
0059 W_Mat=W_Matrix;
0060 W_Matrix=sort(W_Matrix);
0061 mean_value=mean(W_Matrix);
0062 Standard_value=std(W_Matrix);
0063 %%%%%%%%%%%%%%%%%%%%% generate PDF curve %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0064 % [yy,xx]=hist(W_Matrix,100);
0065 % yy=yy/Iter_PDF/(xx(2)-xx(1));
0066 % axes(handles.axes1)
0067 % bar(xx,yy)
0068 % xlabel('Bus type entropy');
0069 % ylabel('PDF');
0070 % hold on
0071 % pd = fitdist(W_Matrix,'Normal')
0072 % y = pdf(pd,W_Matrix);
0073 % plot(W_Matrix,y,'LineWidth',2)
0074 % hold off
0075 %%%%%%%%%%%%%%%%%%%%%%%%Normalized distanse (d)%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0076 % if Entopy_W==0                   % Entopy_W=0 for the first definition
0077 %     if log(N)<= 8
0078 %         d_parameter=(-3.98*(log(N)))+15.85;
0079 %     else
0080 %         d_parameter=(-8.67*(10^(-9))*((log(N))^10.19));
0081 %     end
0082 % else                              % Entopy_W=1 for the second definition
0083 %     if log(N)<= 8
0084 %         d_parameter=(-4.19*(log(N)))+16.48;
0085 %     else
0086 %         d_parameter=-6.77*10^(-9)*(log(N))^10.52;
0087 %     end
0088 % end
0089 if Entopy_W==0                   % Entopy_W=0 for the first definition
0090     if log(N)<= 8
0091         d_parameter=(-1.39*(log(N)))+6.79;
0092     else
0093         d_parameter=(-6.003*(10^(-14))*((log(N))^15.48));
0094     end
0095 else                              % Entopy_W=1 for the second definition
0096     if log(N)<= 8
0097         d_parameter=(-1.748*(log(N)))+8.576;
0098     else
0099         d_parameter=-6.053*10^(-22)*(log(N))^24.1;
0100     end
0101 end
0102 W_star=(d_parameter*Standard_value)+mean_value;
0103 
0104 
0105 M=length(link_ids(:,1));
0106 N=max(max(link_ids));
0107 if N<2000
0108     G=0.23; L=0.55; C=0.22;
0109 else
0110     if N<10000
0111         G=0.33; L=0.44; C=0.23;
0112     else
0113         G=0.2; L=0.4; C=0.4;
0114     end
0115 end
0116 % Iter_PDF=str2double(get(handles.edit7,'String'));
0117 Iter_PDF=2000;
0118 % Max_iter=str2double(get(handles.edit8,'String'));
0119 W_Matrix=[];
0120 
0121 K=1;
0122 pop=[];
0123 Best=[];
0124 V1=[];
0125 V2=[];
0126 n=20;
0127 B=0.4;
0128 s=100;
0129 Ratio_Bus_Type=[G,L,C];
0130 History_Bus_Matrix=[];
0131 for i=1:n
0132     Bus_Matrix=bus_type_assignment(link_ids,Ratio_Bus_Type,History_Bus_Matrix,N);
0133     History_Bus_Matrix=[History_Bus_Matrix;Bus_Matrix];
0134     pop=[pop;Bus_Matrix];
0135 end
0136 Ratio_L_Matrix=rate_link(link_ids,Bus_Matrix,M,pop);
0137 
0138 [W,pop1]=Affinity(M,N,pop,Ratio_Bus_Type,Ratio_L_Matrix,W_star,Entopy_W);
0139 
0140 %--------------------------------------------------------------------------
0141 %--------------------------------------------------------------------------
0142 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Body algorithm   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0143 Iter_Optimization=1;opt_iter=1;
0144 while opt_iter==1
0145     %Iter_Optimization
0146     pop_final=[];
0147     %--------------------- Copy from the initial population -------------------
0148     pop2_clone=Clonal_Operator(pop1);
0149     %----------------------------- Mutation -----------------------------------
0150     pop3=Mutation_Operator(N,pop2_clone);
0151     popnew=vertcat(pop3,pop2_clone);
0152     m=size(popnew,1);
0153     %------------------------- add new solution -------------------------------
0154     ccc =0;
0155     for i=1:m/10
0156         Bus_Matrix=bus_type_assignment(link_ids,Ratio_Bus_Type,History_Bus_Matrix,N);
0157         ccc = ccc+1;
0158         History_Bus_Matrix=[History_Bus_Matrix;Bus_Matrix];
0159         pop4(i,:)=Bus_Matrix;
0160     end
0161     %------------------------- Select the best solutions ----------------------
0162     Pnew=vertcat(pop4,pop3,pop2_clone);
0163     pop=[];
0164     pop=Pnew;
0165     Ratio_L_Matrix=rate_link(link_ids,Bus_Matrix,M,pop);
0166     
0167     [W,pop1,Wbb]=Affinity(M,N,pop,Ratio_Bus_Type,Ratio_L_Matrix,W_star,Entopy_W);
0168     pop_final=pop1((1:n),:);
0169     Wbb;
0170     if N < 50
0171         if Entopy_W == 0
0172             Criteria = Standard_value/10;
0173         elseif Entopy_W == 1
0174             Criteria = Standard_value/2;
0175         end
0176     else
0177         Criteria = Standard_value/1000 ;
0178     end
0179     if W(1)< Criteria
0180         Best=[Best;pop_final(1,:)];
0181         opt_iter=0;
0182     end
0183     V1=[V1,W(1)];
0184     V2=[V2,Iter_Optimization];
0185     pop1=[];
0186     pop1=pop_final;
0187     Iter_Optimization=Iter_Optimization+1;
0188 end
0189 WW = W(1);
0190 %%%%%%%%%%%%%%%%%%%%%End of optimization process %%%%%%%%%%%%%%%%%%%%%%%%%%
0191 % Btypes(:,1)=1:length(Best');
0192 Btypes=Best';
0193 % save  Btypes Btypes;
0194 % axes(handles.axes2)
0195 % plot(V2,V1)
0196 % xlabel('Iteration');
0197 % ylabel('Error');
0198 
0199 Best_Bus_Type_Assignments = Best;
0200 L=size(Best_Bus_Type_Assignments,1);
0201 Ratio_L_Matrix=[];
0202 for k=1 : L
0203     Bus_Matrix=Best_Bus_Type_Assignments(k,:);
0204     Link_CC=0;
0205     Link_LL=0;
0206     Link_GG=0;
0207     Link_LC=0;
0208     Link_GC=0;
0209     Link_GL=0;
0210     
0211     for i=1:M
0212         T1=link_ids(i,1);
0213         T2=link_ids(i,2);
0214         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==1
0215             Link_GG=Link_GG+1;
0216         end
0217         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==2
0218             Link_GL=Link_GL+1;
0219         end
0220         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==3
0221             Link_GC=Link_GC+1;
0222         end
0223         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==1
0224             Link_GL=Link_GL+1;
0225         end
0226         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==2
0227             Link_LL=Link_LL+1;
0228         end
0229         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==3
0230             Link_LC=Link_LC+1;
0231         end
0232         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==1
0233             Link_GC=Link_GC+1;
0234         end
0235         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==2
0236             Link_LC=Link_LC+1;
0237         end
0238         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==3
0239             Link_CC=Link_CC+1;
0240         end
0241     end
0242     Rate_Link_GG=Link_GG/M;
0243     Rate_Link_LL=Link_LL/M;
0244     Rate_Link_CC=Link_CC/M;
0245     Rate_Link_GL=Link_GL/M;
0246     Rate_Link_GC=Link_GC/M;
0247     Rate_Link_LC=Link_LC/M;
0248     
0249     Ratio_Link=[Rate_Link_GG,Rate_Link_LL,Rate_Link_CC,Rate_Link_GL,Rate_Link_GC,Rate_Link_LC];
0250     Ratio_L_Matrix=[Ratio_L_Matrix;Ratio_Link];
0251 end
0252 for i=1:L
0253     W(i)=0;
0254     if Entopy_W==0
0255         X1=0;
0256         for f=1 : 3             %%% Three different bus types : G/L/C %%%
0257             X1=X1+(Ratio_Bus_Type(1,f)*log(Ratio_Bus_Type(1,f)));
0258         end
0259         X2=0;
0260         for f=1 : 6          %%% Six different link types : GG/LL/CC/GL/GC/LC %%%
0261             if Ratio_Link(1,f) ~=0
0262                 X2=X2+(Ratio_Link(1,f)*log(Ratio_Link(1,f)));
0263             else
0264                 X2=X2;
0265             end
0266         end
0267     end
0268     if Entopy_W==1
0269         X1=0;
0270         for f=1 : 3             %%% Three different bus types : G/L/C %%%
0271             X1=X1+(log(Ratio_Bus_Type(1,f))*(Ratio_Bus_Type(1,f)*N));
0272         end
0273         X2=0;
0274         for f=1 : 6          %%% Six different link types : GG/LL/CC/GL/GC/LC %%%
0275             if Ratio_Link(1,f) ~=0
0276                 X2=X2+(log(Ratio_Link(1,f))*(Ratio_Link(1,f)*M));
0277             else
0278                 X2=X2;
0279             end
0280         end
0281     end
0282     W(i)=-(X1+X2);
0283 end
0284 Best_W=W(1,1:L)';
0285 % assignin('base','Best_Bus_Type_Assignments',Best_Bus_Type_Assignments);
0286 % assignin('base','Best_W',Best_W);
0287 % save Best_W Best_W
0288 
0289 %%
0290 
0291 function W_Matrix=PDF_Final(link_ids,Ratio_Bus_Type,History_Bus_Matrix,N,M,Iter_PDF,Entopy_W)
0292 
0293 BBus =  (1:N)';
0294 BBranch = link_ids;
0295 Bus_degs=zeros(length(BBus(:,1)),1);
0296 for Bii=1:length(BBus(:,1))
0297     count=0;
0298     for Li=1:length(BBranch)
0299         if BBranch(Li,1)==BBus(Bii,1)
0300             count=count+1;
0301         elseif BBranch(Li,2)==BBus(Bii,1)
0302             count=count+1;
0303         end
0304     end
0305     Bus_degs(Bii)=count;
0306 end
0307 
0308 
0309 W_Matrix=[];
0310 
0311 K=1;
0312 for iter_PDF=1:Iter_PDF
0313     %iter_PDF
0314     k=1;
0315     Bus_Matrix=[];
0316 
0317     G= round(N*Ratio_Bus_Type(1,1));             %%%%%%% total Number of Generator Bus %%%%%%%
0318     while K>0
0319         Bus_Matrix= zeros(1,N);
0320         C= round(N*Ratio_Bus_Type(1,3));  %%%%%% total Number of Connection Bus %%%%%%%
0321         for i=1:C
0322             T=round(1+rand*(N-1));
0323 %             Bus_Matrix(1,T)
0324 %             Bus_degs(T,1)
0325             if Bus_Matrix(1,T)==0 && Bus_degs(T,1) ~= 1;
0326                 Bus_Matrix(1,T)=3;
0327             else
0328                 while Bus_Matrix(1,T)~=0 && Bus_degs(T) == 1
0329                     T=round(1+rand*(N-1));
0330                 end
0331                 Bus_Matrix(1,T)=3;
0332             end
0333         end
0334         
0335         for i=1:G
0336             T=round(1+rand*(N-1));
0337             if Bus_Matrix(1,T)==0
0338                 Bus_Matrix(1,T)=1;
0339             else
0340                 while Bus_Matrix(1,T)~=0
0341                     T=round(1+rand*(N-1));
0342                 end
0343                 Bus_Matrix(1,T)=1;
0344             end
0345         end
0346         % L= round(N*Ratio_Bus_Type(1,2));             %%%%%%% total Number of Generator Bus %%%%%%%
0347         % for i=1:L
0348         %     T=round(1+rand*(N-1));
0349         %     if Bus_Matrix(1,T)==0
0350         %         Bus_Matrix(1,T)=2;
0351         %     else
0352         %         while Bus_Matrix(1,T)~=0
0353         %               T=round(1+rand*(N-1));
0354         %         end
0355         %          Bus_Matrix(1,T)=2;
0356         %     end
0357         % end
0358         for i=1:N
0359             if Bus_Matrix(1,i)==0
0360                 %         Bus_Matrix(1,i)=3;
0361                 Bus_Matrix(1,i)=2;
0362             end
0363         end
0364         ind=strfind(char(reshape(History_Bus_Matrix',1,[])),char(Bus_Matrix));
0365         K=size(ind);
0366     end
0367     History_Bus_Matrix=[History_Bus_Matrix;Bus_Matrix];
0368     K=1;
0369     Link_CC=0;
0370     Link_LL=0;
0371     Link_GG=0;
0372     Link_LC=0;
0373     Link_GC=0;
0374     Link_GL=0;
0375     for i=1:M
0376         T1=link_ids(i,1);
0377         T2=link_ids(i,2);
0378         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==1
0379             Link_GG=Link_GG+1;
0380         end
0381         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==2
0382             Link_GL=Link_GL+1;
0383         end
0384         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==3
0385             Link_GC=Link_GC+1;
0386         end
0387         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==1
0388             Link_GL=Link_GL+1;
0389         end
0390         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==2
0391             Link_LL=Link_LL+1;
0392         end
0393         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==3
0394             Link_LC=Link_LC+1;
0395         end
0396         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==1
0397             Link_GC=Link_GC+1;
0398         end
0399         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==2
0400             Link_LC=Link_LC+1;
0401         end
0402         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==3
0403             Link_CC=Link_CC+1;
0404         end
0405     end
0406     Rate_Link_GG=Link_GG/M;
0407     Rate_Link_LL=Link_LL/M;
0408     Rate_Link_CC=Link_CC/M;
0409     Rate_Link_GL=Link_GL/M;
0410     Rate_Link_GC=Link_GC/M;
0411     Rate_Link_LC=Link_LC/M;
0412     Ratio_Link=[Rate_Link_GG,Rate_Link_LL,Rate_Link_CC,Rate_Link_GL,Rate_Link_GC,Rate_Link_LC];
0413     
0414     
0415     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0416     if Entopy_W==0
0417         X1=0;
0418         for f=1 : 3             %%% Three different bus types : G/L/C %%%
0419             X1=X1+(log(Ratio_Bus_Type(1,f))*Ratio_Bus_Type(1,f));
0420         end
0421         X2=0;
0422         for f=1 : 6          %%% Six different link types : GG/LL/CC/GL/GC/LC %%%
0423             if Ratio_Link(1,f) ~=0
0424                 X2=X2+(log(Ratio_Link(1,f))*Ratio_Link(1,f));
0425             else
0426                 X2=X2;
0427             end
0428         end
0429     end
0430     
0431     if Entopy_W==1
0432         X1=0;
0433         for f=1 : 3             %%% Three different bus types : G/L/C %%%
0434             X1=X1+(log(Ratio_Bus_Type(1,f))*(Ratio_Bus_Type(1,f)*N));
0435         end
0436         X2=0;
0437         for f=1 : 6          %%% Six different link types : GG/LL/CC/GL/GC/LC %%%
0438             if Ratio_Link(1,f) ~=0
0439                 X2=X2+(log(Ratio_Link(1,f))*(Ratio_Link(1,f)*M));
0440             else
0441                 X2=X2;
0442             end
0443         end
0444     end
0445     
0446     W=-(X1+X2);
0447     
0448     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0449     W_Matrix=[W_Matrix;W];
0450 end
0451 
0452 %%
0453 function Bus_Matrix=bus_type_assignment(link_ids,Ratio_Bus_Type,History_Bus_Matrix,N)
0454 %--------------------------------------------------------------------------
0455 % bus_type_assignment function generates a random bus type assignment.
0456 % In order to prevent re-use of previous bus type assignment this function
0457 % considers the History_Bus_Matrix to record all previous generated ones.
0458 %
0459 % Output:   for example: [2 2 3 1 3 2 1  ..... 3 1 2 2 3]
0460 % The output is going to be an array (1 by N) including just 1,2 or 3 and
0461 % as a candidate for bus type assignment. In this array 1: presents
0462 % generation bus    2: presents load bus     3: presnets connection bus
0463 %--------------------------------------------------------------------------
0464 
0465 
0466 K=1;                                   %%%% Control parameter
0467 Bus_Matrix=[];                         %%%% storage matrix
0468 
0469 BBus =  (1:N)';
0470 BBranch = link_ids;
0471 Bus_degs=zeros(length(BBus(:,1)),1);
0472 for Bii=1:length(BBus(:,1))
0473     count=0;
0474     for Li=1:length(BBranch)
0475         if BBranch(Li,1)==BBus(Bii,1)
0476             count=count+1;
0477         elseif BBranch(Li,2)==BBus(Bii,1)
0478             count=count+1;
0479         end
0480     end
0481     Bus_degs(Bii)=count;
0482 end
0483 
0484 %--------------------------------------------------------------------------
0485 Node_no1 = find(Bus_degs(:,1) ~= 1);
0486 
0487 while K>0
0488     Bus_Matrix= zeros(1,N);
0489     CC= round(N*Ratio_Bus_Type(1,2));     %%%% Total Number of Load Bus
0490     for i=1:CC
0491         T = sg_datasample(Node_no1,1);
0492 %         T=round(1+rand*(N-1));
0493         if Bus_Matrix(1,T)==0
0494             Bus_Matrix(1,T)=3;
0495         else
0496             while Bus_Matrix(1,T)~=0
0497                 T = sg_datasample(Node_no1,1);
0498 %                 T=round(1+rand*(N-1));
0499             end
0500             Bus_Matrix(1,T)=3;
0501         end
0502     end
0503     
0504     
0505     G= round(N*Ratio_Bus_Type(1,1));       %%%% Total Number of Generator Bus
0506     %--------------------------------------------------------------------------
0507     %%%%%%%%%% Random procedure to determine the generation buses %%%%%%%%%%%%%
0508     for i=1:G
0509         T=round(1+rand*(N-1));
0510         if Bus_Matrix(1,T)==0
0511             Bus_Matrix(1,T)=1;
0512         else
0513             while Bus_Matrix(1,T)~=0
0514                 T=round(1+rand*(N-1));
0515             end
0516             Bus_Matrix(1,T)=1;
0517         end
0518     end
0519     %--------------------------------------------------------------------------
0520 %     %%%%%%%%%% Random procedure to determine the load buses %%%%%%%%%%%%%%%%%%%
0521 %
0522 %     L= round(N*Ratio_Bus_Type(1,2));     %%%% Total Number of Load Bus
0523 %
0524 %     for i=1:L
0525 %         T=round(1+rand*(N-1));
0526 %         if Bus_Matrix(1,T)==0
0527 %             Bus_Matrix(1,T)=2;
0528 %         else
0529 %             while Bus_Matrix(1,T)~=0
0530 %                 T=round(1+rand*(N-1));
0531 %             end
0532 %             Bus_Matrix(1,T)=2;
0533 %         end
0534 %     end
0535     %--------------------------------------------------------------------------
0536     %%%%%%%%%% Consider remained buses as connection buses  %%%%%%%%%%%%%%%%%%%
0537     
0538     for i=1:N
0539         if Bus_Matrix(1,i)==0
0540             Bus_Matrix(1,i)=2;
0541         end
0542     end
0543     %--------------------------------------------------------------------------
0544     %%%%%%%%%%%%% Check the generated bus type assignment   %%%%%%%%%%%%%%%%%%%
0545     
0546     ind=strfind(char(reshape(History_Bus_Matrix',1,[])),char(Bus_Matrix));
0547     K=size(ind);
0548     %--------------------------------------------------------------------------
0549 end
0550 
0551 %%
0552 function Ratio_L_Matrix=rate_link(link_ids,Bus_Matrix,M,pop)
0553 %--------------------------------------------------------------------------
0554 % rate_link provides the ratio of six different link types.
0555 %
0556 %    Link_CC :  total number of links between two connection buses
0557 %    Link_LL :  total number of links between two load buses
0558 %    Link_GG :  total number of links between two generation buses
0559 %    Link_LC :  total number of links between two connection and load buses
0560 %    Link_GC :  total number of links between two connection and generation buses
0561 %    Link_GL :  total number of links between two generation and load buses
0562 %
0563 %
0564 % Output: The output is a matrix which each single row presents the link
0565 % ratios for each random generated bus type assignments.
0566 %
0567 %--------------------------------------------------------------------------
0568 
0569 Ratio_L_Matrix=[];
0570 L=size(pop,1);
0571 for k=1 : L
0572     Bus_Matrix=pop(k,:);
0573     Link_CC=0;
0574     Link_LL=0;
0575     Link_GG=0;
0576     Link_LC=0;
0577     Link_GC=0;
0578     Link_GL=0;
0579     
0580     for i=1:M
0581         T1=link_ids(i,1);
0582         T2=link_ids(i,2);
0583         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==1
0584             Link_GG=Link_GG+1;
0585         end
0586         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==2
0587             Link_GL=Link_GL+1;
0588         end
0589         if Bus_Matrix(1,T1)==1 && Bus_Matrix(1,T2)==3
0590             Link_GC=Link_GC+1;
0591         end
0592         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==1
0593             Link_GL=Link_GL+1;
0594         end
0595         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==2
0596             Link_LL=Link_LL+1;
0597         end
0598         if Bus_Matrix(1,T1)==2 && Bus_Matrix(1,T2)==3
0599             Link_LC=Link_LC+1;
0600         end
0601         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==1
0602             Link_GC=Link_GC+1;
0603         end
0604         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==2
0605             Link_LC=Link_LC+1;
0606         end
0607         if Bus_Matrix(1,T1)==3 && Bus_Matrix(1,T2)==3
0608             Link_CC=Link_CC+1;
0609         end
0610     end
0611     Rate_Link_GG=Link_GG/M;
0612     Rate_Link_LL=Link_LL/M;
0613     Rate_Link_CC=Link_CC/M;
0614     Rate_Link_GL=Link_GL/M;
0615     Rate_Link_GC=Link_GC/M;
0616     Rate_Link_LC=Link_LC/M;
0617     
0618     Ratio_Link=[Rate_Link_GG,Rate_Link_LL,Rate_Link_CC,Rate_Link_GL,Rate_Link_GC,Rate_Link_LC];
0619     Ratio_L_Matrix=[Ratio_L_Matrix;Ratio_Link];
0620 end
0621 
0622 %%
0623 function [W,pop1,Wbb]=Affinity(M,N,pop,Ratio_Bus_Type,Ratio_L_Matrix,W_star,Entopy_W)
0624 %--------------------------------------------------------------------------
0625 %
0626 % Affinity is a function which sorts the population "pop" from the best one
0627 % to the worst one.
0628 %
0629 % Objective function :    Min (abs(W_star-W0))
0630 %
0631 % X=-sigma[Ratio_bus_types*log(Ratio_bus_types)]-sigma[Ratio_links*log(Ratio_links)]
0632 %
0633 
0634 %In other words we try to find the minimum value for objective function, say zero, to find
0635 %some random bus type assignment which have maximum similarity to W_star
0636 %
0637 %
0638 %Output: pop1 is the sorted version of pop from the maximum to minimum similarity
0639 %
0640 %--------------------------------------------------------------------------
0641 
0642 L=size(pop,1);
0643 
0644 for i=1:L
0645     W(i)=0;
0646     
0647     if Entopy_W==0
0648         X1=0;
0649         for f=1 : 3          %%% Three different bus types : G/L/C
0650             Ratio_Bus_Type(1,f);
0651             X1=X1+(Ratio_Bus_Type(1,f)*log(Ratio_Bus_Type(1,f)));
0652         end
0653         X2=0;
0654         
0655         for d=1 : 6          %%% Six different link types : GG/LL/CC/GL/GC/LC
0656             
0657             Ratio_L_Matrix(i,d);
0658             X2=X2+(Ratio_L_Matrix(i,d)*log(Ratio_L_Matrix(i,d))) ;
0659         end
0660     end
0661     if Entopy_W==1
0662         X1=0;
0663         for f=1 : 3             %%% Three different bus types : G/L/C %%%
0664             X1=X1+(log(Ratio_Bus_Type(1,f))*(Ratio_Bus_Type(1,f)*N));
0665         end
0666         X2=0;
0667         
0668         for d=1 : 6          %%% Six different link types : GG/LL/CC/GL/GC/LC
0669             Ratio_L_Matrix(i,d);
0670             X2=X2+(log(Ratio_L_Matrix(i,d))*(Ratio_L_Matrix(i,d)*M));
0671         end
0672     end
0673     
0674     X=-(X1+X2);
0675     Wbb (i) = X;
0676     W(i)=abs(W_star-X);
0677 end
0678 [W,ind]=sort(W);
0679 Wbb = Wbb(ind);
0680 pop1=pop(ind,:);
0681 
0682 %%
0683 function pop2_clone=Clonal_Operator(pop1)
0684 %--------------------------------------------------------------------------
0685 % Clone_Operator is used to copy the each single row of pop1. the pop1 is
0686 % sorted from the best to worst. So the number of copies will be decreased
0687 % from the first row, as the best one, to the last row, as the worst one.
0688 %
0689 % "B" and "s" are two parameters in the loop to generate different number of
0690 % copies. The optimum values for 'B" and "s" are determined experimentally.
0691 %
0692 % Output: the output "pop2_clone" is a matrix including C1 copies from the
0693 % first row of pop1, C2 copies from the second row of pop1, .... , Cn copies
0694 % from the last row of pop1.
0695 
0696 %               C1>C2>C3> ... >Cn
0697 %
0698 %--------------------------------------------------------------------------
0699 
0700 B=0.4;
0701 s=100;
0702 X1=[];
0703 L=size(pop1,1);
0704 for i=1:L
0705     nc=round(B*s/i);
0706     for j=1:nc
0707         X(j,:)=pop1(i,:);
0708     end
0709     % X
0710     X1=[X1;X];
0711     X=[];
0712 end
0713 pop2_clone=X1;
0714 
0715 %%
0716 function pop3=Mutation_Operator(N,pop2_clone)
0717 %--------------------------------------------------------------------------
0718 % Mutation operator mutates the output of Clonal_operator. this function
0719 % plays a very important role in local search and to skip the local
0720 % optimum solutions.
0721 %
0722 % pop2_clone is a sorted matrix from the best to worst. So from the first
0723 % row (the best one) to the last row (the worst one) we should increas the
0724 % effect of mutation operator on random population.
0725 % The first row of pop2_clone -----> minimum change
0726 %  .
0727 %  .
0728 %  .
0729 % The last row of pop2_clone -----> maximum change
0730 %
0731 %
0732 % Output: the output is pop3 which includes mutated random bus type
0733 % assignments. The size of pop3 and pop2_clone are the same.
0734 %
0735 %
0736 %--------------------------------------------------------------------------
0737 
0738 L=size(pop2_clone,1);
0739 pop3=[];
0740 
0741 for b=1:10
0742     for j=round(((b-1)*(L/10)))+1 : round(b*(L/10))
0743         for c=1:b
0744             X=round(1+rand*(N-1));
0745             mr=rand;
0746             pop2_clone(j,X);
0747             if mr<0.50
0748 %             if mr<0.33
0749                 pop2_clone(j,X)=1;
0750             else
0751 %             if mr>0.33 && mr<0.66
0752                 pop2_clone(j,X)=2;
0753             end
0754 %             if mr>0.66
0755 %                 pop2_clone(j,X)=1;
0756 %             end
0757         end
0758         pop3=[pop3;pop2_clone(j,:)];
0759     end
0760 end
0761 pop3;

Generated on Fri 09-Oct-2020 11:21:31 by m2html © 2005