ai4co / rl4co

A PyTorch library for all things Reinforcement Learning (RL) for Combinatorial Optimization (CO)
https://rl4.co
MIT License
455 stars 84 forks source link

[BUG] Code for real-world cvrplib instance inference. #223

Closed Elon-Lau closed 1 month ago

Elon-Lau commented 1 month ago

Hello, I can't solve the instances in cvrplib with the trained model.

Traceback (most recent call last): File "/NCO/RL4CO/examples/test_cvrplib_demo.py", line 91, in out = policy(td.clone(), decode_type='greedy', return_actions=True) File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl return self._call_impl(*args, *kwargs) File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl return forward_call(args, **kwargs) File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/rl4co/models/common/constructive/base.py", line 237, in forward td = env.step(td)["next"] File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/rl4co/envs/common/base.py", line 129, in step td = self._step(td) File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/rl4co/envs/routing/cvrp/env.py", line 89, in _step visited = td["visited"].scatter(-1, current_node, 1) RuntimeError: Index tensor must have the same number of dimensions as self tensor

fedebotu commented 1 month ago

Have you checked out this notebook?

https://github.com/ai4co/rl4co/blob/main/examples/datasets/2-test-on-cvrplib.ipynb

There was some small problem with squeezing dimensions for evaluating with batch size of 1, so we made some monkey patching (that should be solved in 0.5.1)

Also, here is an example of a recent project based on RL4CO in which we evaluate CVRPLIB (with some extra utilities):

https://github.com/ai4co/routefinder/blob/main/examples/2.eval-cvrplib.ipynb

Elon-Lau commented 1 month ago

Follow the https://github.com/ai4co/rl4co/blob/main/examples/datasets/2-test-on-cvrplib.ipynb, i set batch_size=2. However, I still have this problem. If the current version can be used for CVRPlib instance inference?

Elon-Lau commented 1 month ago

I use the same code in https://github.com/ai4co/rl4co/blob/main/examples/2d-meta_train.py for training and the same code in https://github.com/ai4co/rl4co/blob/main/examples/datasets/2-test-on-cvrplib.ipynb for cvrplib instance inference. But the following bug still occur.

Traceback (most recent call last): File "/NCO/RL4CO/examples/test_cvrplib_demo.py", line 91, in out = policy(td.clone(), decode_type='greedy', return_actions=True) File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl return self._call_impl(*args, *kwargs) File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl return forward_call(args, **kwargs) File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/rl4co/models/common/constructive/base.py", line 237, in forward td = env.step(td)["next"] File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/rl4co/envs/common/base.py", line 129, in step td = self._step(td) File "/miniconda3/envs/rl4co/lib/python3.9/site-packages/rl4co/envs/routing/cvrp/env.py", line 89, in _step visited = td["visited"].scatter(-1, current_node, 1) RuntimeError: Index tensor must have the same number of dimensions as self tensor

fedebotu commented 1 month ago

Thanks for reporting the bug - you may check the most recent improvements! Please let us know if now it works well on your end ~

Elon-Lau commented 1 month ago

Thanks for the update, it looks like the patch will fix the current bug.