Coverage for moptipyapps / binpacking2d / instgen / problem.py: 93%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-11 04:40 +0000

1"""An instance of the instance generation problem.""" 

2 

3from typing import Final 

4 

5from moptipy.api.component import Component 

6from moptipy.spaces.vectorspace import VectorSpace 

7 

8from moptipyapps.binpacking2d.instance import Instance 

9from moptipyapps.binpacking2d.instgen.inst_decoding import InstanceDecoder 

10from moptipyapps.binpacking2d.instgen.instance_space import InstanceSpace 

11 

12 

13class Problem(Component): 

14 """An instance of the 2D Bin Packing Instance Generation Problem.""" 

15 

16 def __init__(self, name: str, slack: float) -> None: 

17 """ 

18 Create an instance of the 2D bin packing instance generation problem. 

19 

20 :param name: the name of the instance to select 

21 :param slack: the additional fraction of slack 

22 """ 

23 super().__init__() 

24 #: the instance to be used as template 

25 self.solution_space: Final[InstanceSpace] = InstanceSpace( 

26 Instance.from_resource(name)) 

27 #: the internal decoding 

28 self.encoding: Final[InstanceDecoder] = InstanceDecoder( 

29 self.solution_space) 

30 n: Final[int] = self.encoding.get_x_dim(slack) 

31 #: the search space 

32 self.search_space: Final[VectorSpace] = VectorSpace(n, -1.0, 1.0) 

33 

34 def __str__(self) -> str: 

35 """ 

36 Get the string representation of this problem. 

37 

38 :return: the string representation of this problem 

39 """ 

40 return (f"{self.solution_space.inst_name}_" 

41 f"{self.search_space.dimension}")