Why am i getting this error>>>Index exceeds the number of array elements (1). Error in Boom (line 38) Y(t) = X_best_so_far(t-1); % Use X_best_so_far value for the

1 Ansicht (letzte 30 Tage)
clear all
clc
%%%%%%% INITILIZATION PHASE STARTS %%%%%%%
N = 4; % From the paper
Xmin = -10; % Lower limit of search space
Xmax = 10; % Upper limit of search space
T = 1000; % Maximum number of iterations
% Generate random initial values for Y(0), Y(t-1), Y(t-2), and Y(t-3)
Y = zeros(1, N); % Initialize array for Y
for i = 1:N
Y(i) = Xmin + (Xmax - Xmin) * rand(); % Generate,Y(0), Y(t-1), Y(t-2), Y(t-3) randomly within limit
end
% Display initial values
fprintf('Y(0) = %f\n', Y(1));
for i = 2:N
fprintf('Y(t-%d) = %f\n', i, Y(i));
end
X_best_so_far = min(Y(1:N)); % Assign the measurement as X_best_so_far
fprintf('Initial X_best_so_far(min of 4 (Y)): %f\n', X_best_so_far)
%%%%%%% INITIALIZATION PHASE ENDS %%%%%%%
%%%%%%% MEASUREMENT PHASE STARTS %%%%%%%
X_best_so_far = X_best_so_far(1); % Initialize X_best_so_far as a scalar
x = rand(1, 4);
for t = 2:T
for d = 1:N
if x(d) > 0.5 % Check if the dimension is selected for mutation
delta = (exp(-10 * t / T) * ((Xmax - Xmin)) / 2); % Compute the radius of the local neighborhood, delta
Y(t) = X_best_so_far(t-1) + (-delta) + rand() * (delta - (-delta)); % Compute measurement value using equation (8)
else
Y(t) = X_best_so_far(t-1); % Use X_best_so_far value for the dimension as the measurement value, equation (10)
end
end
end
fprintf('Measurement X_best_so_far: %f\n', Y(end));
%%%%%%% MEASUREMENT PHASE ENDS %%%%%%%
%%%%%%% ESTIMATION PHASE STARTS %%%%%%%
X = zeros(1, T);
for k = 2:N
% Generate initial estimation X(k) at k > 1
if Y(t - N + 1) < Y(t - N + 2)
X(2) = Y(t - N + 1) + (Y(t - N + 2) - Y(t - N + 1)) * rand();
else
X(2) = Y(t - N + 2) + (Y(t - N + 1) - Y(t - N + 2)) * rand();
end
end
for k = 3:N
% Iteration of estimation: as in (11) and (12)
X(k) = X(k - 1) + (1/k) * (Y(t - N + k) - X(k - 1));
% Update X_bar_prev for the next iteration
X(k) = X(t);
end
% Display X(k) estimation
fprintf('Estimation X = %f\n', X(end));
%%%%%%% ESTIMATION PHASE ENDS %%%%%%%
%%%%%%% FITNESS EVALUATION AND X_best_so_far UPDATE PHASE STARTS %%%%%%%
% Define fitness evaluation function
fitness = @(x) sum((x - 1).^2);
% Initialize variables for storing agent fitness and best fitness
agent_fitness = zeros(1, T);
best_fitness = zeros(1, T);
for t = 1:T
% Evaluate fitness for agent
agent_fitness(t) = fitness(X(k));
% Evaluate fitness for X_best_so_far
best_fitness(t) = fitness(X_best_so_far);
% Update X_best_so_far if a better solution is found
if agent_fitness(t) < best_fitness(t)
X_best_so_far = X(t);
end
end
% Display agent fitness and best fitness
fprintf('Final Best Fitness = %f\n', best_fitness(end));
fprintf('Final Best X So Far = %f\n', X_best_so_far);
%%%%%%% FITNESS EVALUATION AND X_best_so_far UPDATE PHASE ENDS %%%%%%%

Antworten (1)

Torsten
Torsten am 2 Jun. 2023
As you correctly write in your code, the line
X_best_so_far = X_best_so_far(1); % Initialize X_best_so_far as a scalar
sets X_best_so_far to a scalar.
Thus in the following loop,
X_best_so_far(t-1)
does not exist.
  1 Kommentar
JESHURUN AUGUSTINE
JESHURUN AUGUSTINE am 2 Jun. 2023
Bearbeitet: JESHURUN AUGUSTINE am 2 Jun. 2023
X_best_so_far(t-1), is needed in the equation as I am following a thesis paper to optimize a function using SAFIRO algorithm. How do I fix this?
Link to the thesis paper:
https://ieeexplore.ieee.org/abstract/document/8267497

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by