function [BestGene,aa] = GA(MaxGeneration,GeneSize,GeneNum,pcross,pmute,minGene,maxGene)
Parent = Init(GeneSize,GeneNum,minGene,maxGene);
[BestGene,Parent] = KeepBest(Parent);
aa = [];
for i = 1:MaxGeneration
[i 1/value(BestGene)]
Child = chose(Parent);
Child = cross(Child,pcross);
Child = mute(Child,pmute,maxGene);
[BestGene,Parent] = KeepBest(Child);
aa = [aa;value(BestGene)];
end
function GeneInit = Init(GeneSize,GeneNum,minGene,maxGene)
GeneInit = [];
for i = 1:GeneSize
x = []; x = ceil(rand(1,GeneNum).*(maxGene-minGene)) + minGene;
GeneInit = [GeneInit;x];
end
GeneInit = [GeneInit;x];
function Child = chose(Parent)
GeneSize = size(Parent,1);
for i = 1:GeneSize
x = Parent(i,:);
val(i) = value(x);
end
ValSum = sum(val);
val = val / ValSum;
for i = 2:GeneSize
val(i) = val(i) + val(i-1);
end
for i = 1:GeneSize
randval = rand;
if randval <= val(1)
Child(i,:) = Parent(1,:);
end
for j = 2:GeneSize
if randval > val(j-1) && randval <= val(j)
Child(i,:) = Parent(j,:);
break;
end
end
end
Child(end,:) = Parent(end,:);
function Child = cross(Parent,pcross)
[GeneSize,GeneNum] = size(Parent);
GeneSize = GeneSize - 1;
Child = Parent;
for i = 1:GeneSize/2
if rand < pcross
flag = 0;
while( flag==0 )
randval1 = floor((GeneNum-1)*rand) + 1;
randval2 = floor((GeneNum-1)*rand) + 1;
if randval1 ~= randval2
flag = 1;
end
end
temp = Child(2*i-1,randval1:randval2);
Child(2*i-1,randval1:randval2) = Child(2*i,randval1:randval2);
Child(2*i,randval1:randval2) = temp;
end
end
function Child = mute(Parent,pmute,maxGene)
[GeneSize,GeneNum] = size(Parent);
GeneSize = GeneSize - 1;
Child = Parent;
for i = 1:GeneSize
if rand < pmute
randval = ceil((GeneNum-1)*rand) + 1;
Child(i,randval) = maxGene(randval) - Child(i,randval) + 1;
end
end
function [BestGene,Parent] = KeepBest(Child)
[GeneSize,GeneNum] = size(Child);
for i = 1:GeneSize
x = Child(i,:);
val(i) = value(x);
end
BigVal = val(1);
flag = 1;
for i = 2:GeneSize
if BigVal < val(i)
BigVal = val(i);
flag = i;
end
end
BestGene = Child(flag,:);
Parent = Child;
Parent(1,:) = BestGene;
Parent(end,:) = BestGene;
GA.zip_算法 社
版权申诉
100 浏览量
2022-09-23
17:36:51
上传
评论
收藏 804B ZIP 举报
局外狗
- 粉丝: 69
- 资源: 1万+
最新资源
- MyBatisCodeHelperPro 3.3.2-2322 2023.2-2024.1
- 基于Python的简单的学生成绩管理程序设计(课程设计)
- jdk-8u20-windows-x64安装版本-jdk-8u301-linux-x64解压版
- 植物大战僵尸杂交版 修改阳光和冷却
- html css js网页设计ntion-model-for-开发笔记
- 数据库课程设计-processing开发笔记
- matlab画图sic-开发demo
- 数据库原理课程设计-图书馆数据库管理系统
- IEC101Sim IEC870-5-101协议仿真主站、子站 (含源码 C++Builder)
- 2212503007陈熙炜跨境2202作业2.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈