% ---------------------------------
% Cuckoo Optimization Algorithm |
% Matlab Toolbox Ver. 1.0.1 |
% Ramin Rajabioun, 2011 |
% ---------------------------------
% This code minimizes your cost function, so don't change any part of your
% code
clc, clear, close all
warning off
%% Set problem parameters
% select a cost function:
costFunction = 'CostFunction1';
npar = 2; % number of optimization variables
varLo = 0; % Lower band of parameter
varHi = 10; % Higher band of parameter
%% Set COA parameters
numCuckooS = 5; % number of initial population
minNumberOfEggs = 2; % minimum number of eggs for each cuckoo
maxNumberOfEggs = 4; % maximum number of eggs for each cuckoo
maxIter = 20; % maximum iterations of the Cuckoo Algorithm
knnClusterNum = 1; % number of clusters that we want to make
motionCoeff = 9; % Lambda variable in COA paper, default=2
accuracy = 0; % How much accuracy in answer is needed
maxNumOfCuckoos = 10; % maximum number of cuckoos that can live at the same time
radiusCoeff = 5; % Control parameter of egg laying
cuckooPopVariance = 1e-13; % population variance that cuts the optimization
%% initialize population:
cuckooPop = cell(numCuckooS,1);
% initialize egg laying center for each cuckoo
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.center = ( varHi-varLo )*rand(1,npar) + varLo;
end
%% initialize COA cost minimization plot
figure(1)
hold on
xlabel 'Cuckoo iteration'
ylabel 'Cost Value'
%% Start Cuckoo Optimization Algorithm
iteration = 0;
goalPoint = (varHi - varLo)*rand(1,npar) + varLo; % a random goalpoint to start COA
maxProfit = -feval(costFunction,goalPoint);
globalBestCuckoo = goalPoint;
globalMaxProfit = maxProfit;
profitVector = [];
while ( (iteration <= maxIter) && (-maxProfit > accuracy) )
iteration = iteration + 1
% initialize number of eggs for each cuckoo
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.numberOfEggs = floor( (maxNumberOfEggs - minNumberOfEggs) * rand + minNumberOfEggs );
end
% get total number of available eggs between all cuckooS
summ = 0;
for cuckooNumber = 1:numCuckooS
summ = summ + cuckooPop{cuckooNumber}.numberOfEggs;
end
% calculate egg laying radius for each Cuckoo, considering problem
% limitations and ratio of each cuckoo's eggs
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.eggLayingRadius = cuckooPop{cuckooNumber}.numberOfEggs/summ * ( radiusCoeff * (varHi-varLo) );
end
% To lay eggs, we produced some radius values less than egg laying
% radius of each cuckoo
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.eggLayingRadiuses = cuckooPop{cuckooNumber}.eggLayingRadius * rand(cuckooPop{cuckooNumber}.numberOfEggs,1);
end
for cuckooNumber = 1:numCuckooS
params = cuckooPop{cuckooNumber}.center; % get center values
tmpRadiuses = cuckooPop{cuckooNumber}.eggLayingRadiuses;
numRadiuses = numel(tmpRadiuses);
% divide a (hyper)circle to 'numRadiuses' segments
% This is to search all over the current habitat
angles = linspace(0,2*pi,numRadiuses); % in Radians
newParams = [];
for cnt = 1:numRadiuses
addingValue = zeros(1,npar);
for iii = 1:npar
randNum = floor(2*rand)+1;
addingValue(iii) = (-1)^randNum * tmpRadiuses(cnt)*cos(angles(cnt)) + tmpRadiuses(cnt)*sin(angles(cnt));
end
newParams = [newParams; params + addingValue ];
end
% check for variable limits
newParams(find(newParams>varHi)) = varHi;
newParams(find(newParams<varLo)) = varLo;
cuckooPop{cuckooNumber}.newPosition4Egg = newParams;
end
% Now egg laying is done
% Now that egg positions are found, they are laid, and so its time to
% remove the eggs on the same positions (because each egg only can go to one nest)
for cuckooNumber = 1:numCuckooS
tmpPopulation = cuckooPop{cuckooNumber}.newPosition4Egg;
tmpPopulation = floor(tmpPopulation * 1e20)/1e20;
ii = 2;
cntt = 1;
while ii <= size(tmpPopulation,1) || cntt <= size(tmpPopulation,1)
if all((tmpPopulation(cntt,:) == tmpPopulation(ii,:)))
tmpPopulation(ii,:) = [];
end
ii = ii + 1;
if ii > size(tmpPopulation,1) && cntt <= size(tmpPopulation,1)
cntt = cntt + 1;
ii = cntt + 1;
if ii > size(tmpPopulation,1)
break
end
end
end
cuckooPop{cuckooNumber}.newPosition4Egg = tmpPopulation;
end
% Now we evalute egg positions
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.profitValues = -feval(costFunction,[cuckooPop{cuckooNumber}.center ; cuckooPop{cuckooNumber}.newPosition4Egg]);
end
% Now we check to see if cuckoo population is more than maxNumOfCuckoos
% this case we should keep 1st maxNumOfCuckoos cuckoos and kill the others
allPositions = [];
whichCuckooPopTheEggBelongs = [];
tmpProfits = [];
if numCuckooS > maxNumOfCuckoos
for cuckooNumber = 1:numCuckooS
tmpProfits = [tmpProfits; cuckooPop{cuckooNumber}.profitValues];
allPositions = [allPositions; [cuckooPop{cuckooNumber}.center; cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar)]];
whichCuckooPopTheEggBelongs = [whichCuckooPopTheEggBelongs; cuckooNumber*ones(size(cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar),1),1) ];
end
% now we sort cuckoo profits
[sortedProfits, sortingIndex] = sort(tmpProfits,'descend');
% Get best cuckoo to be copied to next generation
bestCuckooCenter = allPositions(sortingIndex(1),1:npar);
sortedProfits = sortedProfits(1:maxNumOfCuckoos);
allPositions = allPositions(sortingIndex(1:maxNumOfCuckoos),:);
clear cuckooPop
for ii = 1:maxNumOfCuckoos
cuckooPop{ii}.newPosition4Egg = allPositions(ii,:);
cuckooPop{ii}.center = allPositions(ii,:);
cuckooPop{ii}.profitValues = sortedProfits(ii);
end
numCuckooS = maxNumOfCuckoos;
else
for cuckooNumber = 1:numCuckooS
tmpProfits = [tmpProfits; cuckooPop{cuckooNumber}.profitValues];
allPositions = [allPositions; [cuckooPop{cuckooNumber}.center; cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar)] ];
whichCuckooPopTheEggBelongs = [whichCuckooPopTheEggBelongs; cuckooNumber*ones(size(cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar),1),1) ];
end
[sortedProfits, sortingIndex] = sort(tmpProfits,'descend');
% Get best cuckoo to be copied to next generation
bestCuckooCenter = allPositions(sortingIndex(1),1:npar);
end
maxProfit = sortedProfits(1);
currentBestCuckoo = bestCuckooCenter;
currentMaxProfit = -feval(costFunction,currentBestCuckoo);
if currentMaxProfit > globalMaxProfit
globalBestCuckoo = currentBestCuckoo;
globalMaxProfit = currentMaxProfit;
end
% Update cost minimization plot
plot(iteration, -globalMaxProfit,'ks','linewidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)
title([ 'Curent Cost = ' num2str(-globalMaxProfit) ' , at Iteration = ' num2str(iteration) ])
pause(0.01)
profitVector = [profitVector globalMaxProfit];
% ======== now we have some eggs that are safe and will grow up ==========
%===
cuckoo.rar_cuckoo_itselfo7w_matlab_optimization
版权申诉
67 浏览量
2022-07-15
20:40:38
上传
评论
收藏 4KB RAR 举报
朱moyimi
- 粉丝: 64
- 资源: 1万+
最新资源
- 基于QT的地图可视化桌面系统后台数据库为MySQL5.7源码.zip
- 基于simulink的PLL锁相环系统仿真【包括模型,文档,参考文献,操作步骤】
- 基于EM-GMM模型的目标跟踪和异常行为检测matlab仿真【包括程序,注释,参考文献,操作步骤,说明文档】
- 2109010044_胡晨燕_选课管理数据库设计与实现.prj
- 帕鲁介绍的PPT备份没什么好下的
- demo1-202405
- 两种方式修改Intel网卡MAC地址
- 服务器搭建所需资源:static文件夹
- Vue02的源码学习资料
- Python 程序语言设计模式思路-行为型模式:访问者模式:在不改变被访问对象结构的情况下,定义对其元素的新操作
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈