Trying to solve a variation of the traveling salesman. The output of the following calculates the next closest location based on imported values minus locations already visited. I'm trying to keep track of the total distance traveled (variable min_distance) but having issues figuring out how to add the values without looping and adding the values to each other too many times.
def get_best_route_t3():
i = 1
curr_location_to_look_for = 0
visited_locations = []
while i < len(distance_csv):
curr_location_to_look_for = get_next_location(curr_location_to_look_for, distance_csv, visited_locations)
if curr_location_to_look_for in visited_locations:
break
else:
visited_locations.append(curr_location_to_look_for)
i += 1
"""DETERMINES THE NEXT CLOSEST AVAILABLE LOCATION"""
def get_next_location(location_index, locations_to_look_in, visited_locations):
i = 0
min_distance = -1
min_row = 0
while i < len(locations_to_look_in) - 1:
i += 1
curr_distance = locations_to_look_in[i][location_index]
if curr_distance == '' or curr_distance == "0.0":
curr_distance = locations_to_look_in[location_index][i]
if float(curr_distance) != 0.0 and (min_distance == -1 or (min_distance > float(curr_distance) and i not in visited_locations)):
min_distance = float(curr_distance)
min_row = i
print(min_row, min_distance)
return min_row
question from:
https://stackoverflow.com/questions/65864637/trying-to-keep-track-of-total-distance-in-a-while-loop 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…