%**************************************************************************
% 随机分形搜索算法
%**************************************************************************
function [fbest,pbest, cg_curve] = SFS(Start_Point,Maximum_Generation,lb,ub,Ndim,fobj)
S.Start_Point = Start_Point;
S.Maximum_Generation = Maximum_Generation;
S.Maximum_Diffusion = 2;
S.Walk = 1; % *Important
S.Function_Name = fobj;
S.Ndim = Ndim;
if length(lb)==1
S.Lband = ones(1, S.Ndim)*(lb);
S.Uband = ones(1, S.Ndim)*(ub);
end
%Creating random points in considered search space=========================
point = repmat(S.Lband,S.Start_Point,1) + rand(S.Start_Point, S.Ndim).* ...
(repmat(S.Uband - S.Lband,S.Start_Point,1));
%==========================================================================
%Calculating the fitness of first created points===========================
FirstFit = [];
for i = 1 : size(point,1)
FirstFit(i,:) = feval(S.Function_Name, point(i,:));
end
[Sorted_FitVector, Indecis] = sort(FirstFit);
point = point(Indecis,:);%sorting the points based on obtaind result
%==========================================================================
%Finding the Best point in the group=======================================
BestPoint = point(1, :);
F = Sorted_FitVector(1);%saving the first best fitness
%==========================================================================
%Starting Optimizer========================================================
for G = 1 : S.Maximum_Generation
New_Point = [];%creating new point
FitVector = [];%creating vector of fitness functions
%diffusion process occurs for all points in the group
for i = 1 : size(point,1)
%creating new points based on diffusion process
[NP, fit] = Diffusion_Process(point(i,:),S,G,BestPoint);
New_Point = [New_Point;NP];
FitVector = [FitVector,fit];
end
%======================================================================
%updating best point obtained by diffusion process
BestFit = min(FitVector);
BestPoint = New_Point(find(FitVector == BestFit),:);
S.Start_Point = size(New_Point,1);
fit = FitVector';
[sortVal, sortIndex] = sort(fit);
%Starting The First Updating Process====================================
for i=1:1:S.Start_Point
Pa(sortIndex(i)) = (S.Start_Point - i + 1) / S.Start_Point;
end
RandVec1 = randperm(S.Start_Point);
RandVec2 = randperm(S.Start_Point);
for i = 1 : S.Start_Point
for j = 1 : size(New_Point,2)
if rand > Pa(i)
P(i,j) = New_Point(RandVec1(i),j) - rand*(New_Point(RandVec2(i),j) - ...
New_Point(i,j));
else
P(i,j)= New_Point(i,j);
end
end
end
P = Bound_Checking(P,S.Lband,S.Uband);%for checking bounds
Fit_FirstProcess = [];
for i = 1 : S.Start_Point
Fit_FirstProcess = [Fit_FirstProcess;feval(S.Function_Name,P(i,:))];
end
for i=1:S.Start_Point
if Fit_FirstProcess(i,:)<=fit(i,:)
New_Point(i,:)=P(i,:);
fit(i,:)=Fit_FirstProcess(i,:);
end
end
FitVector = fit;
%======================================================================
[SortedFit,SortedIndex] = sort(FitVector);
New_Point = New_Point(SortedIndex,:);
BestPoint = New_Point(1,:);%first point is the best
F = [F;FitVector(1,1)];
F = sort(F);
%======================================================================
fbest = FitVector(1,:);
if fbest <= F(1,:)
pbest = New_Point(1,:);
fbest = F(1,:);
end
point = New_Point;
%Starting The Second Updating Process==================================
Pa = sort(SortedIndex/S.Start_Point, 'descend');
for i = 1 : S.Start_Point
if rand > Pa(i)
%selecting two different points in the group
R1 = ceil(rand*size(point,1));
R2 = ceil(rand*size(point,1));
while R1 == R2
R2 = ceil(rand*size(point,1));
end
if rand < .5
ReplacePoint = point(i,:) - ...
rand * (point(R2,:) - BestPoint);
ReplacePoint = Bound_Checking(ReplacePoint,S.Lband,S.Uband);
else
ReplacePoint = point(i,:) + ...
rand * (point(R2,:) - point(R1,:));
ReplacePoint = Bound_Checking(ReplacePoint,S.Lband,S.Uband);
end
if feval(S.Function_Name, ReplacePoint) < ...
feval(S.Function_Name, point(i,:))
point(i,:) = ReplacePoint;
end
end
end
%======================================================================
fbest = min(F);
cg_curve (G) =fbest;
end
end
function p = Bound_Checking(p,lowB,upB)
for i = 1 : size(p,1)
upper = double(gt(p(i,:),upB));
lower = double(lt(p(i,:),lowB));
up = find(upper == 1);
lo = find(lower == 1);
if (size(up,2)+ size(lo,2) > 0 )
for j = 1 : size(up,2)
p(i, up(j)) = (upB(up(j)) - lowB(up(j)))*rand()...
+ lowB(up(j));
end
for j = 1 : size(lo,2)
p(i, lo(j)) = (upB(lo(j)) - lowB(lo(j)))*rand()...
+ lowB(lo(j));
end
end
end
end
%This function is used to mimic diffusion process, and creates some
%new points based on Gaussian Walks.
%**************************************************************************
%The input function is: %
%Point: the input point which is going to be diffused %
%S: structure of problem information %
%g: generation number %
%BestPoint: the best point in group % %
%==========================================================================
%The output function is: %
%createPoint: the new points created by Diffusion process %
%fitness: the value of fitness function %
%**************************************************************************
function [createPoint, fitness] = Diffusion_Process(Point,S,g,BestPoint)
%calculating the maximum diffusion for each point
NumDiffiusion = S.Maximum_Diffusion;
New_Point = Point;
%Diffiusing Part*******************************************************
for i = 1 : NumDiffiusion
%consider which walks should be selected.
if rand < S.Walk
GeneratePoint = normrnd(BestPoint, (log(g)/g)*(abs((Point - BestPoint))), [1 size(Point,2)]) + ...
(randn*BestPoint - randn*Point);
else
GeneratePoint = normrnd(Point, (log(g)/g)*(abs((Point - BestPoint))),...
[1 size(Point,2)]);
end
New_Point = [New_Point;GeneratePoint];
end
%check bounds of New Point
New_Point = Bound_Checking(New_Point,S.Lband,S.Uband);
%sorting fitness
fitness = [];
for i = 1 : size(New_Point,1)
fitness = [fitness;feval(S.Function_Name,New_Point(i,:))];
end
[fit_value,fit_index] = sort(fitness);
fitness = fit_value(1,1);
New_Point = New_Point(fit_index,:);
createPoint = New_Point(1,:);
%======================================================================
end