Amna Habib asked . 2023-07-20

How to change my matlab code. Is has no error in its present form?

Hi, I want to make a change in the following code. It has been run successfully.
 
Before starting a loop, this line is written
 
''currentTour = randperm(n); % Random initial solution''
 
I need that the algorithm will run on all possible tours and give all possible outputs, except of taking a random tour.
 
Please note that when we run the current code, every time we obtain equal value (76) of best distance, but different best tours as output.
 
(Actually the distance matrix represents a network, where the nodes indicates cities. I want to find best possible tour started from each city one by one.)
% Example distance matrix (replace it with your own)
distanceMatrix = [
    0  10  11  20;
   10   0  90 25;
   11  90   0  30;
   20  25  30   0
];
% Set algorithm parameters
maxIterations = 1000;
tabuSize = 10;
% Run Tabu Search
[bestTour, bestDistance] = tabuSearchTSP(distanceMatrix, maxIterations, tabuSize);
% Display the best tour and its distance
disp('Best Tour:');
Best Tour:
disp(bestTour);
   3     1     2     4
disp('Best Distance:');
Best Distance:
disp(bestDistance);
    76
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)
    [~, tabuSize] = size(tabuList);
    tabuList = [candidate' tabuList(:, 1:tabuSize-1)];
end

 

permutations , randperm , shortest path , MATLAB , Language Fundamentals

Expert Answer

Kshitij Singh answered . 2024-12-22 14:01:44

You can replace the line 'currentTour = randperm(n)' with a loop that iterates over each city as the starting point.

 

% Example distance matrix (replace it with your own)
distanceMatrix = [
    0  10  11  20;
    10   0  90  25;
    11  90   0  30;
    20  25  30   0
    ];
% Set algorithm parameters
maxIterations = 1000;
tabuSize = 10;

n = size(distanceMatrix, 1); % Number of cities

% Initialize variables for the best tour and distance
bestTour = [];
bestDistance = Inf;

% Iterate over each city as the starting point
for startingCity = 1:n
    currentTour = startingCity:n;
    currentTour = [currentTour, 1:startingCity-1];
    
    % Run Tabu Search
    [tour, distance] = tabuSearchTSP(distanceMatrix, maxIterations, tabuSize, currentTour);
    
    % Update the best tour and distance if necessary
    if distance < bestDistance
        bestTour = tour;
        bestDistance = distance;
    end
end

% Display the best tour and its distance
disp('Best Tour:');
disp(bestTour);
disp('Best Distance:');
disp(bestDistance);

function [bestTour, bestDistance] = tabuSearchTSP(distanceMatrix, maxIterations, tabuSize, currentTour)
% Initialize variables
n = size(distanceMatrix, 1); % Number of cities
tabuList = zeros(n, tabuSize); % Tabu list to store recently visited solutions
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

% The remaining functions remain the same

 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.