Skip to content

Commit

Permalink
Merge pull request #116 from ai4co/v0.3.2
Browse files Browse the repository at this point in the history
V0.3.2
  • Loading branch information
fedebotu authored Feb 21, 2024
2 parents 75eba9c + 7be225d commit 83b8550
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 110 deletions.
8 changes: 4 additions & 4 deletions notebooks/tutorials/2-creating-new-env-model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@
" \"\"\"Make the observation and action specs from the parameters\"\"\"\n",
" self.observation_spec = CompositeSpec(\n",
" locs=BoundedTensorSpec(\n",
" minimum=self.min_loc,\n",
" maximum=self.max_loc,\n",
" low=self.min_loc,\n",
" high=self.max_loc,\n",
" shape=(self.num_loc, 2),\n",
" dtype=torch.float32,\n",
" ),\n",
Expand All @@ -316,8 +316,8 @@
" self.action_spec = BoundedTensorSpec(\n",
" shape=(1,),\n",
" dtype=torch.int64,\n",
" minimum=0,\n",
" maximum=self.num_loc,\n",
" low=0,\n",
" high=self.num_loc,\n",
" )\n",
" self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))\n",
" self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)"
Expand Down
2 changes: 1 addition & 1 deletion rl4co/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.1"
__version__ = "0.3.2"
25 changes: 23 additions & 2 deletions rl4co/envs/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@


class RL4COEnvBase(EnvBase):
"""Base class for RL4CO environments based on TorchRL EnvBase
"""Base class for RL4CO environments based on TorchRL EnvBase.
The environment has the usual methods for stepping, resetting, and getting the specifications of the environment
that shoud be implemented by the subclasses of this class.
It also has methods for getting the reward, action mask, and checking the validity of the solution, and
for generating and loading the datasets (supporting multiple dataloaders as well for validation and testing).
Args:
data_dir: Root directory for the dataset
Expand All @@ -27,6 +31,9 @@ class RL4COEnvBase(EnvBase):
dataset_cls: Dataset class to use for the environment (which can influence performance)
seed: Seed for the environment
device: Device to use. Generally, no need to set as tensors are updated on the fly
batch_size: Batch size to use for the environment. Generally, no need to set as tensors are updated on the fly
run_type_checks: If True, run type checks on the TensorDicts at each step
allow_done_after_reset: If True, an environment can be done after a reset
_torchrl_mode: Whether to use the TorchRL mode (see :meth:`step` for more details)
"""

Expand All @@ -45,10 +52,24 @@ def __init__(
dataset_cls: callable = TensorDictDataset,
seed: int = None,
device: str = "cpu",
batch_size: torch.Size = None,
run_type_checks: bool = False,
allow_done_after_reset: bool = False,
_torchrl_mode: bool = False,
**kwargs,
):
super().__init__(device=device, batch_size=[])
super().__init__(
device=device,
batch_size=batch_size,
run_type_checks=run_type_checks,
allow_done_after_reset=allow_done_after_reset,
)
# if any kwargs are left, we want to warn the user
if kwargs:
log.warning(
f"Unused keyword arguments: {', '.join(kwargs.keys())}. "
"Please check the documentation for the correct keyword arguments"
)
self.data_dir = data_dir
self.train_file = pjoin(data_dir, train_file) if train_file is not None else None
self._torchrl_mode = _torchrl_mode
Expand Down
8 changes: 4 additions & 4 deletions rl4co/envs/eda/dpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def _make_spec(self, td_params):
"""Make the observation and action specs from the parameters"""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.size**2, 2),
dtype=torch.float32,
),
Expand All @@ -168,8 +168,8 @@ def _make_spec(self, td_params):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.size**2,
low=0,
high=self.size**2,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
8 changes: 4 additions & 4 deletions rl4co/envs/eda/mdpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def _make_spec(self, td_params):
"""Make the observation and action specs from the parameters"""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.size**2, 2),
dtype=torch.float32,
),
Expand All @@ -102,8 +102,8 @@ def _make_spec(self, td_params):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.size**2,
low=0,
high=self.size**2,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
8 changes: 4 additions & 4 deletions rl4co/envs/routing/atsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def _reset(self, td: Optional[TensorDict] = None, batch_size=None) -> TensorDict
def _make_spec(self, td_params: TensorDict = None):
self.observation_spec = CompositeSpec(
cost_matrix=BoundedTensorSpec(
minimum=self.min_dist,
maximum=self.max_dist,
low=self.min_dist,
high=self.max_dist,
shape=(self.num_loc, self.num_loc),
dtype=torch.float32,
),
Expand All @@ -140,8 +140,8 @@ def _make_spec(self, td_params: TensorDict = None):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.num_loc,
low=0,
high=self.num_loc,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
12 changes: 6 additions & 6 deletions rl4co/envs/routing/cvrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ def _make_spec(self, td_params: TensorDict):
"""Make the observation and action specs from the parameters."""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.num_loc + 1, 2),
dtype=torch.float32,
),
Expand All @@ -270,8 +270,8 @@ def _make_spec(self, td_params: TensorDict):
dtype=torch.int64,
),
demand=BoundedTensorSpec(
minimum=-self.capacity,
maximum=self.max_demand,
low=-self.capacity,
high=self.max_demand,
shape=(self.num_loc, 1), # demand is only for customers
dtype=torch.float32,
),
Expand All @@ -284,8 +284,8 @@ def _make_spec(self, td_params: TensorDict):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.num_loc + 1,
low=0,
high=self.num_loc + 1,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
12 changes: 6 additions & 6 deletions rl4co/envs/routing/mpdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ def _make_spec(self, td_params: TensorDict):
max_nodes = self.num_loc + self.max_num_agents + 1
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(max_nodes, 2),
dtype=torch.float32,
),
Expand All @@ -363,8 +363,8 @@ def _make_spec(self, td_params: TensorDict):
dtype=torch.float32,
),
cur_coord=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(2,),
dtype=torch.float32,
),
Expand Down Expand Up @@ -417,8 +417,8 @@ def _make_spec(self, td_params: TensorDict):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=max_nodes,
low=0,
high=max_nodes,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
8 changes: 4 additions & 4 deletions rl4co/envs/routing/mtsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ def _make_spec(self, td_params: TensorDict):
"""Make the observation and action specs from the parameters."""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.num_loc, 2),
dtype=torch.float32,
),
Expand Down Expand Up @@ -213,8 +213,8 @@ def _make_spec(self, td_params: TensorDict):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.num_loc,
low=0,
high=self.num_loc,
)
self.reward_spec = UnboundedContinuousTensorSpec()
self.done_spec = UnboundedDiscreteTensorSpec(dtype=torch.bool)
Expand Down
8 changes: 4 additions & 4 deletions rl4co/envs/routing/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ def _make_spec(self, td_params: TensorDict):
"""Make the observation and action specs from the parameters."""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.num_loc + 1, 2),
dtype=torch.float32,
),
Expand Down Expand Up @@ -307,8 +307,8 @@ def _make_spec(self, td_params: TensorDict):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.num_loc + 1,
low=0,
high=self.num_loc + 1,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
8 changes: 4 additions & 4 deletions rl4co/envs/routing/pctsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ def _make_spec(self, td_params: TensorDict):
"""Make the locs and action specs from the parameters."""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.num_loc, 2),
dtype=torch.float32,
),
Expand Down Expand Up @@ -321,8 +321,8 @@ def _make_spec(self, td_params: TensorDict):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.num_loc,
low=0,
high=self.num_loc,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
8 changes: 4 additions & 4 deletions rl4co/envs/routing/pdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ def _make_spec(self, td_params: TensorDict):
"""Make the observation and action specs from the parameters."""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.num_loc + 1, 2),
dtype=torch.float32,
),
Expand All @@ -170,8 +170,8 @@ def _make_spec(self, td_params: TensorDict):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.num_loc + 1,
low=0,
high=self.num_loc + 1,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
20 changes: 10 additions & 10 deletions rl4co/envs/routing/sdvrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def _make_spec(self, td_params: TensorDict):
"""Make the observation and action specs from the parameters."""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.num_loc + 1, 2),
dtype=torch.float32,
),
Expand All @@ -186,20 +186,20 @@ def _make_spec(self, td_params: TensorDict):
dtype=torch.int64,
),
demand=BoundedTensorSpec(
minimum=self.min_demand,
maximum=self.max_demand,
low=self.min_demand,
high=self.max_demand,
shape=(self.num_loc, 1), # demand is only for customers
dtype=torch.float32,
),
demand_with_depot=BoundedTensorSpec(
minimum=self.min_demand,
maximum=self.max_demand,
low=self.min_demand,
high=self.max_demand,
shape=(self.num_loc + 1, 1),
dtype=torch.float32,
),
used_capacity=BoundedTensorSpec(
minimum=0,
maximum=self.vehicle_capacity,
low=0,
high=self.vehicle_capacity,
shape=(1,),
dtype=torch.float32,
),
Expand All @@ -212,8 +212,8 @@ def _make_spec(self, td_params: TensorDict):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.num_loc + 1,
low=0,
high=self.num_loc + 1,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
8 changes: 4 additions & 4 deletions rl4co/envs/routing/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def _make_spec(self, td_params):
"""Make the observation and action specs from the parameters"""
self.observation_spec = CompositeSpec(
locs=BoundedTensorSpec(
minimum=self.min_loc,
maximum=self.max_loc,
low=self.min_loc,
high=self.max_loc,
shape=(self.num_loc, 2),
dtype=torch.float32,
),
Expand All @@ -137,8 +137,8 @@ def _make_spec(self, td_params):
self.action_spec = BoundedTensorSpec(
shape=(1,),
dtype=torch.int64,
minimum=0,
maximum=self.num_loc,
low=0,
high=self.num_loc,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(1,))
self.done_spec = UnboundedDiscreteTensorSpec(shape=(1,), dtype=torch.bool)
Expand Down
Loading

0 comments on commit 83b8550

Please sign in to comment.