def test_has_collision_with_overlapping_rectangles(self) -> None: item_a = Rectangle(6, 2) item_b = Rectangle(2, 6) instance = BinPacking2D(Rectangle(10, 10), [item_a, item_b]) sol = Solution(instance.get_instance_size()) sol[0] = Coordinate(2, 3) sol[1] = Coordinate(4, 1) self.assertTrue(instance.has_collision(sol[0], item_a, sol[1], item_b)) self.assertTrue(instance.has_collision(sol[1], item_b, sol[0], item_a)) item_a = Rectangle(2, 4) item_b = Rectangle(4, 2) instance = BinPacking2D(Rectangle(10, 10), [item_a, item_b]) sol[0] = Coordinate(2, 2) sol[1] = Coordinate(1, 3) self.assertTrue(instance.has_collision(sol[0], item_a, sol[1], item_b)) self.assertTrue(instance.has_collision(sol[1], item_a, sol[0], item_b))
from binpacking.solver.bin_packing_2d import BinPacking2D, Rectangle from binpacking.solver.data_structure.solution import Solution, Coordinate from binpacking.plot.plot_handler import PlotHandler if __name__ == '__main__': print('Processing and printing to file...') instance = BinPacking2D( Rectangle(5, 5), [Rectangle(1, 1), Rectangle(1, 1), Rectangle(1, 1), Rectangle(1, 1)]) sol = Solution(instance.get_instance_size()) sol[0] = Coordinate(0, 0) sol[1] = Coordinate(1, 1) sol[2] = Coordinate(2, 2) sol[3] = Coordinate(3, 3) plot_handler = PlotHandler(instance, sol) results_filepath = plot_handler.save_to_file('test1_plt.png') instance = BinPacking2D( Rectangle(5, 5), [Rectangle(1, 1), Rectangle(1, 1), Rectangle(1, 1), Rectangle(1, 1)]) sol = Solution(instance.get_instance_size()) sol[0] = Coordinate(0, 0) sol[1] = Coordinate(1, 0)
def test_solution_size(self) -> None: expected_size: int = 3 sol = Solution(expected_size) self.assertEqual(len(sol), expected_size)
def test_fitness_getter_setter(self) -> None: size: int = 3 sol = Solution(size) fitness: float = 42.42 sol.set_fitness(fitness) self.assertEqual(sol.get_fitness(), fitness)
def run(self, sol: Solution) -> Dict[str, Solution]: if not self.sol_star.has_valid_fitness(): self.sol_star = copy.deepcopy(sol) elif self.sol_star.get_fitness() < sol.get_fitness(): self.sol_star = copy.deepcopy(sol) return {'sol_star': self.sol_star}
def __init__(self) -> None: self.sol_star: Solution = Solution(1)
def run(self, sol: Solution) -> Dict[str, float]: return {'fitness': sol.get_fitness()}