Skip to content

Commit

Permalink
simplify solver tests, also test by trimmings, create and test combin…
Browse files Browse the repository at this point in the history
…ed trimmings
  • Loading branch information
ModischFabrications committed Mar 29, 2024
1 parent b888104 commit 2697094
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
5 changes: 5 additions & 0 deletions app/solver/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ def _get_trimming(
return trimmings


def _get_trimmings(
max_length: int, lengths: Collection[Collection[Tuple[int, str | None]]], cut_width: int
) -> int:
return sum(_get_trimming(max_length, x, cut_width) for x in lengths)

def _sorted(lengths: List[List[Tuple[int, str | None]]]) -> List[List[Tuple[int, str | None]]]:
# keep most cuts at the top, getting simpler towards the end
# this could also sort by trimmings but that is more work
Expand Down
44 changes: 21 additions & 23 deletions tests/solver/test_solver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from app.solver.solver import (
_get_trimming,
_get_trimmings,
_solve_bruteforce,
_solve_gapfill,
_solve_FFD,
Expand All @@ -8,57 +9,54 @@
from tests.test_fixtures import *


def test_trimmings():
def test_trimming():
trimming = _get_trimming(
max_length=1500,
lengths=((300, ""), (400, ""), (600, ""), (100, "")),
cut_width=2,
lengths=((500, ""), (500, ""), (400, "")),
cut_width=10,
)

assert trimming == 92
assert trimming == 70


def test_trimmings_raise():
def test_trimming_raise():
# raises Error if more stock was used than available
with pytest.raises(OverflowError):
_get_trimming(1500, ((300, ""), (400, ""), (600, ""), (200, "")), 2)


def test_bruteforce(testjob_s):
orig_job = testjob_s.model_copy(deep=True)
solved = _solve_bruteforce(testjob_s)
def test_trimmings():
trimming = _get_trimmings(
max_length=1500,
lengths=(((500, ""), (500, ""), (400, "")), ((500, ""), (500, ""), (400, ""))),
cut_width=10,
)

assert solved == [
[(500, "Part1"), (500, "Part1"), (200, "Part2"), (200, "Part2")],
[(200, "Part2"), (200, "Part2")],
]
assert orig_job == testjob_s
assert trimming == 140


def test_gapfill(testjob_s):
@pytest.mark.parametrize("solver", [_solve_bruteforce, _solve_FFD, _solve_gapfill])
def test_solver_is_optimal(testjob_s, solver):
orig_job = testjob_s.model_copy(deep=True)
solved = _solve_gapfill(testjob_s)
solved = solver(testjob_s)

assert solved == [
[(500, "Part1"), (500, "Part1"), (200, "Part2"), (200, "Part2")],
[(200, "Part2"), (200, "Part2")],
]
assert _get_trimmings(orig_job.max_length, solved, orig_job.cut_width) == 1188
assert orig_job == testjob_s


def test_FFD(testjob_s):
@pytest.mark.parametrize("solver", [_solve_bruteforce, _solve_FFD, _solve_gapfill])
def test_solver_is_exactly(testjob_s, solver):
orig_job = testjob_s.model_copy(deep=True)
solved = _solve_FFD(testjob_s)
solved = solver(testjob_s)

# assert solved == [[500, 500, 200, 200], [200, 200]]
assert solved == [
[(500, "Part1"), (500, "Part1"), (200, "Part2"), (200, "Part2")],
[(200, "Part2"), (200, "Part2")],
]
assert orig_job == testjob_s


def test_full_model():
def test_full_solver():
json_job = Path("./tests/res/in/testjob_s.json")
assert json_job.exists()

Expand Down

0 comments on commit 2697094

Please sign in to comment.