Has anyone written a code in QBasic to solve the Travelling Salesman Problem?
I can solve it in Excel Solver but would be interested in the code.
Pervez
I can solve it in Excel Solver but would be interested in the code.
Pervez
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
[COLOR=#0000ff]'Travelling Salesman Problem[/color]
[COLOR=#0000ff]'number of cities[/color]
N = [COLOR=#ff00ff]7[/color]
[COLOR=#a52a2a][b]Dim[/b][/color] cities(N, N)
[COLOR=#a52a2a][b]Dim[/b][/color] visit(N + [COLOR=#ff00ff]1[/color])
[COLOR=#a52a2a][b]Dim[/b][/color] visit_distance(N)
[COLOR=#0000ff]'read data into matrix[/color]
[COLOR=#a52a2a][b]Print[/b][/color] [COLOR=#ff00ff]"Matrix of distances between the cities:"[/color]
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#a52a2a][b]For[/b][/color] i = [COLOR=#ff00ff]1[/color] To N
[COLOR=#a52a2a][b]For[/b][/color] j = [COLOR=#ff00ff]1[/color] To N
[COLOR=#a52a2a][b]Read[/b][/color] cities(i, j)
[COLOR=#a52a2a][b]Print[/b][/color] cities(i, j);
[COLOR=#a52a2a][b]Next[/b][/color] j
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#a52a2a][b]Next[/b][/color] i
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#0000ff]'read data into vector[/color]
[COLOR=#a52a2a][b]Print[/b][/color] [COLOR=#ff00ff]"Visiting cities in this order:"[/color]
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#a52a2a][b]For[/b][/color] i = [COLOR=#ff00ff]1[/color] To N + [COLOR=#ff00ff]1[/color]
[COLOR=#a52a2a][b]Read[/b][/color] visit(i)
[COLOR=#a52a2a][b]Print[/b][/color] visit(i);
[COLOR=#a52a2a][b]Next[/b][/color] i
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#0000ff]'compute partial distances[/color]
[COLOR=#a52a2a][b]Print[/b][/color] [COLOR=#ff00ff]"Distances between cities travelled:"[/color]
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#a52a2a][b]For[/b][/color] i = [COLOR=#ff00ff]1[/color] To N
visit_distance(i) = cities(visit(i), visit(i + [COLOR=#ff00ff]1[/color]))
[COLOR=#a52a2a][b]Print[/b][/color] visit_distance(i);
[COLOR=#a52a2a][b]Next[/b][/color] i
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#a52a2a][b]Print[/b][/color]
[COLOR=#0000ff]'compute sum of distances[/color]
total_distance = [COLOR=#ff00ff]0[/color]
[COLOR=#a52a2a][b]For[/b][/color] i = [COLOR=#ff00ff]1[/color] To N
total_distance = total_distance + visit_distance(i)
[COLOR=#a52a2a][b]Next[/b][/color] i
[COLOR=#a52a2a][b]Print[/b][/color] [COLOR=#ff00ff]"Total distance travelled ="[/color]; total_distance
[COLOR=#0000ff]'---------------------------------------------------------------------[/color]
[COLOR=#0000ff]'Matrix contains distances between the cities[/color]
Distance_Matrix:
[COLOR=#a52a2a][b]Data[/b][/color] [COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]34[/color],[COLOR=#ff00ff]36[/color],[COLOR=#ff00ff]37[/color],[COLOR=#ff00ff]31[/color],[COLOR=#ff00ff]33[/color],[COLOR=#ff00ff]35[/color]
[COLOR=#a52a2a][b]Data[/b][/color] [COLOR=#ff00ff]34[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]29[/color],[COLOR=#ff00ff]23[/color],[COLOR=#ff00ff]22[/color],[COLOR=#ff00ff]25[/color],[COLOR=#ff00ff]24[/color]
[COLOR=#a52a2a][b]Data[/b][/color] [COLOR=#ff00ff]36[/color],[COLOR=#ff00ff]29[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]17[/color],[COLOR=#ff00ff]12[/color],[COLOR=#ff00ff]18[/color],[COLOR=#ff00ff]17[/color]
[COLOR=#a52a2a][b]Data[/b][/color] [COLOR=#ff00ff]37[/color],[COLOR=#ff00ff]23[/color],[COLOR=#ff00ff]17[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]32[/color],[COLOR=#ff00ff]30[/color],[COLOR=#ff00ff]29[/color]
[COLOR=#a52a2a][b]Data[/b][/color] [COLOR=#ff00ff]31[/color],[COLOR=#ff00ff]22[/color],[COLOR=#ff00ff]12[/color],[COLOR=#ff00ff]32[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]26[/color],[COLOR=#ff00ff]24[/color]
[COLOR=#a52a2a][b]Data[/b][/color] [COLOR=#ff00ff]33[/color],[COLOR=#ff00ff]25[/color],[COLOR=#ff00ff]18[/color],[COLOR=#ff00ff]30[/color],[COLOR=#ff00ff]26[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]19[/color]
[COLOR=#a52a2a][b]Data[/b][/color] [COLOR=#ff00ff]35[/color],[COLOR=#ff00ff]24[/color],[COLOR=#ff00ff]17[/color],[COLOR=#ff00ff]29[/color],[COLOR=#ff00ff]24[/color],[COLOR=#ff00ff]19[/color],[COLOR=#ff00ff]0[/color]
[COLOR=#0000ff]'Vector contains the order in which to visit the cities[/color]
Visit_vector:
[COLOR=#a52a2a][b]Data[/b][/color] [COLOR=#ff00ff]4[/color],[COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]5[/color],[COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]6[/color],[COLOR=#ff00ff]7[/color],[COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]4[/color]
[COLOR=#0000ff]'Travelling Salesman Problem[/color]
[COLOR=#0000ff]'number of cities[/color]
[COLOR=#804040][b]Const[/b][/color] N = [COLOR=#ff00ff]7[/color]
[COLOR=#804040][b]Dim[/b][/color] cities(N - [COLOR=#ff00ff]1[/color], N - [COLOR=#ff00ff]1[/color])
[COLOR=#804040][b]Dim[/b][/color] visit(N)
[COLOR=#804040][b]Dim[/b][/color] minimum_visit(N)
[COLOR=#804040][b]Dim[/b][/color] visit_distance(N - [COLOR=#ff00ff]1[/color])
[COLOR=#804040][b]Dim[/b][/color] minimum_visit_distance(N - [COLOR=#ff00ff]1[/color])
[COLOR=#804040][b]Dim[/b][/color] a(N - [COLOR=#ff00ff]1[/color])
[COLOR=#804040][b]Dim[/b][/color] p(N)
[COLOR=#804040][b]Call[/b][/color] populate_distance_matrix(cities())
output_file[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"tsp_solver.txt"[/color]
[COLOR=#804040][b]Open[/b][/color] output_file[COLOR=#2e8b57][b]$[/b][/color] [COLOR=#804040][b]For[/b][/color] Output As [COLOR=#2e8b57][b]#1[/b][/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
a(i) = i + [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]Next[/b][/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To N
p(i) = i
[COLOR=#804040][b]Next[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"Given number of cities:"[/color]
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"a = "[/color] + int_array_to_string[COLOR=#2e8b57][b]$[/b][/color](a())
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"All permutations:"[/color]
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
minimum_distance = [COLOR=#ff00ff]0[/color]
[COLOR=#0000ff]'-- QuickPerm algorithm[/color]
i = [COLOR=#ff00ff]0[/color]
nr = [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]Do[/b][/color] [COLOR=#804040][b]While[/b][/color] i < N
p(i) = p(i) - [COLOR=#ff00ff]1[/color]
j = [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]If[/b][/color] i Mod [COLOR=#ff00ff]2[/color] = [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]Then[/b][/color]
j = p(i)
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]
[COLOR=#804040][b]Swap[/b][/color] a(i), a(j)
i = [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]Do[/b][/color] [COLOR=#804040][b]While[/b][/color] p(i) = [COLOR=#ff00ff]0[/color]
p(i) = i
i = i + [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]Loop[/b][/color]
[COLOR=#0000ff] '#-- processing one permutation[/color]
[COLOR=#0000ff] 'create visit vector from permutation a()[/color]
[COLOR=#804040][b]Call[/b][/color] permutation_to_visit(a(), visit())
[COLOR=#0000ff] 'compute vector of distances between cities travelled[/color]
[COLOR=#804040][b]Call[/b][/color] compute_visit_distances(cities(), visit(), visit_distance())
[COLOR=#0000ff] 'compute total distance travelled[/color]
total_distance = total_distance_travelled(visit_distance())
[COLOR=#0000ff] '-- print visit info[/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#804040][b]Using[/b][/color] [COLOR=#ff00ff]"####)"[/color]; nr,
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], [COLOR=#804040][b]Using[/b][/color] [COLOR=#ff00ff]"####)"[/color]; nr,
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]" "[/color] _
+ [COLOR=#ff00ff]"permutation a = "[/color] + int_array_to_string[COLOR=#2e8b57][b]$[/b][/color](a())
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]" "[/color] _
+ [COLOR=#ff00ff]"visit = "[/color] + int_array_to_string[COLOR=#2e8b57][b]$[/b][/color](visit())
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]" "[/color] _
+ [COLOR=#ff00ff]"visit_distance = "[/color] _
+ int_array_to_string[COLOR=#2e8b57][b]$[/b][/color](visit_distance())
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]" "[/color] _
+ [COLOR=#ff00ff]"total_distance = "[/color] + str[COLOR=#2e8b57][b]$[/b][/color](total_distance)
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#0000ff] '-- end of printing visit info[/color]
[COLOR=#0000ff] 'check minimum[/color]
[COLOR=#804040][b]If[/b][/color] nr = [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]Then[/b][/color]
minimum_distance = total_distance
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]
[COLOR=#804040][b]If[/b][/color] total_distance < minimum_distance [COLOR=#804040][b]Then[/b][/color]
minimum_distance = total_distance
[COLOR=#804040][b]Call[/b][/color] assign_array(visit(), minimum_visit())
[COLOR=#804040][b]Call[/b][/color] assign_array(visit_distance(), minimum_visit_distance())
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]" * ==> Less value found !"[/color]
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]
[COLOR=#0000ff] '#-- processing end[/color]
nr = nr + [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]Loop[/b][/color]
[COLOR=#804040][b]Print[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], [COLOR=#ff00ff]""[/color]
[COLOR=#0000ff]'print result[/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"*******************"[/color]
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"* Minimum Result: *"[/color]
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"*******************"[/color]
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"Visiting cities in this order:"[/color]
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], [COLOR=#ff00ff]""[/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = int_array_to_string[COLOR=#2e8b57][b]$[/b][/color](minimum_visit())
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], [COLOR=#ff00ff]""[/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"Distances between cities travelled:"[/color]
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], [COLOR=#ff00ff]""[/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = int_array_to_string[COLOR=#2e8b57][b]$[/b][/color](minimum_visit_distance())
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], [COLOR=#ff00ff]""[/color]
output_line[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"Total distance travelled = "[/color] + Str[COLOR=#2e8b57][b]$[/b][/color](minimum_distance)
[COLOR=#804040][b]Print[/b][/color] output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color], output_line[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#0000ff]'close output file[/color]
[COLOR=#804040][b]Close[/b][/color] [COLOR=#2e8b57][b]#1[/b][/color]
[COLOR=#0000ff]'---------------------------------------------------------------------[/color]
[COLOR=#0000ff]'Data[/color]
[COLOR=#0000ff]'---------------------------------------------------------------------[/color]
[COLOR=#0000ff]'Matrix contains distances between the cities[/color]
[COLOR=#0000ff]'Distance_Matrix:[/color]
[COLOR=#0000ff]'Data 0,34,36[/color]
[COLOR=#0000ff]'Data 35,20,29[/color]
[COLOR=#0000ff]'Data 40,29,10[/color]
Distance_Matrix:
[COLOR=#804040][b]Data[/b][/color] [COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]34[/color],[COLOR=#ff00ff]36[/color],[COLOR=#ff00ff]37[/color],[COLOR=#ff00ff]31[/color],[COLOR=#ff00ff]33[/color],[COLOR=#ff00ff]35[/color]
[COLOR=#804040][b]Data[/b][/color] [COLOR=#ff00ff]34[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]29[/color],[COLOR=#ff00ff]23[/color],[COLOR=#ff00ff]22[/color],[COLOR=#ff00ff]25[/color],[COLOR=#ff00ff]24[/color]
[COLOR=#804040][b]Data[/b][/color] [COLOR=#ff00ff]36[/color],[COLOR=#ff00ff]29[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]17[/color],[COLOR=#ff00ff]12[/color],[COLOR=#ff00ff]18[/color],[COLOR=#ff00ff]17[/color]
[COLOR=#804040][b]Data[/b][/color] [COLOR=#ff00ff]37[/color],[COLOR=#ff00ff]23[/color],[COLOR=#ff00ff]17[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]32[/color],[COLOR=#ff00ff]30[/color],[COLOR=#ff00ff]29[/color]
[COLOR=#804040][b]Data[/b][/color] [COLOR=#ff00ff]31[/color],[COLOR=#ff00ff]22[/color],[COLOR=#ff00ff]12[/color],[COLOR=#ff00ff]32[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]26[/color],[COLOR=#ff00ff]24[/color]
[COLOR=#804040][b]Data[/b][/color] [COLOR=#ff00ff]33[/color],[COLOR=#ff00ff]25[/color],[COLOR=#ff00ff]18[/color],[COLOR=#ff00ff]30[/color],[COLOR=#ff00ff]26[/color],[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]19[/color]
[COLOR=#804040][b]Data[/b][/color] [COLOR=#ff00ff]35[/color],[COLOR=#ff00ff]24[/color],[COLOR=#ff00ff]17[/color],[COLOR=#ff00ff]29[/color],[COLOR=#ff00ff]24[/color],[COLOR=#ff00ff]19[/color],[COLOR=#ff00ff]0[/color]
[COLOR=#0000ff]'---------------------------------------------------------------------[/color]
[COLOR=#0000ff]'Functions and Subroutines[/color]
[COLOR=#0000ff]'---------------------------------------------------------------------[/color]
[COLOR=#804040][b]Function[/b][/color] int_array_to_string[COLOR=#2e8b57][b]$[/b][/color] (a())
res[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"["[/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To UBound(a)
res[COLOR=#2e8b57][b]$[/b][/color] = res[COLOR=#2e8b57][b]$[/b][/color] + LTrim[COLOR=#2e8b57][b]$[/b][/color](Str[COLOR=#2e8b57][b]$[/b][/color](a(i)))
[COLOR=#804040][b]If[/b][/color] i < UBound(a) [COLOR=#804040][b]Then[/b][/color]
res[COLOR=#2e8b57][b]$[/b][/color] = res[COLOR=#2e8b57][b]$[/b][/color] + [COLOR=#ff00ff]", "[/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]
[COLOR=#804040][b]Next[/b][/color] i
res[COLOR=#2e8b57][b]$[/b][/color] = res[COLOR=#2e8b57][b]$[/b][/color] + [COLOR=#ff00ff]"]"[/color]
int_array_to_string[COLOR=#2e8b57][b]$[/b][/color] = res[COLOR=#2e8b57][b]$[/b][/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]Function[/b][/color]
[COLOR=#804040][b]Function[/b][/color] total_distance_travelled (visit_distance())
total_distance = [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
total_distance = total_distance + visit_distance(i)
[COLOR=#804040][b]Next[/b][/color]
total_distance_travelled = total_distance
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]Function[/b][/color]
[COLOR=#804040][b]Sub[/b][/color] permutation_to_visit (a(), visit())
[COLOR=#0000ff] 'create visit vector from one permutation[/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
visit(i) = a(i)
[COLOR=#804040][b]Next[/b][/color]
visit(N) = a([COLOR=#ff00ff]0[/color])
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]Sub[/b][/color]
[COLOR=#804040][b]Sub[/b][/color] compute_visit_distances (cities(), visit(), visit_distance())
[COLOR=#0000ff] 'compute partial distances[/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
visit_distance(i) = cities(visit(i) - [COLOR=#ff00ff]1[/color], visit(i + [COLOR=#ff00ff]1[/color]) - [COLOR=#ff00ff]1[/color])
[COLOR=#804040][b]Next[/b][/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]Sub[/b][/color]
[COLOR=#804040][b]Sub[/b][/color] populate_distance_matrix (cities())
[COLOR=#0000ff] 'read data into matrix[/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#ff00ff]"Matrix of distances between the cities:"[/color]
[COLOR=#804040][b]Print[/b][/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]For[/b][/color] j = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]Read[/b][/color] cities(i, j)
[COLOR=#804040][b]Print[/b][/color] cities(i, j);
[COLOR=#804040][b]Next[/b][/color] j
[COLOR=#804040][b]Print[/b][/color]
[COLOR=#804040][b]Next[/b][/color] i
[COLOR=#804040][b]Print[/b][/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]Sub[/b][/color]
[COLOR=#804040][b]Sub[/b][/color] assign_array (a(), b())
[COLOR=#0000ff] 'b() <- a()[/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To UBound(a)
b(i) = a(i)
[COLOR=#804040][b]Next[/b][/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]Sub[/b][/color]
Given number of cities:
a = [1, 2, 3, 4, 5, 6, 7]
All permutations:
1) permutation a = [1, 2, 3, 4, 5, 6, 7]
visit = [1, 2, 3, 4, 5, 6, 7, 1]
visit_distance = [34, 29, 17, 32, 26, 19, 35]
total_distance = 192
2) permutation a = [2, 1, 3, 4, 5, 6, 7]
visit = [2, 1, 3, 4, 5, 6, 7, 2]
visit_distance = [34, 36, 17, 32, 26, 19, 24]
total_distance = 188
* ==> Less value found !
...
...
...
...
5039) permutation a = [2, 7, 3, 4, 5, 6, 1]
visit = [2, 7, 3, 4, 5, 6, 1, 2]
visit_distance = [24, 17, 17, 32, 26, 33, 34]
total_distance = 183
5040) permutation a = [7, 2, 3, 4, 5, 6, 1]
visit = [7, 2, 3, 4, 5, 6, 1, 7]
visit_distance = [24, 29, 17, 32, 26, 33, 35]
total_distance = 196
*******************
* Minimum Result: *
*******************
Visiting cities in this order:
[2, 4, 3, 5, 1, 6, 7, 2]
Distances between cities travelled:
[23, 17, 12, 31, 33, 19, 24]
Total distance travelled = 159
pya said:When you have time can you please look at the ERROR I get when running your code.
...
Attached is the compilelog ...
In file included from qbx.cpp:2120:0:
..\\temp\\main.txt:1285:1: error: 'LABEL_2700' does not name a type
compilation terminated due to -Wfatal-errors.
I don't know what line numbers you mean.pya said:Also, I do not understand Lines 2320 - 2360.
As you could see these lines begin with ' and are set on comments. It was only a small example of three cities on which I tested the program during development. You can delete these lines.I do not understand the first set of Distance Matrix - DATA 0,34,36 and the following 2 lines.
I would START the timer as you have done and END it after the main program is over i.e. after these linesMy 2nd question is where is the right place to put a TIMER?
I start the timer after the initial 7 DIM statements and end it just before Functions and Subroutines
'close output file
Close #1
Then make your own modification of the program, where you remove:My aim is to calculate the computing time only and not include any printing or output time.