I have developed Matlab code of an algorithm, but I am unable to run it. Please help me to find error.
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
I have developed Matlab code of  an algorithm, but I am unable to run it. Please help me to ffix the errors. 
Urgent reply will be highly appreciated. Thanks in advnce!
distance_matrix = [0, 29, 20, 21;
                   29, 0, 15, 18;
                   20, 15, 0, 30;
                   21, 18, 30, 0];
tabu_tenure = 5;
max_iterations = 1000;
[best_tour, best_distance] = tabu_search_tsp(distance_matrix, tabu_tenure, max_iterations);
function [best_tour, best_distance] = tabu_search_tsp(distance_matrix, tabu_list_size, max_iterations)
    n = size(distance_matrix, 1);  % Number of cities
    best_tour = randperm(n);       % Initialize the best tour with a random permutation
    best_distance = calculate_tour_distance(best_tour, distance_matrix);
    tabu_list = zeros(tabu_list_size, n); % Initialize the tabu list to store forbidden moves
    tabu_tenure = 5;  % Tabu tenure (number of iterations for which a move is forbidden)
    % Main loop of Tabu Search
    for iter = 1:max_iterations
        initial_solution = nearest_neighbor_heuristic(distance_matrix);
        [current_tour, current_distance] = reversal_heuristic(initial_solution, distance_matrix);
        % Check if the current tour is better than the best tour found so far
        if current_distance < best_distance
            best_tour = current_tour;
            best_distance = current_distance;
        end
        % Add the current tour to the tabu list
        tabu_list = update_tabu_list(tabu_list, current_tour, tabu_tenure);
        % Perform Tabu Search on the current tour
        [current_tour, current_distance] = perform_tabu_search(current_tour, distance_matrix, tabu_list);
        % Check if the current tour is better than the best tour found so far
        if current_distance < best_distance
            best_tour = current_tour;
            best_distance = current_distance;
        end
        % Display the current best distance every 100 iterations (optional)
        if mod(iter, 100) == 0
            fprintf('Iteration %d, Best Distance: %.2f\n', iter, best_distance);
        end
    end
end
function distance = calculate_tour_distance(tour, distance_matrix)
    n = length(tour);
    distance = 0;
    for i = 1:n-1
        distance = distance + distance_matrix(tour(i), tour(i+1));
    end
    distance = distance + distance_matrix(tour(n), tour(1)); % Return to the starting city
end
function tour = nearest_neighbor_heuristic(distance_matrix)
    n = size(distance_matrix, 1);  % Number of cities
    tour = zeros(1, n);
    unvisited = 1:n;
    current_city = randi(n);  % Start from a random city
    for i = 1:n
        tour(i) = current_city;
        unvisited(unvisited == current_city) = [];
        if isempty(unvisited)
            break;
        end
        [~, next_city] = min(distance_matrix(current_city, unvisited));
        current_city = unvisited(next_city);
    end
end
function [tour, distance] = reversal_heuristic(tour, distance_matrix)
    n = length(tour);
    distance = calculate_tour_distance(tour, distance_matrix);
    for i = 1:n-1
        for j = i+1:n
            % Try reversing the segment between city i and city j
            new_tour = apply_reverse(tour, i, j);
            new_distance = calculate_tour_distance(new_tour, distance_matrix);
            % If the new tour is better, update the tour and distance
            if new_distance < distance
                tour = new_tour;
                distance = new_distance;
            end
        end
    end
end
function new_tour = apply_reverse(tour, i, j)
    new_tour = [tour(1:i-1), fliplr(tour(i:j)), tour(j+1:end)];
end
function tabu_list = update_tabu_list(tabu_list, tour, tabu_tenure)
    tabu_list(2:end, :) = tabu_list(1:end-1, :);
    tabu_list(1, :) = tour;
    tabu_list(:, tabu_tenure+1:end) = 0;
end
function [best_tour, best_distance] = perform_tabu_search(initial_tour, distance_matrix, tabu_list)
end
1 comentario
  John D'Errico
      
      
 el 4 de Ag. de 2023
				Does your need for an urgent reply make anyone reply more quickly? No. All it does is make somepne like me waste my time, telling you why your question is not in fact urgent. Unless, of course, you are much more important than any of the other people who have asked questions. I am sure they would like a quick response too.
Respuesta aceptada
  Voss
      
      
 el 4 de Ag. de 2023
        The function perform_tabu_search doesn't do anything.
function [best_tour, best_distance] = perform_tabu_search(initial_tour, distance_matrix, tabu_list)
end
That's the reason for the error you got. Specifically, perform_tabu_search doesn't assign a value to its output arguments best_tour and best_distance.
2 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


