% Project Title: A New Hybrid Firefly ?Genetic Algorithm (FAGA) in MATLAB
%%
clc; clear; close all;
%% Problem Definition
% Objective Function
ANSWER=listdlg('PromptString','Choose Objective Function','SelectionMode','single', 'ListString', {'1. Sphere', '2. Rastrigin'});
if eq(ANSWER,1); ObjectiveFunction=@(x) Sphere(x); funcname='Sphere';
elseif eq(ANSWER,2); ObjectiveFunction=@(x) Rastrigin(x); funcname='Rastrigin';
else; disp('Terminated'); return
end
ProblemSize=[1 50]; % Decision Variables Size
LowerBound=-10; % Decision Variables Lower Bound
UpperBound= 10; % Decision Variables Upper Bound
%% Firefly-Genetic Algorithm Parameters
methname='Hybrid Firefly - Genetic Algorithm';
MaxIt=500; % Maximum Number of Iterations
nPop=10; % Population Size
mu=0.01; % Mutation Rate
funccount=0; % Function Evaluations counter
%% Initialization
empty_firefly.Position=[];
empty_firefly.Cost=[];
% Initialize Population Array
pop=repmat(empty_firefly,nPop,1);
% Initialize Best Solution Ever Found
BestSol.Cost=inf;
% Create Initial Fireflies
for i=1:nPop
pop(i).Position=unifrnd(LowerBound,UpperBound,ProblemSize);
pop(i).Cost=ObjectiveFunction(pop(i).Position);
if pop(i).Cost<=BestSol.Cost
BestSol=pop(i);
end
end
% Array to Hold Best Cost Values
BestCost=zeros(MaxIt,1);
%% Firefly-Genetic Algorithm Main Loop
for it=1:MaxIt
newfireflies=repmat(empty_firefly,0,1);
for i=1:nPop
for j=1:nPop
if i~=j
newsol=repmat(empty_firefly,2,1);
if pop(j).Cost < pop(i).Cost
[newsol(1).Position, newsol(2).Position]=Crossover(pop(i).Position,pop(j).Position,LowerBound,UpperBound);
else
newsol(1).Position=Mutate(pop(i).Position,mu,LowerBound,UpperBound);
newsol(2).Position=Mutate(pop(j).Position,mu,LowerBound,UpperBound);
end
newsol(1).Cost=ObjectiveFunction(newsol(1).Position); funccount=funccount+1;
if newsol(1).Cost<pop(i).Cost
pop(i)=newsol(1);
end
newsol(2).Cost=ObjectiveFunction(newsol(2).Position); funccount=funccount+1;
if newsol(2).Cost<pop(j).Cost
pop(j)=newsol(2);
end
for k=1:2
if newsol(k).Cost<=BestSol.Cost
BestSol=newsol(k);
end
end
newfireflies=[newfireflies;newsol];
end
end
end
% Merge
pop=[pop;newfireflies]; %#ok
[~, idx] = unique([pop.Cost].', 'rows', 'stable'); %stable optional if you don't care about the order.
pop = pop(idx);
% Sort
[~, SortOrder]=sort([pop.Cost]);
pop=pop(SortOrder);
% Truncate
pop=pop(1:nPop);
% Store Best Cost Ever Found
BestCost(it)=BestSol.Cost;
disp([methname ' on the ' funcname ' Function: Iteration = ' num2str(it) ', ' funcname ', Evaluations = ' num2str(funccount) '. Best Cost = ' num2str(BestCost(it))]);
end
%% Results
figure;
plot(BestCost,'LineWidth',2); semilogy(BestCost,'LineWidth',2);
xlabel('Iterations'); ylabel('Objective function'); grid on;
%%
%%
没有合适的资源?快使用搜索试试~ 我知道了~
基于萤火虫结合遗传算法求解单目标优化问题附matlab代码
共6个文件
m:5个
png:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 84 浏览量
2022-10-16
11:40:34
上传
评论
收藏 10KB ZIP 举报
温馨提示
1.版本:matlab2019a,不会运行可私信 2.领域:【智能优化算法-遗传算法】 3.内容:基于萤火虫结合遗传算法求解单目标优化问题附matlab代码.zip 4.适合人群:本科,硕士等教研学习使用
资源推荐
资源详情
资源评论
收起资源包目录
【智能优化算法-遗传算法】基于萤火虫结合遗传算法求解单目标优化问题附matlab代码.zip (6个子文件)
FAGA.m 3KB
Rastrigin.m 80B
Mutate.m 246B
1.png 8KB
Crossover.m 277B
Sphere.m 41B
共 6 条
- 1
资源评论
天天Matlab科研工作室
- 粉丝: 3w+
- 资源: 7261
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功