clc
clear
close all
% Number of search agents (population size in optimization algorithms)
SearchAgents_no = 30;  
% Dimension of the problem (number of decision variables)
dim = 20;  
% Objective function: 'cec22_func' (CEC 2022 benchmark function)
fhd = str2func('cec22_func');  
% Function number (specific benchmark function in CEC 2022 suite)
Fnum = 12;  
% Maximum number of iterations (stopping condition)
Max_iteration = 1000;  
% Lower bound of the search space (for each variable)
lb = -100;  
% Upper bound of the search space (for each variable)
ub = 100;  

%% optimization algorithm
for i = 1:12
Function_name = i;
% [Best_score,Best_pos,cg_curve,search_history,ave_fit,x_1st]=GWO2(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
% Best_score
[Best_score,Best_pos,cg_curve,search_history,ave_fit,x_1st] = AOOv4(fhd,dim,SearchAgents_no,Max_iteration,lb,ub,Function_name);
Best_score
%% plot
figure(i);
set(gcf, 'Position', [150, 350, 1500, 300]);


subplot_width = 1500 / 5;  
subplot_height = 300 / 1;   
padding = 0.05;  
effective_width = (1 - padding * 4) / 5.5;
effective_height = 0.7 ;


left_positions = 0.04 + (0:4) * (effective_width + padding);


adjustedColormap = parula;


colormap(adjustedColormap);
subplot('Position', [left_positions(1), padding*3, effective_width, effective_height]);
func_plot(Function_name);
title(strcat('F', num2str(Function_name)));
xlabel('x_1');
ylabel('x_2');
zlabel('fitness');
set(gca, 'Box', 'on', 'BoxStyle', 'full', 'LineWidth', 0.25);
view(3);
grid on;

% search history
subplot('Position', [left_positions(2), padding*3, effective_width, effective_height]);
grid off;
func_plot_2(Function_name);
hold on;
scatter(search_history(:,1), search_history(:,2), '.');
hold on;
scatter(Best_pos(:,1), Best_pos(:,2), 'r.');
title('Search history');
xlabel('x_1');
ylabel('x_2');
xlim([lb, ub]);
ylim([lb, ub]);
set(gca, 'color', 'none');
grid on;

% average fitness
subplot('Position', [left_positions(3), padding*3, effective_width, effective_height]);
semilogy(ave_fit, 'Linewidth', 1);
grid on;
title('Average fitness');
xlabel('Function calls');
ylabel('Fitness');
xlim([0, Max_iteration ]);
set(gca, 'XTickLabel', arrayfun(@(x) num2str(x), 0:6000:30000, 'UniformOutput', false), 'FontAngle', 'normal');


% Trajectory of 1st dimension
subplot('Position', [left_positions(4), padding*3, effective_width, effective_height]);
plot(x_1st(:,1), 'Linewidth', 1);
title('Trajectory of 1st dimension');
grid on;
xlabel('Function calls');
ylabel('value');
xlim([0, Max_iteration]);
set(gca, 'XTickLabel', arrayfun(@(x) num2str(x), 0:6000:30000, 'UniformOutput', false), 'FontAngle', 'normal');

% 收敛曲线图
subplot('Position', [left_positions(5), padding*3, effective_width, effective_height]);
semilogy(cg_curve, 'Linewidth', 1);
title('Convergence curve');
xlabel('Function calls');
ylabel('Best score');
axis tight;
grid on;
box on;
legend('AOO', 'fontsize', 6, 'location', 'best');
set(gca, 'color', 'none');
xlim([0, Max_iteration]);
set(gca, 'XTickLabel', arrayfun(@(x) num2str(x), 0:6000:30000, 'UniformOutput', false), 'FontAngle', 'normal');
saveas(gcf, sprintf('F%d.eps', Function_name), 'epsc'); % 保存为 EPS
saveas(gcf, sprintf('F%d.svg', Function_name), 'svg');  % 保存为 SVG
Dive_Explor_Exploit(SearchAgents_no,Max_iteration,dim,Function_name,search_history)

end