Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

python - Trying to keep track of total distance in a while loop

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...