Loader and writer dispatchers (cpmpy.tools.io.loader, cpmpy.tools.io.writer)

1) Loading from file

CPMpy tools for loading models from files.

List of functions

load

Load an instance from a file into a CPMpy model..

load_formats

List of supported load formats.

cpmpy.tools.io.loader.load(instance: str | PathLike | TextIO, format: str | None = None, open: Callable = <built-in function open>) Model[source]

Load an instance from a file into a CPMpy model..

Parameters:
  • instance (str or os.PathLike or TextIO) – The path to the instance file to load, the instance itself as a string, or a TextIO object.

  • format (Optional[str]) – The format of the file to load. If None, the format will be derived from the file path (best effort). Might raise a ValueError if the format could not be derived from the file path, or if the format is not supported.

  • open (Callable) – callable to open the file for reading (default: builtin open). Use for decompression, e.g. lambda p: lzma.open(p, 'rt') for .xz.

Raises:

ValueError – If the format is not supported or could not be derived from the file path.

Returns:

A CPMpy model.

cpmpy.tools.io.loader.load_formats() List[str][source]

List of supported load formats.

Each can be used as the format argument to the load function. E.g.:

from cpmpy.tools.io import load, load_formats
assert "mps" in load_formats()
model = load(file_path_to_mps, format="mps")
assert "lp" in load_formats()
model = load(file_path_to_lp, format="lp")
cpmpy.tools.io.loader.main()[source]

2) Writing to file

CPMpy tools for writing models to files.

List of functions

write

Write a model to a file.

write_formats

List of supported write formats.

cpmpy.tools.io.writer.write(model: Model, path: str | PathLike | None = None, format: str | None = None, verbose: bool = False, header: str | None = None, open: Callable = functools.partial(<built-in function open>, mode='w'), **kwargs) str[source]

Write a model to a file.

Parameters:
  • model (cp.Model) – The model to write.

  • path (str or os.PathLike, optional) – The file path to write the model to. If None, only a string containing the model will be returned and nothing will be written.

  • format (Optional[str]) – The format to write the model in. If None and path is provided, the format will be derived from the file path extension (best effort, might raise a ValueError if the format could not be derived from the file path). Might raise a ValueError if the format is not supported.

  • verbose (bool) – Whether to print verbose output.

  • header (Optional[str]) – The header to put at the top of the file. If None, a default header will be created. Pass an empty string to skip adding a header.

  • open (Callable) – callable to open the file for writing (default: builtin open). Called as open(path). This mirrors the open= argument in loaders and allows custom compression or I/O (e.g. lambda p: lzma.open(p, 'wt')).

  • **kwargs – Additional arguments to pass to the writer.

Raises:

ValueError – If the format is not supported or could not be derived from the file path.

Example

>>> write(model, "output.opb")                # Format auto-detected from .opb
>>> write(model, "output.txt", format="opb")  # Format explicitly specified
>>> write(model, format="opb")                # Only returns a string, format must be specified
cpmpy.tools.io.writer.write_formats() List[str][source]

List of supported write formats.

Each can be used as the format argument to the write function. E.g.:

from cpmpy.tools.io import write, write_formats
assert "mps" in write_formats()
write(model, format="mps")

write(model, format=write_formats()[0])  # Returns string
from cpmpy.tools.io import get_extension
write(model, f"model.{get_extension(write_formats()[1])}")  # Writes to file, format auto-detected