Skip to content

Write function #15

@leonlan

Description

@leonlan
from pathlib import Path

from .Instance import Instance


def write(where: Path | str, instance: Instance):
    """
    Writes a problem instance to file in FJSPLIB format.

    Parameters
    ----------
    where
        Location to write the instance to.
    instance
        The problem instance.
    """
    lines = []

    # The flexibility is the average number of eligible machines per operation.
    num_eligible = sum([len(task) for ops in instance.jobs for task in ops])
    flexibility = round(num_eligible / instance.num_operations, 1)

    metadata = f"{instance.num_jobs} {instance.num_machines} {flexibility}"
    lines.append(metadata)

    for operations in instance.jobs:
        job = [len(operations)]

        for processing_data in operations:
            num_eligible = len(processing_data)
            job.append(num_eligible)

            for machine, duration in processing_data:
                # Machine indices are 1-indexed in FJSPLIB.
                job.extend([machine + 1, duration])

        line = " ".join(str(num) for num in job)
        lines.append(line)

    formatted = "\n".join(lines)

    with open(where, "w") as fh:
        fh.write(formatted)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions