Dataset (cpmpy.tools.opb.dataset)
PyTorch-style Dataset for PB competition instances in restricted OPB PB24 format.
Simply create a dataset instance (configured for the targeted competition year/track) and start iterating over its contents:
from cpmpy.tools.opb import OPBDataset, read_opb
for filename, metadata in OPBDataset(year=2016, track="DEC-LIN", download=True): # auto download dataset and iterate over its instances
# Do whatever you want here, e.g. reading to a CPMpy model and solving it:
model = read_opb(filename)
model.solve()
print(model.status())
The metadata contains usefull information about the current problem instance.
Since the dataset is PyTorch compatible, it can be used with a DataLoader:
from cpmpy.tools.opb import OPBDataset, read_opb
# Initialize the dataset
dataset = OPBDataset(year=2016, track="DEC-LIN", download=True):
from torch.utils.data import DataLoader
# Wrap dataset in a DataLoader
data_loader = DataLoader(dataset, batch_size=10, shuffle=False)
# Iterate over the dataset
for batch in data_loader:
# Your code here
- class cpmpy.tools.opb.dataset.OPBDataset(root: str = '.', year: int = 2023, track: Optional[str] = None, transform=None, target_transform=None, download: bool = False)[source]
OPB PB24 Dataset in a PyTorch compatible format.
- Parameters:
root (str) – Root directory containing the OPB instances (if ‘download’, instances will be downloaded to this location)
year (int) – Competition year (2006, 2007, 2009, 2010, 2011, 2012, 2015, 2016 or 2024)
track (str, optional) – Filter instances by track type (e.g., “DEC-LIN”, “DEC-NLC”, “OPT-LIN”, “OPT-NLC”)
transform (callable, optional) – Optional transform to be applied on the instance data (the file path of each problem instance)
target_transform (callable, optional) – Optional transform to be applied on the metadata (the metadata dictionary of each problem instance)
download (bool) – If True, downloads the dataset from the internet and puts it in root directory