Problem: Multi-Verse Optimizer Implementation with Auxiliary Functions

The problem requires implementing MATLAB functions for math operations, optimization utilities, and the MVO algorithm to solve minimization problems with specified inputs/outputs.


Input/Output: The problem takes function-specific inputs including x,a,k,m (Ufun), x (F1-F23), F (Get_Functions_details), SearchAgents_no,dim,ub,lb (initialization), weights (RouletteWheelSelection), and N,Max_time,lb,ub,dim,fobj (MVO). It returns the computed outputs: o for Ufun/F1-F23, lb,ub,dim,fobj for Get_Functions_details, X for initialization, choice for RouletteWheelSelection, and Best_universe_Inflation_rate, Best_universe, Convergence_curve for MVO, adhering to the specified mathematical definitions and algorithm flows.

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please write a MATLAB function named Ufun that implements a piecewise power function based on inputs x, a, k, and m. Function definition: function o = Ufun(x, a, k, m). Algorithm flow: Calculate the output o as the sum of two components: the first component is k multiplied element-wise by (x minus a) raised to the power of m, then multiplied element-wise by the logical array where x is greater than a; the second component is k multiplied element-wise by (-x minus a) raised to the power of m, then multiplied element-wise by the logical array where x is less than -a. Any other functions called in the code are assumed to be pre-defined and do not need to be redefined.
function_header: function o=Ufun(x,a,k,m)

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

step_number: 2
step_description_prompt: Please write MATLAB code to generate functions named F1, F2, F3, F4, F5, F6, F7, F8 that implement the following functionalities respectively. Each function takes a vector x as input and returns a scalar o as output. The functions are defined and implemented as follows:

1. Function F1: Computes the sum of the squares of the elements in x.
   - Function definition: function o = F1(x)
   - Algorithm flow: Compute the sum of the squares of all elements in the input vector x using element-wise squaring and summation.

2. Function F2: Computes the sum of the absolute values of elements in x plus the product of the absolute values of elements in x.
   - Function definition: function o = F2(x)
   - Algorithm flow: Calculate the sum of abs(x) and add the product of abs(x) to it.

3. Function F3: Computes the sum of the squares of the cumulative sums of the first i elements in x (where i ranges from 1 to the number of columns in x).
   - Function definition: function o = F3(x)
   - Algorithm flow: Get the number of columns dim of the input vector x using size(x,2). Initialize the output o to 0. For each i from 1 to dim, compute the sum of the first i elements of x, square that sum, and add it to o.

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

5. Function F5: Computes the sum over i from 1 to (dim-1) of [100*(x(i+1) - x(i)^2)^2 + (x(i) - 1)^2], where dim is the number of columns in x (this is the Rosenbrock function).
   - Function definition: function o = F5(x)
   - Algorithm flow: Get the number of columns dim of the input vector x using size(x,2). Compute the sum for each i from 1 to dim-1: 100 multiplied by the square of (x(i+1) minus x(i) squared) plus the square of (x(i) minus 1). Sum all these values to get o.

6. Function F6: Computes the sum of the squares of the absolute values of (x + 0.5) for each element in x.
   - Function definition: function o = F6(x)
   - Algorithm flow: For each element in x, compute the absolute value of (x + 0.5), square the result, then sum all these squared values.

7. Function F7: Computes the sum of i multiplied by x(i)^4 (for i from 1 to the number of columns in x) plus a random number between 0 and 1 (generated by the built-in rand() function).
   - Function definition: function o = F7(x)
   - Algorithm flow: Get the number of columns dim of the input vector x using size(x,2). Compute the element-wise product of the vector [1:dim] and the fourth power of x, sum the product, then add a random number generated by rand() to the sum.

8. Function F8: Computes the sum of (-x(i) * sin(sqrt(abs(x(i)))) for each element i in x (this is the Schwefel function).
   - Function definition: function o = F8(x)
   - Algorithm flow: For each element in x, compute the product of -x, sin(sqrt(abs(x))), 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 a MATLAB function named F9 that implements the Rastrigin function. The function takes an input x (a vector or matrix where each row is a sample and each column is a dimension) and returns the computed result o. Define the function as: function o = F9(x). The algorithm flow is: 1. Compute dim as the number of columns of x using size(x,2). 2. Calculate o as the sum of (x squared minus 10 times cosine of 2π times x) for all elements, then add 10 multiplied by dim. Mathematically: o = sum(x.^2 - 10*cos(2*pi.*x)) + 10*dim.

Write a MATLAB function named F10 that implements the Ackley function. The function takes an input x (a vector or matrix where each row is a sample and each column is a dimension) and returns the computed result o. Define the function as: function o = F10(x). The algorithm flow is: 1. Compute dim as size(x,2). 2. Calculate o as -20 multiplied by exp(-0.2 times the square root of (sum of x squared divided by dim)) minus exp(sum of cosine of 2π times x divided by dim) plus 20 plus exp(1). Mathematically: o = -20*exp(-.2*sqrt(sum(x.^2)/dim)) - exp(sum(cos(2*pi.*x))/dim) + 20 + exp(1).

Write a MATLAB function named F11 that implements the Griewank function. The function takes an input x (a vector or matrix where each row is a sample and each column is a dimension) and returns the computed result o. Define the function as: function o = F11(x). The algorithm flow is: 1. Compute dim as size(x,2). 2. Calculate o as (sum of x squared divided by 4000) minus the product of cosine of (x divided by the square root of [1:dim]) plus 1. Mathematically: o = sum(x.^2)/4000 - prod(cos(x./sqrt([1:dim]))) + 1.

Write a MATLAB function named F12 that implements the Schwefel 2.13 function. The function takes an input x (a vector or matrix where each row is a sample and each column is a dimension) and returns the computed result o. Note that the function Ufun is assumed to be already defined. Define the function as: function o = F12(x). The algorithm flow is: 1. Compute dim as size(x,2). 2. Calculate o as (pi/dim) multiplied by (10 times (sin(pi times (1 plus (x(1)+1)/4)) squared) plus the sum of (((x(1:dim-1)+1)/4) squared multiplied by (1 plus 10 times (sin(pi times (1 plus (x(2:dim)+1)/4)) squared)) plus ((x(dim)+1)/4) squared) plus the sum of Ufun(x,10,100,4). Mathematically: o = (pi/dim)*(10*((sin(pi*(1+(x(1)+1)/4)))^2)+sum((((x(1:dim-1)+1)./4).^2).*(1+10.*((sin(pi.*(1+(x(2:dim)+1)/4)))).^2))+((x(dim)+1)/4)^2)+sum(Ufun(x,10,100,4)).

Write a MATLAB function named F13 that implements a variant of the Schwefel function. The function takes an input x (a vector or matrix where each row is a sample and each column is a dimension) and returns the computed result o. Note that the function Ufun is assumed to be already defined. Define the function as: function o = F13(x). The algorithm flow is: 1. Compute dim as size(x,2). 2. Calculate o as 0.1 multiplied by ((sin(3πx(1)) squared) plus the sum of ((x(1:dim-1)-1) squared multiplied by (1 plus (sin(3πx(2:dim)) squared)) plus ((x(dim)-1) squared multiplied by (1 plus (sin(2πx(dim)) squared))) plus the sum of Ufun(x,5,100,4). Mathematically: o = .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)).

Write a MATLAB function named F14 that implements the Kowalik function. The function takes an input x (a vector) and returns the computed result o. Define the function as: function o = F14(x). The algorithm flow is: 1. 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 ]. 2. Initialize bS as a 1x25 array. 3. For j from 1 to 25: compute bS(j) as sum((x' - aS(:,j)).^6). 4. Calculate o as the reciprocal of (1/500 plus sum(1 divided by ([1:25] plus bS))). Mathematically: o = (1/500+sum(1./([1:25]+bS))).^(-1).

Write a MATLAB function named F15 that implements the Martin-Gaddy function. The function takes an input x (a 4-dimensional vector) and returns the computed result o. Define the function as: function o = F15(x). The algorithm flow is: 1. Define aK as [.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246]. 2. Define bK as 1./[.25 .5 1 2 4 6 8 10 12 14 16]. 3. Calculate o as sum((aK minus (x(1) times (bK squared plus x(2) times bK) divided by (bK squared plus x(3) times bK plus x(4)))). squared. Mathematically: o = sum((aK - ((x(1).*(bK.^2+x(2).*bK))./(bK.^2+x(3).*bK+x(4)))).^2).

Write a MATLAB function named F16 that implements the Six-Hump Camel Back function. The function takes an input x (a 2-dimensional vector) and returns the computed result o. Define the function as: function o = F16(x). The algorithm flow is: Calculate o as 4 times x(1) squared minus 2.1 times x(1) to the 4th power plus x(1) to the 6th power divided by 3 plus x(1) times x(2) minus 4 times x(2) squared plus 4 times x(2) to the 4th power. Mathematically: o = 4*(x(1)^2)-2.1*(x(1)^4)+(x(1)^6)/3+x(1)*x(2)-4*(x(2)^2)+4*(x(2)^4).

Write a MATLAB function named F17 that implements the Branin function. The function takes an input x (a 2-dimensional vector) and returns the computed result o. Define the function as: function o = F17(x). The algorithm flow is: Calculate o as (x(2) minus (x(1) squared times 5.1 divided by (4 times pi squared)) plus 5 over pi times x(1) minus 6) squared plus 10 times (1 minus 1 over (8 times pi)) times cos(x(1)) plus 10. Mathematically: o = (x(2)-(x(1)^2)*5.1/(4*(pi^2))+5/pi*x(1)-6)^2+10*(1-1/(8*pi))*cos(x(1))+10.
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 (with at least 2 elements) as input and returns the computed value o. The function calculates o using the following formula: o is the product of two terms. The first term is 1 plus ((x(1) + x(2) + 1)^2) 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 ((2*x(1) - 3*x(2))^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 (with exactly 3 elements) as input and returns the computed value o. First, define the matrices and vector: 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, for each i from 1 to 4, update o as o = o - cH(i)*exp(-sum(aH(i,:).*((x - pH(i,:)).^2))). The function definition is function o = F19(x).

Please write a MATLAB function named F20 that takes a vector x (with exactly 6 elements) as input and returns the computed value o. First, define the matrices and vector: 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, for each i from 1 to 4, update o as o = o - cH(i)*exp(-sum(aH(i,:).*((x - pH(i,:)).^2))). The function definition is function o = F20(x).

Please write a MATLAB function named F21 that takes a vector x (with exactly 4 elements) as input and returns the computed value 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, for each i from 1 to 5, update o as o = o - ((x - aSH(i,:))*(x - aSH(i,:))' + cSH(i))^(-1). The function definition is function o = F21(x).

Please write a MATLAB function named F22 that takes a vector x (with exactly 4 elements) as input and returns the computed value 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, for each i from 1 to 7, update o as o = o - ((x - aSH(i,:))*(x - aSH(i,:))' + cSH(i))^(-1). The function definition is function o = F21(x).

Please write a MATLAB function named F22 that takes a vector x (with exactly 4 elements) as input and returns the computed value 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, for each i from 1 to 7, update o as o = o - ((x - aSH(i,:))*(x - aSH(i,:))' + cSH(i))^(-1). The function definition is function o = F22(x).

Please write a MATLAB function named F23 that takes a vector x (with exactly 4 elements) as input and returns the computed value 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, for each i from 1 to 10, update o as o = o - ((x - aSH(i,:))*(x - aSH(i,:))' + cSH(i))^(-1). The function definition is function o = F23(x).
test_cases: 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 representing a function name and returns the corresponding lower bound lb, upper bound ub, dimension dim, and objective function handle fobj for the given function. The function is defined as: function [lb,ub,dim,fobj] = Get_Functions_details(F). Use a switch-case structure to handle different values of F. For each case (from 'F1' to 'F23'), assign the corresponding values to lb, ub, dim, and set fobj to the function handle of the corresponding function (e.g., @F1 for 'F1'). The functions F1 to F23 called in the code are assumed to be already defined and do not need to be redefined here. The specific assignments for each case are as follows: Case 'F1': fobj = @F1; lb = -100; ub = 100; dim = 10; 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.
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. The function definition is function X = initialization(SearchAgents_no, dim, ub, lb). The algorithm flow is as follows: First, calculate Boundary_no as the number of columns in ub using size(ub,2). If Boundary_no equals 1 (meaning all variables have the same upper and lower boundaries and the user entered a single number for both ub and lb), compute X as a matrix of size SearchAgents_no-by-dim where each element is generated by rand(SearchAgents_no,dim) multiplied by (ub - lb) plus lb. If Boundary_no is greater than 1 (meaning each variable has a different lb and ub), loop from i=1 to dim: for each i, get ub_i = ub(i) and lb_i = lb(i), then set the i-th column of X as a column vector of size SearchAgents_no-by-1 generated by rand(SearchAgents_no,1) multiplied by (ub_i - lb_i) plus lb_i. Functions called in the code are assumed to be already defined.
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 RouletteWheelSelection that implements a roulette wheel selection algorithm. This function takes a set of weights (representing the probability of selection for each individual in a group of choices) as input and returns the index of the chosen individual. The function definition is: function choice = RouletteWheelSelection(weights). The algorithm flow is as follows: 1. Compute the cumulative sum of the input weights using the cumsum function. 2. Generate a random value p by multiplying a uniformly distributed random number (from rand()) with the last element of the cumulative sum (which is the total sum of the weights). 3. Initialize a variable chosen_index to -1. 4. Iterate over each index from 1 to the length of the cumulative sum: for each index, if the cumulative sum at that index is greater than p, set chosen_index to that index and break the loop immediately. 5. Assign chosen_index to the output variable choice. Note: The functions cumsum and rand used in the code are pre-defined and do not need to be redefined. The usage example is: calling RouletteWheelSelection([1 5 3 15 8 1]) will most probably return 4 (since the weight 15 is at index 4).
function_header: function choice = RouletteWheelSelection(weights)

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

w1 = [1, 2, 8, 4];
w2 = [7, 11, 19, 4];
w3 = [5, 3, 1];

val = RouletteWheelSelection(w1);
correct = 3.000000;
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
val = RouletteWheelSelection(w2);
correct = 4.000000;
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
val = RouletteWheelSelection(w3);
correct = 2.000000;
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 MVO that implements the Multi-Verse Optimizer algorithm for minimization problems, taking population size N, maximum iterations Max_time, lower bound lb, upper bound ub, dimension dim, and fitness function fobj as inputs, and returning the best fitness value Best_universe_Inflation_rate, best solution vector Best_universe, and convergence curve Convergence_curve.

Function definition: [Best_universe_Inflation_rate, Best_universe, Convergence_curve] = MVO(N, Max_time, lb, ub, dim, fobj)

Algorithm flow:
1. Initialize Best_universe as a 1-by-dim zero vector and Best_universe_Inflation_rate as infinity.
2. Generate the initial universe population using the initialization(N, dim, ub, lb) function and assign it to Universes.
3. Set WEP_Max = 1, WEP_Min = 0.2, Convergence_curve as a 1-by-Max_time zero vector, and Time = 1.
4. While Time <= Max_time:
   a. Calculate WEP as WEP_Min + Time * ((WEP_Max - WEP_Min) / Max_time).
   b. Calculate TDR as 1 - ((Time)^(1/6) / (Max_time)^(1/6)).
   c. For each universe in Universes:
      i. Create Flag4ub (1 if a dimension exceeds ub, 0 otherwise) and Flag4lb (1 if a dimension is below lb, 0 otherwise).
      ii. Clip the universe's dimensions to the bounds: Universes(i,:) = Universes(i,:) .* (~(Flag4ub + Flag4lb)) + ub .* Flag4ub + lb .* Flag4lb.
   d. Compute Inflation_rates (fitness values) for all universes using fobj(Universes(i,:)) for each i.
   e. Update Best_universe and Best_universe_Inflation_rate if Inflation_rates(1,i) is smaller than the current Best_universe_Inflation_rate.
   f. Sort Inflation_rates in ascending order and get the corresponding sorted indices; create Sorted_universes by sorting Universes using these indices.
   g. Normalize sorted_Inflation_rates using the normr function to get normalized_sorted_Inflation_rates.
   h. Set Universes(1,:) to Sorted_universes(1,:) (elite universe).
   i. For each universe from 2 to N (Back_hole_index = i):
      For each dimension j:
         1. Generate r1 = rand(); if r1 < normalized_sorted_Inflation_rates(i), select White_hole_index using RouletteWheelSelection(-sorted_Inflation_rates); if White_hole_index == -1, set it to 1; then set Universes(Back_hole_index,j) to Sorted_universes(White_hole_index,j).
         2. If lb is a scalar (size(lb,2) == 1):
             Generate r2 = rand(); if r2 < WEP:
                 Generate r3 = rand(); if r3 < 0.5, set Universes(i,j) to Best_universe(1,j) + TDR * ((ub - lb)*rand + lb); else, set to Best_universe(1,j) - TDR * ((ub - lb)*rand + lb).
         3. If lb is a vector (size(lb,2) ~= 1):
             Generate r2 = rand(); if r2 < WEP:
                 Generate r3 = rand(); if r3 < 0.5, set Universes(i,j) to Best_universe(1,j) + TDR * ((ub(j) - lb(j))*rand + lb(j)); else, set to Best_universe(1,j) - TDR * ((ub(j) - lb(j))*rand + lb(j)).
   j. Store Best_universe_Inflation_rate in Convergence_curve(Time).
   k. If Time is a multiple of 50, display the current iteration and best fitness using: display(['At iteration ', num2str(Time), ' the best universes fitness is ', num2str(Best_universe_Inflation_rate)]).
   l. Increment Time by 1.

Note: The functions initialization, RouletteWheelSelection, and normr are pre-defined and do not need to be re-implemented.
function_header: function [Best_universe_Inflation_rate,Best_universe,Convergence_curve]=MVO(N,Max_time,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]=MVO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 456.61523091;
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]=MVO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 298.61833941;
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]=MVO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = -17.58767082;
tol = abs(val-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 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;
    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') && ~strcmp(func_name,'F19') && ~strcmp(func_name,'F20') && ~strcmp(func_name,'F21') && ~strcmp(func_name,'F22') && ~strcmp(func_name,'F23')
            f(i,j) = fobj([x(i),y(j)]);
        end
        if strcmp(func_name,'F15')
            f(i,j) = fobj([x(i),y(j),0,0]);
        end
        if strcmp(func_name,'F19')
            f(i,j) = fobj([x(i),y(j),0]);
        end
        if strcmp(func_name,'F20')
            f(i,j) = fobj([x(i),y(j),0,0,0,0]);
        end
        if strcmp(func_name,'F21') || strcmp(func_name,'F22') || strcmp(func_name,'F23')
            f(i,j) = fobj([x(i),y(j),0,0]);
        end
    end
end
surfc(x,y,f,'LineStyle','none');
end
The function Get_Functions_details is assumed to be already defined and does not need to be redefined. The function func_plot takes the function name as input, retrieves the details of the benchmark function, generates the appropriate x and y vectors based on the function name, computes the function values using nested loops with different input dimensions for specific functions, and finally plots the 3D surface of the benchmark function using surfc with no line style.
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; 
        
    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: 10
step_description_prompt: Please write a MATLAB code to create a function named MVO that implements the Multi-Verse Optimizer algorithm for optimizing benchmark functions, and includes a script to run the algorithm, plot results, and display outputs. The MVO function takes the number of search agents (universes), maximum iterations, lower bounds, upper bounds, problem dimension, and objective function as inputs, returning the best score, best position, and convergence curve.

The MVO function must have the signature: [Best_score, Best_pos, cg_curve] = MVO(Universes_no, Max_iteration, lb, ub, dim, fobj). Inside the function: 1. Initialize universe positions randomly within the bounds [lb, ub] for each dimension. 2. Evaluate the fitness of each universe using the fobj function. 3. Initialize Best_pos and Best_score with the fittest universe's position and fitness. 4. Initialize cg_curve as a vector of size Max_iteration to store the best score at each iteration. 5. Iterate from 1 to Max_iteration: a. Calculate normalized fitness for each universe. b. Update the Wormhole Existence Probability (WEP) based on the current iteration. c. Update the Traveling Distance Rate (TDR) based on the current iteration. d. For each universe: i. Select a random universe via roulette wheel selection using normalized fitness. ii. Update the universe's position using TDR and the selected universe's position. iii. Apply boundary constraints to keep the position within [lb, ub]. 6. Re-evaluate the fitness of all updated universes. 7. Update Best_pos and Best_score if any universe has a better fitness. 8. Store the current Best_score in cg_curve for the current iteration. 9. Return Best_score, Best_pos, and cg_curve after all iterations.

The script must: 1. Run clear all and clc to initialize the workspace and command window. 2. Set the random seed to 42 with rng(42). 3. Define parameters: Universes_no = 60, Function_name = 'F17', Max_iteration = 500. 4. Load benchmark function details using [lb, ub, dim, fobj] = Get_Functions_details(Function_name). 5. Call the MVO function: [Best_score, Best_pos, cg_curve] = MVO(Universes_no, Max_iteration, lb, ub, dim, fobj). 6. Create a figure with Position [290 206 648 287]. 7. Use subplot(1,2,1) to plot the test function: call func_plot(Function_name), set Title 'Test function', XLabel 'x_1', YLabel 'x_2', ZLabel strcat(Function_name, '( x_1 , x_2 )'), set Grid off, Shading interp, add Light, set Lighting phong. 8. Use subplot(1,2,2) to plot the convergence curve: use semilogy(cg_curve, 'Color', 'r'), set Title 'Convergence curve', XLabel 'Iteration', YLabel 'Best score obtained so far', set Axis tight, Grid off, Box on, add Legend 'MVO'. 9. Display the best solution with display(strcat('The best solution obtained by MVO is : ', num2str(Best_pos))). 10. Display the best score with display(strcat('The best optimal value of the objective funciton found by MVO is : ', num2str(Best_score))).

The functions Get_Functions_details and func_plot are assumed to be pre-defined and do not need reimplementation.
test_cases: rng(42)
correct_score = 0.397888;
correct_pos = 11.898874;
val_score = sum(Best_score(:));
val_pos = sum(Best_pos(:));
tol_score = abs(val_score-correct_score);
if tol_score > 1e-4
    error('Wrong')
end
tol_pos = abs(val_pos-correct_pos);
if tol_pos > 1e-4
    error('Wrong')
end
disp('Completely right!')

General solution:

General tests: