-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Split-up Instance; implement write * Update docstring Instance.py * Correct calculation of metadata * Update README
- Loading branch information
Showing
7 changed files
with
103 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Instance: | ||
""" | ||
The FJSPLIB instance data. | ||
Parameters | ||
---------- | ||
num_jobs | ||
The number of jobs. | ||
num_machines | ||
The number of machines. | ||
num_operations | ||
The number of operations. | ||
jobs | ||
A list of job data, each job consisting of a list of operations. | ||
Operations are list of tuples, where each tuple consists of a machine | ||
index and a processing time. | ||
precedences | ||
A list of tuples consisting of two operation indices, representing the | ||
precedence relationship of two operations. | ||
""" | ||
|
||
num_jobs: int | ||
num_machines: int | ||
num_operations: int | ||
jobs: list[list[list[tuple[int, int]]]] | ||
precedences: list[tuple[int, int]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .Instance import Instance as Instance | ||
from .read import read as read | ||
from .write import write as write |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
from .Instance import Instance | ||
|
||
|
||
def write(where: Union[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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
2 3 2.2 | ||
2 3 1.7 | ||
1 2 1 1 2 2 | ||
2 1 1 1 2 3 1 2 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from pathlib import Path | ||
|
||
from numpy.testing import assert_equal | ||
|
||
from fjsplib.write import write | ||
from tests.utils import read | ||
|
||
|
||
def test_write(tmp_path: Path): | ||
""" | ||
Tests that ``write`` correctly writes an FJSPLIB instance to a file. | ||
""" | ||
instance = read("data/classic.fjs") | ||
write(tmp_path / "classic.fjs", instance) | ||
|
||
expected = [ | ||
"2 3 1.7", | ||
"1 2 1 1 2 2", | ||
"2 1 1 1 2 3 1 2 1", | ||
] | ||
with open(tmp_path / "classic.fjs", "r") as fh: | ||
assert_equal(fh.read(), "\n".join(expected)) |