-
First of all, thank you very much for this package. It is very easy to understand and great to dive into vehicle routing. I wanted to ask if it is possible to use PyVRP in a dynamic way. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi @johege! 👋 We do not support this directly, unfortunately. But it should not be hard to do, following the example on how to write your own def solve(data, prev_best=None):
rng = RandomNumberGenerator(seed=42)
pm = PenaltyManager.init_from(data)
pop = Population(broken_pairs_distance)
neighbours = compute_neighbours(data)
ls = LocalSearch(INSTANCE, rng, neighbours)
for node_op in NODE_OPERATORS:
ls.add_node_operator(node_op(data))
for route_op in ROUTE_OPERATORS:
ls.add_route_operator(route_op(data))
init = [Solution.make_random(data, rng) for _ in range(25)]
if prev_best:
init.append(prev_best)
algo = GeneticAlgorithm(data, pm, rng, pop, ls, srex, init)
return algo.run(stop=MaxRuntime(10)) Suppose we have some initial data object, res0 = solve(data0) then add some clients to obtain res1 = solve(data1, res0.best) This should work. With new clients, I hope this helps. Please let us know if you have any further questions! |
Beta Was this translation helpful? Give feedback.
-
Hi @N-Wouda, Thanks for the quick reply. It's really cool how quickly you respond to the issues and discussions in this repo. 😄 if prev_best:
init.append(prev_best) added the original best solution to the Here's the adapted code. Maybe it helps someone else as well 😄 def solve(new_data, prev_data=None, prev_best=None):
rng = RandomNumberGenerator(seed=42)
pm = PenaltyManager.init_from(new_data)
pop = Population(broken_pairs_distance)
neighbours = compute_neighbours(new_data)
ls = LocalSearch(new_data, rng, neighbours)
for node_op in NODE_OPERATORS:
ls.add_node_operator(node_op(new_data))
for route_op in ROUTE_OPERATORS:
ls.add_route_operator(route_op(new_data))
init = [Solution.make_random(new_data, rng) for _ in range(25)]
if prev_best:
num_old_clients = prev_data.num_clients prev_data.num_depots
num_new_clients = new_data.num_clients new_data.num_depots
new_clients_ids = list(range(num_old_clients, num_new_clients))
new_route = Route(
data=new_data,
visits=new_clients_ids,
vehicle_type=0
)
new_routes = prev_best.routes()
new_routes.append(new_route)
adapted_prev_best = Solution(new_data, new_routes)
init.append(adapted_prev_best)
algo = GeneticAlgorithm(new_data, pm, rng, pop, ls, srex, init)
return algo.run(stop=MaxRuntime(10), display=True) There are probably smarter ways to integrate the new clients into the previous routes. But this works for now. |
Beta Was this translation helpful? Give feedback.
That's right. I thought about using the whole previous population. I just wasn't familiar enough with the code yet to do that.
I think I have that implemented now: