一、遗传算法简介
1 引言
2.1 遗传算法的生物学基础
二、部分源代码
% this program is designed for optimal substation placement
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function SP_main()
clear;clc;
pack;
tic;
display('______________________________________ RESULTS STARTED _____________________________________');
global T L FinalTransPow FinalLoad FinalTransCap TransTypes AuxTransCap AuxTransPow K2 K3 MaxT
global SelCaseRow FinalTrans_x FinalTrans_y FinalLoad_x FinalLoad_x FinalLoad_y BrTransIndex
global AuxFinalTrans_x AuxFinalTrans_y AuxFinalLoad_x AuxFinalLoad_x AuxFinalLoad_y AuxFinalLoad
global FinalTLDistances VarCheckPrg
global finaltranspow finaltranscap
global PowerFactor UtilizationFactor TransformersTypes KWTransformersTypes
global InstallCosts OpenCircuitLosses ShortCircuitLosses
global CurrentTr_x CurrentTr_y
K2 = 0.0035e-3;
K3 = 0.64;
% determination of load centers & the vector of load values
DATA = xlsread('DATA.xls','Loads');
CANDIDATES = xlsread('DATA.xls','Candidates');
CURRENT_TRANSFORMERS = xlsread('DATA.xls','Current_Transformers');
DESIGN_CONSTANTS = xlsread('DATA.xls','Design_Constants');
TRANSFORMERS_TYPES = xlsread('DATA.xls','Transformers_Types');
ExcelLoad_x = DATA(:,1);
ExcelLoad_y = DATA(:,2);
ExcelLoads = DATA(:,3);
ExcelCandidates_x = CANDIDATES(:,1);
ExcelCandidates_y = CANDIDATES(:,2);
PowerFactor = DESIGN_CONSTANTS(1,1);
UtilizationFactor = DESIGN_CONSTANTS(1,2)/100;
TransformersTypes(1,:) = sort(TRANSFORMERS_TYPES(:,1));
KWTransformersTypes = ceil(UtilizationFactor*PowerFactor*TransformersTypes);
InstallCosts(1,:) = sort(TRANSFORMERS_TYPES(:,2));
OpenCircuitLosses(1,:) = sort(TRANSFORMERS_TYPES(:,3));
ShortCircuitLosses(1,:) = sort(TRANSFORMERS_TYPES(:,4));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Loads(1,:) = ExcelLoads(:,1);
Load_x(1,:) = ExcelLoad_x(:,1);
Load_y(1,:) = ExcelLoad_y(:,1);
Candidates_x(1,:) = ExcelCandidates_x(:,1);
Candidates_y(1,:) = ExcelCandidates_y(:,1);
if length(CURRENT_TRANSFORMERS) ~= 0
CurrentTr_x = CURRENT_TRANSFORMERS(:,1)';
CurrentTr_y = CURRENT_TRANSFORMERS(:,2)';
CurrentTrCap = CURRENT_TRANSFORMERS(:,3)';
CurrentTrPow = ceil(UtilizationFactor*PowerFactor*CurrentTrCap);
else
CurrentTr_x = [];
CurrentTr_y = [];
CurrentTrCap = [];
CurrentTrPow = [];
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% initialize transformer locations and powers
% one transformer in every load center. initially we assume the first
% transformer for all candidate points.
TransTypes = length(TransformersTypes);
LoadCenters = length(ExcelLoads);
CurrentTrNo = length(CurrentTr_x);
CandidateNo = length(ExcelCandidates_x);
for count1=1:CurrentTrNo,
if CurrentTrPow(1,count1) >= ceil((400*UtilizationFactor*PowerFactor))
LimitedCurrentTrPow(1,count1) = KWTransformersTypes(1,6);
else
LimitedCurrentTrPow(1,count1) = KWTransformersTypes(1,1);
end;
end;
Trans_x = [CurrentTr_x Candidates_x];
Trans_y = [CurrentTr_y Candidates_y];
TransCap = [LimitedCurrentTrPow (KWTransformersTypes(1,1) + zeros(1,length(ExcelCandidates_x)))];
CandidateCenters = length(TransCap); % current + candidate transformers
TransPow = zeros(1,CandidateCenters);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculation of the distances between transformers
% this is a matrice with dimensions: (LoadCenters)*(CandidateCenters)
Distances = zeros(LoadCenters,CandidateCenters);
for count1=1:LoadCenters,
for count2=1:CandidateCenters,
Distances(count1,count2) = sqrt((Load_x(1,count1)-Trans_x(1,count2))^2 + (Load_y(1,count1)-Trans_y(1,count2))^2);
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CanDistances = zeros(CandidateCenters);
for count1=1:CandidateCenters,
for count2=1:count1,
CanDistances(count1,count2) = sqrt((Trans_x(1,count1)-Trans_x(1,count2))^2 + (Trans_y(1,count1)-Trans_y(1,count2))^2);
CanDistances(count2,count1) = CanDistances(count1,count2);
end;
end;
% the distances are pairly compared with 100, if they are smaller than 100
% the transformer with smaller value of load is detached.
for count1=1:CandidateCenters,
for count2=1:CandidateCenters,
if (CanDistances(count1,count2) <= 100) & (count1 ~= count2) & (count1 > CurrentTrNo)
TransCap(1,count1) = 0;
TransPow(1,count1) = 0;
end;
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% this function step-up the transformers size until it can feed the total
% loads connected to it.
% for count1=1:TransTypes,
% for count2=1:LoadCenters,
% if (TransCap(1,count2) < Loads(1,count2)) && (TransCap(1,count2) ~= 0)
% TransCap(1,count2) = SP_stepup(TransCap(1,count2));
% end;
% end;
% end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% in this subsection, the coordinations of the final possible transformers
% are saved in FinalTrans_x & FinalTrans_y and their values in
% FinalTransCap. similarly, the coordinations of the loadcenters to be
% attached to transformers are saved in FinalLoad_x & FinalLoad_y and their
% values in FinalLoad.
T = 1; L = LoadCenters;
for count1 = 1:CandidateCenters,
if (TransCap(1,count1) ~= 0)
FinalTransCap(1,T) = TransCap(1,count1);
FinalTransPow(1,T) = 0;
FinalTrans_x(1,T) = Trans_x(1,count1);
FinalTrans_y(1,T) = Trans_y(1,count1);
T = T+1;
end;
end;
MaxT = T-1;
% MinL = L-1;
% if ((MaxT > 23) & (VarCheckPrg == 1))
% warndlg('MATLAB unable to solve! Try smaller number of loads',' DSP Warning');
% display('MATLAB unable to solve! Try smaller number of loads');
% return;
% end;
AuxTransCap = FinalTransCap;
AuxTransPow = FinalTransPow;
AuxFinalTrans_x = FinalTrans_x;
AuxFinalTrans_y = FinalTrans_y;
AuxFinalLoad_x = Load_x;
AuxFinalLoad_y = Load_y;
AuxFinalLoad = Loads;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculation of Distances beetween transformers and loads in a (MinL*MaxT) matrice.
for count1=1:LoadCenters,
for count2=1:MaxT,
TLDistances(count1,count2) = sqrt((AuxFinalLoad_x(1,count1)-AuxFinalTrans_x(1,count2))^2 + (AuxFinalLoad_y(1,count1)-AuxFinalTrans_y(1,count2))^2);
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HERE, THE OPERATION OF GENETIC ALGORITHM IS STARTED.
TotalLevels = 0;
LowLim = CurrentTrNo;
MaxLim = LowLim + (MaxT-CurrentTrNo);
for count5=LowLim:MaxLim,
TotalLevels = TotalLevels + ceil(nchoosek(MaxT,count5));
end;
ScalingFactor = ceil(TotalLevels/20);
TotalLevels = ceil(TotalLevels/ScalingFactor);
Index = 0;
TMat = [(CurrentTrNo+1):MaxT];
% if ((TotalLevels < 20) & (VarCheckPrg == 1))
% warndlg('Try smaller Scaling Factor!',' DSP Warning');
% display('Try smaller Scaling Factor!');
% return;
% end;
% if ((TotalLevels > 300) & (VarCheckPrg == 1))
% warndlg('Try bigger Scaling Factor!',' DSP Warning');
% display('Try bigger Scaling Factor!');
% return;
% end;
% if ((VarCheckPrg == 1) & ((TotalLevels <= 300) & (TotalLevels >= 20)))
% warndlg('No problem in running program. Push "Run DSP"',' DSP Warning');
% LEVELS = TotalLevels;
% LEVELS
% display('No problem in running program. Push "Run DSP"');
% return;
% end;
close all;
pack;
cfinaltranscap = cell(TotalLevels,1);
cfinaltranspow = cell(TotalLevels,1);
cfinaltrans_x = cell(TotalLevels,1);
cfinaltrans_y = cell(TotalLevels,1);
cfinalload = cell(TotalLevels,1);
cfinalload_x = cell(TotalLevels,1);
cfinalload_y = cell(TotalLevels,1);
pack;
h = waitbar(0,'Please wait...');
for count1=0:(MaxLim-LowLim), % MAIN for. NOTE: "count1" stands for the number of new trans.
T = LowLim + count1; % "LowLim" stands for the number of current transformers.
numberOfVariables = LoadCenters;
L = numberOfVariables;
SelCaseMat = nchoosek(TMat,count1);
SIZE = size(SelCaseMat);
RowNumber = SIZE(1,1);
for count2=1:ScalingFactor:RowNumber,
Index = Index + 1;
Level = Index;
SelCaseRow = [[1:CurrentTrNo] SelCaseMat(count2,:)]; % SelCaseRow should be sent to SP_create.m. It is a selection of transformers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% modifying FinalTrans_x,y & FinalLoad_x,y & FinalLoad &
% FinalTransPow & FinalTransCap
finaltranscap = zeros(1,MaxT) -1;
finaltranspow = zeros(1,MaxT) -1;
finaltrans_x = zeros(1,MaxT) -1;
finaltrans_y = zeros(1,MaxT) -1;
for count3=1:T,
finaltranscap(1,SelCaseRow(1,count3)) = AuxTransCap(1,SelCaseRow(1,count3));
finaltranspow(1,SelCaseRow(1,count3)) = AuxTransPow(1,SelCaseRow(1,count3));
finaltrans_x(1,SelCaseRow(1,count3)) = AuxFinalTrans_x(1,SelCaseRow(1,count3));
finaltrans_y(1,SelCaseRow(1,count3)) = AuxFinalTrans_y(1,SelCaseRow(1,count3));
end;
FinalTransCap = finaltranscap;
FinalTransPow = finaltranspow;
FinalTrans_x = finaltrans_x;
FinalTrans_y = finaltrans_y;
cfinaltranscap{Index} = finaltranscap;
cfinaltranspow{Index} = finaltranspow;
cfinaltrans_x{Index} = finaltrans_x;
cfinaltrans_y{Index} = finaltrans_y;
finalload = Loads;
finalload_x = Load_x;
finalload_y = Load_y;
% Li = 0;
% for count3=1:LoadCenters,
% if (size(find(FinalTrans_x==Load_x(1,count3)))==[1 0] | size(find(FinalTrans_y==Load_y(1,count3)))==[1 0])
% Li = Li + 1;
% finalload(1,Li) = Loads(1,count3);
% finalload_x(1,Li) = Load_x(1,count3);
% finalload_y(1,Li) = Load_y(1,count3);
% end;
% end;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.
五、代码下载