Problem: GWO and Objective Functions Implementation

The problem requires implementing specified MATLAB functions (objective functions, initialization, GWO optimizer, plot) for optimization tasks.


Input/Output: The problem takes specifications of MATLAB functions (Ufun, F1-F23, Get_Functions_details, initialization, GWO, func_plot) including names, parameters, algorithms, and math definitions; plus function names, search agent counts, max iterations for GWO. It generates correct MATLAB function code files, convergence curves and best solutions from GWO, and 3D surface plots from func_plot. Each spec becomes code; GWO uses initialization (with lb/ub/dim from Get_Functions_details) to optimize F1-F23, outputting results; func_plot uses Get_Functions_details to plot fobj.

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please write a MATLAB function named Ufun that implements a piecewise function calculation. The function is defined as function o = Ufun(x, a, k, m), where x is the input variable, a, k, m are parameters, and o is the output. Compute the output o using element-wise operations: o is the sum of two parts. The first part is k.*((x - a).^m) multiplied by the logical index (x > a) (which is 1 for elements where x > a and 0 otherwise). The second part is k.*((-x - a).^m) multiplied by the logical index (x < -a) (which is 1 for elements where x < -a and 0 otherwise). For elements where x is between -a and a (inclusive), both parts contribute 0 to the sum. Other functions called in the code are assumed to be pre-defined and do not need to be redefined.
function_header: function o=Ufun(x,a,k,m)

end
test_cases: clear; clc;
rng(42);
correct = 32.0000;
x = [-3, -1, 0, 1, 3];
a = 1;
k = 2;
m = 3;
result = Ufun(x, a, k, m);
val = sum(result);
if val~=correct
    error('Wrong')
end
correct = 500.0000;
x = [-4, -6, 2, 3, 5];
a = 2;
k = 5;
m = 3;
result = Ufun(x, a, k, m);
val = sum(result);
if val~=correct
    error('Wrong')
end
correct = 1302.00000;
x = [-1, -2, 5, 6, 4];
a = 1;
k = 6;
m = 3;
result = Ufun(x, a, k, m);
val = sum(result);
if val~=correct
    error('Wrong')
end
disp('Completely right!')

step_number: 2
step_description_prompt: Please use MATLAB code to generate a function named F1 that calculates the sum of the squares of the elements in the input vector x.
Function Definition: function o = F1(x)
Algorithm Flow: Compute the element-wise square of x and then sum all the squared elements to get the output o.

Please use MATLAB code to generate a function named F2 that calculates the sum of the absolute values of the elements in the input vector x plus the product of the absolute values of the elements in x.
Function Definition: function o = F2(x)
Algorithm Flow: Compute the absolute values of all elements in x, sum these absolute values, compute the product of these absolute values, and then add the sum and the product to get the output o.

Please use MATLAB code to generate a function named F3 that calculates the sum of the squares of the cumulative sums of the first i elements of the input vector x for i from 1 to the number of elements in x.
Function Definition: function o = F3(x)
Algorithm Flow: 
1. Get the number of elements dim in x (dim = size(x, 2)).
2. Initialize the output o to 0.
3. Loop from i = 1 to dim:
   a. Compute the sum of the first i elements of x.
   b. Square this sum.
   c. Add the squared value to o.
4. Return o as the final result.

Please use MATLAB code to generate a function named F4 that calculates the maximum value of the absolute values of the elements in the input vector x.
Function Definition: function o = F4(x)
Algorithm Flow: Compute the absolute values of all elements in x and then take the maximum value of these absolute values to get the output o.

Please use MATLAB code to generate a function named F5 that calculates the Rosenbrock function value for the input vector x.
Function Definition: function o = F5(x)
Algorithm Flow:
1. Get the number of elements dim in x (dim = size(x, 2)).
2. Compute the sum over i from 1 to dim-1 of [100*(x(i+1) - x(i)^2)^2 + (x(i) - 1)^2] to get the output o.

Please use MATLAB code to generate a function named F6 that calculates the sum of the squares of the absolute values of each element in x plus 0.5.
Function Definition: function o = F6(x)
Algorithm Flow: Compute (element-wise) the absolute value of (each element in x plus 0.5), square each of these values, and then sum all the squared values to get the output o.

Please use MATLAB code to generate a function named F7 that calculates the sum of each element in x raised to the 4th power multiplied by its 1-based index, plus a random number between 0 and 1.
Function Definition: function o = F7(x)
Algorithm Flow:
1. Get the number of elements dim in x (dim = size(x, 2)).
2. Create a 1-based index vector [1:dim].
3. Compute the element-wise product of the index vector and x raised to the 4th power.
4. Sum these products.
5. Add a uniformly distributed random number between 0 and 1 (generated by rand()) to the sum to get the output o.

Please use MATLAB code to generate a function named F8 that calculates the sum of -x(i) multiplied by the sine of the square root of the absolute value of x(i) for each element x(i) in the input vector x.
Function Definition: function o = F8(x)
Algorithm Flow: Compute (element-wise) -x multiplied by the sine of the square root of the absolute value of x, and then sum all these values to get the output o.
test_cases: clear; clc;
rng(42);
correct = 6659.399639;
x = [1.2, -0.5, 2.3, -1.8, 0.7];
val = F1(x) + F2(x) + F3(x) + F4(x) + F5(x) + F6(x) + F7(x) + F8(x);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong')
end
correct = 116785.710695;
x = [1.6, -0.3, 5.6, -1.7, 0.9];
val = F1(x) + F2(x) + F3(x) + F4(x) + F5(x) + F6(x) + F7(x) + F8(x);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong')
end
correct = 24863.690416;
x = [2.5, -1.6, 2.6, -3.2, 1.3];
val = F1(x) + F2(x) + F3(x) + F4(x) + F5(x) + F6(x) + F7(x) + F8(x);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong')
end
disp('Completely right!')

step_number: 3
step_description_prompt: Function o = F9(x) takes input x, computes dim = size(x,2), then o = sum(x.^2 - 10*cos(2*pi.*x)) + 10*dim.
Function o = F10(x) takes input x, computes dim = size(x,2), then o = -20*exp(-0.2*sqrt(sum(x.^2)/dim)) - exp(sum(cos(2*pi.*x))/dim) + 20 + exp(1).
Function o = F11(x) takes input x, computes dim = size(x,2), then o = sum(x.^2)/4000 - prod(cos(x./sqrt(1:dim))) + 1.
Function o = F12(x) takes input x, computes dim = size(x,2), then o = (pi/dim)*(10*((sin(pi*(1 + (x(1)+1)/4))).^2) + sum((((x(1:dim-1)+1)./4).^2).*(1 + 10.*((sin(pi.*(1 + (x(2:dim)+1)/4))).^2))) + ((x(dim)+1)/4).^2) + sum(Ufun(x,10,100,4)). Ufun is assumed to be pre-defined.
Function o = F13(x) takes input x, computes dim = size(x,2), then o = 0.1*((sin(3*pi*x(1))).^2 + sum((x(1:dim-1)-1).^2.*(1 + ((sin(3.*pi.*x(2:dim))).^2)) + ((x(dim)-1).^2)*(1 + ((sin(2*pi*x(dim))).^2))) + sum(Ufun(x,5,100,4)). Ufun is assumed to be pre-defined.
Function o = F14(x) takes input x, first defines matrix aS as [-32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32; -32 -32 -32 -32 -32 -16 -16 -16 -16 -16 0 0 0 0 0 16 16 16 16 16 32 32 32 32 32], then computes bS(j) = sum((x' - aS(:,j)).^6) for j=1:25, finally o = (1/500 + sum(1./([1:25] + bS))).^(-1).
Function o = F15(x) takes input x, first defines vector aK = [0.1957 0.1947 0.1735 0.16 0.0844 0.0627 0.0456 0.0342 0.0323 0.0235 0.0246] and vector bK = [0.25 0.5 1 2 4 6 8 10 12 14 16] then bK = 1./bK, then o = sum((aK - ((x(1).*(bK.^2 + x(2).*bK))./(bK.^2 + x(3).*bK + x(4)))).^2).
Function o = F16(x) takes input x, then o = 4*(x(1)^2) - 2.1*(x(1)^4) + (x(1)^6)/3 + x(1)*x(2) - 4*(x(2)^2) + 4*(x(2)^4).
Function o = F17(x) takes input x, then o = (x(2) - (x(1)^2)*5.1/(4*(pi^2)) + 5/pi*x(1) - 6)^2 + 10*(1 - 1/(8*pi))*cos(x(1)) + 10.
All functions use MATLAB element-wise operations (.*, .^). Called functions (Ufun) are pre-defined and do not need redefinition. Each function name, input, output, and calculation must match exactly.
test_cases: clear; clc;
rng(42);
correct = 143.557619;
x5 = [0.5, 1.2, 0.3, 1.5, 0.8];
x2 = [0.5, 1.2];
x4 = [0.3, 0.7, 1.5, 2.1];
val = F9(x5) + F10(x5) + F11(x5) + F12(x5) + F13(x5) + F14(x2) + F15(x4) + F16(x2) + F17(x2);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
correct = 148.143694;
x5 = [0.6, 2.2, 2.2, 1.6, 0.9];
x2 = [0.1, 1.2];
x4 = [0.3, 0.2, 2.3, 1.1];
val = F9(x5) + F10(x5) + F11(x5) + F12(x5) + F13(x5) + F14(x2) + F15(x4) + F16(x2) + F17(x2);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
correct = 131.306685;
x5 = [0.8, 1.4, 0.3, 1.2, 0.8];
x2 = [0.2, 1.1];
x4 = [0.4, 0.3, 1.1, 2.1];
val = F9(x5) + F10(x5) + F11(x5) + F12(x5) + F13(x5) + F14(x2) + F15(x4) + F16(x2) + F17(x2);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 4
step_description_prompt: Please use MATLAB code to generate functions named F18, F19, F20, F21, F22, and F23. Each function takes a vector x as input and returns a scalar o as output, implementing the corresponding mathematical computation.

1. Function F18:
   - Input: x (a 2-dimensional vector, i.e., x = [x1; x2] or x = [x1, x2])
   - Compute o as the product of two parts:
     Part1 = 1 + (x(1) + x(2) + 1)^2 * (19 - 14*x(1) + 3*(x(1)^2) - 14*x(2) + 6*x(1)*x(2) + 3*(x(2)^2));
     Part2 = 30 + (2*x(1) - 3*x(2))^2 * (18 - 32*x(1) + 12*(x(1)^2) + 48*x(2) - 36*x(1)*x(2) + 27*(x(2)^2));
     o = Part1 * Part2;
   - Return o.

2. Function F19:
   - Predefine constants:
     aH = [3 10 30; 0.1 10 35; 3 10 30; 0.1 10 35]; % 4x3 matrix
     cH = [1 1.2 3 3.2]; % 1x4 vector
     pH = [0.3689 0.117 0.2673; 0.4699 0.4387 0.747; 0.1091 0.8732 0.5547; 0.03815 0.5743 0.8828]; % 4x3 matrix
   - Input: x (a 3-dimensional vector)
   - Initialize o = 0;
   - Loop i from 1 to 4:
     temp_sum = sum(aH(i,:) .* ((x - pH(i,:)) .^ 2));
     o = o - cH(i) * exp(-temp_sum);
   - Return o.

3. Function F20:
   - Predefine constants:
     aH = [10 3 17 3.5 1.7 8; 0.05 10 17 0.1 8 14; 3 3.5 1.7 10 17 8; 17 8 0.05 10 0.1 14]; % 4x6 matrix
     cH = [1 1.2 3 3.2]; % 1x4 vector
     pH = [0.1312 0.1696 0.5569 0.0124 0.8283 0.5886; 0.2329 0.4135 0.8307 0.3736 0.1004 0.9991; 0.2348 0.1415 0.3522 0.2883 0.3047 0.6650; 0.4047 0.8828 0.8732 0.5743 0.1091 0.0381]; % 4x6 matrix
   - Input: x (a 6-dimensional vector)
   - Initialize o = 0;
   - Loop i from 1 to 4:
     temp_sum = sum(aH(i,:) .* ((x - pH(i,:)) .^ 2));
     o = o - cH(i) * exp(-temp_sum);
   - Return o.

4. Function F21:
   - Predefine constants:
     aSH = [4 4 4 4; 1 1 1 1; 8 8 8 8; 6 6 6 6; 3 7 3 7; 2 9 2 9; 5 5 3 3; 8 1 8 1; 6 2 6 2; 7 3.6 7 3.6]; % 10x4 matrix
     cSH = [0.1 0.2 0.2 0.4 0.4 0.6 0.3 0.7 0.5 0.5]; % 1x10 vector
   - Input: x (a 4-dimensional vector)
   - Initialize o = 0;
   - Loop i from 1 to 5:
     diff = x - aSH(i,:);
     dot_product = diff * diff';
     term = -1 / (dot_product + cSH(i));
     o = o + term;
   - Return o.

5. Function F22:
   - Predefine the same aSH and cSH as in F21.
   - Input: x (a 4-dimensional vector)
   - Initialize o = 0;
   - Loop i from 1 to 7:
     diff = x - aSH(i,:);
     dot_product = diff * diff';
     term = -1 / (dot_product + cSH(i));
     o = o + term;
   - Return o.

6. Function F23:
   - Predefine the same aSH and cSH as in F21.
   - Input: x (a 4-dimensional vector)
   - Initialize o = 0;
   - Loop i from 1 to 10:
     diff = x - aSH(i,:);
     dot_product = diff * diff';
     term = -1 / (dot_product + cSH(i));
     o = o + term;
   - Return o.
test_cases: clear; clc;
rng(42);
correct = 16649.063712;
x2 = [0.5, 1.2];  
x3 = [0.3, 0.7, 1.5]; 
x4 = [0.3, 0.7, 1.5, 2.1];
x6 = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2]; 
val = F18(x2) + F19(x3) + F20(x6) + F21(x4) + F22(x4) + F23(x4);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

correct = 17899.312189;
x2 = [0.6, 1.3];  
x3 = [0.5, 1.7, 1.2]; 
x4 = [0.1, 1.5, 1.6, 2.1];
x6 = [0.5, 0.2, 1.6, 0.8, 1.1, 1.2]; 
val = F18(x2) + F19(x3) + F20(x6) + F21(x4) + F22(x4) + F23(x4);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

correct = 42552.178761;
x2 = [0.2, 1.3];  
x3 = [1.3, 0.7, 1.3]; 
x4 = [2.2, 0.6, 1.2, 0.5];
x6 = [0.2, 0.1, 0.7, 0.2, 1.0, 1.1]; 
val = F18(x2) + F19(x3) + F20(x6) + F21(x4) + F22(x4) + F23(x4);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 5
step_description_prompt: Please write a MATLAB function named Get_Functions_details that, given a string input F, returns the corresponding objective function handle fobj, lower bound lb, upper bound ub, and dimension dim of the optimization problem.
The function definition is: function [lb, ub, dim, fobj] = Get_Functions_details(F)
Implement a switch-case structure to handle different values of F. For each case from 'F1' to 'F23', assign the corresponding values as follows:
- Case 'F1': fobj = @F1; lb = -100; ub = 100; dim = 30;
- Case 'F2': fobj = @F2; lb = -10; ub = 10; dim = 30;
- Case 'F3': fobj = @F3; lb = -100; ub = 100; dim = 30;
- Case 'F4': fobj = @F4; lb = -100; ub = 100; dim = 30;
- Case 'F5': fobj = @F5; lb = -30; ub = 30; dim = 30;
- Case 'F6': fobj = @F6; lb = -100; ub = 100; dim = 30;
- Case 'F7': fobj = @F7; lb = -1.28; ub = 1.28; dim = 30;
- Case 'F8': fobj = @F8; lb = -500; ub = 500; dim = 30;
- Case 'F9': fobj = @F9; lb = -5.12; ub = 5.12; dim = 30;
- Case 'F10': fobj = @F10; lb = -32; ub = 32; dim = 30;
- Case 'F11': fobj = @F11; lb = -600; ub = 600; dim = 30;
- Case 'F12': fobj = @F12; lb = -50; ub = 50; dim = 30;
- Case 'F13': fobj = @F13; lb = -50; ub = 50; dim = 30;
- Case 'F14': fobj = @F14; lb = -65.536; ub = 65.536; dim = 2;
- Case 'F15': fobj = @F15; lb = -5; ub = 5; dim = 4;
- Case 'F16': fobj = @F16; lb = -5; ub = 5; dim = 2;
- Case 'F17': fobj = @F17; lb = [-5, 0]; ub = [10, 15]; dim = 2;
- Case 'F18': fobj = @F18; lb = -2; ub = 2; dim = 2;
- Case 'F19': fobj = @F19; lb = 0; ub = 1; dim = 3;
- Case 'F20': fobj = @F20; lb = 0; ub = 1; dim = 6;
- Case 'F21': fobj = @F21; lb = 0; ub = 10; dim = 4;
- Case 'F22': fobj = @F22; lb = 0; ub = 10; dim = 4;
- Case 'F23': fobj = @F23; lb = 0; ub = 10; dim = 4;
The functions F1 to F23 are assumed to be pre-defined and do not need to be implemented in this function.
function_header: function [lb,ub,dim,fobj] = Get_Functions_details(F)

end
test_cases: clear; clc;
rng(42);
test_funcs = {'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', ...
              'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', ...
              'F20', 'F21', 'F22', 'F23'};
total = 0;
correct = 1150.207115;
for i = 1:length(test_funcs)
    [lb, ub, dim, fobj] = Get_Functions_details(test_funcs{i});
    if isscalar(lb) && isscalar(ub)
        x = (lb + ub) / 2 * ones(1, dim);
    else
        x = (lb + ub) / 2;
    end
    fval = fobj(x);
    total = total + sum(lb(:)) + sum(ub(:)) + dim + fval;
end
tol = abs(total-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 6
step_description_prompt: Please write a MATLAB function named initialization that initializes the positions of search agents. The function definition is: function Positions = initialization(SearchAgents_no, dim, ub, lb). The algorithm process is as follows: 1. Compute Boundary_no as the number of columns of ub using size(ub, 2). 2. If Boundary_no equals 1, generate Positions with the formula: rand(SearchAgents_no, dim) .* (ub - lb) + lb. 3. If Boundary_no is greater than 1, loop over each dimension i from 1 to dim: get ub_i = ub(i) and lb_i = lb(i), then assign the i-th column of Positions as rand(SearchAgents_no, 1) .* (ub_i - lb_i) + lb_i. Functions called in the code are assumed to be already defined and do not need to be redefined.
function_header: function Positions=initialization(SearchAgents_no,dim,ub,lb)

end
test_cases: clear; clc;
rng(42);

correct = 85.422048;
X1 = initialization(5, 3, 10, -5);
X2 = initialization(4, 3, [5,10,15], [-2,0,3]);
X3 = initialization(3, 2, 0, 0);
val = sum(X1(:)) + sum(X2(:)) + sum(X3(:));
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

correct = 94.834255;
X1 = initialization(2, 5, 10, -7);
X2 = initialization(4, 3, [5,8,12], [-1,1,3]);
X3 = initialization(2, 2, 1, 4);
val = sum(X1(:)) + sum(X2(:)) + sum(X3(:));
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

correct = 59.955864;
X1 = initialization(6, 2, 8, -8);
X2 = initialization(4, 3, [2,10,12], [-2,2,6]);
X3 = initialization(3, 4, 0, 1);
val = sum(X1(:)) + sum(X2(:)) + sum(X3(:));
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 7
step_description_prompt: Please write a MATLAB function named GWO that implements the Grey Wolf Optimizer algorithm to optimize a given objective function. The function signature must be exactly: [Alpha_score, Alpha_pos, Convergence_curve] = GWO(SearchAgents_no, Max_iter, lb, ub, dim, fobj). 

The algorithm flow must be implemented as follows:
1. Initialize Alpha_pos as a 1-by-dim zero vector and Alpha_score as infinity.
2. Initialize Beta_pos as a 1-by-dim zero vector and Beta_score as infinity.
3. Initialize Delta_pos as a 1-by-dim zero vector and Delta_score as infinity.
4. Generate initial positions using the pre-defined 'initialization' function with inputs SearchAgents_no, dim, ub, lb, and store the result in the variable Positions.
5. Initialize Convergence_curve as a 1-by-Max_iter zero vector.
6. Set the iteration counter l to 0.
7. Start a while loop that runs while l is less than Max_iter:
   a. For each agent i from 1 to the number of rows in Positions:
      i. Create Flag4ub as a logical vector where Positions(i,:) is greater than ub.
      ii. Create Flag4lb as a logical vector where Positions(i,:) is less than lb.
      iii. Adjust Positions(i,:) to stay within bounds using: Positions(i,:) = (Positions(i,:) .* (~(Flag4ub + Flag4lb))) + ub .* Flag4ub + lb .* Flag4lb.
      iv. Compute the fitness value of Positions(i,:) using the objective function fobj, and store it in the variable fitness.
      v. If fitness is less than Alpha_score, update Alpha_score to fitness and Alpha_pos to Positions(i,:).
      vi. If fitness is greater than Alpha_score and less than Beta_score, update Beta_score to fitness and Beta_pos to Positions(i,:).
      vii. If fitness is greater than Alpha_score, greater than Beta_score, and less than Delta_score, update Delta_score to fitness and Delta_pos to Positions(i,:).
   b. Calculate the linear decreasing parameter a as 2 - l * (2 / Max_iter).
   c. For each agent i from 1 to the number of rows in Positions:
      i. For each dimension j from 1 to the number of columns in Positions:
         - Generate random numbers r1 and r2 (each between 0 and 1).
         - Compute A1 = 2 * a * r1 - a and C1 = 2 * r2.
         - Compute D_alpha = abs(C1 * Alpha_pos(j) - Positions(i,j)).
         - Compute X1 = Alpha_pos(j) - A1 * D_alpha.
         - Generate new random numbers r1 and r2 (each between 0 and 1).
         - Compute A2 = 2 * a * r1 - a and C2 = 2 * r2.
         - Compute D_beta = abs(C2 * Beta_pos(j) - Positions(i,j)).
         - Compute X2 = Beta_pos(j) - A2 * D_beta.
         - Generate new random numbers r1 and r2 (each between 0 and 1).
         - Compute A3 = 2 * a * r1 - a and C3 = 2 * r2.
         - Compute D_delta = abs(C3 * Delta_pos(j) - Positions(i,j)).
         - Compute X3 = Delta_pos(j) - A3 * D_delta.
         - Update Positions(i,j) as (X1 + X2 + X3) / 3.
   d. Increment l by 1.
   e. Assign Alpha_score to Convergence_curve(l).
8. The 'initialization' function used to generate initial Positions is assumed to be pre-defined and does not need to be re-implemented.
9. Ensure all variable names, mathematical operations, loop structures, and logic match exactly the above steps to produce identical outputs as the original algorithm.
function_header: function [Alpha_score,Alpha_pos,Convergence_curve]=GWO(SearchAgents_no,Max_iter,lb,ub,dim,fobj)

end
test_cases: clear; clc;
rng(42);

SearchAgents_no = 30;
Max_iteration = 5;
Function_name = 'F2';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=GWO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 1328762515970.625244;
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

SearchAgents_no = 20;
Max_iteration = 10;
Function_name = 'F11';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=GWO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 3283.893247;
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

SearchAgents_no = 30;
Max_iteration = 6;
Function_name = 'F20';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=GWO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = -14.782880;
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 8
step_description_prompt: Please write a MATLAB function named func_plot that takes a function name (func_name) as input and generates a 3D surface plot of the corresponding function. The function relies on a pre-defined function Get_Functions_details which returns the lower bound (lb), upper bound (ub), dimension (dim), and objective function (fobj) based on func_name.
Function Definition: function func_plot(func_name)
Algorithm Flow:
1. Call Get_Functions_details(func_name) to obtain lb, ub, dim, and fobj.
2. Determine the ranges of x and y vectors using a switch-case structure based on func_name:
   - Case 'F1', 'F2', 'F3', 'F4', 'F6', 'F14': x = -100:2:100; y = x;
   - Case 'F5': x = -200:2:200; y = x;
   - Case 'F7': x = -1:0.03:1; y = x;
   - Case 'F8', 'F11': x = -500:10:500; y = x;
   - Case 'F9', 'F15', 'F17', 'F19', 'F20', 'F21', 'F22', 'F23': x = -5:0.1:5; y = x;
   - Case 'F10': x = -20:0.5:20; y = x;
   - Case 'F12': x = -10:0.1:10; y = x;
   - Case 'F13': x = -5:0.08:5; y = x;
   - Case 'F16': x = -1:0.01:1; y = x;
   - Case 'F18': x = -5:0.06:5; y = x;
3. Calculate L as the length of x and initialize an empty matrix f.
4. Use nested for loops (i from 1 to L, j from 1 to L) to compute function values:
   - If func_name is not 'F15', 'F19', 'F20', 'F21', 'F22', or 'F23', set f(i,j) = fobj([x(i), y(j)]).
   - If func_name is 'F15', set f(i,j) = fobj([x(i), y(j), 0, 0]).
   - If func_name is 'F19', set f(i,j) = fobj([x(i), y(j), 0]).
   - If func_name is 'F20', set f(i,j) = fobj([x(i), y(j), 0, 0, 0, 0]).
   - If func_name is 'F21', 'F22', or 'F23', set f(i,j) = fobj([x(i), y(j), 0, 0]).
5. Create a 3D surface plot with surfc(x, y, f, 'LineStyle', 'none').
The function Get_Functions_details is assumed to be pre-defined and does not need to be reimplemented.
ground_truth_code: function func_plot(func_name)

[lb,ub,dim,fobj]=Get_Functions_details(func_name);

switch func_name
    case 'F1'
        x=-100:2:100; y=x;

    case 'F2'
        x=-100:2:100; y=x;

    case 'F3'
        x=-100:2:100; y=x;

    case 'F4'
        x=-100:2:100; y=x;
    case 'F5'
        x=-200:2:200; y=x;
    case 'F6'
        x=-100:2:100; y=x;
    case 'F7'
        x=-1:0.03:1;  y=x
    case 'F8'
        x=-500:10:500;y=x;
    case 'F9'
        x=-5:0.1:5;   y=x;
    case 'F10'
        x=-20:0.5:20; y=x;
    case 'F11'
        x=-500:10:500; y=x;
    case 'F12'
        x=-10:0.1:10; y=x;
    case 'F13'
        x=-5:0.08:5; y=x;
    case 'F14'
        x=-100:2:100; y=x;
    case 'F15'
        x=-5:0.1:5; y=x;
    case 'F16'
        x=-1:0.01:1; y=x;
    case 'F17'
        x=-5:0.1:5; y=x;
    case 'F18'
        x=-5:0.06:5; y=x;
    case 'F19'
        x=-5:0.1:5; y=x;
    case 'F20'
        x=-5:0.1:5; y=x;
    case 'F21'
        x=-5:0.1:5; y=x;
    case 'F22'
        x=-5:0.1:5; y=x;
    case 'F23'
        x=-5:0.1:5; y=x;
end



L=length(x);
f=[];

for i=1:L
    for j=1:L
        if strcmp(func_name,'F15')==0 && strcmp(func_name,'F19')==0 && strcmp(func_name,'F20')==0 && strcmp(func_name,'F21')==0 && strcmp(func_name,'F22')==0 && strcmp(func_name,'F23')==0
            f(i,j)=fobj([x(i),y(j)]);
        end
        if strcmp(func_name,'F15')==1
            f(i,j)=fobj([x(i),y(j),0,0]);
        end
        if strcmp(func_name,'F19')==1
            f(i,j)=fobj([x(i),y(j),0]);
        end
        if strcmp(func_name,'F20')==1
            f(i,j)=fobj([x(i),y(j),0,0,0,0]);
        end
        if strcmp(func_name,'F21')==1 || strcmp(func_name,'F22')==1 ||strcmp(func_name,'F23')==1
            f(i,j)=fobj([x(i),y(j),0,0]);
        end
    end
end

surfc(x,y,f,'LineStyle','none');

end
function_header: function func_plot(func_name)

end

step_number: 9
step_description_prompt: Please use MATLAB code to generate a script that implements the following workflow for optimizing a benchmark function using the pre-defined Grey Wolf Optimizer (GWO) algorithm, with exact reproduction of the original logic and outputs:

1. Clear the workspace with `clear all` and command window with `clc`.
2. Set the random number generator seed to 42 using `rng(42)`.
3. Define the following parameters:
   - Number of search agents: `SearchAgents_no = 30`
   - Benchmark function name: `Function_name = 'F10'`
   - Maximum number of iterations: `Max_iteration = 500`
4. Call the pre-defined function `Get_Functions_details(Function_name)` to obtain the lower bound (`lb`), upper bound (`ub`), dimension (`dim`), and objective function handle (`fobj`) of the benchmark function. Assume `Get_Functions_details` is already defined.
5. Call the pre-defined GWO function with the parameters: `[Best_score, Best_pos, GWO_cg_curve] = GWO(SearchAgents_no, Max_iteration, lb, ub, dim, fobj)`. Assume `GWO` is already defined.
6. Create a figure with position `[500 500 660 290]` using `figure('Position', [500 500 660 290])`.
7. Create a 1x2 subplot grid:
   - For `subplot(1,2,1)`:
     - Plot the benchmark function using the pre-defined `func_plot(Function_name)` (assume `func_plot` is already defined).
     - Set the title to `'Parameter space'`, x-label to `'x_1'`, y-label to `'x_2'`, and z-label to `[Function_name, '( x_1 , x_2 )']`.
   - For `subplot(1,2,2)`:
     - Plot the convergence curve `GWO_cg_curve` using `semilogy` with color `'r'` (i.e., `semilogy(GWO_cg_curve, 'Color', 'r')`).
     - Set the title to `'Objective space'`, x-label to `'Iteration'`, and y-label to `'Best score obtained so far'`.
     - Enable tight axes with `axis tight`, grid with `grid on`, boxed axes with `box on`, and add a legend with `'GWO'`.
8. Display the best solution and best objective value using:
   - `display(['The best solution obtained by GWO is : ', num2str(Best_pos)]);`
   - `display(['The best optimal value of the objective funciton found by GWO is : ', num2str(Best_score)]);` (note: retain the original typo "funciton").
9. Ensure that the functions `GWO`, `Get_Functions_details`, and `func_plot` are assumed to be pre-defined and do not need to be re-implemented.

The script must produce the exact same outputs, figures, and convergence curve as the original code, with no deviations in parameter values, function calls, plotting details, or display messages.
test_cases: rng(42)
correct_score = 9.992007e-14;
correct_pos = 6.481397e-14;
val_score = sum(Best_score(:));
val_pos = sum(Best_pos(:));
tol_score = abs((val_score-correct_score)/correct_score);
if tol_score > 1e-4
    error('Wrong')
end
tol_pos = abs((val_pos-correct_pos)/correct_pos);
if tol_pos > 1e-4
    error('Wrong')
end
disp('Completely right!')

General solution:

General tests: