%% Simple EMOO Problem 简单的EMOO问题
% 这段Matlab代码讲的是运用遗传算法来解决EMOO问题:在一个布满星星的2D空间上,画一个
% 尽可能大的圆,并且保证这个圆里不能含有星星。
% 作者: Wesam ELSHAMY,然后我稍加解释。
%%%%% 初始化参数
clear; %清除所有相关的参数
stars = 30; %星星的数量
iterations = 500; %迭代数
population = 100; %种群规模
mutation = 0.7; %变异率
mutation_step = 0.8; %变异的步长
grid_size = 10; %星星的坐标的范围,不超过10。
%%%%% 生成星星
for i = 1:stars
for j = 1:2
star(i,j) = rand(1)*grid_size; %星星的位置即坐标随机产生,0到10之间。
end
end
plot(star(:,1),star(:,2),'x') %在屏幕上显示所有的星星
hold on %显示出图像
%%%%% 生成初始种群
for i = 1:population
for j = 1:2
cit(i,j)= rand(1)*grid_size; %种群中的每一个个体就是一个解,即一个圆的圆心坐标。
end
end
%%%%% 种群中个体适应度计算
for j = 1:population
for k = 1:stars
d(k) = sqrt((star(k,1)-cit(j,1))^2 + (star(k,2)-cit(j,2))^2); %求当前圆心到所有星星的距离
end
d(stars+1) = cit(j,1);
d(stars+2) = grid_size - cit(j,1);
d(stars+3) = cit(j,2);
d(stars+4) = grid_size - cit(j,2); %求这四个是为了防止当前解产生的圆出界
d = sort(d); %把d中的元素从小到大排列
cit(j,3) = d(1); %把最小的赋值给cit(j,3)
end
cit = sortrows(cit,-3); %将cit按照其第三列降序排列
cit(1,:) %显示cit的第一行,即适应度最大的那一行,这一行是当前种群中的最优个体。
cit(:,3) = cit(:,3)/cit(1,3); %cit的第三列除以最大的那个距离(半径)
%%%%% 迭代过程
for i = 1:iterations
%--------------- 选择 -----------------
cit(:,3) = cit(:,3)/cit(1,3);
pool = [];
for l = 1:population
if rand(1)<cit(l,3),pool=[pool;cit(l,:)];,end %选择某些好的个体加入pool
end
%--------------- 交叉 ------------------
s = size(pool,1); %一般情况下,s是大于2的。我在想
if s/2 - round(s/2) ~=0 , pool = pool(1:s-1,:); , s = size(pool,1); , end
%如果s是奇数,那么pool去掉最后一个个体,这样pool就有偶数个个体。
for m = 1:2:s
pool = [ pool ; [pool(m,1) , pool(m+1,2) , 0]]; %交叉
pool = [ pool ; [pool(m+1,1) , pool(m,2) , 0]];
end
%--------------- 变异 -------------------
s = size(pool,1);
for m = 1:s
if ((rand < mutation) & (pool(m,1) < grid_size - mutation_step)) , pool(m,1)=pool(m,1)+(2*rand - 1)*mutation_step;,end
if ((rand < mutation) & (pool(m,2) < grid_size - mutation_step)) , pool(m,2)=pool(m,2)+(2*rand - 1)*mutation_step;,end
%只有在个体的横坐标和纵坐标小于grid_size-mutation_step的情况下,才按照变异率进行变异,这一点有待改进。
%我们可以采取某些小策略,使得种群中个体即一个解,无论在怎样的情况下都能对其进行变异。
end
%--------------- 更新种群------------
temp = [cit ; pool];
ts = size(temp,1);
for j = 1:ts
for k = 1:stars
d(k) = sqrt((star(k,1)-temp(j,1))^2 + (star(k,2)-temp(j,2))^2);
end
d(stars+1) = temp(j,1);
d(stars+2) = grid_size - temp(j,1);
d(stars+3) = temp(j,2);
d(stars+4) = grid_size - temp(j,2);
d = sort(d);
temp(j,3) = d(1);
end
temp = sortrows(temp,-3);
cit = temp(1:population,:); %选择temp中好的个体作为新种群
end
%好像这里没有采取最优保存策略,有待改进。
xc = cit(1,1); %最优解即圆心的横坐标
yc = cit(1,2); %圆心的纵坐标
r = cit(1,3); %半径
x = (xc-r) : 0.05 : (xc+r);
for i = 1:size(x,2)
y(i) = yc + sqrt(r^2-(x(i)-xc)^2); %上半圆
end
plot (x,y)
for i = 1:size(x,2)
y(i) = yc - sqrt(r^2-(x(i)-xc)^2); %下半圆
end
plot (x,y)
hold off
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
circle1.rar (6个子文件)
circle1
html
circle1_01.png 3KB
circle1.html 11KB
circle1_02.png 4KB
circle1.png 2KB
circle1.asv 3KB
circle1.m 3KB
共 6 条
- 1
资源评论
竹子的信仰
- 粉丝: 54
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功