clear; clc;
tic;
options = gaoptimset('PopulationSize', 300, 'Generations', 24); % 遗传算法相关配置
fun = @fitnessfun; % 设置适应度函数句柄
nonlcon = @nonlconfun; % 设置非线性约束函数句柄
nvars = 10; % 自变量个数
A = []; b = [];
Aeq = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1];
beq = [1];
lb = [-1; -1; -1; -1; -1; -1; -1; -1; -inf; -inf];
ub = [1; 1; 1; 1; 1; 1; 1; 1; inf; inf];
% 添加收敛曲线绘制函数
options.PlotFcns = {@gaplotbestf};
[x_best, fval] = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options);
toc;
function [c, ceq] = nonlconfun(x) % 编写非线性约束函数
c = [ ];
ceq(1) = x(1)^2 + x(5)^2 - 1;
ceq(2) = x(2)^2 + x(6)^2 - 1;
ceq(3) = x(3)^2 + x(7)^2 - 1;
ceq(4) = x(4)^2 + x(8)^2 - 1;
end
function f = fitnessfun(x) % 编写适应度函数(即目标函数)
n1 = [1+1i, -1+1i, -1-1i, 1-1i] / sqrt(2);
n2 = norm(n1);
n3 = [x(1) + x(5)*1i, x(2) + x(6)*1i, x(3) + x(7)*1i, x(4) + x(8)*1i];
n4 = norm(n3);
f = x(9) * norm((n1 / n2) - (n3 / n4)) + x(10) * 2;
end
这是代码,设置的迭代次数是24次,而实际只迭代了5次,求求大神解答
tic;
options = gaoptimset('PopulationSize', 300, 'Generations', 24); % 遗传算法相关配置
fun = @fitnessfun; % 设置适应度函数句柄
nonlcon = @nonlconfun; % 设置非线性约束函数句柄
nvars = 10; % 自变量个数
A = []; b = [];
Aeq = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1];
beq = [1];
lb = [-1; -1; -1; -1; -1; -1; -1; -1; -inf; -inf];
ub = [1; 1; 1; 1; 1; 1; 1; 1; inf; inf];
% 添加收敛曲线绘制函数
options.PlotFcns = {@gaplotbestf};
[x_best, fval] = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options);
toc;
function [c, ceq] = nonlconfun(x) % 编写非线性约束函数
c = [ ];
ceq(1) = x(1)^2 + x(5)^2 - 1;
ceq(2) = x(2)^2 + x(6)^2 - 1;
ceq(3) = x(3)^2 + x(7)^2 - 1;
ceq(4) = x(4)^2 + x(8)^2 - 1;
end
function f = fitnessfun(x) % 编写适应度函数(即目标函数)
n1 = [1+1i, -1+1i, -1-1i, 1-1i] / sqrt(2);
n2 = norm(n1);
n3 = [x(1) + x(5)*1i, x(2) + x(6)*1i, x(3) + x(7)*1i, x(4) + x(8)*1i];
n4 = norm(n3);
f = x(9) * norm((n1 / n2) - (n3 / n4)) + x(10) * 2;
end
这是代码,设置的迭代次数是24次,而实际只迭代了5次,求求大神解答