Problem: Implement SSA and Benchmark Functions

The problem involves developing MATLAB functions (Ufun, F1-F23, Get_Functions_details, initialization, SSA, func_plot) for function optimization and visualization.


Input/Output: The problem takes function logic descriptions (for Ufun, F1-F23, Get_Functions_details, initialization, SSA, func_plot), parameter mappings, and test case specifications. It generates MATLAB function codes adhering to the logic, test cases for validation, and 3D surface plots via func_plot for selected functions.

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please write a MATLAB function named Ufun that computes a piecewise function based on the input parameters. The function is defined as function o = Ufun(x, a, k, m). The algorithm flow is: Calculate the output o through element-wise operations: for elements in x greater than a, compute k multiplied by (x minus a) raised to the power m and multiply by the logical mask (x > a); for elements in x less than -a, compute k multiplied by (-x minus a) raised to the power m and multiply by the logical mask (x < -a); elements of x between -a and a (inclusive) result in 0 because both logical masks are false for those elements. Sum these two parts (using element-wise addition) to get the final o. Functions called in the code are assumed to be already defined.
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 functions named F1, F2, F3, F4, F5, F6, F7, F8, each implementing the corresponding mathematical computation. The detailed definitions and algorithm flows are as follows:

- Function F1:
  - Function definition: function o = F1(x)
  - Algorithm flow: Calculate the sum of the squares of all elements in the input vector x.

- Function F2:
  - Function definition: function o = F2(x)
  - Algorithm flow: Calculate the sum of the absolute values of all elements in x, then add the product of the absolute values of all elements in x.

- Function F3:
  - Function definition: function o = F3(x)
  - Algorithm flow: 1. Get the number of columns (dimensions) of x as dim using size(x, 2). 2. Initialize output o to 0. 3. For each i from 1 to dim, compute the sum of the first i elements of x, square this sum, and accumulate the result to o.

- Function F4:
  - Function definition: function o = F4(x)
  - Algorithm flow: Compute the maximum value among the absolute values of all elements in x.

- Function F5:
  - Function definition: function o = F5(x)
  - Algorithm flow: 1. Get the number of columns (dimensions) of x as dim using size(x, 2). 2. Compute the sum over indices from 1 to dim-1 of [100 multiplied by the square of (the (i+1)-th element of x minus the square of the i-th element of x) plus the square of (the i-th element of x minus 1)].

- Function F6:
  - Function definition: function o = F6(x)
  - Algorithm flow: For each element in x, add 0.5 to the element, take its absolute value, square the result, then sum all these squared values.

- Function F7:
  - Function definition: function o = F7(x)
  - Algorithm flow: 1. Get the number of columns (dimensions) of x as dim using size(x, 2). 2. Compute the sum of each element in x raised to the 4th power multiplied by its corresponding index (from 1 to dim), then add a random number between 0 and 1 generated by rand().

- Function F8:
  - Function definition: function o = F8(x)
  - Algorithm flow: For each element in x, compute the product of the negative of the element and the sine of the square root of the absolute value of the element, then sum all these products.
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: Write MATLAB functions with the exact names F9, F10, F11, F12, F13, F14, F15, F16, F17 to implement the following logic. Functions called within these functions (e.g., Ufun) are assumed to be pre-defined and do not need redefinition.

1. Function F9:
   - Input: x (a matrix or vector)
   - Compute dim as the number of columns of x.
   - Compute o as sum(x.^2 - 10*cos(2*pi.*x)) + 10*dim.

2. Function F10:
   - Input: x (a matrix or vector)
   - Compute dim as the number of columns of x.
   - Compute o as -20*exp(-0.2*sqrt(sum(x.^2)/dim)) - exp(sum(cos(2*pi.*x))/dim) + 20 + exp(1).

3. Function F11:
   - Input: x (a matrix or vector)
   - Compute dim as the number of columns of x.
   - Compute o as sum(x.^2)/4000 - prod(cos(x./sqrt(1:dim))) + 1.

4. Function F12:
   - Input: x (a matrix or vector)
   - Compute dim as the number of columns of x.
   - Compute o as (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)).

5. Function F13:
   - Input: x (a matrix or vector)
   - Compute dim as the number of columns of x.
   - Compute o as 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)).

6. Function F14:
   - Input: x (a matrix or vector)
   - Define aS as a 2-by-25 matrix: first row is [-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], second row is [-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].
   - For j from 1 to 25, compute bS(j) = sum((x' - aS(:,j)).^6).
   - Compute o as (1/500 + sum(1./([1:25] + bS)))^(-1).

7. Function F15:
   - Input: x (a matrix or vector)
   - Define aK as [0.1957 0.1947 0.1735 0.16 0.0844 0.0627 0.0456 0.0342 0.0323 0.0235 0.0246] and bK as 1./[0.25 0.5 1 2 4 6 8 10 12 14 16].
   - Compute o as sum((aK - (x(1)*(bK.^2 + x(2)*bK))./(bK.^2 + x(3)*bK + x(4))).^2).

8. Function F16:
   - Input: x (a matrix or vector)
   - Compute o as 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).

9. Function F17:
   - Input: x (a matrix or vector)
   - Compute o as (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.
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 six functions named F18, F19, F20, F21, F22, F23. Each function takes a vector x as input and returns a scalar o as output, which implements the corresponding mathematical computation based on the given formulas.

Function F18 definition: function o = F18(x)
Algorithm flow for F18: Compute o as the product of two terms. The first term is 1 plus the square of (x(1) + x(2) + 1) multiplied by (19 - 14*x(1) + 3*(x(1)^2) - 14*x(2) + 6*x(1)*x(2) + 3*(x(2)^2)). The second term is 30 plus the square of (2*x(1) - 3*x(2)) multiplied by (18 - 32*x(1) + 12*(x(1)^2) + 48*x(2) - 36*x(1)*x(2) + 27*(x(2)^2)). Multiply these two terms to get o.

Function F19 definition: function o = F19(x)
Algorithm flow for F19: First, define matrix aH = [3 10 30; .1 10 35; 3 10 30; .1 10 35], vector cH = [1 1.2 3 3.2], and matrix pH = [.3689 .117 .2673; .4699 .4387 .747; .1091 .8732 .5547; .03815 .5743 .8828]. Initialize o to 0. Then loop from i = 1 to 4: for each i, compute the sum of aH(i,:) element-wise multiplied by the square of (x - pH(i,:)), take the negative exponential of this sum, multiply by cH(i), and subtract this value from o.

Function F20 definition: function o = F20(x)
Algorithm flow for F20: First, define matrix aH = [10 3 17 3.5 1.7 8; .05 10 17 .1 8 14; 3 3.5 1.7 10 17 8; 17 8 .05 10 .1 14], vector cH = [1 1.2 3 3.2], and matrix pH = [.1312 .1696 .5569 .0124 .8283 .5886; .2329 .4135 .8307 .3736 .1004 .9991; .2348 .1415 .3522 .2883 .3047 .6650; .4047 .8828 .8732 .5743 .1091 .0381]. Initialize o to 0. Then loop from i = 1 to 4: for each i, compute the sum of aH(i,:) element-wise multiplied by the square of (x - pH(i,:)), take the negative exponential of this sum, multiply by cH(i), and subtract this value from o.

Function F21 definition: function o = F21(x)
Algorithm flow for F21: First, define matrix 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] and vector cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5]. Initialize o to 0. Then loop from i = 1 to 5: for each i, compute the dot product of (x - aSH(i,:)) with its transpose (i.e., the squared Euclidean norm of (x - aSH(i,:))), add cSH(i) to this value, take the reciprocal, and subtract this reciprocal from o.

Function F22 definition: function o = F22(x)
Algorithm flow for F22: First, define matrix 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] and vector cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5]. Initialize o to 0. Then loop from i = 1 to 7: for each i, compute the dot product of (x - aSH(i,:)) with its transpose (i.e., the squared Euclidean norm of (x - aSH(i,:))), add cSH(i) to this value, take the reciprocal, and subtract this reciprocal from o.

Function F23 definition: function o = F23(x)
Algorithm flow for F23: First, define matrix 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] and vector cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5]. Initialize o to 0. Then loop from i = 1 to 10: for each i, compute the dot product of (x - aSH(i,:)) with its transpose (i.e., the squared Euclidean norm of (x - aSH(i,:))), add cSH(i) to this value, take the reciprocal, and subtract this reciprocal from o.

All constants (matrices aH, cH, pH, aSH, cSH) must be exactly the same as given. The input x for F18 is a 2-dimensional vector; for F19 it is a 3-dimensional vector; for F20 it is a 6-dimensional vector; for F21, F22, F23 it is a 4-dimensional vector. The functions do not require defining other helper functions as they are assumed to be already available.
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 takes a string parameter F and returns four outputs: lb (lower bound), ub (upper bound), dim (dimension), and fobj (objective function handle). The function implements the logic of using a switch-case structure to map the input string F to corresponding optimization problem parameters (lb, ub, dim) and the objective function handle (fobj).

Function definition: function [lb,ub,dim,fobj] = Get_Functions_details(F)

Algorithm flow:
1. Use a switch-case structure to check the value of the input string F.
2. For each case (from 'F1' to 'F23'), assign the corresponding values to fobj (a handle to the pre-defined function F1-F23), lb (can be a scalar or vector), ub (can be a scalar or vector), and dim (integer dimension of the problem).
3. The mappings between F and parameters are strictly as follows:
   - Case 'F1': fobj = @F1, lb = -100, ub = 100, dim = 30
   - Case 'F2': fobj = @F2, lb = -10, ub = 10, dim = 10
   - Case 'F3': fobj = @F3, lb = -100, ub = 100, dim = 10
   - Case 'F4': fobj = @F4, lb = -100, ub = 100, dim = 10
   - Case 'F5': fobj = @F5, lb = -30, ub = 30, dim = 10
   - Case 'F6': fobj = @F6, lb = -100, ub = 100, dim = 10
   - Case 'F7': fobj = @F7, lb = -1.28, ub = 1.28, dim = 10
   - Case 'F8': fobj = @F8, lb = -500, ub = 500, dim = 10
   - Case 'F9': fobj = @F9, lb = -5.12, ub = 5.12, dim = 10
   - Case 'F10': fobj = @F10, lb = -32, ub = 32, dim = 10
   - Case 'F11': fobj = @F11, lb = -600, ub = 600, dim = 10
   - Case 'F12': fobj = @F12, lb = -50, ub = 50, dim = 10
   - Case 'F13': fobj = @F13, lb = -50, ub = 50, dim = 10
   - 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

Note: The functions F1 to F23 are assumed to be pre-defined, so you do not need to define them. The function must strictly follow the above parameter mappings for all cases.
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 = 884.188863;
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 generates the initial positions of search agents within given boundary constraints. The function takes four inputs: SearchAgents_no (the number of search agents), dim (the dimensionality of the search space), ub (the upper bounds of the search space), lb (the lower bounds of the search space), and returns Positions (a SearchAgents_no×dim matrix where each row represents a search agent's position and each column represents a dimension).
Function definition: function Positions = initialization(SearchAgents_no, dim, ub, lb)
Algorithm flow: 
1. Compute Boundary_no as the number of rows of ub using size(ub, 1).
2. If Boundary_no equals 1 (all dimensions share the same bounds), calculate Positions as rand(SearchAgents_no, dim) .* (ub - lb) + lb.
3. If Boundary_no is greater than 1 (each dimension has independent bounds), loop over each dimension i from 1 to dim:
   a. Extract ub_i as ub(i) and lb_i as lb(i).
   b. Assign the i-th column of Positions as rand(SearchAgents_no, 1) .* (ub_i - lb_i) + lb_i.
The functions called in the code are assumed to be pre-defined. The generated code must strictly replicate the input-output behavior, logical structure, and numerical results of the original code.
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: Write a MATLAB function named SSA that implements the Salp Swarm Algorithm to optimize a given objective function, returning the optimal fitness value, optimal position, and convergence curve. The function definition is function [FoodFitness,FoodPosition,Convergence_curve]=SSA(N,Max_iter,lb,ub,dim,fobj). The initialization function called in the code is assumed to be pre-defined.

First, handle the upper and lower bounds: if ub is a 1-by-1 vector, expand it to a dim-by-1 vector using ones(dim,1)*ub; do the same for lb with ones(dim,1)*lb. Initialize Convergence_curve as a 1-by-Max_iter zero vector. Use the initialization function to generate N dim-dimensional salp positions stored in SalpPositions. Initialize FoodPosition as a 1-by-dim zero vector and FoodFitness as infinity.

Calculate the fitness of each salp using the objective function fobj, storing results in SalpFitness. Sort SalpFitness in ascending order and get the corresponding indexes. Sort SalpPositions based on these indexes to get Sorted_salps. Set FoodPosition to Sorted_salps(1,:) and FoodFitness to the first value of the sorted SalpFitness.

Set the iteration counter l to 2. Enter a loop until l exceeds Max_iter: compute c1 as 2*exp(-(4*l/Max_iter)^2). For each salp i: transpose SalpPositions. If i <= N/2, iterate over each dimension j: generate random numbers c2 and c3. If c3 < 0.5, update SalpPositions(j,i) as FoodPosition(j) + c1*((ub(j)-lb(j))*c2 + lb(j)); else, update it as FoodPosition(j) - c1*((ub(j)-lb(j))*c2 + lb(j)). If i > N/2 and i <= N, get point1 (SalpPositions(:,i-1)) and point2 (SalpPositions(:,i)), then update SalpPositions(:,i) as (point2 + point1)/2. Transpose SalpPositions back to its original shape.

For each salp i: perform boundary handling: create Tp (SalpPositions(i,:) > ub') and Tm (SalpPositions(i,:) < lb'). Update SalpPositions(i,:) as SalpPositions(i,:).*(~(Tp+Tm)) + ub'.*Tp + lb'.*Tm. Calculate the fitness of the current salp using fobj. If the current fitness is less than FoodFitness, update FoodPosition to the current salp's position and FoodFitness to the current fitness.

Store FoodFitness in Convergence_curve(l), then increment l by 1.

The function returns FoodFitness (optimal fitness value), FoodPosition (optimal position), and Convergence_curve (convergence curve over iterations).
function_header: function [FoodFitness,FoodPosition,Convergence_curve]=SSA(N,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]=SSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 125.214418;
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]=SSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 501.598046;
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]=SSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = -11.499372;
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 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 lb, ub, dim, and fobj when called with func_name. Function definition: function func_plot(func_name). Algorithm steps: 1. Call [lb,ub,dim,fobj] = Get_Functions_details(func_name). 2. Set x and y vectors based on func_name using switch-case: case 'F1' to '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' to '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. Compute L = length(x) and initialize f as an empty matrix. 4. Use nested loops (i from 1 to L, j from 1 to L) to calculate f(i,j): if func_name is not 'F15','F19','F20','F21','F22','F23', f(i,j) = fobj([x(i),y(j)]); if func_name is 'F15', f(i,j) = fobj([x(i),y(j),0,0]); if func_name is 'F19', f(i,j) = fobj([x(i),y(j),0]); if func_name is 'F20', f(i,j) = fobj([x(i),y(j),0,0,0,0]); if func_name is 'F21','F22','F23', f(i,j) = fobj([x(i),y(j),0,0]). 5. Plot with surfc(x,y,f,'LineStyle','none').
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 create a script that implements the function of running the Sparrow Search Algorithm (SSA) to optimize the benchmark function specified by 'Function_name', visualizing the parameter space of the function and the convergence curve of the algorithm, and displaying the best solution and optimal objective value. The script should follow these steps strictly:

1. Clear all variables in the workspace with `clear all`, clear the command window with `clc`, and set the random number generator seed to 42 using `rng(42)`.
2. Define the parameters: `SearchAgents_no = 30` (number of search agents), `Function_name = 'F1'` (name of the benchmark function to optimize), `Max_iteration = 1000` (maximum number of iterations).
3. Call `Get_Functions_details(Function_name)` to obtain the lower bound (`lb`), upper bound (`ub`), dimension (`dim`), and objective function handle (`fobj`) of the specified benchmark function. (Assume `Get_Functions_details` is already defined.)
4. Call the `SSA` function with inputs `SearchAgents_no`, `Max_iteration`, `lb`, `ub`, `dim`, `fobj` to get three outputs: `Best_score` (the best objective value found), `Best_pos` (the best solution corresponding to `Best_score`), and `SSA_cg_curve` (the convergence curve recording the best score at each iteration). (Assume `SSA` is already defined.)
5. Create a figure with position `[500 500 660 290]` using `figure('Position',[500 500 660 290])`.
6. For the first subplot (`subplot(1,2,1)`):
   - Plot the parameter space of the benchmark function by calling `func_plot(Function_name)`. (Assume `func_plot` is already defined.)
   - Set the title to `'Parameter space'`, xlabel to `'x_1'`, ylabel to `'x_2'`, and zlabel to `[Function_name,'( x_1 , x_2 )']`.
7. For the second subplot (`subplot(1,2,2)`):
   - Plot the convergence curve using `semilogy(SSA_cg_curve,'Color','r')`.
   - Set the title to `'Objective space'`, xlabel to `'Iteration'`, ylabel to `'Best score obtained so far'`.
   - Enable tight axes with `axis tight`, display grid with `grid on`, enable box around the plot with `box on`, and add a legend with `legend('SSA')`.
8. Display the best solution using `display(['The best solution obtained by SSA is \m ', num2str(Best_pos)])` and the best optimal value using `display(['The best optimal value of the objective function found by SSA is \n ', num2str(Best_score)])`.

Make sure the script strictly reproduces the input, output, logical details, and visualization effects of the original code. All called functions (`Get_Functions_details`, `SSA`, `func_plot`) are assumed to be pre-defined and available in the MATLAB path.
test_cases: rng(42)
correct_score = 8.193895e-09;
correct_pos = 1.960453e-05;
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: