def opt2(points): distance=find_distance(points) # n*n distance matrix route=np.arange(distance.shape[0]) # initilize route in chronological order route=np.append(route,0) # final point is point zero (close loop) segments=find_segment_length(route,distance) #length of each segment flag=0 while flag ==0: best_distance= np.sum(segments) for i in range(1,route.shape[0]-2): for j in range (i+1,route.shape[0]-1): new_route=find_neighbour(route,i,j) new_distance=np.sum(find_segment_length(new_route,distance)) if new_distance<best_distance: best_distance=new_distance route=new_route i=1 j=1 flag=1 return (best_distance,route)
def greedy(points): distance=find_distance(points) #creates n*n distance matrix route = np.empty(0,dtype=int) not_visited=np.ones(points.shape[0],dtype=bool) # True is point is not visited not_visited[0]=0 route=np.append(route,0) now=0 for i in range(points.shape[0]-1): dis=distance[now][:] nxt=next_point(not_visited,dis,points) route=np.append(route,nxt) not_visited[nxt]=0 now=nxt route=np.append(route,0) travel_distance=np.sum(find_segment_length(route,distance)) return (travel_distance,route)