目录
前言
学生心理优化算法(Student psychology based optimization algorithm,SPBO)是Das等人模拟学生在考试中争取最高分数的心理,于2020年提出的一种模拟学生心理的元启发式智能优化算法,具有数学模型简单、设置参数少、易于编程实现等特点。
一、SPBO算法
学生心理优化算法认为:
- 学生的表现可用考试成绩来衡量,在考试中取得最高分的学生被认为是班上最好的学生;
- 要想成为最好的学生,他们需要对每一门学科投入更多的精力;
- 学生对一门学科的努力学习程度取决于学生对该学科的兴趣;
- 学生成绩的提高取决于他们的努力;
- 学生付出的努力取决于学生的心理;
- 学生对任何学科的努力都取决于学生的能力、效率以及对该学科的兴趣。
基于此,SPBO算法认为学生的表现取决于学生的心理,并将一个班级的学生按照科目成绩分为最好学生、好学生、普通学生、尝试随机改进的学生四类,并针对每个不同类型的学生分别训练。
记N为班级总人数(种群个数),D为学科数目(维度),和 分别表示最好学生和学生 i 科目 j(于第t次考试)的考试成绩并将最好学生和学生 i的考试成绩分别记录在变量 和 中,j=1,…,D;i=1,…,N。则对于第 t+1次考试,四类学生各科目的考试成绩分别按如下方式进行更新:
1、最好学生
学生 i是从班上随机选取的一名学生; 为学生 i科目 j的考试成绩(第 t次考试);为[0,1]中的随机数;k是随机选取的 1或 2。
2、好学生
表示第j门考试科目的全班平均数。好学生是按如下方法来选择式(2)或(3)
的:从(0,1)中随机取两个数 r1和r2,若 r2<r1,则选择式(2);否则选择式(3)。
3、普通学生
4、尝试随机改进的学生
其中、分别表示科目j考试的最高分和最低分,上式表示是最高分和最低分中间随机的一个数。
从上面的四个阶段的迭代公式可以看出前三个部分的学生都会在自身的基础上受到其他的人影响,最好的学生受到学科最优秀的人影响;好学生可能受到硬性也有可能受到班级平均分的影响; 普通学生只收到 的影响;而差学生不受其他人的影响,范围只是随机在最大最小值之间取一个。
二、代码设计
1.需要的文件
文件如下,主要是Functions.m、initialization.m、Main_SPBO.m、SPBO.m这四个文件。
2 、Functions.m文件
保存了5个测试函数,代码如下:
function [mini,maxi,variable,fobj] = Functions(F)
switch F
case 'F1'
fobj = @F1;
mini=-5.12;
maxi=5.12;
variable=10;
case 'F2'
fobj = @F2;
mini=-10;
maxi=10;
variable=10;
case 'F3'
fobj = @F3;
mini=-100;
maxi=100;
variable=10;
case 'F4'
fobj = @F4;
mini=-5.12;
maxi=5.12;
variable=10;
case 'F5'
fobj = @F5;
mini=-1.28;
maxi=1.28;
variable=10;
end
end
% Step
% F1
function o = F1(x)
o=sum(((x+.5)).^2);
end
% Sum Square
% F2
function o = F2(x)
variable=size(x,2);
o=sum([1:variable].*(x.^2));
end
% Sphere
% F3
function o = F3(x)
o=sum((x).^2);
end
% Rastrigin
% F4
function o = F4(x)
variable=size(x,2);
o=sum(x.^2-10*cos(2*pi.*x))+10*variable;
end
% Quartic
% F5
function o = F5(x)
variable=size(x,2);
o=sum([1:variable].*(x.^4));
end
3、initialization.m文件
初始化函数,代码如下:
function X=initialization(student,variable,maxi,mini)
Boundary_no= size(maxi,2); % numnber of boundaries
% If the boundaries of all variables are equal and user enter a single number for both maxi and mini
if Boundary_no==1
X=mini+rand(student,variable).*(maxi-mini);
end
display (X);
% If each variable has a different mini and maxi
if Boundary_no>1
for i=1:1:variable
maxi_i=maxi(i);
mini_i=mini(i);
X(:,i)=mini_i+rand(student,1).*(maxi_i-mini_i);
end
end
4、SPBO.M文件
函数的主要循环部分:
这里对于对于学生的分类,对应最好的适应度函数的学生是最好的学生,但是对于好学生和普通学生的分类,这里是用随机数来确定的。check=rand(student,1); mid=rand(student,1);
比较上面两个随机向量,如果check<mid则将该粒子分为好学生,否则分成普通学生,在根据不同的公式迭代位置。
function [Best_fitness,Best_student,Convergence_curve]=SPBO(student,Max_iteration,maxi,mini,variable,fobj)
display('SPBO is optimizing your problem');
%Initialize the set of random solutions
X=initialization(student,variable,maxi,mini);
sol=zeros(1,variable);
ans=inf;
Convergence_curve=zeros(1,Max_iteration);
Objective_values = zeros(1,size(X,1));
% Calculate the fitness of the first set and find the Best_fitness one
for i=1:student
Objective_values(i,1)=fobj(X(i,:));
sol(i,:)=X(i,:);
ans(i,1)=Objective_values(i,1);
end
% display (sol);
% display (ans);
%找到最优的粒子,以及粒子(学生)位置(各科成绩)信息。
Best_fitness = min(ans);
for ft=1:1:student
if Best_fitness==ans(ft,1)
Best_student=sol(ft,:);
end;
end;
%Main loop
for t=1:1:Max_iteration
for do=1:1:variable
sum=zeros(1,variable);
for gw=1:1:variable
for fi=1:1:student
sum(1,gw)=sum(1,gw)+sol(fi,gw);
end;
mean(1,gw)=sum(1,gw)/student; %计算所有学科(维度)的平均数
end;
par=sol;
par1=sol;
check=rand(student,1);
mid=rand(student,1);
for dw=1:1:student
% Best Student
if Best_fitness==ans(dw,1)
jg=ans(randperm(numel(ans),1)); %随机选取一个学生
for oi=1:1:student
if jg==ans(oi,1)
lk=oi;
end;
end;
par1(dw,do)=par(dw,do)+(((-1)^(round(1+rand)))*rand*(par(dw,do)-par(lk,do))); % Equation (1)
else if check(dw,1)<mid(dw,1)
% Good Student
rta=rand;
if rta>rand
par1(dw,do)=Best_student(1,do)+(rand*(Best_student(1,do)-par(dw,do))); % Equation (2a)
else
par1(dw,do)=par(dw,do)+(rand*(Best_student(1,do)-par(dw,do)))+((rand*(par(dw,do)-mean(1,do)))); % Equation (2b)
end;
else
an=rand;
% Average Student
if rand>an
par1(dw,do)=par(dw,do)+(rand*(mean(1,do)-par(dw,do))); % Equation (3)
else
% Students who improves randomly
par1(dw,do)=mini+(rand*(maxi-mini)); % Equation (4)
end;
end;
end;
end;
% Boundary checking of the improvement of the students
for z=1:1:student
if par1(z,do)>maxi
par1(z,do)=maxi;
else if par1(z,do)<mini
par1(z,do)=mini;
end;
end;
end;
X=par1;
for i=1:1:size(X,1)
% Calculate the objective values
Objective_values(i,1)=fobj(X(i,:));
end;
fun1=Objective_values;
% Update the solution if there is a better solution
for vt=1:1:student
if ans(vt,1)>fun1(vt,1)
ans(vt,1)=fun1(vt,1);
sol(vt,:)=par1(vt,:);
end;
end;
Best_fitness1=min(ans);
for fo=1:1:student
if Best_fitness1==ans(fo,1)
Best_student1=sol(fo,:);
end;
end;
% Update the best student
if Best_fitness>Best_fitness1
Best_fitness=Best_fitness1;
Best_student=Best_student1;
end;
end;
% Display the iteration and Best_fitness optimum obtained so far
Convergence_curve(t)=Best_fitness;
display (t);
display (Best_fitness);
end
5、Main_SPBO.M
运行代码部分,设计的参数以及展示结果,只要运行该文件就可以运行SPBO算法。
clear all
clc
student=20; % Number of student (population)
Function_name='F5'; % Name of the test function that can be from F1 to F23
Max_iteration=1000; % Maximum number of iterations
% Load details of the selected benchmark function
[mini,maxi,variable,fobj]=Functions(Function_name);
%Solution obtained using SPBO
[Best_fitness,Best_student,Convergence_curve]=SPBO(student,Max_iteration,maxi,mini,variable,fobj);
% Converging Curve
figure (1)
plot (Convergence_curve);
title('Convergence curve')
xlabel('Iteration');
ylabel('Fitness of best student so far');
display(['The best solution obtained by SPBO is : ', num2str(Best_student)]);
display(['The best optimal value of the objective funciton found by SPBO is : ', num2str(Best_fitness)]);
实验结果
总结
可以看出迭代效果不错,可以尝试更新目标函数以及其他的参数多实验几次。
参考文件:
[1]张伟,王勇,张宁.采用混合策略的改进学生心理优化算法[J].计算机应用研究,2022,39(06):1718-1724.DOI:10.19734/j.issn.1001-3695.2021.11.0630.
[2]Das B, Mukherjee V, Das D. Student psychology based optimization algorithm: A new population based optimization algorithm for solving optimization problems[J]. Advances in Engineering software, 2020, 146: 102804.