• 大小: 25KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-11
  • 语言: Matlab
  • 标签: 遗传算法  MATLAB  

资源简介

遗传算法实现公交车调度,人工智能课程设计,自己原创,在MATLAB上调试通过。

资源截图

代码片段和文件信息

clear all;
close all;
clc;

%变量初始化
populationSize=80;   %群体大小  
generationSize=800;     %进化代数
codeLength=35;  %编码长度
children=zeros(populationSize7); %存储子代分量及适应度
pr=0.15;    %复制概率 
pm=0.1;     %变异概率
pc=0.60;    %交叉概率
E=round(rand(populationSizecodeLength));    %随机产生二进制串

%主函数
for k=1:1:generationSize
    
for s=1:1:populationSize
x=E(s:);
temp=zeros(17);%临时保存子代各分量的十进制值
%编码
for i=1:35
    if i<=6
        temp(1)=temp(1)+x(i)*2^(i-1);
    elseif i>=7 && i<=13
        temp(2)=temp(2)+x(i)*2^(i-6-1);
    elseif i>=14 && i<=19
        temp(3)=temp(3)+x(i)*2^(i-13-1);
    elseif i>=20 && i<=25
        temp(4)=temp(4)+x(i)*2^(i-19-1);
    elseif i>=26 && i<=30
        temp(5)=temp(5)+x(i)*2^(i-25-1);
    elseif i>=31 && i<=35
        temp(6)=temp(6)+x(i)*2^(i-30-1);
    end
end
children(s1)=round(60*temp(1)/(2^6-1));
children(s2)=round(70*temp(2)/(2^7-1));
children(s3)=round(60*temp(3)/(2^6-1));
children(s4)=round(50*temp(4)/(2^6-1));
children(s5)=round(20*temp(5)/(2^5-1));
children(s6)=round(30*temp(6)/(2^5-1));
children(s7)=round(children(s1)+children(s2)+children(s3)+children(s4)+children(s5)+children(s6));

while children(s1)+children(s6)<60||children(s1)+children(s2)<70||children(s2)+children(s3)<60||...
        children(s3)+children(s4)<50||children(s4)+children(s5)<20||children(s5)+children(s6)<30
    E(s:)=round(rand(1codeLength));
    x=E(s:);
    temp=zeros(17);
 for i=1:35
    if i<=6
        temp(1)=temp(1)+x(i)*2^(i-1);
    elseif i>=7 && i<=13
        temp(2)=temp(2)+x(i)*2^(i-6-1);
    elseif i>=14 && i<=19
        temp(3)=temp(3)+x(i)*2^(i-13-1);
    elseif i>=20 && i<=25
        temp(4)=temp(4)+x(i)*2^(i-19-1);
    elseif i>=26 && i<=30
        temp(5)=temp(5)+x(i)*2^(i-25-1);
    elseif i>=31 && i<=35
        temp(6)=temp(6)+x(i)*2^(i-30-1);
    end
 end
children(s1)=round(60*temp(1)/(2^6-1));
children(s2)=round(70*temp(2)/(2^7-1));
children(s3)=round(60*temp(3)/(2^6-1));
children(s4)=round(50*temp(4)/(2^6-1));
children(s5)=round(20*temp(5)/(2^5-1));
children(s6)=round(30*temp(6)/(2^5-1));
children(s7)=round(children(s1)+children(s2)+children(s3)+children(s4)+children(s5)+children(s6));
end
end

% Step 1 : 确定适应度函数
fitness=1./children(:7);                      
[orderFitnessindexFitness]=sort(fitness);     %orderFitness按行从小到大排序的新矩阵indexFitness元素位置构成的新矩阵                                                                                 
bestFitness=orderFitness(populationSize);      %bestFitness=max(fitness)
bestS=E(indexFitness(populationSize):);       %bestS 精英保留

%Step 2 : 复制操作
sumFitness=sum(fitness);
fitnessSize=(orderFitness/sumFitness)*populationSize;
fitnessSelect=floor(fitnessSize);        %朝负无穷大方向取整                        
kk=1;
for i=1:1:populationSize
    for j=1:1:fitnessSelect(i)            %Select and Reproduce 
        TempE(kk:)=E(indexFitness(i):);  
        kk=kk+1;                    

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      91648  2014-03-21 19:37  GeneticAlgorithm\BusDispatch.doc

     文件       3981  2013-06-20 22:23  GeneticAlgorithm\GeneticAlgorithm.m

     目录          0  2014-03-21 19:50  GeneticAlgorithm

----------- ---------  ---------- -----  ----

                95629                    3


评论

共有 条评论