Friday 20 August 2021

find the least euclidean distance from the given points

I have a list of dictionaries. The key is item_cd and value is location_coordinates. I would like to find the euclidian distance of these locations and create a separate similar list based on the proximity of their locations.

Steps

  • The initial distance will be calculated from the origin i.e. (0,0).
  • The closest item will be added to a separate list suppose it is {5036885955: [90.0, 61.73]} in this case.
  • Now I would like to find the closest order from the centroid of the previous orders. In this case, the centroid will be [90.0, 61.73] of order {5036885955: [90.0, 61.73]} as there is only one coordinate at the moment. The next closest order from the centroid of the previous order will be {5036885984: [86.0, 73.03]}
  • I would like to repeat the above steps until the list is empty

Input :

 [{5036885850: [92.0, 88.73]}, {5036885955: [90.0, 61.73]}, {5036885984: [86.0, 73.03]}, {5036885998: [102.0, 77.54]}]

What I have tried :

def calculate_centroid(self, locations: list) -> list:
        """
        get the centroid of the  order
        param: Location object
        return: centroid of an order
        """
        x, y = [p[0] for p in locations], [p[1] for p in locations]
        centroid = [round(sum(x) / len(locations), 2), round(sum(y) / len(locations), 2)]
        return centroid 

def get_closest_order_to_origin(self, order_centroid_list: list) -> list:
    """
     get the next closest order 
    """
        initial_order = [[0, 0]]
        next_order = []
        centroids = []
        
    for order in order_centroid_list:

        for shipping_req_key, centroid in order.items():
            distance = math.sqrt(((initial_order[0][0] - centroid[0]) ** 2) + ((initial_order[0][1] - centroid[1]) ** 2))

I am not sure how should I proceed further. I would appreciate your feedback



from find the least euclidean distance from the given points

No comments:

Post a Comment