matlab遗传算法 chc,遗传算法matlab实现源程序

该博客介绍了一个使用遗传算法解决路径优化问题的MATLAB实现。算法首先初始化种群,然后通过适应度函数计算每个个体的目标函数值。接着进行选择、交叉和变异操作以迭代优化种群。在每代结束后,找到最优解并更新,直至达到最大迭代次数。最终,绘制目标函数值的变化曲线,展示算法的收敛过程。
摘要由CSDN通过智能技术生成

clc;

clear;

%各份订单基本数据

phen=[1 2 3 4 5 6 7 8 9 10 11 12 13 14

41,52,-23,-46,-143,-74,-56,101,73,74,95,86,-35,32

65,23,-76,104,34,38,4,-23,55,-49,39,89,-86,52

7716,9887,12188,8819,4002,6119,3284,4607,5600,4587,9821,13024,6547,2684

500,400,1000,120,0,235,654,241,0,361,120,254,300,150

1,4,2,2,4,4,3,3,3,1,4,5,1,3

2.7,1.8,4,2.5,1.6,1,3.6,5,4.2,1.9,6.4,2.8,1.4,8];

hromlength=14;

popsize=30;

maxgen=500; pc=0.8;

pm=0.04;

for kem=1:popsize

population(kem,:)=randperm(hromlength);

end

population;

%评价目标函数值

for uim=1:popsize

vector=population(uim,:);

obj(uim)=hanshu(hromlength,vector,phen);

end

%obj

%min(obj)

clear uim;

objmin=min(obj);

for sequ=1:popsize

if obj(sequ)==objmin

opti=population(sequ,:);

end

end

clear sequ;

fmax=22000;

%==

for gen=1:maxgen

%选择操作

%将求最小值的函数转化为适应度函数

for indivi=1:popsize

obj1(indivi)=1/obj(indivi);

end

clear indivi;

%适应度函数累加总合

total=0;

for indivi=1:popsize

total=total+obj1(indivi);

end

clear indivi;

%每条染色体被选中的几率

for indivi=1:popsize

fitness1(indivi)=obj1(indivi)/total;

end

clear indivi;

%各条染色体被选中的范围

for indivi=1:popsize

fitness(indivi)=0;

for j=1:indivi

fitness(indivi)=fitness(indivi)+fitness1(j);

end

end

clear j;

fitness;

%选择适应度高的个体

for ranseti=1:popsize

ran=rand;

while (ran>1||ran<0)

ran=rand;

end

ran;

if ran<=fitness(1)

newpopulation(ranseti,:)=population(1,:);

else

for fet=2:popsize

if (ran>fitness(fet-1))&&(ran<=fitness(fet))

newpopulation(ranseti,:)=population(fet,:);

end

end

end

end

clear ran;

newpopulation;

%交叉

for int=1:2:popsize-1

popmoth=newpopulation(int,:);

popfath=newpopulation(int+1,:);

popcross(int,:)=popmoth;

popcross(int+1,:)=popfath;

randnum=rand;

if(randnum< P>

cpoint1=round(rand*hromlength);

cpoint2=round(rand*hromlength);

while (cpoint2==cpoint1)

cpoint2=round(rand*hromlength);

end

if cpoint1>cpoint2

tem=cpoint1;

cpoint1=cpoint2;

cpoint2=tem;

end

cpoint1;

cpoint2;

for term=cpoint1+1:cpoint2

for ss=1:hromlength

if popcross(int,ss)==popfath(term)

tem1=popcross(int,ss);

popcross(int,ss)=popcross(int,term);

popcross(int,term)=tem1;

end

end

clear tem1;

end

for term=cpoint1+1:cpoint2

for ss=1:hromlength

if popcross(int+1,ss)==popmoth(term)

tem1=popcross(int+1,ss);

popcross(int+1,ss)=popcross(int+1,term);

popcross(int+1,term)=tem1;

end

end

clear tem1;

end

end

clear term;

end

clear randnum;

popcross;

%变异操作

newpop=popcross;

for int=1:popsize

randnum=rand;

if randnum

cpoint12=round(rand*hromlength);

cpoint22=round(rand*hromlength);

if (cpoint12==0)

cpoint12=1;

end

if (cpoint22==0)

cpoint22=1;

end

while (cpoint22==cpoint12)

cpoint22=round(rand*hromlength);

if cpoint22==0;

cpoint22=1;

end

end

temp=newpop(int,cpoint12);

newpop(int,cpoint12)=newpop(int,cpoint22);

newpop(int,cpoint22)=temp;

end

end

newpop;

clear cpoint12;

clear cpoint22;

clear randnum;

clear int;

for ium=1:popsize

vector1=newpop(ium,:);

obj1(ium)=hanshu(hromlength,vector1,phen);

end

clear ium;

obj1max=max(obj1);

for ar=1:popsize

if obj1(ar)==obj1max

newpop(ar,:)=opti;

end

end

clear population;

clear objmin;

clear objmean;

%遗传操作结束

population=newpop;

for ium=1:popsize

vector2=population(ium,:);

obj(ium)=object(hromlength,vector2,phen);

end

objmin=min(obj);

objmean=mean(obj);

clear opti;

for sequ1=1:popsize

if obj(sequ1)==objmin

opti=population(sequ1,:);

end

end

solution=objmin;

final(gen)=objmin;

final1(gen)=objmean;

end

opti

solution

plot(final);

hold on;

plot(final1,--)

hold off

%目标函数值子函数

function[cost]=hanshu(hromlength,vector,phen)

wmax=20000;

ct=1.2;

ch=0.5;

for num=1:hromlength

line=vector(num);

s(:,num)=phen(:,line);

end

m=1;

cshort=0;

chold=0;

ctrans=0;

while m<=hromlength

j=m;

weight=s(4,j);

day=s(6,j);

dis=sqrt(s(2,j)^2+s(3,j)^2);

while ((j< P>

weight=weight+s(4,j+1);

if (s(6,j+1)< P>

cshort=(s(5,j+1))*(s(7,j+1))*0.1+cshort;

chold=(s(4,j+1))*ch+chold;

end

dis=sqrt((s(2,j)-s(2,j+1))^2+(s(3,j)-s(3,j+1))^2);

j=j+1;

end

dis=dis+sqrt(s(2,j)^2+s(3,j)^2);

ctrans=ctrans+dis*weight*ct;

m=j+1;

end

cost=cshort+chold+ctrans;

展开阅读全文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值