Exemple #1
0
    def testSkyline(self):
        """
        Bin First Fit
        Skyline (bottom_left)
        Item Sorting == True
        Item Rotation == True
        """
        M = greedypacker.BinManager(10,
                                    5,
                                    pack_algo='skyline',
                                    heuristic='bottom_left',
                                    bin_algo='bin_first_fit',
                                    sorting=True,
                                    rotation=True)

        ITEM = greedypacker.Item(10, 3)
        ITEM2 = greedypacker.Item(10, 4)
        ITEM3 = greedypacker.Item(1, 1)
        M.add_items(ITEM, ITEM2, ITEM3)
        M.execute()
        correct = [ITEM, ITEM2, ITEM3]
        with self.subTest():
            self.assertCountEqual(M.items, correct)
        with self.subTest():
            self.assertEqual(ITEM.x, 0)
            self.assertEqual(ITEM.y, 0)
        with self.subTest():
            self.assertEqual(ITEM2.x, 0)
            self.assertEqual(ITEM2.y, 0)
        with self.subTest():
            self.assertEqual(ITEM3.x, 0)
            self.assertEqual(ITEM3.y, 4)
Exemple #2
0
 def testGuillotineBWFSortingRotation(self):
     """
     Best Bin Fit
     Guillotine Bins (best_area)
     Split Horizontal
     Item Sorting == True
     Item Rotation == True
     """
     M = greedypacker.BinManager(10,
                                 5,
                                 pack_algo='guillotine',
                                 bin_algo="bin_best_fit",
                                 heuristic='best_area',
                                 sorting=True,
                                 rotation=True)
     ITEM = greedypacker.Item(4, 3)
     ITEM2 = greedypacker.Item(5, 3)
     ITEM3 = greedypacker.Item(2, 2)
     M.add_items(ITEM, ITEM2, ITEM3)
     M.execute()
     correct = [ITEM2, ITEM, ITEM3]
     with self.subTest():
         self.assertEqual(M.items, correct)
     with self.subTest():
         self.assertEqual(ITEM.x, 5)
         self.assertEqual(ITEM.y, 0)
     with self.subTest():
         self.assertEqual(ITEM2.x, 0)
         self.assertEqual(ITEM2.y, 0)
     with self.subTest():
         self.assertEqual(ITEM3.x, 0)
         self.assertEqual(ITEM3.y, 3)
Exemple #3
0
    def testMaximalRectangleBSS(self):
        """
        Best Bin Fit
        Maximal Rectangle Bins (Best Short Side)
        Split Horizontal
        Item Sorting == False
        Item Rotation == False
        """
        M = greedypacker.BinManager(8,
                                    4,
                                    pack_algo='maximal_rectangle',
                                    bin_algo="bin_best_fit",
                                    heuristic='best_shortside',
                                    sorting=False,
                                    rotation=False)

        I = greedypacker.Item(1, 1)
        I2 = greedypacker.Item(2, 2)
        F0 = greedypacker.maximal_rectangles.FreeRectangle(7, 1, 1, 0)
        F1 = greedypacker.maximal_rectangles.FreeRectangle(8, 1, 0, 3)
        F2 = greedypacker.maximal_rectangles.FreeRectangle(6, 4, 2, 0)
        M.add_items(I, I2)
        M.execute()
        with self.subTest():
            self.assertCountEqual(M.bins[0].freerects, [F0, F1, F2])
        with self.subTest():
            self.assertEqual(I.x, 0)
            self.assertEqual(I.y, 0)
        with self.subTest():
            self.assertEqual(I2.x, 0)
            self.assertEqual(I2.y, 1)
Exemple #4
0
 def testShelfBWFRotation(self):
     """
     Best Bin Fit
     Shelf Bins (best_width_fit)
     Item Sorting == False
     Item Rotation == True
     """
     M = greedypacker.BinManager(10,
                                 5,
                                 pack_algo='shelf',
                                 bin_algo='bin_first_fit',
                                 heuristic='best_width_fit',
                                 sorting=False,
                                 rotation=True)
     ITEM = greedypacker.Item(1, 1)
     ITEM2 = greedypacker.Item(4, 3)
     ITEM3 = greedypacker.Item(2, 2)
     M.add_items(ITEM, ITEM2, ITEM3)
     M.execute()
     correct = [ITEM, ITEM2, ITEM3]
     with self.subTest():
         self.assertEqual(M.items, correct)
     with self.subTest():
         self.assertEqual(ITEM.x, 0)
         self.assertEqual(ITEM.y, 0)
     with self.subTest():
         self.assertEqual(ITEM2.x, 0)
         self.assertEqual(ITEM2.y, 1)
     with self.subTest():
         self.assertEqual(ITEM3.x, 4)
         self.assertEqual(ITEM3.y, 1)
Exemple #5
0
 def testReadme(self):
     """
     Example insertion from README.md
     """
     M = greedypacker.BinManager(8,
                                 4,
                                 pack_algo='shelf',
                                 heuristic='next_fit')
     ITEM = greedypacker.Item(4, 2)
     ITEM2 = greedypacker.Item(5, 2)
     ITEM3 = greedypacker.Item(2, 2)
     M.add_items(ITEM, ITEM2, ITEM3)
     M.execute()
     correct = [ITEM2, ITEM, ITEM3]
     with self.subTest():
         self.assertEqual(M.items, correct)
     with self.subTest():
         self.assertEqual(ITEM.x, 0)
         self.assertEqual(ITEM.y, 2)
     with self.subTest():
         self.assertEqual(ITEM2.x, 0)
         self.assertEqual(ITEM2.y, 0)
     with self.subTest():
         self.assertEqual(ITEM3.x, 5)
         self.assertEqual(ITEM3.y, 0)
Exemple #6
0
 def testItemTooBig(self):
     M = greedypacker.BinManager(8,
                                 4,
                                 pack_algo='skyline',
                                 bin_algo='bin_best_fit')
     I = greedypacker.Item(10, 20)
     with self.assertRaises(ValueError):
         M.add_items(I)
         M.execute()
Exemple #7
0
    def testShelfBigInsert(self):
        M = greedypacker.BinManager(2,
                                    4,
                                    pack_algo='shelf',
                                    heuristic='best_width_fit',
                                    wastemap=True,
                                    rotation=True)

        I1 = greedypacker.item.Item(1, 1)
        I2 = greedypacker.item.Item(2, 1)
        I3 = greedypacker.item.Item(2, 2)
        M.add_items(*[I1, I2, I3])
        M.execute()

        self.assertCountEqual(M.items, [I1, I2, I3])
Exemple #8
0
 def testShelfBWFRotationIdenticalItems(self):
     """
     Manually insert two identical items
     """
     ITEM = greedypacker.Item(1, 2)
     ITEM2 = greedypacker.Item(1, 2)
     M = greedypacker.BinManager(10,
                                 5,
                                 pack_algo='shelf',
                                 bin_algo='bin_first_fit',
                                 heuristic='best_width_fit',
                                 sorting_heuristic='ASCA',
                                 rotation=True)
     M.add_items(ITEM, ITEM2)
     M.execute()
    plt.legend(handles=handles, bbox_to_anchor=(1.04, 1), loc="upper left")

    if save:
        plt.savefig('%s Algorithm - %r Heuristic' % (M.pack_algo, M.heuristic),
                    bbox_inches="tight",
                    dpi=150)
    else:
        plt.show()
    return


if __name__ == '__main__':
    #M = g.BinManager(10, 6, pack_algo='maximal_rectangle', heuristic='bottom_left', rotation=False, sorting=False, wastemap=False)
    M = g.BinManager(10,
                     6,
                     pack_algo='guillotine',
                     heuristic='best_shortside',
                     rotation=False,
                     sorting=False)
    guillotine = [
        g.Item(2, 3),
        g.Item(2, 2),
        g.Item(2, 1),
        g.Item(2, 3),
        g.Item(2, 2),
        g.Item(3, 2)
    ]
    maximal = [
        g.Item(2, 3),
        g.Item(3, 3),
        g.Item(4, 1),
        g.Item(2, 3),
Exemple #10
0
    g.Item(270, 150, 45, 0, 0, 45),
    g.Item(270, 150, 0, 45, 45, 0),
    g.Item(250, 150, 0, 45, 0, 45),
    g.Item(250, 150, 45, 0, 45, 0),
    g.Item(250, 150, 45, 0, 0, 45),
    g.Item(250, 150, 0, 45, 45, 0)
    """

    result = []
    #['best_area', 'best_shortside', 'best_longside', 'worst_area', 'worst_shortside', 'worst_longside', 'bottom_left', 'contact_point']
    for heur in ['bottom_left']:
        for c in itertools.permutations(maximal):
            M = g.BinManager(700,
                             500,
                             pack_algo='maximal_rectangle',
                             heuristic=heur,
                             rotation=False,
                             sorting=False,
                             wastemap=False)
            M.add_items(*list(c))
            M.execute()
            print(M.bins[0].bin_stats()['free_area'])
            #render_bin(M)
            result += [(M.bins[0].bin_stats(), M)]

    # select M with more efficiency
    best_bin = max(result, key=lambda x: x[0]['efficiency'])
    render_bin(best_bin[1], save=True)
    """
    g.Item(300, 150, 0, 90, 0, 90), g.Item(270, 150, 0, 90, 0, 90), g.Item(250, 150, 0, 90, 0, 90),
    g.Item(300, 150, 90, 0, 90, 0), g.Item(270, 150, 90, 0, 90, 0), g.Item(250, 150, 90, 0, 90, 0),
Exemple #11
0
 def testNoSuchAlgo(self):
     with self.assertRaises(ValueError):
         M = greedypacker.BinManager(8, 4, pack_algo='foo')
Exemple #12
0
    AMOUNT_ITERATES = 4
    for i in range(AMOUNT_ITERATES):
        if i % 2 == 0:
            new_point = create_point(points[i], sizes[i], 0)
            points.append(new_point)
        else:
            new_point = create_point(points[i], sizes[i], 1)
            points.append(new_point)
    points = create_array_of_double(two_demention_list_to_list(points))
    ms.AddPolyline(points)
    return points


M = greedypacker.BinManager(20,
                            10,
                            pack_algo='maximal_rectangle',
                            heuristic='bottom_left',
                            rotation=True)

ITEM = greedypacker.Item(5, 6)
ITEM4 = greedypacker.Item(3, 2)
# ITEM2 = greedypacker.Item(1320, 700)
# ITEM3 = greedypacker.Item(1320, 720)
# ITEM5 = greedypacker.Item(1000, 670)

M.add_items(ITEM, ITEM4)

M.execute()


def draw_packs():
    plt.legend(handles=handles, bbox_to_anchor=(1.04, 1), loc="upper left")

    if save:
        plt.savefig('%s Algorithm - %r Heuristic' % (M.pack_algo, M.heuristic),
                    bbox_inches="tight",
                    dpi=150)
    else:
        plt.show()
    return


if __name__ == '__main__':
    M = g.BinManager(10,
                     6,
                     pack_algo='maximal_rectangle',
                     heuristic='bottom_left',
                     rotation=False,
                     sorting=False,
                     wastemap=False)
    guillotine = [
        g.Item(2, 3),
        g.Item(2, 2),
        g.Item(2, 1),
        g.Item(2, 3),
        g.Item(2, 2),
        g.Item(3, 2)
    ]
    maximal = [
        g.Item(2, 3),
        g.Item(3, 3),
        g.Item(4, 1),
Exemple #14
0
    return img


## generate all mini images
minis = []
for m in do_these:
    print('Doing {}...'.format(m))
    mini = create_mini(m)
    if not isinstance(mini, str):
        minis.append(mini)
    else:
        print('{} skipped with error: {}'.format(m, mini))

## optimization algorithm to align images on canvas
M = greedypacker.BinManager(canvas[0], canvas[1], bin_algo='bin_best_fit', pack_algo='shelf', heuristic='best_area_fit',
                            split_heuristic='default', rotation=True, rectangle_merge=True, wastemap=True, sorting=True,
                            sorting_heuristic='DESCA')

its = {}
item_id = 0
for it in minis:
    its[item_id] = it
    item = greedypacker.Item(it.shape[1], it.shape[0])
    item.id = item_id
    M.add_items(item)
    item_id += 1

M.execute()
result = M.bins

## Create sheets