pack_boxes#
- m4opt.utils.optimization.pack_boxes(wh, **kwargs)[source] [edit on github]#
Pack non-overlapping hypercubes into the smallest possible hypercube.
- Parameters:
wh (ndarray) – A Numpy array of shape
(n, m)containing the dimensions ofnhypercubes inmdimensions.kwargs – Additional arguments passed to
m4opt.milp.Model.
- Returns:
The anchor points of the rectangles, and the total dimensions.
- Return type:
Examples
import numpy as np from matplotlib import pyplot as plt from m4opt.utils.optimization import pack_boxes rng = np.random.RandomState(seed=42) for n in range(1, 5): wh = rng.uniform(size=(n, 2)) xy, (total_width, total_height) = pack_boxes(wh) fig, ax = plt.subplots(subplot_kw=dict(aspect=1)) ax.set_xlim(0, total_width) ax.set_ylim(0, total_height) for xy_, wh_ in zip(xy, wh): ax.add_patch(plt.Rectangle(xy_, *wh_, edgecolor="black"))