why function giving error?
显示 更早的评论
%% Clear previous data and close all figures
clear all
close all
clc
%% Phase 1: Input parameters
FoodSource = 100; % Food Sources Bees
D = 3; % Dimension of the problem (K, z, p)
lb = [0, 0, 0]; % Lower bound of the variables (K, z, p)
ub = [100, 10, 10]; % Upper bound of the variables (K, z, p)
max_iter = 100; % Maximum number of iterations/max no of cycle
N = FoodSource/2; % Population Size
limit = (N * D); % Used for Scout Phase
trial = zeros(N, 1); % Initialize to zero
% Simulink model name (custom Simulink model with the lead compensator)
simulink_model = 'lead_compensator_model';
%% Phase 2: Load or define the fitness function
% Note: The fitness function is already defined in the 'fitness_function.m' file.
%% Phase 3: Generation of initial population
pos = zeros(N, D);
for i = 1:N
for j = 1:D
pos(i, j) = lb(j) + rand() * (ub(j) - lb(j));
end
end
%% Phase 4: Calculate the fitness value for each solution in the initial population
fx = zeros(N, 1); % Initialize fitness values
for i = 1:N
% Evaluate fitness for each parameter set (row in "pos")
fx(i) = fitness_function(pos(i, :), simulink_model);
end
%% Main ABC loop
maxCycle = max_iter / N; % Maximum number of cycles
cycle = 0;
while cycle < maxCycle
% Employed bee phase
for i = 1:N
phi = -1 + 2 * rand(1, D); % Random parameter for mutation
vi = pos(i, :) + phi .* (pos(i, :) - pos(mod(i-1, N) + 1, :)); % Mutation
vi = max(vi, lb); % Ensure that the new solution is within bounds
vi = min(vi, ub);
fi = fitness_function(vi, simulink_model); % Calculate fitness for the new solution
if fi < fx(i) % Greedy selection
pos(i, :) = vi;
fx(i) = fi;
end
end
% Onlooker bee phase
prob = fx ./ sum(fx); % Probability for selecting each solution
onlookers = rand(N, 1); % Random numbers for selection
for i = 1:N
selected = find(onlookers <= cumsum(prob), 1, 'first'); % Roulette wheel selection
phi = -1 + 2 * rand(1, D); % Random parameter for mutation
vi = pos(selected, :) + phi .* (pos(selected, :) - pos(mod(selected-1, N) + 1, :)); % Mutation
vi = max(vi, lb); % Ensure that the new solution is within bounds
vi = min(vi, ub);
fi = fitness_function(vi, simulink_model); % Calculate fitness for the new solution
if fi < fx(selected) % Greedy selection
pos(selected, :) = vi;
fx(selected) = fi;
end
end
% Scout bee phase
[~, minIndex] = min(fx); % Find the index of the worst solution
if trial(minIndex) >= limit
% If the solution has not improved for "limit" cycles, create a new random solution
for j = 1:D
pos(minIndex, j) = lb(j) + rand() * (ub(j) - lb(j));
end
fx(minIndex) = fitness_function(pos(minIndex, :), simulink_model);
trial(minIndex) = 0; % Reset trial counter
else
trial(minIndex) = trial(minIndex) + 1; % Increment trial counter
end
% Update cycle count
cycle = cycle + 1;
end
%% Phase 5: Plot the result
plot(fx, 'r', 'LineWidth', 2);
xlabel('Iteration');
ylabel('TIAE Fitness Value');
grid on;
title('Fitness Value Convergence');
% Retrieve the best lead compensator parameters from the last iteration
best_fxval
3 个评论
Sam Chak
2023-8-1
Hi @ANDREW
Might be a bit of trouble to simulate the Simulink model lead_compensator_model.slx on the forum.
To test the ABC optimizer, please provide the Plant (transfer function or state-space also acceptable) that you are trying to control with the Lead Compensator.
Also specify the Performance Cost function, either a custom one, or the standard ones like ISE, ITAE, etc.
Rik
2023-8-7
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
ANDREW
2023-8-19
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





