Problem in ant colony optimization code.

5 次查看(过去 30 天)
hey there!
i have a function
function t=strength(x)
t(1) = -0.804-0.001.*x(1)-0.001.*x(2)+0.0.*x(3)+0.0.*x(4)-0.031.*x(5)+0.005.*x(6)-0.002.*x(7)+0.001.*x(8)-0.735.*x(9)+0.097.*x(10)+0.012.*x(11)+0.078.*x(12)-0.220.*x(13);
end
with 13 variables and their lower and upper bounds are
lb = [490 0 0 19 0 170 700 990 0.35 1.5 16.8 18.5 1]
ub = [500 0 0 22 0 180 710 1000 0.35 1.5 16.8 18.5 1]
i need to perform ACO to minimize the function and get t(1).
this is my code so far. it may be complete wrong or half wrong. I have just started learning matlab and its not been easy for me. it would be very helpful if you guys could help me completing the code.
clear all
close all
% problem definition
obj = @(x)strength(x)
obj = function_handle with value:
@(x)strength(x)
nVar = 13; % number of parameter
min = [490 0 0 19 0 170 700 990 0.35 1.5 16.8 18.5 1]
min = 1×13
490.0000 0 0 19.0000 0 170.0000 700.0000 990.0000 0.3500 1.5000 16.8000 18.5000 1.0000
max = [500 0 0 22 0 180 710 1000 0.35 1.5 16.8 18.5 1]
max = 1×13
1.0e+03 * 0.5000 0 0 0.0220 0 0.1800 0.7100 1.0000 0.0003 0.0015 0.0168 0.0185 0.0010
%parameters of ACO
MaxIt=500; % Maximum Number of Iterations
nAnt=50; % Number of Ants (Population Size)
Q=1;
tau0=10; % Initial Phromone
alpha=0.3; % Phromone Exponential Weight
rho=0.1;
tau = tau0 * ones(nVar); % Phromone matirx
Bestfitness=zeros(MaxIt,1);
% Empty Ant
empty_ant.Tour=[];
empty_ant.fitness=[];
% Ant Colony Matrix
ant=repmat(empty_ant,nAnt,1);
BestSol.fitness=inf;
% Main loop of ACO
for it=1:MaxIt
% Move Ants
for k=1:nAnt
ant(k).Tour=[];
for l=1:nVar
P=tau(:,l).^alpha;
P(ant(k).Tour)=0;
P=P/sum(P);
j=RouletteWheelSelection(P);
ant(k).Tour=[ant(k).Tour j];
end
ant(k).fitness=fitnessFunction(ant(k).Tour);
if ant(k).fitness<BestSol.fitness
BestSol=ant(k);
end
end
% Update Phromones
for k=1:nAnt
tour=ant(k).Tour;
for l=1:nVar
tau(tour(l),l)=tau(tour(l),l)+Q/ant(k).fitness;
end
end
% Evaporation
tau=(1-rho)*tau;
% Store Best Cost
Bestfitness(it)=BestSol.fitness;
% Show Iteration Information
disp(['Iteration ' num2str(it) ': Best fitness = ' num2str(Bestfitness(it))]);
% Plot Solution
figure(1);
PlotSolution(BestSol.Tour,model);
pause(0.01);
% Results
figure;
plot(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best fitness');
grid on;
end
Unrecognized function or variable 'RouletteWheelSelection'.

回答(1 个)

Brahmadev
Brahmadev 2023-11-17
I understand that you would like to apply “Ant Colony Optimization” but are facing the error "Unrecognized function or variable 'RouletteWheelSelection'. This is because the function  "RouletteWheelSelection" is not defined in your MATLAB path.
The Roulette Wheel Selection is a method used in genetic algorithms for selecting potentially useful solutions for recombination. Here's a simple implementation of the Roulette Wheel Selection:
function j = RouletteWheelSelection(P)
r = rand;
C = cumsum(P);
i = find(r <= C, 1, 'first');
if isempty(i)
j = length(P);
else
j = i;
end
end
You can add this function to your code, and it should resolve the error. Make sure to add it outside your main function, at the end of your script.
Also, your code seems to be incomplete. The function "fitnessFunction" is not defined, and the function "PlotSolution" is also missing. You need to define these functions or replace them with appropriate MATLAB functions.
An example of the aforementioned functions is attached below. Note that these functions should be changed according to the need.
function fit = fitnessFunction(x)
fit = strength(x); % Edit the fitnessFunction according to need
end
function PlotSolution(Bestfitness)
plot(Bestfitness, 'LineWidth', 2); % Edit the PlotSolution function according to need
xlabel('Iteration');
ylabel('Best Fitness');
grid on;
end
You would call this function after your main optimization loop, like this:
% After your main ACO loop:
PlotSolution(Bestfitness);
Please refer to the following documentation for more information on the functions used in "Roulette Wheel Selection":
  1. https://www.mathworks.com/help/matlab/ref/cumsum.html
  2. https://www.mathworks.com/help/matlab/ref/find.html
Hope this helps in resolving your issue!

类别

Help CenterFile Exchange 中查找有关 Particle Swarm 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by