0001 function [Btypes] = sg_bus_type(link_ids, W)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 if W==0
0023 Entopy_W=0;
0024
0025 elseif W==1
0026 Entopy_W=1;
0027
0028 end
0029
0030
0031
0032 M=length(link_ids(:,1));
0033 N=max(max(link_ids));
0034
0035
0036 if N<2000
0037 G=0.23; L=0.55; C=0.22;
0038 elseif N<10000
0039 G=0.33; L=0.44; C=0.23;
0040 else
0041 G=0.2; L=0.4; C=0.4;
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);
0059 W_Mat=W_Matrix;
0060 W_Matrix=sort(W_Matrix);
0061 mean_value=mean(W_Matrix);
0062 Standard_value=std(W_Matrix);
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 if Entopy_W==0
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
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
0117 Iter_PDF=2000;
0118
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
0143 Iter_Optimization=1;opt_iter=1;
0144 while opt_iter==1
0145
0146 pop_final=[];
0147
0148 pop2_clone=Clonal_Operator(pop1);
0149
0150 pop3=Mutation_Operator(N,pop2_clone);
0151 popnew=vertcat(pop3,pop2_clone);
0152 m=size(popnew,1);
0153
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
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
0191
0192 Btypes=Best';
0193
0194
0195
0196
0197
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
0257 X1=X1+(Ratio_Bus_Type(1,f)*log(Ratio_Bus_Type(1,f)));
0258 end
0259 X2=0;
0260 for f=1 : 6
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
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
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
0286
0287
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
0314 k=1;
0315 Bus_Matrix=[];
0316
0317 G= round(N*Ratio_Bus_Type(1,1));
0318 while K>0
0319 Bus_Matrix= zeros(1,N);
0320 C= round(N*Ratio_Bus_Type(1,3));
0321 for i=1:C
0322 T=round(1+rand*(N-1));
0323
0324
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
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358 for i=1:N
0359 if Bus_Matrix(1,i)==0
0360
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
0419 X1=X1+(log(Ratio_Bus_Type(1,f))*Ratio_Bus_Type(1,f));
0420 end
0421 X2=0;
0422 for f=1 : 6
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
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
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
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466 K=1;
0467 Bus_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));
0490 for i=1:CC
0491 T = sg_datasample(Node_no1,1);
0492
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
0499 end
0500 Bus_Matrix(1,T)=3;
0501 end
0502 end
0503
0504
0505 G= round(N*Ratio_Bus_Type(1,1));
0506
0507
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
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
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
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
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
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
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
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
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
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
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
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
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
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
0710 X1=[X1;X];
0711 X=[];
0712 end
0713 pop2_clone=X1;
0714
0715
0716 function pop3=Mutation_Operator(N,pop2_clone)
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
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
0749 pop2_clone(j,X)=1;
0750 else
0751
0752 pop2_clone(j,X)=2;
0753 end
0754
0755
0756
0757 end
0758 pop3=[pop3;pop2_clone(j,:)];
0759 end
0760 end
0761 pop3;