首页 技术 正文
技术 2022年11月22日
0 收藏 840 点赞 4,279 浏览 3133 个字

1. 适应度函数:

function z=chaffer(x)%chaffer函数x=(0...0) f(x)=0 x[-10,10]%%没测
n=10;
s1=0;
for i=1:n
s1=s1+x(i)^2;
end
z=((sin(sqrt(s1)))^2-0.5)/(1+0.001*s1)+0.5;
end

2. EHO主函数

% ----------------------------------------------------------
% Title: Elephant Herbing Optimization Algorithm
% Institution: XI'AN POLYTECHNIC UNIVERSITY
% Author: Liwenchao
% Time: 2020-11-8
% ----------------------------------------------------------
clc
clear
% ----------------------------
% Definition of Problems
% ----------------------------CostFunction = @(x) chaffer(x); % cost function
dim_Var = 10; % variables of dimensions
VarMin = -32.768; % lower boundary
VarMax = 32.768; % upper boundary% -------------------------------------------------------------------------
% Setting Parameters of Elephant Herbing Optimization Algorithm
% -------------------------------------------------------------------------alpha = 0.75; % alpha is a scale factor that determines the influence of matriarch on elephant
beta = 0.01; % beta is a scale factor that determines the influence of the center of clan on elephant
epoches = 1000; % the maximum number of epoches
num_clan = 5; % the number of clans
num_pop = 10; % the number of elephants in each clans
num_male = 1; % leave family group % -----------------------------------
% Initialization of Population
% -----------------------------------init_pop = VarMin + rand(num_clan*num_pop, dim_Var) .* (VarMax - VarMin);
pop_fitness = zeros(num_pop, 1);
fit_clan = zeros(num_pop, 1);
pop_best_fitness = zeros(epoches, 1);for n=1: num_clan*num_pop
pop_fitness(n) = CostFunction(init_pop(n, :));
end
[pop_fbest, pop_best_loc] = min(pop_fitness);
best_pop = init_pop(pop_best_loc, :);%------------------------------------------
% EHO generation starts ......
%------------------------------------------
for iter=1:epoches
for j=1: num_clan
clan = init_pop((j-1)*num_pop +1: j * num_pop, :);
for k=1: num_pop
fit_clan(k,:) = CostFunction(clan(k, :));
end
% best fitness value and its location
[fbest, best_loc] = min(fit_clan);
clan_best = clan(best_loc, :);
% worst fitness value and its location
[fworst, worst_loc] = max(fit_clan);
clan_worst = clan(worst_loc, :);
for k=1:num_pop
if any((k~=best_loc)&(k~=worst_loc))
% update elephant position in clan except best and worst elephant
clan(k, :) = clan(k, :) + alpha*(clan_best - clan(k, :))*rand;
elseif k==best_loc
% update leader or matriarch
clan_center = sum(clan) / num_pop;
clan(k, :) = beta*clan_center;
elseif k==worst_loc
% update worst elephant or male
clan(k, :) = VarMin + rand* (VarMax - VarMin + 1);
end
init_pop((j-1)*num_pop +1: j * num_pop, :) = clan;
end
end
% ----------------------------------------------------------------
% evaluation population by the newly updated positions
% ----------------------------------------------------------------
for n=1: num_clan*num_pop
pop_fitness(n) = CostFunction(init_pop(n, :));
end
[new_pop_fbest, new_pop_best_loc] = min(pop_fitness);
if new_pop_fbest<pop_fbest
pop_fbest = new_pop_fbest;
pop_best_loc = new_pop_best_loc;
init_pop(pop_best_loc, :) = init_pop(new_pop_best_loc, :);
end
pop_best_fitness(iter, :) = pop_fbest;
disp(['Iteration ' num2str(iter) ': Best Cost = ' num2str(pop_fbest)]);
disp(init_pop(pop_best_loc, :));
end

3. 可视化

% -------------------------------------
% visualization
% -------------------------------------
figure;
%plot(pop_best_fitness)
semilogy(pop_best_fitness);
xlabel('iteration');
ylabel('fitness');
legend('chaffer');
title('Elephant Herbing Optimization')

4. 结果显示

上一篇: array_filter()用法
下一篇: BUUCTF pwn一分题目
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,906
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727