I have task to create routes. A route can be an object with origin, destination, type.
Origin and Destination are just a FK from Location ojbect.
Imagine I have 3 Locations, a, b, c. with three types, X, Y, Z. Then possible routes are.
routes = [(a,b,x), (a,c,x), (b,a, x), (b,c,x), (c, a, x), (c, b, x) , (a,b,y), (a,c,y), (b,a, y), (b,c,y), (c, a, y), (c, b, y) , (a,b,z), (a,c,z), (b,a, z), (b,c,z), (c, a, z), (c, b, z) ]
Now, Im able to create the routes with same output. but the problem I’m going to have more than 43 thousands unique locations, on estimation it is going to be around 5.5B unique routes based on 3 types. So I wanted to have a different type approach where I can resume the process if something fails. we don’t have problem if this takes days, but there will be a problem if we are starting it from Zero if something fails.
So any idea or concept would be appreciated. We are using Django application to handle this. here’s the real Models and Code. Im planning to use this create_route functions in the background with the help of celery and redis.
class Route(models.Model):
origin = models.ForeignKey(
Location, related_name="route_origin", on_delete=models.CASCADE
)
destination = models.ForeignKey(
Location, related_name="route_destination", on_delete=models.CASCADE
)
# Type options
VAN, REEFER, FLATBED = "V", "R", "F"
equipment_type = models.CharField(
max_length=50,
choices=(
(VAN, "Van"),
(REEFER, "Reefer"),
(FLATBED, "Flatbed"),
),
)
class Location(models.Model):
zip_code = models.CharField(max_length=5)
def create_routes():
locations = Location.objects.all()
equipment_types = [Route.VAN, Route.REEFER, Route.FLATBED]
for origin, destination in permutations(locations, 2):
for equipment_type in equipment_types:
Route.objects.create(
origin=origin,
destination=destination,
equipment_type=equipment_type,
)
user26683540 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.