def test_solver_progress_bar(example_problem, capfd): # noqa: F811 problem = rps.Problem(rectangles=example_problem) solver = rps.Solver() _ = solver.solve(problem=problem, show_progress=True) out, err = capfd.readouterr() assert out == "" assert "Progress: 100%" in err
def test_visualizer(example_problem): # noqa: F811 problem = rps.Problem(rectangles=example_problem) solver = rps.Solver() solution = solver.solve(problem=problem, simanneal_minutes=0.01, simanneal_steps=10) rps.Visualizer().visualize(solution=solution, path="./floorplan.png") mimetype = mimetypes.guess_type("./floorplan.png")[0] assert mimetype == "image/png"
def test_solver_example_large_problem(example_large_problem): # noqa: F811 problem = rps.Problem(rectangles=example_large_problem) solver = rps.Solver() solution = solver.solve(problem=problem, simanneal_minutes=0.2, simanneal_steps=200) assert isinstance(solution, rps.Solution) assert isinstance(solution.sequence_pair, rps.SequencePair) assert isinstance(solution.floorplan, rps.Floorplan) # Not so bad solution assert solution.floorplan.area < 6000
def main(): # Define a problem problem = rps.Problem(rectangles=[(0.1 * i, 0.1 * i) for i in range(100, 200, 5)]) print("problem:", problem) # Find a solution print("\n=== Solving without width/height constraints ===") solution = rps.Solver().solve( problem=problem, simanneal_minutes=1.0, simanneal_steps=500, show_progress=True, seed=1234 ) print("solution:", solution) # Visualization (to floorplan_large.png) rps.Visualizer().visualize(solution=solution, path="./figs/floorplan_large.png") # Find a solution (with limit) print("\n=== Solving with a width/height constraints ===") solution = rps.Solver().solve( problem=problem, simanneal_minutes=1.0, simanneal_steps=500, height_limit=50.0, show_progress=True, seed=1234 ) print("solution:", solution) rps.Visualizer().visualize(solution=solution, path="./figs/floorplan_large_limit.png")
def test_solver_with_tight_limits(width_limit, height_limit): # Note: See PR #23 for details. problem = rps.Problem( rectangles=[ {"width": 5, "height": 7, "rotatable": True}, {"width": 5, "height": 7, "rotatable": True}, {"width": 5, "height": 7, "rotatable": True}, {"width": 5, "height": 7, "rotatable": True}, {"width": 5, "height": 7, "rotatable": True}, ] ) solution = rps.Solver().solve(problem=problem, width_limit=width_limit, height_limit=height_limit) assert solution.floorplan.bounding_box[0] <= width_limit assert solution.floorplan.bounding_box[1] <= height_limit
def main(): # Define a problem problem = rps.Problem(rectangles=[ [4, 6], # Format: [width, height] as list. Default rotatable: False (4, 4), # Format: (width, height) as tuple. Default rotatable: False { "width": 2.1, "height": 3.2, "rotatable": False }, # Or can be defined as dict. { "width": 1, "height": 5, "rotatable": True }, ]) print("problem:", problem) # Find a solution print("\n=== Solving without width/height constraints ===") solution = rps.Solver().solve(problem=problem) print("solution:", solution) # Visualization (to floorplan.png) rps.Visualizer().visualize(solution=solution, path="./figs/floorplan_example.png") # [Other Usages] # We can also give a solution width (and/or height) limit print("\n=== Solving with width/height constraints ===") solution = rps.Solver().solve(problem=problem, height_limit=6.5, show_progress=True, seed=1111) print("solution:", solution) rps.Visualizer().visualize(solution=solution, path="./figs/floorplan_example_limit.png")
def main(): random.seed(12345) n_rectangles = list(range(4, 10)) + list(range(10, 50, 10)) for n in n_rectangles: print("\n================") print("N =", n) rectangles = generate_rectangles(n=n) problem = rps.Problem(rectangles=rectangles) print("problem:", problem) solver = rps.Solver() start = time.time() solution = solver.solve(problem, simanneal_minutes=0.1, simanneal_steps=100) elapsed_time = time.time() - start print("solution:", solution) print(solution.floorplan.bounding_box, solution.floorplan.area) print("elapsed_time:", elapsed_time)
def test_solver_example_problem(example_problem): # noqa: F811 problem = rps.Problem(rectangles=example_problem) solver = rps.Solver() solution = solver.solve(problem=problem) assert isinstance(solution, rps.Solution) assert isinstance(solution.sequence_pair, rps.SequencePair) assert isinstance(solution.floorplan, rps.Floorplan) # The optimal solution is any of # ([0, 1, 3, 2], [3, 0, 2, 1]), # ([1, 2, 0, 3], [2, 3, 1, 0]), # ([2, 3, 1, 0], [1, 2, 0, 3]), or # ([3, 0, 2, 1], [0, 1, 3, 2]) optimal_0 = solution.sequence_pair.pair == ([0, 1, 3, 2], [3, 0, 2, 1]) optimal_1 = solution.sequence_pair.pair == ([1, 2, 0, 3], [2, 3, 1, 0]) optimal_2 = solution.sequence_pair.pair == ([2, 3, 1, 0], [1, 2, 0, 3]) optimal_3 = solution.sequence_pair.pair == ([3, 0, 2, 1], [0, 1, 3, 2]) assert optimal_0 or optimal_1 or optimal_2 or optimal_3 assert solution.floorplan.bounding_box == (8, 7.2) assert solution.floorplan.area == 57.6 assert solution.floorplan.bounding_box[0] * solution.floorplan.bounding_box[1] == solution.floorplan.area
def test_solver_example_large_problem_with_height(example_large_problem): # noqa: F811 problem = rps.Problem(rectangles=example_large_problem) solver = rps.Solver() solution = solver.solve(problem=problem, simanneal_minutes=0.2, simanneal_steps=200, height_limit=50.0) assert solution.floorplan.bounding_box[1] <= 50.0
def test_solver_with_height_limit(example_problem): # noqa: F811 problem = rps.Problem(rectangles=example_problem) solver = rps.Solver() solution = solver.solve(problem=problem, height_limit=6.5) assert solution.floorplan.bounding_box[1] <= 6.5
def test_solver_with_invalid_height_limit(example_problem): # noqa: F811 problem = rps.Problem(rectangles=example_problem) solver = rps.Solver() with pytest.raises(ValueError) as e: solver.solve(problem=problem, height_limit=5.5) assert "'height_limit' must be greater than or equal to 6" in str(e.value)