Problem: MATLAB Implementation of Equilibrium Optimizer

The problem involves implementing MATLAB functions for benchmark optimization problems, initialization, the Equilibrium Optimizer algorithm, and a details-getting function to return convergence, average, and standard deviation over runs.


Input/Output: The problem takes function-specific parameters (e.g., vectors x for F1-F23, function name F for Get_Functions_details, SearchAgents_no/dim/ub/lb for initialization, Particles_no/Max_iter/lb/ub/dim/fobj/Run_no for EO) and predefined test cases. It generates MATLAB functions (Ufun, F1-F23, Get_Functions_details, initialization, EO) implementing specified algorithms, outputs EO's convergence curve, average best fitness, standard deviation across runs, and test validation results.

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please write a MATLAB function named Ufun that implements a piecewise power function for given input and parameters. The function is defined as: function o = Ufun(x, a, k, m). Algorithm flow: 1. Compute the first term: k multiplied by (x - a) raised to the power of m, but only for elements where x > a (others are 0 due to the logical indexing (x > a)). 2. Compute the second term: k multiplied by (-x - a) raised to the power of m, but only for elements where x < -a (others are 0 due to the logical indexing (x < -a)). 3. Sum the two terms to get the output o.
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 takes a vector x as input and returns the sum of the squares of the elements of x.
Function definition: function o = F1(x)
Algorithm flow: Calculate the sum of the element-wise squares of x using sum(x.^2), assign the result to o.

Please use MATLAB code to generate a function named F2 that takes a vector x as input and returns the sum of the absolute values of the elements of x plus the product of the absolute values of the elements of x.
Function definition: function o = F2(x)
Algorithm flow: Compute the sum of absolute values of x using sum(abs(x)), compute the product of absolute values of x using prod(abs(x)), add these two results together and assign to o.

Please use MATLAB code to generate a function named F3 that takes a vector x as input and returns the sum of the squares of the cumulative sums of the first i elements of 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 (columns) of x using dim = size(x, 2). Initialize o to 0. Then, loop i from 1 to dim: for each i, compute the sum of the first i elements of x using sum(x(1:i)), square this sum, and add it to o. After the loop, o holds the result.

Please use MATLAB code to generate a function named F4 that takes a vector x as input and returns the maximum value of the absolute values of the elements of x.
Function definition: function o = F4(x)
Algorithm flow: Calculate the absolute values of all elements in x using abs(x), then find the maximum value of these absolute values using max(abs(x)), assign the result to o.

Please use MATLAB code to generate a function named F5 that takes a vector x as input and returns the sum of 100 times the square of (x(i+1) - x(i)^2) plus the square of (x(i) - 1) for i from 1 to the number of elements in x minus 1.
Function definition: function o = F5(x)
Algorithm flow: First, get the number of elements (columns) of x using dim = size(x, 2). Compute the sum of two parts for each i from 1 to dim-1: the first part is 100*(x(i+1) - x(i)^2)^2, the second part is (x(i) - 1)^2. Sum all these parts together using sum(100*(x(2:dim)-(x(1:dim-1).^2)).^2 + (x(1:dim-1)-1).^2) and assign the result to o.

Please use MATLAB code to generate a function named F6 that takes a vector x as input and returns the sum of the squares of the absolute values of (x elements plus 0.5).
Function definition: function o = F6(x)
Algorithm flow: For each element in x, add 0.5, take the absolute value, square it, then sum all these values using sum(abs((x+.5)).^2), assign the result to o.

Please use MATLAB code to generate a function named F7 that takes a vector x as input and returns the sum of the product of each element's index (starting from 1) and the fourth power of the element, plus a random number between 0 and 1.
Function definition: function o = F7(x)
Algorithm flow: First, get the number of elements (columns) of x using dim = size(x, 2). Create an index vector from 1 to dim using [1:dim]. Compute the element-wise fourth power of x using x.^4. Multiply the index vector with the fourth power vector element-wise. Sum these products using sum([1:dim].*(x.^4)), then add a random number between 0 and 1 (generated by rand()) to the sum, assign the result to o.

Please use MATLAB code to generate a function named F8 that takes a vector x as input and returns 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 x.
Function definition: function o = F8(x)
Algorithm flow: For each element in x, compute -x.*sin(sqrt(abs(x))) element-wise, then sum all these values using sum(-x.*sin(sqrt(abs(x)))), assign the result to 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: Write MATLAB code to generate functions named F9, F10, F11, F12, F13, F14, F15, F16, F17 that implement the corresponding benchmark functions. Each function takes a vector x as input and returns a scalar output o. Assume that any called functions (like Ufun) are already defined. For function F9: Define a function o = F9(x). First, get dim as the number of columns of x. Then compute o as sum(x.^2 - 10*cos(2*pi.*x)) + 10*dim. For function F10: Define a function o = F10(x). First, get dim as the number of columns of x. Then compute o as -20*exp(-0.2*sqrt(sum(x.^2)/dim)) - exp(sum(cos(2*pi.*x))/dim) + 20 + exp(1). For function F11: Define a function o = F11(x). First, get dim as the number of columns of x. Then compute o as sum(x.^2)/4000 - prod(cos(x./sqrt(1:dim))) + 1. For function F12: Define a function o = F12(x). First, get dim as the number of columns of x. Then 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)). For function F13: Define a function o = F13(x). First, get dim as the number of columns of x. Then 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)). For function F14: Define a function o = F14(x). First, define aS as a 2x25 matrix: [-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 sum((x' - aS(:,j)).^6). Finally, compute o as (1/500 + sum(1./([1:25] + bS))).^(-1). For function F15: Define a function o = F15(x). First, define aK as a vector [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 a vector [0.25 0.5 1 2 4 6 8 10 12 14 16] (then bK is 1 divided by each element of this vector). Compute o as sum((aK - (x(1)*(bK.^2 + x(2)*bK))./(bK.^2 + x(3)*bK + x(4))).^2). For function F16: Define a function o = F16(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). For function F17: Define a function o = F17(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.
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, F23. Each function takes a vector x as input and returns a scalar o as output, which computes a specific mathematical expression as follows.

For function F18:
- Function definition: function o = F18(x)
- Algorithm flow: 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.

For function F19:
- Function definition: function o = F19(x)
- Algorithm flow: 
  1. Predefine matrices and vectors: 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].
  2. Initialize o to 0.
  3. Loop i from 1 to 4: For each i, compute the term -cH(i) * exp(-sum(aH(i,:) .* ((x - pH(i,:)) .^ 2))) and add it to o.

For function F20:
- Function definition: function o = F20(x)
- Algorithm flow:
  1. Predefine matrices and vectors: 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].
  2. Initialize o to 0.
  3. Loop i from 1 to 4: For each i, compute the term -cH(i) * exp(-sum(aH(i,:) .* ((x - pH(i,:)) .^ 2))) and add it to o.

For function F21:
- Function definition: function o = F21(x)
- Algorithm flow:
  1. Predefine matrices and vectors: 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].
  2. Initialize o to 0.
  3. Loop i from 1 to 5: For each i, compute the term -1 / ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i)) and add it to o.

For function F22:
- Function definition: function o = F22(x)
- Algorithm flow:
  1. Predefine matrices and vectors: Same aSH and cSH as F21.
  2. Initialize o to 0.
  3. Loop i from 1 to 7: For each i, compute the term -1 / ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i)) and add it to o.

For function F23:
- Function definition: function o = F23(x)
- Algorithm flow:
  1. Predefine matrices and vectors: Same aSH and cSH as F21.
  2. Initialize o to 0.
  3. Loop i from 1 to 10: For each i, compute the term -1 / ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i)) and add it to 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 use MATLAB code to generate a function named Get_Functions_details that takes a string input F and returns four outputs: lb (lower bound), ub (upper bound), dim (dimension of the test function), and fobj (a function handle to the corresponding test function). The function uses a switch-case structure to map the input F to specific values for each output. The function handles F1 to F23 are assumed to be pre-defined and do not need to be implemented in this function.

function [lb,ub,dim,fobj] = Get_Functions_details(F)
switch F
    case 'F1'
        fobj = @F1;
        lb=-100;
        ub=100;
        dim=30;
    case 'F2'
        fobj = @F2;
        lb=-100;
        ub=100;
        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;
end
end
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 and calculates the domain range. The function is defined as [Cin, domain] = initialization(SearchAgents_no, dim, ub, lb), where SearchAgents_no is the number of search agents, dim is the dimension of the search space, ub is the upper bound of the search space (a scalar or a 1-by-dim vector), lb is the lower bound of the search space (a scalar or a 1-by-dim vector), Cin is the initialized positions of search agents (a SearchAgents_no-by-dim matrix), and domain is the range of each dimension in the search space (a 1-by-dim vector). 

First, calculate Boundary_no as the number of columns of ub using Boundary_no = size(ub, 2). If Boundary_no equals 1 (ub and lb are scalars), generate Cin as a SearchAgents_no-by-dim matrix with elements computed as rand(SearchAgents_no, dim) .* (ub - lb) + lb, and set domain as a 1-by-dim vector of ones multiplied by (ub - lb). If Boundary_no is greater than 1 (ub and lb are 1-by-dim vectors), loop over each dimension i from 1 to dim: extract ub_i = ub(i) and lb_i = lb(i), then assign the i-th column of Cin as rand(SearchAgents_no, 1) .* (ub_i - lb_i) + lb_i. After the loop, set domain as a 1-by-dim vector of ones multiplied by (ub - lb). 

Functions called in the provided code are assumed to be already defined and do not need to be redefined.
function_header: function [Cin,domain]=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 EO that implements the Equilibrium Optimizer algorithm for optimization problems. The function optimizes a given objective function over multiple runs, returns the convergence curve, average best fitness, and standard deviation of best fitness across runs. The initialization function called in the code is assumed to be already defined.

Function definition: [Convergence_curve,Ave,Sd]=EO(Particles_no,Max_iter,lb,ub,dim,fobj,Run_no)

Algorithm flow:
1. Loop over each run from 1 to Run_no:
   a. Initialize Ceq1, Ceq2, Ceq3, Ceq4 as zero vectors of size 1×dim, and set their corresponding fitness values (Ceq1_fit, Ceq2_fit, Ceq3_fit, Ceq4_fit) to infinity.
   b. Initialize the population C using the initialization function with parameters Particles_no, dim, ub, lb.
   c. Set Iter=0, V=1, a1=2, a2=1, GP=0.5.
   d. Enter the main iteration loop while Iter < Max_iter:
      i. For each particle in C, check boundary violations (values exceeding ub or below lb) and correct them by clamping to ub or lb.
      ii. Evaluate the fitness of each particle using the objective function fobj, store in the fitness vector.
      iii. Update Ceq1-Ceq4 and their fitness values based on fitness ranking: Ceq1 holds the particle with the smallest fitness (best), Ceq2 holds the second smallest, Ceq3 the third, Ceq4 the fourth.
      iv. If it is the first iteration (Iter==0), store the initial fitness and population as fit_old and C_old.
      v. For each particle, if the old fitness (fit_old(i)) is smaller than the current fitness (fitness(i)), replace the current fitness and particle position with the old ones to preserve better solutions from the previous iteration.
      vi. Update C_old to the current C and fit_old to the current fitness.
      vii. Compute Ceq_ave as the average of Ceq1, Ceq2, Ceq3, Ceq4. Create C_pool as a matrix containing Ceq1, Ceq2, Ceq3, Ceq4, and Ceq_ave.
      viii. Compute t using the formula t=(1-Iter/Max_iter)^(a2*Iter/Max_iter).
      ix. For each particle in the population:
          - Generate random vectors lambda and r of size 1×dim (values between 0 and 1).
          - Randomly select a Ceq from C_pool (using randi to choose a row index).
          - Compute F using F=a1*sign(r-0.5).*(exp(-lambda.*t)-1).
          - Generate random scalars r1 and r2 (between 0 and 1), compute GCP as 0.5*r1*ones(1,dim) if r2>=GP, else 0.
          - Compute G0=GCP.*(Ceq - lambda.*C(i,:)) and G=G0.*F.
          - Update the particle position using C(i,:)=Ceq+(C(i,:)-Ceq).*F+(G./lambda*V).*(1-F).
      x. Increment Iter by 1, store the current best fitness (Ceq1_fit) in Convergence_curve at index Iter.
      xi. Store the best fitness of the current run (Ceq1_fit) in Ceqfit_run(irun).
   e. After each run, display the run number, the best solution (Ceq1 with 10 decimal places), and the best objective value (Ceq1_fit with 10 decimal places), followed by a separator line.
2. After all runs are completed, compute Ave as the mean of Ceqfit_run and Sd as the standard deviation of Ceqfit_run.
3. Return Convergence_curve, Ave, and Sd.
function_header: function [Convergence_curve,Ave,Sd]=EO(Particles_no,Max_iter,lb,ub,dim,fobj,Run_no)

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

Particles_no = 11;
Max_iteration = 5;
Function_name = 'F2';
Run_no = 1;
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Convergence_curve,Ave,Sd]=EO(Particles_no,Max_iteration,lb,ub,dim,fobj,Run_no);
val = sum(Convergence_curve) + Ave + Sd;
correct = 2.889439e+43;
tol = abs((val-correct)/correct);
if tol>1e-4
    error('Wrong!')
end

Particles_no = 20;
Max_iteration = 10;
Function_name = 'F11';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Convergence_curve,Ave,Sd]=EO(Particles_no,Max_iteration,lb,ub,dim,fobj,Run_no);
val = sum(Convergence_curve) + Ave + Sd;
correct = 2.468823e+03;
tol = abs((val-correct)/correct);
if tol>1e-4
    error('Wrong!')
end

Particles_no = 30;
Max_iteration = 6;
Function_name = 'F20';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Convergence_curve,Ave,Sd]=EO(Particles_no,Max_iteration,lb,ub,dim,fobj,Run_no);
val = sum(Convergence_curve) + Ave + Sd;
correct = -1.458591e+01;
tol = abs((val-correct)/correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 8
step_description_prompt: Please write a MATLAB code to create a function named EO that implements the Evolutionary Optimization algorithm for optimization problems, returning the convergence curve, average objective function value, and standard deviation of the objective function over multiple independent runs.
function [Convergence_curve, Ave, Sd] = EO(Particles_no, Max_iteration, lb, ub, dim, fobj, Run_no)
The algorithm flow of the EO function is as follows:
1. Initialize a matrix `Convergence_curve` of size `Max_iteration`×`Run_no` to store the best objective function value of each iteration for each run.
2. For each run from 1 to `Run_no`:
   a. Initialize the population with `Particles_no` particles. Each particle is a `dim`-dimensional vector, with each element randomly sampled from the uniform distribution between `lb` and `ub`.
   b. Evaluate the objective function `fobj` for all particles to get their objective values.
   c. Find the best particle (with the minimum objective function value) in the initial population.
   d. For each iteration from 1 to `Max_iteration`:
      i. Update the position of each particle using the Evolutionary Optimization algorithm's position update rules.
      ii. Bound the particle positions within `[lb, ub]`—if a particle's dimension exceeds `lb` or `ub`, clip it to the corresponding bound.
      iii. Re-evaluate the objective function for all updated particles.
      iv. Update the best particle if any updated particle has a smaller objective function value.
      v. Record the best objective function value of the current iteration in `Convergence_curve(iteration, run)`.
3. After completing all runs:
   a. Calculate `Ave` as the mean of `Convergence_curve` across all runs (average over the second dimension), resulting in a vector of length `Max_iteration` that represents the average best objective value per iteration.
   b. Calculate `Sd` as the standard deviation of the objective function values across all runs (consistent with the original code's standard deviation calculation for the objective function).
4. Return `Convergence_curve`, `Ave`, and `Sd`.
Note: The function `Get_Functions_details` called in the main script is pre-defined and does not need to be implemented.
test_cases: rng(42)
correct_Ave = 8.955979e-41;
correct_Sd = 3.484368e-40;
val_Ave = sum(Ave(:));
val_Sd = sum(Sd(:));
tol_score = abs((val_Ave-correct_Ave)/correct_Ave);
if tol_score > 1e-4
    error('Wrong')
end
tol_pos = abs((val_Sd-correct_Sd)/correct_Sd);
if tol_pos > 1e-4
    error('Wrong')
end
disp('Completely right!')

General solution:

General tests: