Problem: MATLAB MFO and Test Functions Implementation

The problem is to implement MFO with helper functions (Ufun, F1-F23, Get_Functions_details, initialization) to optimize a function and return best fitness, position, and convergence curve.


Input/Output: The problem takes as input function specifications including names, parameters, algorithm steps, and test cases for Ufun, F1-F23, Get_Functions_details, initialization, and MFO. It generates as output the correct MATLAB code for each function that adheres to the specifications and passes the test cases.

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please use MATLAB code to create a function named Ufun that implements a piecewise power function calculation based on the input parameters x, a, k, and m. The function is defined as function o = Ufun(x, a, k, m). Compute two element-wise terms: the first term is k.*((x - a).^m) multiplied by the element-wise logical mask (x > a); the second term is k.*((-x - a).^m) multiplied by the element-wise logical mask (x < (-a)). Add these two terms element-wise to form the output o, where elements in x that are between -a and a (inclusive) result in 0 because both logical masks are false for those elements.
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. The function definition is: function o = F1(x). The algorithm flow is: Compute the sum of each element in x squared using element-wise squaring (x.^2) followed by summing with sum().

Please write a MATLAB function named F2 that computes the sum of the absolute values of the elements in the input vector plus the product of the absolute values of the elements. The function definition is: function o = F2(x). The algorithm flow is: Calculate sum(abs(x)) (sum of absolute values of x) and prod(abs(x)) (product of absolute values of x), then add these two results together.

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 (i ranges from 1 to the dimension of x). The function definition is: function o = F3(x). The algorithm flow is: 1. Get the dimension dim of x as size(x,2). 2. Initialize o to 0. 3. Loop i from 1 to dim: a. Compute the sum of the first i elements of x (sum(x(1:i))). b. Square this sum and add it to o. 4. Return o.

Please write a MATLAB function named F4 that computes the maximum value of the absolute values of the elements in the input vector. The function definition is: function o = F4(x). The algorithm flow is: Compute the maximum value of abs(x) (absolute values of x) using max().

Please write a MATLAB function named F5 (Rosenbrock function) that computes the sum of 100*(x(i+1)-x(i)^2)^2 + (x(i)-1)^2 for i from 1 to dim-1 (dim is the dimension of x). The function definition is: function o = F5(x). The algorithm flow is: 1. Get dim as size(x,2). 2. Compute the sum of 100*(x(2:dim)-(x(1:dim-1).^2)).^2 plus (x(1:dim-1)-1).^2 using sum(). 3. Return the result as o.

Please write a MATLAB function named F6 that computes the sum of the squares of the absolute values of (each element in x plus 0.5). The function definition is: function o = F6(x). The algorithm flow is: Compute sum(abs(x + 0.5).^2) (element-wise absolute value of x+0.5, squared, then summed).

Please write a MATLAB function named F7 that computes the sum of (each index from 1 to dim multiplied by the fourth power of the corresponding element in x) plus a random number between 0 and 1. The function definition is: function o = F7(x). The algorithm flow is: 1. Get dim as size(x,2). 2. Compute sum([1:dim] .* (x.^4)) (indices 1 to dim element-wise multiplied by x's fourth power, then summed). 3. Add rand() (a random number from 0 to 1) to the sum. 4. Return the result as o.

Please write a MATLAB function named F8 that computes the sum of -x.*sin(sqrt(abs(x))) for each element in the input vector. The function definition is: function o = F8(x). The algorithm flow is: Compute sum(-x .* sin(sqrt(abs(x)))) (element-wise product of -x, sin of sqrt(abs(x)), then summed).
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 a MATLAB function named F9 that implements the Rastrigin function, which takes a vector x as input and returns the computed value o.
Function definition: function o = F9(x)
Algorithm steps: Compute the dimension dim as the number of columns of x. Then calculate o as the sum of (x.^2 - 10*cos(2*pi.*x)) plus 10*dim.

Please write a MATLAB function named F10 that implements the Ackley function, which takes a vector x as input and returns the computed value o.
Function definition: function o = F10(x)
Algorithm steps: Compute the dimension dim as the number of columns of x. Then calculate o as -20*exp(-0.2*sqrt(sum(x.^2)/dim)) - exp(sum(cos(2*pi.*x))/dim) + 20 + exp(1).

Please write a MATLAB function named F11 that implements the Griewank function, which takes a vector x as input and returns the computed value o.
Function definition: function o = F11(x)
Algorithm steps: Compute the dimension dim as the number of columns of x. Then calculate o as sum(x.^2)/4000 - prod(cos(x./sqrt([1:dim]))) + 1.

Please write a MATLAB function named F12 that implements the given function, which takes a vector x as input and returns the computed value o (assuming the Ufun function is already defined).
Function definition: function o = F12(x)
Algorithm steps: Compute the dimension dim as the number of columns of x. Then calculate 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)).

Please write a MATLAB function named F13 that implements the given function, which takes a vector x as input and returns the computed value o (assuming the Ufun function is already defined).
Function definition: function o = F13(x)
Algorithm steps: Compute the dimension dim as the number of columns of x. Then calculate 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)).

Please write a MATLAB function named F14 that implements the given function, which takes a vector x as input and returns the computed value o.
Function definition: function o = F14(x)
Algorithm steps: First, define the 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, for each j from 1 to 25, compute bS(j) as the sum of (transpose(x) - aS(:,j)).^6. Finally, calculate o as (1/500 + sum(1./([1:25] + bS))).^(-1).

Please write a MATLAB function named F15 that implements the given function, which takes a vector x as input and returns the computed value o.
Function definition: function o = F15(x)
Algorithm steps: First, define the vector aK as [.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246] and the vector bK as 1./[.25 .5 1 2 4 6 8 10 12 14 16]. Then calculate o as the sum((aK - ((x(1).*(bK.^2 + x(2).*bK))./(bK.^2 + x(3).*bK + x(4)))).^2).

Please write a MATLAB function named F16 that implements the given function, which takes a vector x as input and returns the computed value o.
Function definition: function o = F16(x)
Algorithm steps: Calculate 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).

Please write a MATLAB function named F17 that implements the given function, which takes a vector x as input and returns the computed value o.
Function definition: function o = F17(x)
Algorithm steps: Calculate 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 write a MATLAB function named F18 that takes a vector x as input and returns a scalar o. The function computes 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)). The function definition is function o = F18(x).

Please write a MATLAB function named F19 that takes a vector x as input and returns a scalar o. First, define the matrices aH = [3 10 30; .1 10 35; 3 10 30; .1 10 35], cH = [1 1.2 3 3.2], pH = [.3689 .117 .2673; .4699 .4387 .747; .1091 .8732 .5547; .03815 .5743 .8828]. Initialize o to 0. Then loop i from 1 to 4, and for each i, add -cH(i)*exp(-sum(aH(i,:).*((x - pH(i,:)).^2))) to o. The function definition is function o = F19(x).

Please write a MATLAB function named F20 that takes a vector x as input and returns a scalar o. First, define the matrices 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], cH = [1 1.2 3 3.2], 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 i from 1 to 4, and for each i, add -cH(i)*exp(-sum(aH(i,:).*((x - pH(i,:)).^2))) to o. The function definition is function o = F20(x).

Please write a MATLAB function named F21 that takes a vector x as input and returns a scalar o. First, define the matrices 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], cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5]. Initialize o to 0. Then loop i from 1 to 5, and for each i, add -((x - aSH(i,:))*(x - aSH(i,:))' + cSH(i))^(-1) to o. The function definition is function o = F21(x).

Please write a MATLAB function named F22 that takes a vector x as input and returns a scalar o. First, define the matrices 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], cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5]. Initialize o to 0. Then loop i from 1 to 7, and for each i, add -((x - aSH(i,:))*(x - aSH(i,:))' + cSH(i))^(-1) to o. The function definition is function o = F22(x).

Please write a MATLAB function named F23 that takes a vector x as input and returns a scalar o. First, define the matrices 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], cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5]. Initialize o to 0. Then loop i from 1 to 10, and for each i, add -((x - aSH(i,:))*(x - aSH(i,:))' + cSH(i))^(-1) to o. The function definition is function o = F23(x).
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 use MATLAB code to create a function named Get_Functions_details that takes a string input F and returns four outputs: lb (lower bound), ub (upper bound), dim (dimension), and fobj (function handle to a pre-defined test function). The function maps the input F to specific parameters for test functions F1 to F23.

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

Use a switch statement to evaluate the input string F. For each case from 'F1' to 'F23', assign the corresponding lb, ub, dim, and fobj (a function handle to the pre-defined function with the same name as the case) as follows:
- Case 'F1': lb = -100; ub = 100; dim = 10; fobj = @F1;
- Case 'F2': lb = -10; ub = 10; dim = 10; fobj = @F2;
- Case 'F3': lb = -100; ub = 100; dim = 10; fobj = @F3;
- Case 'F4': lb = -100; ub = 100; dim = 10; fobj = @F4;
- Case 'F5': lb = -30; ub = 30; dim = 10; fobj = @F5;
- Case 'F6': lb = -100; ub = 100; dim = 10; fobj = @F6;
- Case 'F7': lb = -1.28; ub = 1.28; dim = 10; fobj = @F7;
- Case 'F8': lb = -500; ub = 500; dim = 10; fobj = @F8;
- Case 'F9': lb = -5.12; ub = 5.12; dim = 10; fobj = @F9;
- Case 'F10': lb = -32; ub = 32; dim = 10; fobj = @F10;
- Case 'F11': lb = -600; ub = 600; dim = 10; fobj = @F11;
- Case 'F12': lb = -50; ub = 50; dim = 10; fobj = @F12;
- Case 'F13': lb = -50; ub = 50; dim = 10; fobj = @F13;
- Case 'F14': lb = -65.536; ub = 65.536; dim = 2; fobj = @F14;
- Case 'F15': lb = -5; ub = 5; dim = 4; fobj = @F15;
- Case 'F16': lb = -5; ub = 5; dim = 2; fobj = @F16;
- Case 'F17': lb = [-5, 0]; ub = [10, 15]; dim = 2; fobj = @F17;
- Case 'F18': lb = -2; ub = 2; dim = 2; fobj = @F18;
- Case 'F19': lb = 0; ub = 1; dim = 3; fobj = @F19;
- Case 'F20': lb = 0; ub = 1; dim = 6; fobj = @F20;
- Case 'F21': lb = 0; ub = 10; dim = 4; fobj = @F21;
- Case 'F22': lb = 0; ub = 10; dim = 4; fobj = @F22;
- Case 'F23': lb = 0; ub = 10; dim = 4; fobj = @F23;

The functions F1 to F23 referenced by the function handles 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 = 864.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 creates the first random population of moths. The function definition must be exactly: function X=initialization(SearchAgents_no,dim,ub,lb). The algorithm flow is as follows: 1. Compute Boundary_no as the number of columns in ub using size(ub,2). 2. If Boundary_no equals 1 (indicating all variables have the same upper bound and lower bound), generate X as a matrix of size SearchAgents_no-by-dim where each element is calculated by rand(SearchAgents_no,dim).*(ub - lb) + lb. 3. If Boundary_no is greater than 1 (indicating each variable has a different upper bound and lower bound), iterate over each dimension i from 1 to dim: a. Extract the i-th element of ub as ub_i. b. Extract the i-th element of lb as lb_i. c. Assign the i-th column of X with a column vector of size SearchAgents_no-by-1, where each element is calculated by rand(SearchAgents_no,1).*(ub_i - lb_i) + lb_i. Other functions called in the code are assumed to be pre-defined. The output X must be a matrix where each row represents a moth (search agent) and each column represents a dimension of the search space. Ensure the function strictly follows the above logic to produce the identical result as the original code.
function_header: function X=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 MFO that implements the Moth-Flame Optimization (MFO) algorithm to optimize a given objective function. The function will return the best fitness score, the corresponding best position, and the convergence curve.

The function definition must be exactly:
function [Best_flame_score,Best_flame_pos,Convergence_curve]=MFO(N,Max_iteration,lb,ub,dim,fobj)

Algorithm flow:
1. Display 'MFO is optimizing your problem' at the beginning.
2. Initialize moth positions using the pre-defined `initialization` function with parameters N, dim, ub, lb: `Moth_pos = initialization(N, dim, ub, lb)`.
3. Initialize `Convergence_curve` as a zero array of size 1×Max_iteration.
4. Set `Iteration = 1` and start the main loop which runs while `Iteration <= Max_iteration`.
5. In each iteration:
   a. Calculate the number of flames using `Flame_no = round(N - Iteration * ((N - 1)/Max_iteration))`.
   b. For each moth (from 1 to `size(Moth_pos,1)`):
      i. Check boundary constraints: If a moth's position exceeds the upper bound `ub` or is below the lower bound `lb`, bring it back to the boundary using:
         `Flag4ub = Moth_pos(i,:) > ub;`
         `Flag4lb = Moth_pos(i,:) < lb;`
         `Moth_pos(i,:) = (Moth_pos(i,:).*(~(Flag4ub + Flag4lb))) + ub.*Flag4ub + lb.*Flag4lb;`
      ii. Compute the fitness of each moth using the objective function `fobj`: `Moth_fitness(1,i) = fobj(Moth_pos(i,:))`.
   c. If it is the first iteration (`Iteration == 1`):
      i. Sort `Moth_fitness` in ascending order and get the sorted indices: `[fitness_sorted, I] = sort(Moth_fitness)`.
      ii. Sort the moth population based on the sorted fitness: `sorted_population = Moth_pos(I,:)`.
      iii. Update `best_flames` to `sorted_population` and `best_flame_fitness` to `fitness_sorted`.
   d. For iterations after the first:
      i. Combine the previous population and best flames: `double_population = [previous_population; best_flames]`; combine the previous fitness and best flame fitness: `double_fitness = [previous_fitness best_flame_fitness]`.
      ii. Sort `double_fitness` in ascending order and get the sorted indices: `[double_fitness_sorted, I] = sort(double_fitness)`.
      iii. Sort `double_population` based on the sorted fitness: `double_sorted_population = double_population(I,:)`.
      iv. Take the top N elements for fitness and population: `fitness_sorted = double_fitness_sorted(1:N)`; `sorted_population = double_sorted_population(1:N,:)`.
      v. Update `best_flames` to `sorted_population` and `best_flame_fitness` to `fitness_sorted`.
   e. Update the best flame score and position: `Best_flame_score = fitness_sorted(1)`; `Best_flame_pos = sorted_population(1,:)`.
   f. Store current moth positions and fitness for the next iteration: `previous_population = Moth_pos`; `previous_fitness = Moth_fitness`.
   g. Calculate parameter `a` which linearly decreases from -1 to -2: `a = -1 + Iteration * ((-1)/Max_iteration)`.
   h. Update each moth's position:
      i. For each moth `i` (1 to `size(Moth_pos,1)`) and each dimension `j` (1 to `size(Moth_pos,2)`):
         - If `i <= Flame_no`: Update position with respect to the i-th flame. Compute `distance_to_flame = abs(sorted_population(i,j) - Moth_pos(i,j))`; set `b = 1`; compute `t = (a - 1)*rand() + 1`; update `Moth_pos(i,j) = distance_to_flame * exp(b.*t) * cos(t.*2*pi) + sorted_population(i,j)`.
         - If `i > Flame_no`: Update position with respect to the Flame_no-th flame. Compute `distance_to_flame = abs(sorted_population(i,j) - Moth_pos(i,j))`; set `b = 1`; compute `t = (a - 1)*rand() + 1`; update `Moth_pos(i,j) = distance_to_flame * exp(b.*t) * cos(t.*2*pi) + sorted_population(Flame_no,j)`.
   i. Record the best fitness of the current iteration to `Convergence_curve`: `Convergence_curve(Iteration) = Best_flame_score`.
   j. Display the iteration number and best fitness every 50 iterations using: `if mod(Iteration,50) == 0, display(['At iteration ', num2str(Iteration), ' the best fitness is ', num2str(Best_flame_score)]); end`.
   k. Increment `Iteration` by 1.
6. The pre-defined `initialization` function is assumed to be available and does not need to be redefined in the MFO function.
function_header: function [Best_flame_score,Best_flame_pos,Convergence_curve]=MFO(N,Max_iteration,lb,ub,dim,fobj)

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

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

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

Max_iteration = 8;
fobj = @F1;
Function_name = 'F11';
SearchAgents_no = 30;
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=MFO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 2021.383967;
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 draws benchmark functions. The function accepts a string input func_name which specifies the benchmark function to plot. The function follows these steps:

1. Function definition: function func_plot(func_name)
2. Call Get_Functions_details(func_name) to get lb, ub, dim, and fobj (assume Get_Functions_details is pre-defined and returns these values correctly for the given func_name).
3. Use a switch-case structure on func_name to set the x and y vectors with the exact ranges and steps as follows:
   - 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;
4. Compute L as the length of x (L=length(x)).
5. Initialize an empty matrix f (f=[]).
6. Use nested for loops (i from 1 to L, j from 1 to L) to calculate f(i,j) based on func_name:
   - 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]).
7. Plot the surface using surfc(x,y,f,'LineStyle','none') to match the exact visualization settings.

Ensure all logic, variable names, ranges, and function calls are identical to the original code. The Get_Functions_details function is assumed to be pre-defined and functional.
ground_truth_code: % This function draws the benchmark functions

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; %[-100,100]
    case 'F2' 
        x=-100:2:100; y=x; %[-10,10]
    case 'F3' 
        x=-100:2:100; y=x; %[-100,100]
    case 'F4' 
        x=-100:2:100; y=x; %[-100,100]
    case 'F5' 
        x=-200:2:200; y=x; %[-5,5]
    case 'F6' 
        x=-100:2:100; y=x; %[-100,100]
    case 'F7' 
        x=-1:0.03:1;  y=x;  %[-1,1]
    case 'F8' 
        x=-500:10:500;y=x; %[-500,500]
    case 'F9' 
        x=-5:0.1:5;   y=x; %[-5,5]    
    case 'F10' 
        x=-20:0.5:20; y=x;%[-500,500]
    case 'F11' 
        x=-500:10:500; y=x;%[-0.5,0.5]
    case 'F12' 
        x=-10:0.1:10; y=x;%[-pi,pi]
    case 'F13' 
        x=-5:0.08:5; y=x;%[-3,1]
    case 'F14' 
        x=-100:2:100; y=x;%[-100,100]
    case 'F15' 
        x=-5:0.1:5; y=x;%[-5,5]
    case 'F16' 
        x=-1:0.01:1; y=x;%[-5,5]
    case 'F17' 
        x=-5:0.1:5; y=x;%[-5,5]
    case 'F18' 
        x=-5:0.06:5; y=x;%[-5,5]
    case 'F19' 
        x=-5:0.1:5; y=x;%[-5,5]
    case 'F20' 
        x=-5:0.1:5; y=x;%[-5,5]        
    case 'F21' 
        x=-5:0.1:5; y=x;%[-5,5]
    case 'F22' 
        x=-5:0.1:5; y=x;%[-5,5]     
    case 'F23' 
        x=-5:0.1:5; y=x;%[-5,5]  
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 write MATLAB code to create a script that uses the Moth Flame Optimization (MFO) algorithm to optimize a specified benchmark function, outputting the best solution, best objective value, and convergence curve. The script must follow these steps: Clear all variables and the command window. Set the random seed to 42 using rng(42). Define the number of search agents as 30 (SearchAgents_no = 30), the test function name as 'F1' (Function_name = 'F1'), and the maximum number of iterations as 1000 (Max_iteration = 1000). Load the benchmark function details (lower bound lb, upper bound ub, dimension dim, objective function handle fobj) using Get_Functions_details(Function_name). Call the MFO algorithm with [Best_score, Best_pos, cg_curve] = MFO(SearchAgents_no, Max_iteration, lb, ub, dim, fobj). Create a figure with Position set to [284 214 660 290]. Add two subplots: Subplot 1 (1,2,1) uses func_plot(Function_name) to draw the test function surface, with title 'Test function', xlabel 'x_1', ylabel 'x_2', zlabel concatenated as [Function_name,'( x_1 , x_2 )'], and grid turned off. Subplot 2 (1,2,2) plots the convergence curve using semilogy(cg_curve, 'Color','b'), with title 'Convergence curve', xlabel 'Iteration', ylabel 'Best flame (score) obtained so far', axis tight, grid off, box on, and legend 'MFO'. Display the best solution with display(['The best solution obtained by MFO is : ', num2str(Best_pos)]) and the best objective value with display(['The best optimal value of the objective function found by MFO is : ', num2str(Best_score)]). Assume Get_Functions_details, MFO, and func_plot are pre-defined.
test_cases: rng(42)
correct_score = 8.3391e-32;
correct_pos = -4.7995e-16;
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: