Clear Filters
Clear Filters

Maximum recursion limit of 5000 reached. PSO algorithm

4 views (last 30 days)
I'm trying to run a simple PSO algorithm to minimize the function y = obj_func(x). I got this code from gpt-4 but when i run it it gives me the error:
Error in obj_func (line 22)
p_best_scores = arrayfun(@(i) obj_func(positions(i, :)), 1:n_particles)';
Caused by:
Maximum recursion limit of 5000 reached.
The name of the script file is the same as the objective function. I changed it but i got nrecognized function or variable. I increased the recursion limit, but Matlab crushed.
Please find bellow the code and help me to fix this issue
% Define your objective function
function y = obj_func(x)
% Replace this with your objective function
x=[-5:5];
y = sum(x.^2);
% PSO parameters
n_particles = 30; % Number of particles
n_dimensions = 2; % Number of dimensions
n_iterations = 10; % Number of iterations
c1 = 2; % Cognitive coefficient
c2 = 2; % Social coefficient
w = 0.9; % Inertia weight
% Initialize particle positions and velocities
positions = -5 + 10 * rand(n_particles, n_dimensions);
velocities = rand(n_particles, n_dimensions);
% Evaluate the objective function for each particle
p_best = positions;
p_best_scores = arrayfun(@(i) obj_func(positions(i, :)), 1:n_particles)';
% Find the global best particle
[g_best_score, g_best_index] = min(p_best_scores);
g_best = p_best(g_best_index, :);
% Main PSO loop
for iter = 1:n_iterations
% Update particle velocities
rp = rand(n_particles, n_dimensions);
rg = rand(n_particles, n_dimensions);
velocities = w * velocities + c1 * rp .* (p_best - positions) + c2 * rg .* (g_best - positions);
% Update particle positions
positions = positions + velocities;
% Evaluate the objective function for each particle
scores = arrayfun(@(i) obj_func(positions(i, :)), 1:n_particles)';
% Update the personal best positions
update_p_best = scores < p_best_scores;
p_best_scores(update_p_best) = scores(update_p_best);
p_best(update_p_best, :) = positions(update_p_best, :);
% Update the global best position
[new_g_best_score, new_g_best_index] = min(p_best_scores);
if new_g_best_score < g_best_score
g_best_score = new_g_best_score;
g_best = p_best(new_g_best_index, :);
end
% Print the current iteration and global best score
fprintf('Iteration %d: Global best score = %f\n', iter, g_best_score)
end
end

Answers (1)

Star Strider
Star Strider on 11 Aug 2023
Your function is:
function y = obj_func(x)
and the line throwing the error is:
p_best_scores = arrayfun(@(i) obj_func(positions(i, :)), 1:n_particles)';
so the arrayfun call is calling ‘obj_func’ internally to it, and that is throwing the error.
Perhaps knowing what you want to do would help. (Also, having an fprintf call inside the function is going to slow it down considerably.)
The AI routine you used is quite obviously clueless about this!
  4 Comments
Steven Lord
Steven Lord on 11 Aug 2023
Don't put your optimization code inside the function that you're trying to optimize.
Write your optimizer as a separate script or function. Create a separate function (or anonymous function) that evaluates your objective function.
Fangjun Jiang
Fangjun Jiang on 11 Aug 2023
I was going to throw the jab. Does gpt-4 know this error and how to resolve it?
Anyway, optimization or not, it does not fit the pattern of a recursive function.
In a recursive function, usually you have a condition. If the condition is met, exit. Otherwise, call the function itself recursively.

Sign in to comment.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!