-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement floyd on tmat_class atsp generation #226
Merged
Merged
Conversation
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
1 task
I have also added a val/test dataset generator of ATSP using numpy in |
Awesome job! from typing import Union, Callable
import torch
from torch.distributions import Uniform
from tensordict.tensordict import TensorDict
from rl4co.utils.pylogger import get_pylogger
from rl4co.envs.common.utils import get_sampler, Generator
log = get_pylogger(__name__)
class ATSPGenerator(Generator):
"""Data generator for the Asymmetric Travelling Salesman Problem (ATSP)
Generate distance matrices inspired by the reference MatNet (Kwon et al., 2021)
We satifsy the triangle inequality (TMAT class) in a batch
Args:
num_loc: number of locations (customers) in the TSP
min_dist: minimum value for the distance between nodes
max_dist: maximum value for the distance between nodes
dist_distribution: distribution for the distance between nodes
tmat_class: whether to generate a class of distance matrix
Returns:
A TensorDict with the following keys:
locs [batch_size, num_loc, 2]: locations of each customer
"""
def __init__(
self,
num_loc: int = 10,
min_dist: float = 0.0,
max_dist: float = 1.0,
dist_distribution: Union[
int, float, str, type, Callable
] = Uniform,
tmat_class: bool = True,
**kwargs
):
self.num_loc = num_loc
self.min_dist = min_dist
self.max_dist = max_dist
self.tmat_class = tmat_class
# Distance distribution
if kwargs.get("dist_sampler", None) is not None:
self.dist_sampler = kwargs["dist_sampler"]
else:
self.dist_sampler = get_sampler("dist", dist_distribution, 0.0, 1.0, **kwargs)
def _generate(self, batch_size) -> TensorDict:
# Generate distance matrices inspired by the reference MatNet (Kwon et al., 2021)
# We satifsy the triangle inequality (TMAT class) in a batch
batch_size = [batch_size] if isinstance(batch_size, int) else batch_size
dms = (
self.dist_sampler.sample((batch_size + [self.num_loc, self.num_loc]))
* (self.max_dist - self.min_dist)
+ self.min_dist
)
dms[..., torch.arange(self.num_loc), torch.arange(self.num_loc)] = 0
log.info("Using TMAT class (triangle inequality): {}".format(self.tmat_class))
if self.tmat_class:
for i in range(self.num_loc):
dms = torch.minimum(dms, dms[..., :, [i]] + dms[..., [i], :])
return TensorDict({"cost_matrix": dms}, batch_size=batch_size)
class OldATSPGenerator(ATSPGenerator):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _generate(self, batch_size) -> TensorDict:
# Generate distance matrices inspired by the reference MatNet (Kwon et al., 2021)
# We satifsy the triangle inequality (TMAT class) in a batch
batch_size = [batch_size] if isinstance(batch_size, int) else batch_size
dms = (
self.dist_sampler.sample((batch_size + [self.num_loc, self.num_loc]))
* (self.max_dist - self.min_dist)
+ self.min_dist
)
dms[..., torch.arange(self.num_loc), torch.arange(self.num_loc)] = 0
log.info("Using TMAT class (triangle inequality): {}".format(self.tmat_class))
if self.tmat_class:
while True:
old_dms = dms.clone()
dms, _ = (
dms[..., :, None, :] + dms[..., None, :, :].transpose(-2, -1)
).min(dim=-1)
if (dms == old_dms).all():
break
return TensorDict({"cost_matrix": dms}, batch_size=batch_size)
generator = ATSPGenerator(num_loc=100)
generator_old = OldATSPGenerator(num_loc=100)
# generate 1000 samples
data = generator(1000)
data_old = generator_old(1000)
print("new")
print(data["cost_matrix"].shape, data["cost_matrix"].mean(), data["cost_matrix"].std())
print("old")
print(data_old["cost_matrix"].shape, data_old["cost_matrix"].mean(), data_old["cost_matrix"].std()) %timeit generator(1000) 685 ms ± 41.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) %timeit generator_old(1000) 2.71 s ± 245 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) Will merge as soon as the new TorchRL version is released, as the checks will then pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Change the algorithm of tmat_class ATSP generation. Expect lower time and space complexity when producing the same result.
Motivation and Context
See #225
Types of changes
Checklist