OPB (cpmpy.tools.opb)

Set of utilities for working with OPB-formatted CP models.

Currently only the restricted OPB PB24 format is supported (without WBO).

List of functions

read_opb

Parse an OPB (Pseudo-Boolean) file instance (.opb or .opb.xz) or string and return a CPMpy Model.

List of submodules

dataset

PyTorch-style Dataset for PB competition instances in restricted OPB PB24 format.

cpmpy.tools.opb.read_opb(path: Union[str, PathLike]) Model[source]

Parse an OPB (Pseudo-Boolean) file instance (.opb or .opb.xz) or string and return a CPMpy Model.

Based on PyPBLib’s example parser: https://hardlog.udl.cat/static/doc/pypblib/html/library/index.html#example-from-opb-to-cnf-file

Supports:
  • Linear and non-linear terms (e.g., -1 x1 x14 +2 x2)

  • Negated variables using ‘~’ (e.g., ~x5)

  • Minimisation objective

  • Comparison operators in constraints: ‘=’, ‘>=’

Parameters:

path (Union[str, os.PathLike]) –

  • A file path to an OPB file (optionally LZMA-compressed with .xz)

  • OR a string containing the OPB content directly

Returns:

A CPMpy Model representing the parsed OPB problem,

including Boolean variables, constraints, (and objective).

Return type:

cp.Model

Example

>>> opb_text = '''
... * #variable= 5 #constraint= 2 #equal= 1 intsize= 64 #product= 5 sizeproduct= 13
... min: 2 x2 x3 +3 x4 ~x5 +2 ~x1 x2 +3 ~x1 x2 x3 ~x4 ~x5 ;
... 2 x2 x3 -1 x1 ~x3 = 5 ;
... '''
>>> model = read_opb(opb_text)
>>> print(model)
Model(...)

Notes

  • Comment lines starting with ‘*’ are ignored.

  • Only “min:” objectives are supported; “max:” is not recognized.

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