How can i create a general Code .m for TSP nxn, if i just had this code for more cities than 5? for example 6 cities, 7 cities, ... , 10 cities

1 visualización (últimos 30 días)
%B is the matrix i have in a doc.txt
%i want a general code for every number of cities a distances
%B=[0 15 20 25 30; 15 0 16 18 24; 20 16 0 22 26; 25 18 22 0 28; 30 24 26 28 0] ]
function Viajeronew()
B=dlmread("Doc.txt")
tour_length(1)=B(1,2)+B(2,3)+B(3,4)+B(4,5)+B(5,1)
tour1='1,2,3,4,5,1'
tour_length(2)=B(1,2)+B(2,4)+B(4,3)+B(3,5)+B(5,1)
tour2='1,2,4,3,5,1'
tour_length(3)=B(1,2)+B(2,5)+B(5,4)+B(4,3)+B(3,1)
tour3='1,2,5,4,3,1'
tour_length(4)=B(1,3)+B(3,2)+B(2,5)+B(5,4)+B(4,1)
tour4='1,3,2,5,4,1'
tour_length(5)=B(1,3)+B(3,5)+B(5,4)+B(4,2)+B(2,1)
tour5='1,3,5,4,2,1'
tour_length(6)=B(1,3)+B(3,4)+B(4,2)+B(2,5)+B(5,1)
tour6='1,3,4,2,5,1'
tour_length(7)=B(1,2)+B(2,5)+B(5,3)+B(3,4)+B(4,1)
tour7='1,2,5,3,4,1'
tour_length(8)=B(1,5)+B(5,4)+B(4,2)+B(2,3)+B(3,1)
tour8='1,5,4,2,3,1'
tour_length(9)=B(1,4)+B(4,2)+B(2,5)+B(5,3)+B(3,1)
tour9='1,4,2,5,3,1'
tour_length(10)=B(1,4)+B(4,3)+B(3,2)+B(2,5)+B(5,1)
tour10='1,4,3,2,5,1'
tour_length(11)=B(1,5)+B(5,3)+B(3,2)+B(2,4)+B(4,1)
tour11='1,5,3,2,4,1'
tour_length(12)=B(1,4)+B(4,5)+B(5,3)+B(3,2)+B(2,1)
tour12='1,4,5,3,2,1'
tour=[tour1;tour2;tour3;tour4;tour5;tour6;tour7;tour8;tour9;tour10;tour11;tour12]
tour=cellstr(tour)
Opt_Tour_Length=min(tour_length)
Opt_Tour=(find(tour_length == Opt_Tour_Length))
Shortest_Tour=tour(Opt_Tour)

Respuestas (1)

engesraa
engesraa el 17 de Dic. de 2017
can you give me a matlab code for TSP , please

Categorías

Más información sobre Traveling Salesman (TSP) 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!

Translated by