- https://www.mathworks.com/help/matlab/ref/cumsum.html
- https://www.mathworks.com/help/matlab/ref/find.html
Problem in ant colony optimization code.
5 views (last 30 days)
Show older comments
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)
nVar = 13; % number of parameter
min = [490 0 0 19 0 170 700 990 0.35 1.5 16.8 18.5 1]
max = [500 0 0 22 0 180 710 1000 0.35 1.5 16.8 18.5 1]
%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
0 Comments
Answers (1)
Brahmadev
on 17 Nov 2023
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":
Hope this helps in resolving your issue!
0 Comments
See Also
Categories
Find more on Particle Swarm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!