Problem: Marine Predators Algorithm Implementation

The problem requires implementing specified MATLAB functions (Ufun, F1-F23, initialization, levy, MPA, Get_Functions_details) for optimization via the Marine Predators Algorithm.


Input/Output: The problem receives substeps with step numbers, function requirements (definitions, algorithm flows), function headers (if any), and test cases. It generates MATLAB function codes (e.g., Ufun, F1-F23, Get_Functions_details, initialization, levy, MPA) with exact names that follow the requirements, use the headers (if given), and pass the test cases. Each substep's input maps to a corresponding function code.

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please use MATLAB code to generate a function named Ufun that implements a piecewise vectorized calculation for input x (scalar or array) with parameters a, k, m, where the output is k*(x-a)^m when x > a, k*(-x-a)^m when x < -a, and 0 otherwise.
function o = Ufun(x, a, k, m)
The algorithm computes the output o as the sum of two element-wise products: the first product is k.*((x - a).^m) multiplied by the logical array (x > a) (to apply the term only when x > a), and the second product is k.*((-x - a).^m) multiplied by the logical array (x < (-a)) (to apply the term only when x < -a). The sum of these two products results in the final output, where the region between -a and a (inclusive) contributes 0 since neither logical condition is true.
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 write a MATLAB function named F1 that computes the sum of the squares of the elements in the input vector x.
Function Definition: function o = F1(x)
Algorithm Flow: Compute the sum of each element in x squared using element-wise exponentiation (x.^2) and the sum function (sum(x.^2)).

Please write a MATLAB function named F2 that computes the sum of the absolute values of the elements in 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 sum of absolute values of elements in x using abs(x) and sum function (sum(abs(x))), compute the product of absolute values of elements in x using abs(x) and prod function (prod(abs(x))), then add the two results together (sum(abs(x)) + prod(abs(x))).

Please write a MATLAB function named F3 that computes 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: First, get the number of elements (dim) in x using size(x,2). Initialize o to 0. Then loop from i=1 to dim: for each i, compute the sum of the first i elements of x (sum(x(1:i))), square that sum (sum(x(1:i))^2), and add it to o (o = o + sum(x(1:i))^2).

Please write a MATLAB function named F4 that computes 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 using abs(x), then find the maximum value among these absolute values using max function (max(abs(x))).

Please write a MATLAB function named F5 that computes the Rosenbrock function value for the input vector x, which sums 100*(x(i+1)-x(i)^2)^2 + (x(i)-1)^2 for i from 1 to the number of elements minus 1.
Function Definition: function o = F5(x)
Algorithm Flow: First, get the number of elements (dim) in x using size(x,2). Compute the sum of terms where each term is 100*(x(i+1) - x(i)^2)^2 + (x(i)-1)^2 for i=1 to dim-1. Use element-wise operations: x(2:dim) represents x(i+1), x(1:dim-1) represents x(i). The calculation is sum(100*(x(2:dim) - x(1:dim-1).^2).^2 + (x(1:dim-1) - 1).^2).

Please write a MATLAB function named F6 that computes the sum of the squares of the absolute values of (each element in input vector x plus 0.5).
Function Definition: function o = F6(x)
Algorithm Flow: For each element in x, add 0.5 (x + 0.5) using element-wise addition, take the absolute value (abs(x + 0.5)), square the result (abs(x + 0.5).^2), then sum all squared values using sum function (sum(abs(x + 0.5).^2)).

Please write a MATLAB function named F7 that computes the sum of (index i multiplied by the fourth power of element x(i)) for i from 1 to the number of elements in x, plus a random number from MATLAB's rand function (uniform [0,1]).
Function Definition: function o = F7(x)
Algorithm Flow: First, get the number of elements (dim) in x using size(x,2). Create an index vector from 1 to dim ([1:dim]). Compute element-wise multiplication of the index vector with the fourth power of x (x.^4), sum the products (sum([1:dim].*(x.^4))), then add a random scalar generated by rand() (sum([1:dim].*(x.^4)) + rand).

Please write a MATLAB function named F8 that computes the sum of (-x(i) multiplied by sin(sqrt(abs(x(i)))) for each element x(i) in input vector x.
Function Definition: function o = F8(x)
Algorithm Flow: For each element in x, compute the absolute value (abs(x)), take the square root (sqrt(abs(x))), compute the sine (sin(sqrt(abs(x)))), multiply by -x (element-wise: -x .* sin(sqrt(abs(x)))), then sum all products using sum function (sum(-x .* sin(sqrt(abs(x))))).
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: Please write MATLAB code to generate functions named F9, F10, F11, F12, F13, F14, F15, F16, F17 that implement the following functionalities. The functions must have the same input-output behavior, logical details, and rely on any called functions (which are assumed to be pre-defined).

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

2. Function F10: Takes input x. Compute dim as the number of columns of x using `size(x,2)`. 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: Takes input x. Compute dim as the number of columns of x using `size(x,2)`. Compute o as `sum(x.^2)/4000 - prod(cos(x./sqrt(1:dim))) + 1`.

4. Function F12: Takes input x. Compute dim as the number of columns of x using `size(x,2)`. 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: Takes input x. Compute dim as the number of columns of x using `size(x,2)`. 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: Takes input x. Define aS as a 2x25 matrix where the 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]` and the 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]`. Initialize a vector bS of length 25. For each j from 1 to 25, compute `bS(j)` as `sum((x' - aS(:,j)).^6)`. Compute o as `(1/500 + sum(1./([1:25] + bS))).^(-1)`.

7. Function F15: Takes input x. Define aK as a 1x11 vector `[0.1957, 0.1947, 0.1735, 0.16, 0.0844, 0.0627, 0.0456, 0.0342, 0.0323, 0.0235, 0.0246]`. Define bK as a 1x11 vector `[0.25, 0.5, 1, 2, 4, 6, 8, 10, 12, 14, 16]`, then update bK to `1./bK`. Compute o as `sum((aK - (x(1)*(bK.^2 + x(2)*bK) ./ (bK.^2 + x(3)*bK + x(4)))).^2)`.

8. Function F16: Takes input x. 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: Takes input x. 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`.

The functions must use the exact names F9, F10, F11, F12, F13, F14, F15, F16, F17 and follow the above logic precisely. Any called functions (e.g., Ufun) are assumed to be pre-defined and do not need to be redefined.
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 write MATLAB code to create the following functions with exact functionality, input/output, logical details, and hardcoded parameters as specified. Each function is independent.

1. Function F18:
Create a function named F18 that takes a vector x as input and returns the output o defined by a mathematical formula. Function definition: `function o = F18(x)`. Algorithm flow: Compute o using the formula  
`o = (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))) * (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)))`.

2. Function F19:
Create a function named F19 that takes a vector x as input and returns the output o defined by summing weighted exponential terms. Function definition: `function o = F19(x)`. Algorithm flow:  
(1) Predefine matrix `aH = [3 10 30; 0.1 10 35; 3 10 30; 0.1 10 35]`, vector `cH = [1 1.2 3 3.2]`, matrix `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]`.  
(2) Initialize `o = 0`.  
(3) For `i = 1 to 4`, accumulate `o = o - cH(i) * exp(-sum(aH(i,:) .* ((x - pH(i,:)).^2)))`. Return o.

3. Function F20:
Create a function named F20 that takes a vector x as input and returns the output o defined by summing weighted exponential terms. Function definition: `function o = F20(x)`. Algorithm flow:  
(1) Predefine matrix `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]`, vector `cH = [1 1.2 3 3.2]`, matrix `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]`.  
(2) Initialize `o = 0`.  
(3) For `i = 1 to 4`, accumulate `o = o - cH(i) * exp(-sum(aH(i,:) .* ((x - pH(i,:)).^2)))`. Return o.

4. Function F21:
Create a function named F21 that takes a vector x as input and returns the output o defined by summing weighted inverse quadratic terms. Function definition: `function o = F21(x)`. Algorithm flow:  
(1) Predefine 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]`, vector `cSH = [0.1 0.2 0.2 0.4 0.4 0.6 0.3 0.7 0.5 0.5]`.  
(2) Initialize `o = 0`.  
(3) For `i = 1 to 5`, accumulate `o = o - ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i))^(-1)`. Return o.

5. Function F22:
Create a function named F22 that takes a vector x as input and returns the output o defined by summing weighted inverse quadratic terms. Function definition: `function o = F22(x)`. Algorithm flow:  
(1) Predefine 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]`, vector `cSH = [0.1 0.2 0.2 0.4 0.4 0.6 0.3 0.7 0.5 0.5]`.  
(2) Initialize `o = 0`.  
(3) For `i = 1 to 7`, accumulate `o = o - ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i))^(-1)`. Return o.

6. Function F23:
Create a function named F23 that takes a vector x as input and returns the output o defined by summing weighted inverse quadratic terms. Function definition: `function o = F23(x)`. Algorithm flow:  
(1) Predefine 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]`, vector `cSH = [0.1 0.2 0.2 0.4 0.4 0.6 0.3 0.7 0.5 0.5]`.  
(2) Initialize `o = 0`.  
(3) For `i = 1 to 10`, accumulate `o = o - ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i))^(-1)`. Return o.

All matrices/vectors are hardcoded as specified. No additional functions need to be defined (assumed existing if called).
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 input F and returns four outputs: lb (lower bound of the optimization problem), ub (upper bound of the optimization problem), dim (dimension of the problem), and fobj (a handle to the objective function). The function retrieves the corresponding parameters and objective function handle based on the input string F.

The function must be defined with the exact signature: function [lb, ub, dim, fobj] = Get_Functions_details(F).

Implement the following logic using a switch-case structure to map the input F to the corresponding parameters:
- When F is 'F1', set fobj = @F1, lb = -100, ub = 100, dim = 50.
- When F is 'F2', set fobj = @F2, lb = -10, ub = 10, dim = 50.
- When F is 'F3', set fobj = @F3, lb = -100, ub = 100, dim = 50.
- When F is 'F4', set fobj = @F4, lb = -100, ub = 100, dim = 50.
- When F is 'F5', set fobj = @F5, lb = -30, ub = 30, dim = 50.
- When F is 'F6', set fobj = @F6, lb = -100, ub = 100, dim = 50.
- When F is 'F7', set fobj = @F7, lb = -1.28, ub = 1.28, dim = 50.
- When F is 'F8', set fobj = @F8, lb = -500, ub = 500, dim = 50.
- When F is 'F9', set fobj = @F9, lb = -5.12, ub = 5.12, dim = 50.
- When F is 'F10', set fobj = @F10, lb = -32, ub = 32, dim = 50.
- When F is 'F11', set fobj = @F11, lb = -600, ub = 600, dim = 50.
- When F is 'F12', set fobj = @F12, lb = -50, ub = 50, dim = 50.
- When F is 'F13', set fobj = @F13, lb = -50, ub = 50, dim = 50.
- When F is 'F14', set fobj = @F14, lb = -65.536, ub = 65.536, dim = 2.
- When F is 'F15', set fobj = @F15, lb = -5, ub = 5, dim = 4.
- When F is 'F16', set fobj = @F16, lb = -5, ub = 5, dim = 2.
- When F is 'F17', set fobj = @F17, lb = [-5, 0], ub = [10, 15], dim = 2.
- When F is 'F18', set fobj = @F18, lb = -2, ub = 2, dim = 2.
- When F is 'F19', set fobj = @F19, lb = 0, ub = 1, dim = 3.
- When F is 'F20', set fobj = @F20, lb = 0, ub = 1, dim = 6.
- When F is 'F21', set fobj = @F21, lb = 0, ub = 10, dim = 4.
- When F is 'F22', set fobj = @F22, lb = 0, ub = 10, dim = 4.
- When F is 'F23', set fobj = @F23, lb = 0, ub = 10, dim = 4.

Assume that the objective functions F1 to F23 (referenced by the handles @F1 to @F23) are already defined and do not require redefinition 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 = 1437.010766;
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 is defined as: function Positions = initialization(SearchAgents_no, dim, ub, lb). 

1. Calculate Boundary_no as the number of columns of ub using size(ub,2). 
2. If Boundary_no == 1, generate Positions as a matrix of size SearchAgents_no-by-dim where each element is computed by rand(SearchAgents_no,dim).*(ub-lb) + lb. 
3. If Boundary_no > 1, loop over each dimension i from 1 to dim: 
   a. Get ub_i = ub(i) and lb_i = lb(i). 
   b. Set 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.
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 levy that generates a matrix of random numbers following the Levy distribution with parameters n, m, and beta. The function is defined as [z] = levy(n,m,beta). The algorithm steps are: 1. Compute num using gamma(1 + beta) * sin(pi * beta / 2). 2. Compute den using gamma((1 + beta) / 2) * beta * 2 ^ ((beta - 1) / 2). 3. Compute sigma_u as (num / den) ^ (1 / beta). 4. Generate u as a n-by-m matrix of random numbers from the Normal distribution with mean 0 and standard deviation sigma_u using random('Normal', 0, sigma_u, n, m). 5. Generate v as a n-by-m matrix of random numbers from the Normal distribution with mean 0 and standard deviation 1 using random('Normal', 0, 1, n, m). 6. Compute z as the element-wise division of u by (the absolute value of v raised to the power of (1 / beta)), i.e., u ./ (abs(v) .^ (1 / beta)). Functions like gamma, sin, pi, and random are assumed to be already defined and do not need to be redefined.
function_header: function [z] = levy(n,m,beta)

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

z1 = levy(2, 3, 1.5);
val = sum(z1(:));
correct = 1.540845;
tol = abs(val-correct);
if tol>1e-3
    error('Wrong!')
end

z2 = levy(3, 3, 1.5);
val = sum(z2(:));
correct = -6.073943;
tol = abs(val-correct);
if tol>1e-3
    error('Wrong!')
end

z3 = levy(2, 5, 1.8);
val = sum(z3(:));
correct = -3.713469;
tol = abs(val-correct);
if tol>1e-3
    error('Wrong!')
end
disp('Completely right!')

step_number: 8
step_description_prompt: Please write a MATLAB function named MPA that implements the Marine Predators Algorithm (MPA) for optimization. The function takes five inputs: SearchAgents_no (number of search agents), Max_iter (maximum number of iterations), lb (lower bound of variables), ub (upper bound of variables), dim (dimensionality of the problem), and fobj (the objective function to minimize). It returns three outputs: Top_predator_fit (the fitness value of the best solution), Top_predator_pos (the position of the best solution), and Convergence_curve (a vector recording the best fitness value at each iteration).

Function Definition:
function [Top_predator_fit,Top_predator_pos,Convergence_curve]=MPA(SearchAgents_no,Max_iter,lb,ub,dim,fobj)

Algorithm Flow (must strictly follow the original logic):
1. Initialize variables:
   - Top_predator_pos as a 1×dim zero vector.
   - Top_predator_fit as infinity (inf).
   - Convergence_curve as a 1×Max_iter zero vector.
   - stepsize as a SearchAgents_no×dim zero matrix.
   - fitness as a SearchAgents_no×1 column vector filled with inf.
2. Initialize prey positions using the initialization(SearchAgents_no,dim,ub,lb) function (assumed pre-defined).
3. Create Xmin (SearchAgents_no×dim matrix, each row is lb) and Xmax (SearchAgents_no×dim matrix, each row is ub) using repmat.
4. Set Iter (current iteration) to 0, FADs to 0.2, and P to 0.5.
5. Start the main loop while Iter < Max_iter:
   a. For each prey:
      i. Handle boundary violations: if a prey's position exceeds ub or lb, clip it to the boundary.
      ii. Compute the fitness of the prey using fobj.
      iii. Update Top_predator_fit and Top_predator_pos if the current prey's fitness is better (smaller) than Top_predator_fit.
   b. If it's the first iteration (Iter==0), save the current fitness as fit_old and prey positions as Prey_old.
   c. Update prey positions and fitness:
      i. Create Inx as a logical vector where fit_old < fitness.
      ii. Create Indx as a SearchAgents_no×dim logical matrix by repeating Inx for each dimension.
      iii. Update Prey: Indx.*Prey_old + ~Indx.*Prey.
      iv. Update fitness: Inx.*fit_old + ~Indx.*fitness.
      v. Save current fitness as fit_old and prey positions as Prey_old.
   d. Create Elite as a SearchAgents_no×dim matrix by repeating Top_predator_pos for each row.
   e. Compute CF (Conversion Factor) as (1 - Iter/Max_iter)^(2*Iter/Max_iter).
   f. Generate RL (Levy flight random vector) using levy(SearchAgents_no,dim,1.5) (assumed pre-defined) scaled by 0.05.
   g. Generate RB (Brownian motion random vector) using randn(SearchAgents_no,dim).
   h. Update stepsize and Prey based on three iteration stages:
      i. If Iter < Max_iter/3:
         - stepsize(i,j) = RB(i,j) * (Elite(i,j) - RB(i,j) * Prey(i,j)).
         - Prey(i,j) = Prey(i,j) + P * rand() * stepsize(i,j).
      ii. If Iter is between Max_iter/3 and 2*Max_iter/3:
          - For i > SearchAgents_no/2:
              stepsize(i,j) = RB(i,j) * (RB(i,j) * Elite(i,j) - Prey(i,j)).
              Prey(i,j) = Elite(i,j) + P * CF * stepsize(i,j).
          - For i ≤ SearchAgents_no/2:
              stepsize(i,j) = RL(i,j) * (Elite(i,j) - RL(i,j) * Prey(i,j)).
              Prey(i,j) = Prey(i,j) + P * rand() * stepsize(i,j).
      iii. If Iter ≥ 2*Max_iter/3:
          - stepsize(i,j) = RL(i,j) * (RL(i,j) * Elite(i,j) - Prey(i,j)).
          - Prey(i,j) = Elite(i,j) + P * CF * stepsize(i,j).
   i. Re-handle boundary violations for Prey (same as step 5a i).
   j. Recompute fitness for updated Prey (same as step 5a ii) and update Top_predator_fit and Top_predator_pos (same as step 5a iii).
   k. Repeat step 5b-5e (save old prey and fitness, update based on old vs current fitness).
   l. Handle Fish Aggregating Devices (FADs):
      i. If rand() < FADs:
          - Generate U as a SearchAgents_no×dim logical matrix where rand(SearchAgents_no,dim) < FADs.
          - Update Prey: Prey + CF * ((Xmin + rand(SearchAgents_no,dim).*(Xmax - Xmin)).*U).
      ii. Else:
          - Compute r = rand().
          - Rs = size(Prey,1).
          - stepsize = (FADs*(1 - r) + r) * (Prey(randperm(Rs),:) - Prey(randperm(Rs),:)).
          - Update Prey: Prey + stepsize.
   m. Increment Iter by 1.
   n. Record the current Top_predator_fit in Convergence_curve(Iter).
6. End the loop and return the outputs.

Note: The functions initialization and levy are assumed to be pre-defined and do not need to be reimplemented. Ensure all variable initializations (e.g., Top_predator_fit=inf, stepsize=zeros(SearchAgents_no,dim)), parameter settings (FADs=0.2, P=0.5), and logical flows (including the two times saving/updating fit_old/Prey_old, three iteration stages, FADs handling) are strictly consistent with the original code.
function_header: function [Top_predator_fit,Top_predator_pos,Convergence_curve]=MPA(SearchAgents_no,Max_iter,lb,ub,dim,fobj)

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

SearchAgents_no = 10;
Max_iteration = 5;
Function_name = 'F2';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=MPA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 6933690340587279;
tol = abs((val-correct)/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]=MPA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 4167.946169;
tol = abs((val-correct)/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]=MPA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = -15.985084;
tol = abs((val-correct)/correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 9
step_description_prompt: Please write a MATLAB function named func_plot that takes a function name as input and plots the 3D surface graph of the corresponding function. The function is defined as function func_plot(func_name). First, call Get_Functions_details(func_name) to get lb, ub, dim, and fobj. Then, set the ranges and steps of x and y based on func_name: for 'F1','F2','F3','F4','F6','F14', x = -100:2:100, y = x; for 'F5', x = -200:2:200, y = x; for 'F7', x = -1:0.03:1, y = x; for 'F8','F11', x = -500:10:500, y = x; for 'F9','F15','F17','F19','F20','F21','F22','F23', x = -5:0.1:5, y = x; for 'F10', x = -20:0.5:20, y = x; for 'F12', x = -10:0.1:10, y = x; for 'F13', x = -5:0.08:5, y = x; for 'F16', x = -1:0.01:1, y = x; for 'F18', x = -5:0.06:5, y = x. Calculate L = length(x), initialize f as an empty matrix. Use two nested loops (i from 1 to L, j from 1 to L) to compute 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]). Finally, use surfc(x, y, f, 'LineStyle', 'none') to plot the 3D surface graph and set colormap to winter. Note that Get_Functions_details is predefined and does not need to be redefined.</think>Please write a MATLAB function named func_plot that takes a function name as input and plots the 3D surface graph of the corresponding function. The function is defined as function func_plot(func_name). First, call Get_Functions_details(func_name) to get lb, ub, dim, and fobj. Then, set the ranges and steps of x and y based on func_name: for 'F1','F2','F3','F4','F6','F14', x = -100:2:100, y = x; for 'F5', x = -200:2:200, y = x; for 'F7', x = -1:0.03:1, y = x; for 'F8','F11', x = -500:10:500, y = x; for 'F9','F15','F17','F19','F20','F21','F22','F23', x = -5:0.1:5, y = x; for 'F10', x = -20:0.5:20, y = x; for 'F12', x = -10:0.1:10, y = x; for 'F13', x = -5:0.08:5, y = x; for 'F16', x = -1:0.01:1, y = x; for 'F18', x = -5:0.06:5, y = x. Calculate L = length(x) and initialize f as an empty matrix. Use two nested loops (i from 1 to L, j from 1 to L) to compute 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]). Finally, use surfc(x, y, f, 'LineStyle', 'none') to plot the 3D surface graph and set the colormap to winter. Note that Get_Functions_details is predefined and does not need to be redefined.
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');
colormap winter

end
function_header: function func_plot(func_name)

end

step_number: 10
step_description_prompt: Please use MATLAB code to generate a script that executes the Marine Predators Algorithm (MPA) to optimize the benchmark function F23, achieving identical functionality to the original code including parameter setup, function calls, plotting, and result presentation. The script must follow these steps:  
1. Clear the workspace and command window with `clear all` and `clc`.  
2. Set the random seed to 42 using `rng(42)`.  
3. Enable long numeric format via `format long`.  
4. Initialize `SearchAgents_no` (number of search agents) to 25.  
5. Assign `Function_name` to 'F23'.  
6. Set `Max_iteration` (maximum iterations) to 500.  
7. Retrieve the target function’s details (lower bound `lb`, upper bound `ub`, dimension `dim`, objective function handle `fobj`) by calling `[lb, ub, dim, fobj] = Get_Functions_details(Function_name)` (assume this function is pre-defined).  
8. Run MPA to get optimization results: `[Best_score, Best_pos, Convergence_curve] = MPA(SearchAgents_no, Max_iteration, lb, ub, dim, fobj)` (MPA is pre-defined).  
9. Create a figure with position `[500 400 700 290]` using `figure('Position', [500 400 700 290])`.  
10. For `subplot(1,2,1)`: Plot the function topology with `func_plot(Function_name)` (pre-defined), set title to 'Function Topology', x-label to 'x_1', y-label to 'x_2', and z-label to `[Function_name, '( x_1 , x_2 )']`.  
11. For `subplot(1,2,2)`: Plot the convergence curve using `semilogy(Convergence_curve, 'Color', 'r')`, set title to 'Objective space', x-label to 'Iteration', and y-label to 'Best score obtained so far'.  
12. Display the best solution with 10 decimal places via `display(['The best solution obtained by MPA is : ', num2str(Best_pos, 10)])`.  
13. Display the best objective value with 10 decimal places using `display(['The best optimal value of the objective function found by MPA is : ', num2str(Best_score, 10)])`.  
14. Show a separator line with `disp(sprintf('--------------------------------------'))`.  

The functions `Get_Functions_details`, `MPA`, and `func_plot` are assumed to be pre-defined and do not require reimplementation. Ensure all variable names, parameters, and function calls match the original code exactly.
test_cases: rng(42)
correct_score = -10.536410;
correct_pos = 16.000513;
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: