@File : bin_packing.py @Time : 2024/06/18 09:15:05 @Author : Alejandro Marrero @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2024, Alejandro Marrero @Desc : None

BPP

Bases: Problem

Source code in digneapy/domains/bpp.py
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
class BPP(Problem):
    def __init__(
        self,
        items: Iterable[int],
        capacity: int,
        seed: int = 42,
        *args,
        **kwargs,
    ):
        self._items = tuple(items)
        self._capacity = capacity
        dim = len(self._items)
        assert len(self._items) > 0
        assert self._capacity > 0

        bounds = list((0, dim - 1) for _ in range(dim))
        super().__init__(dimension=dim, bounds=bounds, name="BPP", seed=seed)

    def evaluate(self, individual: Sequence | Solution) -> tuple[float]:
        """Evaluates the candidate individual with the information of the Bin Packing.
        The fitness of the solution is the amount of unused space, as well as the
        number of bins for a specific solution. Falkenauer (1998) performance metric
        defined as:
            (x) = \\frac{\\sum_{k=1}^{N} \\left(\\frac{fill_k}{C}\\right)^2}{N}

        Args:
            individual (Sequence | Solution): Individual to evaluate

        Returns:
            Tuple[float]: Falkenauer Fitness
        """
        if len(individual) != self._dimension:
            msg = f"Mismatch between individual variables ({len(individual)}) and instance variables ({self._dimension}) in {self.__class__.__name__}"
            raise ValueError(msg)

        used_bins = np.max(individual).astype(int) + 1
        fill_i = np.zeros(used_bins)

        for item_idx, bin in enumerate(individual):
            fill_i[bin] += self._items[item_idx]

        fitness = (
            sum(((f_i / self._capacity) * (f_i / self._capacity)) for f_i in fill_i)
            / used_bins
        )
        if isinstance(individual, Solution):
            individual.fitness = fitness
            individual.objectives = (fitness,)

        return (fitness,)

    def __call__(self, individual: Sequence | Solution) -> tuple[float]:
        return self.evaluate(individual)

    def __repr__(self):
        return f"BPP<n={self._dimension},C={self._capacity},I={self._items}>"

    def __len__(self):
        return self._dimension

    def __array__(self, dtype=np.int32, copy: Optional[bool] = False) -> npt.ArrayLike:
        return np.asarray([self._capacity, *self._items], dtype=dtype, copy=copy)

    def create_solution(self) -> Solution:
        items = list(range(self._dimension))
        return Solution(variables=items)

    def to_file(self, filename: str = "instance.bpp"):
        with open(filename, "w") as file:
            file.write(f"{len(self)}\t{self._capacity}\n\n")
            content = "\n".join(str(i) for i in self._items)
            file.write(content)

    @classmethod
    def from_file(cls, filename: str):
        with open(filename) as f:
            lines = f.readlines()
            lines = [line.rstrip() for line in lines]

        (_, capacity) = lines[0].split()
        items = list(int(i) for i in lines[2:])

        return cls(items=items, capacity=int(capacity))

    def to_instance(self) -> Instance:
        _vars = [self._capacity, *self._items]
        return Instance(variables=_vars)

evaluate(individual)

Evaluates the candidate individual with the information of the Bin Packing. The fitness of the solution is the amount of unused space, as well as the number of bins for a specific solution. Falkenauer (1998) performance metric defined as: (x) = \frac{\sum_{k=1}^{N} \left(\frac{fill_k}{C}\right)^2}{N}

Parameters:
  • individual (Sequence | Solution) –

    Individual to evaluate

Returns:
  • tuple[float]

    Tuple[float]: Falkenauer Fitness

Source code in digneapy/domains/bpp.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def evaluate(self, individual: Sequence | Solution) -> tuple[float]:
    """Evaluates the candidate individual with the information of the Bin Packing.
    The fitness of the solution is the amount of unused space, as well as the
    number of bins for a specific solution. Falkenauer (1998) performance metric
    defined as:
        (x) = \\frac{\\sum_{k=1}^{N} \\left(\\frac{fill_k}{C}\\right)^2}{N}

    Args:
        individual (Sequence | Solution): Individual to evaluate

    Returns:
        Tuple[float]: Falkenauer Fitness
    """
    if len(individual) != self._dimension:
        msg = f"Mismatch between individual variables ({len(individual)}) and instance variables ({self._dimension}) in {self.__class__.__name__}"
        raise ValueError(msg)

    used_bins = np.max(individual).astype(int) + 1
    fill_i = np.zeros(used_bins)

    for item_idx, bin in enumerate(individual):
        fill_i[bin] += self._items[item_idx]

    fitness = (
        sum(((f_i / self._capacity) * (f_i / self._capacity)) for f_i in fill_i)
        / used_bins
    )
    if isinstance(individual, Solution):
        individual.fitness = fitness
        individual.objectives = (fitness,)

    return (fitness,)

BPPDomain

Bases: Domain

Source code in digneapy/domains/bpp.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
class BPPDomain(Domain):
    __capacity_approaches = ("evolved", "percentage", "fixed")
    __feat_names = names = "mean,std,median,max,min,tiny,small,medium,large,huge".split(
        ","
    )

    def __init__(
        self,
        dimension: int = 50,
        min_i: int = 1,
        max_i: int = 1000,
        capacity_approach: str = "fixed",
        max_capacity: int = 100,
        capacity_ratio: float = 0.8,
        seed: int = 42,
    ):
        if dimension < 0:
            raise ValueError(f"Expected dimension > 0 got {dimension}")
        if min_i < 0:
            raise ValueError(f"Expected min_i > 0 got {min_i}")
        if max_i < 0:
            raise ValueError(f"Expected max_i > 0 got {max_i}")
        if min_i > max_i:
            raise ValueError(
                f"Expected min_i to be less than max_i got ({min_i}, {max_i})"
            )

        self._dimension = dimension
        self._min_i = min_i
        self._max_i = max_i
        self._max_capacity = max_capacity

        if capacity_ratio < 0.0 or capacity_ratio > 1.0 or not float(capacity_ratio):
            self.capacity_ratio = 0.8  # Default
            msg = "The capacity ratio must be a float number in the range [0.0-1.0]. Set as 0.8 as default."
            print(msg)
        else:
            self.capacity_ratio = capacity_ratio

        if capacity_approach not in self.__capacity_approaches:
            msg = f"The capacity approach {capacity_approach} is not available. Please choose between {self.__capacity_approaches}. Evolved approach set as default."
            print(msg)
            self._capacity_approach = "fixed"
        else:
            self._capacity_approach = capacity_approach

        bounds = [(1.0, self._max_capacity)] + [
            (self._min_i, self._max_i) for _ in range(self._dimension)
        ]
        super().__init__(dimension=dimension, bounds=bounds, name="BPP", seed=seed)

    @property
    def capacity_approach(self):
        return self._capacity_approach

    @capacity_approach.setter
    def capacity_approach(self, app):
        """Setter for the Maximum capacity generator approach.
        It forces to update the variable to one of the specify values

        Args:
            app (str): Approach for setting the capacity. It should be fixed, evolved or percentage.
        """
        if app not in self.__capacity_approaches:
            msg = f"The capacity approach {app} is not available. Please choose between {self.__capacity_approaches}. Evolved approach set as default."
            print(msg)
            self._capacity_approach = "fixed"
        else:
            self._capacity_approach = app

    def generate_instances(self, n: int = 1) -> List[Instance]:
        """Generates N instances for the domain.

        Args:
            n (int, optional): Number of instances to generate. Defaults to 1.

        Returns:
            List[Instance]: A list of Instance objects created from the raw numpy generation
        """
        instances = np.empty(shape=(n, self.dimension + 1), dtype=np.int32)
        instances = self._rng.integers(
            low=self._min_i, high=self._max_i, size=(n, self._dimension + 1), dtype=int
        )
        # Sets the capacity according to the method
        match self.capacity_approach:
            case "evolved":
                instances[:, 0] = self._rng.integers(1, self._max_capacity, size=n)
            case "percentage":
                instances[:, 0] = (
                    np.sum(instances[:, 1:], axis=1, dtype=int) * self.capacity_ratio
                )
            case "fixed":
                instances[:, 0] = self._max_capacity
        return list(Instance(i) for i in instances)

    def extract_features(self, instances: Sequence[Instance]) -> np.ndarray:
        """Extract the features of the instance based on the BPP domain.
           For the BPP the features are:
           N, Capacity, MeanWeights, MedianWeights, VarianceWeights, MaxWeight,
           MinWeight, Huge, Large, Medium, Small, Tiny

        Args:
            instances (Instance): Instances to extract the features from

        Returns:
            np.ndarray: Values of each feature
        """
        if not isinstance(instances, np.ndarray):
            instances = np.asarray(instances)

        norm_variables = np.asarray(instances, copy=True)
        norm_variables[:, 1:] = norm_variables[:, 1:] / norm_variables[:, [0]]

        return np.column_stack(
            [
                np.mean(norm_variables, axis=1),
                np.std(norm_variables, axis=1),
                np.median(norm_variables, axis=1),
                np.max(norm_variables, axis=1),
                np.min(norm_variables, axis=1),
                np.mean(norm_variables > 0.5, axis=1),  # Huge
                np.mean(
                    (0.5 >= norm_variables) & (norm_variables > 0.33333333333), axis=1
                ),
                np.mean(
                    (0.33333333333 >= norm_variables) & (norm_variables > 0.25), axis=1
                ),
                np.mean(0.25 >= norm_variables, axis=1),  # Small
                np.mean(0.1 >= norm_variables, axis=1),  # Tiny
            ],
        ).astype(np.float32)

    def extract_features_as_dict(
        self, instances: Sequence[Instance]
    ) -> List[Dict[str, np.float32]]:
        """Creates a dictionary with the features of the instances.
        The key are the names of each feature and the values are
        the values extracted from instance.

        Args:
            instances (Sequence[Instance]): Instances to extract the features from.
        Returns:
            Dict[str, np.float32]: Dictionary with the names/values of each feature
        """
        features = self.extract_features(instances)
        named_features: list[dict[str, np.float32]] = [{}] * len(features)
        for i, feats in enumerate(features):
            named_features[i] = {k: v for k, v in zip(BPPDomain.__feat_names, feats)}

        return named_features

    def generate_problems_from_instances(
        self, instances: Sequence[Instance]
    ) -> List[Problem]:
        if not isinstance(instances, np.ndarray):
            instances = np.asarray(instances)

        # Assume evolved capacities
        capacities = instances[:, 0].astype(np.int32)
        match self.capacity_approach:
            case "percentage":
                capacities[:] = (
                    np.sum(instances[:, 1:], axis=1) * self.capacity_ratio
                ).astype(np.int32)
            case "fixed":
                capacities[:] = self._max_capacity
        return list(
            BPP(items=instances[i, 1:], capacity=capacities[i])
            for i in range(len(instances))
        )

extract_features(instances)

Extract the features of the instance based on the BPP domain. For the BPP the features are: N, Capacity, MeanWeights, MedianWeights, VarianceWeights, MaxWeight, MinWeight, Huge, Large, Medium, Small, Tiny

Parameters:
  • instances (Instance) –

    Instances to extract the features from

Returns:
  • ndarray

    np.ndarray: Values of each feature

Source code in digneapy/domains/bpp.py
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
def extract_features(self, instances: Sequence[Instance]) -> np.ndarray:
    """Extract the features of the instance based on the BPP domain.
       For the BPP the features are:
       N, Capacity, MeanWeights, MedianWeights, VarianceWeights, MaxWeight,
       MinWeight, Huge, Large, Medium, Small, Tiny

    Args:
        instances (Instance): Instances to extract the features from

    Returns:
        np.ndarray: Values of each feature
    """
    if not isinstance(instances, np.ndarray):
        instances = np.asarray(instances)

    norm_variables = np.asarray(instances, copy=True)
    norm_variables[:, 1:] = norm_variables[:, 1:] / norm_variables[:, [0]]

    return np.column_stack(
        [
            np.mean(norm_variables, axis=1),
            np.std(norm_variables, axis=1),
            np.median(norm_variables, axis=1),
            np.max(norm_variables, axis=1),
            np.min(norm_variables, axis=1),
            np.mean(norm_variables > 0.5, axis=1),  # Huge
            np.mean(
                (0.5 >= norm_variables) & (norm_variables > 0.33333333333), axis=1
            ),
            np.mean(
                (0.33333333333 >= norm_variables) & (norm_variables > 0.25), axis=1
            ),
            np.mean(0.25 >= norm_variables, axis=1),  # Small
            np.mean(0.1 >= norm_variables, axis=1),  # Tiny
        ],
    ).astype(np.float32)

extract_features_as_dict(instances)

Creates a dictionary with the features of the instances. The key are the names of each feature and the values are the values extracted from instance.

Parameters:
  • instances (Sequence[Instance]) –

    Instances to extract the features from.

Returns: Dict[str, np.float32]: Dictionary with the names/values of each feature

Source code in digneapy/domains/bpp.py
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def extract_features_as_dict(
    self, instances: Sequence[Instance]
) -> List[Dict[str, np.float32]]:
    """Creates a dictionary with the features of the instances.
    The key are the names of each feature and the values are
    the values extracted from instance.

    Args:
        instances (Sequence[Instance]): Instances to extract the features from.
    Returns:
        Dict[str, np.float32]: Dictionary with the names/values of each feature
    """
    features = self.extract_features(instances)
    named_features: list[dict[str, np.float32]] = [{}] * len(features)
    for i, feats in enumerate(features):
        named_features[i] = {k: v for k, v in zip(BPPDomain.__feat_names, feats)}

    return named_features

generate_instances(n=1)

Generates N instances for the domain.

Parameters:
  • n (int, default: 1 ) –

    Number of instances to generate. Defaults to 1.

Returns:
  • List[Instance]

    List[Instance]: A list of Instance objects created from the raw numpy generation

Source code in digneapy/domains/bpp.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def generate_instances(self, n: int = 1) -> List[Instance]:
    """Generates N instances for the domain.

    Args:
        n (int, optional): Number of instances to generate. Defaults to 1.

    Returns:
        List[Instance]: A list of Instance objects created from the raw numpy generation
    """
    instances = np.empty(shape=(n, self.dimension + 1), dtype=np.int32)
    instances = self._rng.integers(
        low=self._min_i, high=self._max_i, size=(n, self._dimension + 1), dtype=int
    )
    # Sets the capacity according to the method
    match self.capacity_approach:
        case "evolved":
            instances[:, 0] = self._rng.integers(1, self._max_capacity, size=n)
        case "percentage":
            instances[:, 0] = (
                np.sum(instances[:, 1:], axis=1, dtype=int) * self.capacity_ratio
            )
        case "fixed":
            instances[:, 0] = self._max_capacity
    return list(Instance(i) for i in instances)