Plotting an algorithm with distance matrix as input and all iteration results as output

조회 수: 2 (최근 30일)
% Example distance matrix (replace it with your own)
distanceMatrix = [
0 10 15 20;
10 0 35 25;
15 35 0 30;
20 25 30 0
];
% Set algorithm parameters
maxIterations = 1000;
tabuSize = 10;
% Run Tabu Search
[bestTour, bestDistance, iterationResults] = tabuSearchTSP(distanceMatrix, maxIterations, tabuSize);
Error using solution>tabuSearchTSP
Too many output arguments.
% Display the best tour and its distance
disp('Best Tour:');
disp(bestTour);
disp('Best Distance:');
disp(bestDistance);
% Display the output of each iteration
disp('Iteration Results:');
for i = 1:maxIterations
disp(['Iteration ', num2str(iterationResults(i).Iteration)]);
disp('Best Tour:');
disp(iterationResults(i).BestTour);
disp('Best Distance:');
disp(iterationResults(i).BestDistance);
disp('-----------------');
end
function [bestTour, bestDistance] = tabuSearchTSP(distanceMatrix, maxIterations, tabuSize)
% Initialize variables
n = size(distanceMatrix, 1); % Number of cities
tabuList = zeros(n, tabuSize); % Tabu list to store recently visited solutions
currentTour = randperm(n); % Random initial solution
bestTour = currentTour;
bestDistance = calculateTourDistance(currentTour, distanceMatrix);
iteration = 1;
while iteration <= maxIterations
candidateList = generateCandidateList(currentTour, tabuList);
[bestCandidate, bestCandidateDistance] = evaluateCandidates(candidateList, distanceMatrix);
if bestCandidateDistance < bestDistance
bestTour = bestCandidate;
bestDistance = bestCandidateDistance;
end
currentTour = bestCandidate;
tabuList = updateTabuList(tabuList, currentTour);
iteration = iteration + 1;
end
end
function distance = calculateTourDistance(tour, distanceMatrix)
n = length(tour);
distance = 0;
for i = 1:n-1
distance = distance + distanceMatrix(tour(i), tour(i+1));
end
distance = distance + distanceMatrix(tour(n), tour(1)); % Return to the starting city
end
function candidateList = generateCandidateList(currentTour, tabuList)
candidateList = [];
n = length(currentTour);
for i = 1:n-1
for j = i+1:n
candidate = currentTour;
candidate(i) = currentTour(j);
candidate(j) = currentTour(i);
if ~isTabu(candidate, tabuList)
candidateList = [candidateList; candidate];
end
end
end
end
function isTabu = isTabu(candidate, tabuList)
[n, tabuSize] = size(tabuList);
isTabu = false;
for i = 1:tabuSize
if isequal(candidate, tabuList(:, i))
isTabu = true;
break;
end
end
end
function [bestCandidate, bestCandidateDistance] = evaluateCandidates(candidateList, distanceMatrix)
numCandidates = size(candidateList, 1);
bestCandidateDistance = Inf;
for i = 1:numCandidates
candidate = candidateList(i, :);
candidateDistance = calculateTourDistance(candidate, distanceMatrix);
if candidateDistance < bestCandidateDistance
bestCandidate = candidate;
bestCandidateDistance = candidateDistance;
end
end
end
function tabuList = updateTabuList(tabuList, candidate)
[n, tabuSize] = size(tabuList);
tabuList = [candidate' tabuList(:, 1:tabuSize-1)];
end
Hi,
The output is not shown here as I need result for each iteration. But if I want only final best solution, it gives output.
Please modify it or help to fix the error. Thanks

답변 (1개)

ProblemSolver
ProblemSolver 2023년 7월 10일
Please find the change. You need to add an output argument that will store the best tour and distance for each iteration.
% Example distance matrix (replace it with your own)
distanceMatrix = [
0 10 15 20;
10 0 35 25;
15 35 0 30;
20 25 30 0
];
% Set algorithm parameters
maxIterations = 10;
tabuSize = 10;
% Run Tabu Search
[bestTour, bestDistance, iterationResults] = tabuSearchTSP(distanceMatrix, maxIterations, tabuSize);
% Display the best tour and its distance
disp('Best Tour:');
Best Tour:
disp(bestTour);
3 4 2 1
disp('Best Distance:');
Best Distance:
disp(bestDistance);
80
% Display the output of each iteration
disp('Iteration Results:');
Iteration Results:
for i = 1:maxIterations
disp(['Iteration ', num2str(iterationResults(i).Iteration)]);
disp('Best Tour:');
disp(iterationResults(i).BestTour);
disp('Best Distance:');
disp(iterationResults(i).BestDistance);
disp('-----------------');
end
Iteration 1
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 2
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 3
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 4
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 5
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 6
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 7
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 8
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 9
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
Iteration 10
Best Tour:
3 4 2 1
Best Distance:
80
-----------------
function [bestTour, bestDistance, iterationResults] = tabuSearchTSP(distanceMatrix, maxIterations, tabuSize)
% Initialize variables
n = size(distanceMatrix, 1); % Number of cities
tabuList = zeros(n, tabuSize); % Tabu list to store recently visited solutions
currentTour = randperm(n); % Random initial solution
bestTour = currentTour;
bestDistance = calculateTourDistance(currentTour, distanceMatrix);
% Initialize iteration results
iterationResults = struct('Iteration', {}, 'BestTour', {}, 'BestDistance', {});
iteration = 1;
while iteration <= maxIterations
candidateList = generateCandidateList(currentTour, tabuList);
[bestCandidate, bestCandidateDistance] = evaluateCandidates(candidateList, distanceMatrix);
if bestCandidateDistance < bestDistance
bestTour = bestCandidate;
bestDistance = bestCandidateDistance;
end
currentTour = bestCandidate;
tabuList = updateTabuList(tabuList, currentTour);
% Store iteration results
iterationResults(iteration).Iteration = iteration;
iterationResults(iteration).BestTour = bestTour;
iterationResults(iteration).BestDistance = bestDistance;
iteration = iteration + 1;
end
end
function distance = calculateTourDistance(tour, distanceMatrix)
n = length(tour);
distance = 0;
for i = 1:n-1
distance = distance + distanceMatrix(tour(i), tour(i+1));
end
distance = distance + distanceMatrix(tour(n), tour(1)); % Return to the starting city
end
function candidateList = generateCandidateList(currentTour, tabuList)
candidateList = [];
n = length(currentTour);
for i = 1:n-1
for j = i+1:n
candidate = currentTour;
candidate(i) = currentTour(j);
candidate(j) = currentTour(i);
if ~isTabu(candidate, tabuList)
candidateList = [candidateList; candidate];
end
end
end
end
function isTabu = isTabu(candidate, tabuList)
[n, tabuSize] = size(tabuList);
isTabu = false;
for i = 1:tabuSize
if isequal(candidate, tabuList(:, i))
isTabu = true;
break;
end
end
end
function [bestCandidate, bestCandidateDistance] = evaluateCandidates(candidateList, distanceMatrix)
numCandidates = size(candidateList, 1);
bestCandidateDistance = Inf;
for i = 1:numCandidates
candidate = candidateList(i, :);
candidateDistance = calculateTourDistance(candidate, distanceMatrix);
if candidateDistance < bestCandidateDistance
bestCandidate = candidate;
bestCandidateDistance = candidateDistance;
end
end
end
function tabuList = updateTabuList(tabuList, candidate)
[n, tabuSize] = size(tabuList);
tabuList = [candidate' tabuList(:, 1:tabuSize-1)];
end
  댓글 수: 2
Amna Habib
Amna Habib 2023년 7월 10일
OK Got it!
Thanks a lot @ProblemSolver
Now I am shock that all iterations result are same. it means there is some issue in matlb code?
do you have any idea that when portion of code has some mistake? i could not find any error yet
ProblemSolver
ProblemSolver 2023년 7월 11일
@Amna Habib. You need to explain me what are you looking for by performing this algorithm? as in theoritically what is expected? and other details to really see what part of the code is generating that.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by