Created
November 20, 2020 01:35
-
-
Save AlexandreProenca/4f47f5ced0f107af4479266ece3ece2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchTree: | |
def __init__(self, cargos: List[dict], trucks: List[dict]): | |
self.cargo_nodes, self.truck_nodes = self.build_nodes(cargos, trucks, Node) | |
self.tree = KdTree(self.truck_nodes) | |
self.delivery_distance = { | |
item['product']: geodesic( | |
(item['origin_lat'], item['origin_lng']), | |
(item['destination_lat'], item['destination_lng']) | |
) for item in cargos | |
} | |
@staticmethod | |
def build_nodes(cargos: List[dict], trucks: List[dict], node_class: Any) -> Tuple[List[Any], List[Any]]: | |
cargo_nodes = [node_class((item['origin_lat'], item['origin_lng']), item['product']) for item in cargos] | |
truck_nodes = [node_class((item['lat'], item['lng']), item['truck']) for item in trucks] | |
return cargo_nodes, truck_nodes | |
def truck_assignment(self) -> List[dict]: | |
result = [] | |
for cargo in self.cargo_nodes: | |
path, truck = self.tree.get_closest(cargo) | |
result.append({ | |
"cargo": cargo.name, | |
"truck": truck.name, | |
"distance": round(path[truck].miles + self.delivery_distance[cargo.name].miles, 2) | |
}) | |
return sorted(result, key=lambda x: x['distance']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment