樽海鞘算法解决约束优化问题
1. 约束优化问题
\[Minimize \qquad f(Y) = (y_3+2)y_2y_1^2 \\
s.t.\begin{cases}
g_1(Y) = 1 - \frac{y_2^3y_3}{71785y_1^4} \leq 0 \\
g_2(Y) = \frac{4y_2^2 - y_1y_2}{12566(y_2y_1^3 - y_1^4)} + \frac{1}{5108y_1^2} - 1 \leq 0 \\
g_3(Y) = 1 - \frac{140.45y_1}{y_2^2y_3} \leq 0 \\
g_4(Y) = 1 - \frac{y_1 + y_2}{1.5} - 1 \leq 0 \\
\end{cases}
\]
\[0.05 \leq y_1 \leq 2; \qquad 0.25 \leq y_2 \leq 1.3; \qquad 2 \leq y_3 \leq 15
\]
2. 樽海鞘算法解决约束优化问题
function [fitness_set, best_Solves] = SSA()
%% 问题参数设置
ub = [2, 1.3, 15]; % 上界
lb = [0.05, 0.25, 2]; % 下界
Max_iter = 300; % 最大迭代次数
n = 50; % 种群数目
dim = 3; % 维数
Convergence_curve = zeros(1,Max_iter); % 算法每一次迭代的最好值
run_times = 30; % 运行次数
fitness_set = zeros(run_times,1);
best_Solves = zeros(run_times,dim);
for run = 1:run_times
%% 初始化
FoodPosition = zeros(1,dim); % 食物位置
FoodFitness = inf; % 食物适应度值
%% 初始化种群
SalpPositions = zeros(n,dim);
SalpFitness = zeros(n,1);
for i=1:n
for j=1:dim
SalpPositions(i,j) = z(i,j)*(ub(j) - lb(j)) + lb(j); % 初始化个体
end
SalpFitness(i) = fobj(SalpPositions(i,:));
if SalpFitness(i)<FoodFitness
FoodPosition = SalpPositions(i,:);
FoodFitness = SalpFitness(i);
end
end
%% 开始仿真
l = 0; % 迭代次数
while (l<=Max_iter)
c1 = 2*exp(-(4*l/Max_iter)^2);
for i=1:n
SalpPositions = SalpPositions';
if i<=n/2
for j=1:dim
c2 = rand;
c3 = rand;
if c3<0.5
SalpPositions(j,i) = FoodPosition(j)+c1*((ub(j)-lb(j))*c2+lb(j));
else
SalpPositions(j,i) = FoodPosition(j)-c1*((ub(j)-lb(j))*c2+lb(j));
end
end
elseif i>n/2 && i<n+1
point1 = SalpPositions(:,i-1);
point2 = SalpPositions(:,i);
SalpPositions(:,i)=(point2+point1)/2; % SSA算法追随者更新公式
end
SalpPositions = SalpPositions';
end
%% 边界处理<-修改了
for i=1:n
Tp = SalpPositions(i,:)>ub;
Tm = SalpPositions(i,:)<lb;
SalpPositions(i,:) = (SalpPositions(i,:).*(~(Tp+Tm))) + ub.*Tp + lb.*Tm;
SalpFitness(i) = fobj(SalpPositions(i,:));
%% 精英保留机制
if SalpFitness(i)<FoodFitness
FoodPosition = SalpPositions(i,:);
FoodFitness = SalpFitness(i);
end
end
l = l + 1; % 更新迭代次数
Convergence_curve(l) = FoodFitness;
end
best_Solves(run,:) = FoodPosition;
fitness_set(run) = FoodFitness; % 算法每次运行的结果
end
end
%% 拉伸弹簧
function out = fobj(X)
y1 = X(:,1);
y2 = X(:,2);
y3 = X(:,3);
% 目标函数
fx = (y3+2).*y2.*y1.^2;
% 不等式约束
g(:,1) = 1-(y2.^3.*y3)./(71785.*y1.^4);
g(:,2) = (4.*y2^2-y1.*y2)./(12566.*(y2.*y1.^3-y1.^4))+(1./(5108.*y1.^2)) -1;
g(:,3) = 1-(140.45.*y1./(y2.^2.*y3));
g(:,4) = (y1+y2)./1.5-1;
% 惩罚项定义
pp=10^9;
penalty =[];
for i=1:size(g,1)
for j=1:size(g,2)
if g(i,j)>0
penalty(i,j) = pp.*g(i,j);
else
penalty(i,j) = 0;
end
end
end
out = fx + sum(penalty,2);
end