function [blackHole, bestSolution, blackHoleCost, allBestCost] = BH_Func(blackHole)
locationOfStars = repmat(blackHole.varMin ,blackHole.numOfStars,1)...
+ repmat(blackHole.varMax - blackHole.varMin ,blackHole.numOfStars,1).*rand(blackHole.numOfStars,blackHole.nVar);
allBestCost = zeros(blackHole.maxIter,1);
for iter = 1:blackHole.maxIter
% Evaluation
starsCost = ObjectiveFunc(locationOfStars);
% Selection the Best Star that has the best fitness value as the Black Hole
[blackHoleCost, BH_ind] = max(starsCost);
% Change the location of each star and compute new fitness values
locationOfStars = locationOfStars + (repmat(locationOfStars(BH_ind),blackHole.numOfStars,blackHole.nVar) - locationOfStars)...
.*unifrnd(0,1,blackHole.numOfStars,blackHole.nVar);
starsCost = ObjectiveFunc(locationOfStars);
% If there is a star reaches a location with lower cost than the black hole, exchange their locations
if blackHoleCost < max(starsCost)
[blackHoleCost, BH_ind] = max(starsCost);
end
% store the best cost value
allBestCost(iter) = blackHoleCost;
if iter<blackHole.maxIter
% Compute the Radius of the Event Horizon of the Black Hole
R = blackHoleCost / sum(starsCost);
distances = (sqrt(sum(locationOfStars-repmat(locationOfStars(BH_ind),blackHole.numOfStars,blackHole.nVar),2).^2))';
crosserStarsInd = find(distances<R);
if length(crosserStarsInd)>0
for i=1:length(crosserStarsInd)
locationOfStars(crosserStarsInd(i),:) = unifrnd(0,1,blackHole.nVar,1);
end
end
end
end
bestSolution = locationOfStars(BH_ind,:);
disp(['BH_index is' num2str(BH_ind)]);
end
matlab-基于matlab的黑洞优化算法仿真-源码
版权申诉
117 浏览量
2021-09-29
01:34:40
上传
评论
收藏 2KB RAR 举报
mYlEaVeiSmVp
- 粉丝: 1882
- 资源: 19万+