function [final, maxarr, modearr, stdarr] = GA(chromosomes, chlen, popnum, generationnum)
% 0 moAdele C va 1 moAdele D ast
maxarr = zeros(generationnum, 1);
modearr = zeros(generationnum, 1);
stdarr = zeros(generationnum, 1);
for k=1 : generationnum
fit = zeros(popnum,1);
for i=1 : popnum
fit(i) = fitness(chromosomes, popnum, i);
end
maxarr(k) = max(fit);
modearr(k) = mode(fit);
stdarr(k) = std(fit,1);
wheelspace = zeros(popnum, 2);
rg = 1;
for i=1 : popnum
wheelspace(i,1)=rg;
wheelspace(i,2)=fit(i)+rg-1;
rg = rg + fit(i);
end
newgeneration = zeros(popnum,chlen);
% tolide nasle jadid
for i=1 : 10
rand1 = randint(1,1,[1,rg-1]);
rand2 = randint(1,1,[1,rg-1]);
p1=0;
p2=0;
for j=1 : popnum
if rand1>=wheelspace(j,1) && rand1<=wheelspace(j,2)
p1 = j;
end
if rand2>=wheelspace(j,1) && rand2<=wheelspace(j,2)
p2 = j;
end
end
[ch1,ch2] = generate(chromosomes, chlen, p1, p2);
newgeneration((2.*i)-1,:)=ch1;
newgeneration(2.*i,:)=ch2;
end
chromosomes = newgeneration;
%%%%%%%%%%%%%%%%%%%%
end
%%%%%%%%%%%%%%%%%%%%
final = zeros(1,20);
for i=1 : 20
final(i)=fitness(chromosomes, popnum, i);
end
end
%%%%%%%%%%%%%%%%%%%%
function [c1, c2] = generate(chromosomes, chlen, p1, p2)
c1 = zeros(1,chlen);
c2 = zeros(1,chlen);
pc = randint(1,1,[1,10]);
if pc<=7
% bayad crossover rokh dahad
locus = randint(1,1,[1,chlen-1]);
for i=1 : locus
c1(i) = chromosomes(p1,i);
c2(i) = chromosomes(p2,i);
end
for i=locus+1 : chlen
c1(i) = chromosomes(p2,i);
c2(i) = chromosomes(p1,i);
end
else
c1 = chromosomes(p1,:);
c2 = chromosomes(p2,:);
end
for i=1 : chlen
pm = randint(1,1,[1,1000]);
if pm==1
c1(i) = 1-c1(i);
end
pm = randint(1,1,[1,1000]);
if pm==1
c2(i) = 1-c2(i);
end
end
end
%%%%%%%%%%%%%%%%%%%%
% this is true
function sumscoreA = play100(strategyA, strategyB)
sumscoreA = 0;
for k=1 : 100
[sA, mA, mB] = play(strategyA, strategyB);
sumscoreA = sumscoreA+sA;
for i=1 : 4
strategyA(i)=strategyA(i+1);
strategyB(i)=strategyB(i+1);
end
for i=1 : 4
strategyA(i)=strategyA(i+1);
strategyB(i)=strategyB(i+1);
end
strategyA(5)=mA;
strategyA(6)=mB;
strategyB(5)=mB;
strategyB(6)=mA;
end
end
%%%%%%%%%%%%%%%
% this is true
function [scoreA, moveA, moveB] = play(strategyA , strategyB)
%return payoff of strategy1
scoreA = 0;
numberA = findcase(strategyA);
numberB = findcase(strategyB);
moveA = strategyA(numberA+6);
moveB = strategyB(numberB+6);
scoreA = payoff(moveA, moveB);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% this is true
function gainA = payoff(moveA, moveB)
%return payoff of A
gainA = 0;
if moveA==0 && moveB==0
gainA = 3;
else
if moveA==0 && moveB==1
gainA = 0;
else
if moveA==1 && moveB==0
gainA = 5;
else
gainA = 1;
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% it is true
function number = findcase(strategy)
number = 0;
for i=1 : 6
if strategy(i) == 1
number = number + (2.^(6-i));
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function code = chromosomecode(chromosome)
code =0;
for i=7 : 70
code = code + chromosome(i)*(2.^(70-i));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sum = fitness(chromosomes, popnum, k)
sum=0;
for j=1 : popnum
sum = sum + play100(chromosomes(k,:), chromosomes(j,:));
end
end
GA.rar_algorithms
版权申诉
49 浏览量
2022-09-24
16:37:08
上传
评论
收藏 1KB RAR 举报
林当时
- 粉丝: 102
- 资源: 1万+
最新资源
- 28统计字符串中各个数字字符出现的次数(3).cpp
- 数字信号处理课程设计指南
- SSM3J312T-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- SSM3J307T-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 电气创新项目实训大作业要求及评分标准
- SSM3J306T-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- elasticsearch-analysis-dynamic-synonym-7.0.0编译后的插件
- matlab课程时间安排题目
- 111111111111111
- 笔记Typora语法应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈