Skip to content

Instantly share code, notes, and snippets.

@dsoprea
Last active December 22, 2024 22:08
Show Gist options
  • Save dsoprea/ef8b5a1a565427203b5b0897d7283426 to your computer and use it in GitHub Desktop.
Save dsoprea/ef8b5a1a565427203b5b0897d7283426 to your computer and use it in GitHub Desktop.
Produce all possible groups given a list of items. We provide a box-fitting algorithm. We provide all tests.
"""
https://gist.github.com/dsoprea/ef8b5a1a565427203b5b0897d7283426
Copyright 2024 Dustin Oprea
MIT LICENSE
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import itertools
import uuid
def permute_items_into_groups_of_all_sizes_gen(
items, groups=None, level=0, visited_s=None):
"""Yields all possible groupings of all possible sizes from the given list
of items.
The ordering is stable, so the permutations will be deterministic.
"""
# NOTE(dustin): Supports analysis
# invocation = str(uuid.uuid4())[:8]
if groups is None:
groups = []
# Sometimes the higher and lower invocations can produce the same sequences,
# so we filter those out
if visited_s is None:
visited_s = set()
prefix = ' ' * level
# NOTE(dustin): Supports analysis
# print("[{}] {}TOP: ITEMS={} GROUPS={}".format(invocation, prefix, items, groups))
if not items:
# Base step
# NOTE(dustin): Supports analysis
# print("[{}] {}<- Return: GROUPS={}".format(invocation, prefix, groups))
# print('')
# Guarding with this condition supports the special case of being given
# an empty set from the beginning
if groups:
groups = tuple(groups)
if groups not in visited_s:
visited_s.add(groups)
yield groups
return
len_ = len(items)
# Traverse all possible orders of the remaining items
subpermutations = itertools.permutations(items)
for p in subpermutations:
# Use all lengths of prefixes of the permutation for the next group
for i in range(len_):
left, right = p[:i + 1], p[i + 1:]
# NOTE(dustin): Supports analysis
# print("[{}] {}< P={} I={} G={} L={} R={}".format(invocation, prefix, p, i, groups, left, right))
# Combine the new group and the previous groups and forward the
# remaining items following the prefix to the next invocation
children = \
permute_items_into_groups_of_all_sizes_gen(
right,
groups=groups + [tuple(left)],
level=level + 1,
visited_s=visited_s)
yield from children
def permute_items_into_groups_of_all_sizes__unique__gen(items, **kwargs):
"""Similar to `permute_items_into_groups_of_all_sizes_gen()` but exclude all
permutations where the groups match another permutation save the order of
the items in each groups. Useful for box-fitting where the order of items
going into the box doesn't matter.
"""
permutations = permute_items_into_groups_of_all_sizes_gen(items, **kwargs)
visited_s = set()
for permutation in permutations:
# Everything must be tupleized in order to use the set
t = tuple([
tuple(sorted(group))
for group
in permutation
])
if t not in visited_s:
visited_s.add(t)
yield t
def dynamic(
items, group_cost_cb, cached_costs, attempts_cb=None,
system_cost_cb=None):
"""A simple dynamic-programming runner to determine the total costs of all
possible groupings of these items, and to partially cache the results for
next time.
`system_cost_cb` can be provided to assign additional costs based on all
final groups. For example, to assign a costs associated with the number of
groups in order to incentivize a lesser number.
The permutations are deterministic, so the cost is as well.
If the price is a given group is determined only by the items within it, the
ordering will depend on the system cost of the set of final groups. If there
is no system cost callback (or the system cost is the same for every
permutation) then the choice of groupings should be considered arbitrary and
the final cost will always be the same.
"""
assert \
items, \
"No items given."
permutations = permute_items_into_groups_of_all_sizes__unique__gen(items)
candidate_cost = 0.0
candidate_permutation = None
for p in permutations:
total = 0.0
attempt_components = []
for group in p:
cost = cached_costs.get(group)
if cost is None:
cost = group_cost_cb(group)
cached_costs[group] = cost
if attempts_cb is not None:
attempt_components.append((group, cost))
total += cost
if system_cost_cb is None:
system_cost = None
else:
system_cost = system_cost_cb(p, total)
total += system_cost
if attempts_cb is not None:
attempts_cb(attempt_components, system_cost, total)
if candidate_permutation is None or candidate_cost > total:
candidate_cost = total
candidate_permutation = p
return candidate_permutation, candidate_cost
# import unittest
# class Test(unittest.TestCase):
# maxDiff = None
# def test_permute_items_into_groups_of_all_sizes_gen(self):
# # Test with 0
# items = []
# permutations = \
# permute_items_into_groups_of_all_sizes_gen(
# items)
# expected = []
# self.assertEquals(sorted(permutations), expected)
# # Test with 1
# items = [11]
# permutations = \
# permute_items_into_groups_of_all_sizes_gen(
# items)
# expected = [
# ((11,),),
# ]
# self.assertEquals(sorted(permutations), expected)
# # Test with 2
# items = [11, 22]
# permutations = \
# permute_items_into_groups_of_all_sizes_gen(
# items)
# expected = [
# ((11,), (22,)),
# ((11, 22),),
# ((22,), (11,)),
# ((22, 11),),
# ]
# self.assertEquals(sorted(permutations), expected)
# # Test with 3
# items = [11, 22, 33]
# permutations = \
# permute_items_into_groups_of_all_sizes_gen(
# items)
# expected = [
# ((11,), (22,), (33,)),
# ((11,), (22, 33)),
# ((11,), (33,), (22,)),
# ((11,), (33, 22)),
# ((11, 22), (33,)),
# ((11, 22, 33),),
# ((11, 33), (22,)),
# ((11, 33, 22),),
# ((22,), (11,), (33,)),
# ((22,), (11, 33)),
# ((22,), (33,), (11,)),
# ((22,), (33, 11)),
# ((22, 11), (33,)),
# ((22, 11, 33),),
# ((22, 33), (11,)),
# ((22, 33, 11),),
# ((33,), (11,), (22,)),
# ((33,), (11, 22)),
# ((33,), (22,), (11,)),
# ((33,), (22, 11)),
# ((33, 11), (22,)),
# ((33, 11, 22),),
# ((33, 22), (11,)),
# ((33, 22, 11),),
# ]
# self.assertEquals(sorted(permutations), expected)
# # Test with 4
# items = [11, 22, 33, 44]
# permutations = \
# permute_items_into_groups_of_all_sizes_gen(
# items)
# expected = [
# ((11,), (22,), (33,), (44,)),
# ((11,), (22,), (33, 44)),
# ((11,), (22,), (44,), (33,)),
# ((11,), (22,), (44, 33)),
# ((11,), (22, 33), (44,)),
# ((11,), (22, 33, 44)),
# ((11,), (22, 44), (33,)),
# ((11,), (22, 44, 33)),
# ((11,), (33,), (22,), (44,)),
# ((11,), (33,), (22, 44)),
# ((11,), (33,), (44,), (22,)),
# ((11,), (33,), (44, 22)),
# ((11,), (33, 22), (44,)),
# ((11,), (33, 22, 44)),
# ((11,), (33, 44), (22,)),
# ((11,), (33, 44, 22)),
# ((11,), (44,), (22,), (33,)),
# ((11,), (44,), (22, 33)),
# ((11,), (44,), (33,), (22,)),
# ((11,), (44,), (33, 22)),
# ((11,), (44, 22), (33,)),
# ((11,), (44, 22, 33)),
# ((11,), (44, 33), (22,)),
# ((11,), (44, 33, 22)),
# ((11, 22), (33,), (44,)),
# ((11, 22), (33, 44)),
# ((11, 22), (44,), (33,)),
# ((11, 22), (44, 33)),
# ((11, 22, 33), (44,)),
# ((11, 22, 33, 44),),
# ((11, 22, 44), (33,)),
# ((11, 22, 44, 33),),
# ((11, 33), (22,), (44,)),
# ((11, 33), (22, 44)),
# ((11, 33), (44,), (22,)),
# ((11, 33), (44, 22)),
# ((11, 33, 22), (44,)),
# ((11, 33, 22, 44),),
# ((11, 33, 44), (22,)),
# ((11, 33, 44, 22),),
# ((11, 44), (22,), (33,)),
# ((11, 44), (22, 33)),
# ((11, 44), (33,), (22,)),
# ((11, 44), (33, 22)),
# ((11, 44, 22), (33,)),
# ((11, 44, 22, 33),),
# ((11, 44, 33), (22,)),
# ((11, 44, 33, 22),),
# ((22,), (11,), (33,), (44,)),
# ((22,), (11,), (33, 44)),
# ((22,), (11,), (44,), (33,)),
# ((22,), (11,), (44, 33)),
# ((22,), (11, 33), (44,)),
# ((22,), (11, 33, 44)),
# ((22,), (11, 44), (33,)),
# ((22,), (11, 44, 33)),
# ((22,), (33,), (11,), (44,)),
# ((22,), (33,), (11, 44)),
# ((22,), (33,), (44,), (11,)),
# ((22,), (33,), (44, 11)),
# ((22,), (33, 11), (44,)),
# ((22,), (33, 11, 44)),
# ((22,), (33, 44), (11,)),
# ((22,), (33, 44, 11)),
# ((22,), (44,), (11,), (33,)),
# ((22,), (44,), (11, 33)),
# ((22,), (44,), (33,), (11,)),
# ((22,), (44,), (33, 11)),
# ((22,), (44, 11), (33,)),
# ((22,), (44, 11, 33)),
# ((22,), (44, 33), (11,)),
# ((22,), (44, 33, 11)),
# ((22, 11), (33,), (44,)),
# ((22, 11), (33, 44)),
# ((22, 11), (44,), (33,)),
# ((22, 11), (44, 33)),
# ((22, 11, 33), (44,)),
# ((22, 11, 33, 44),),
# ((22, 11, 44), (33,)),
# ((22, 11, 44, 33),),
# ((22, 33), (11,), (44,)),
# ((22, 33), (11, 44)),
# ((22, 33), (44,), (11,)),
# ((22, 33), (44, 11)),
# ((22, 33, 11), (44,)),
# ((22, 33, 11, 44),),
# ((22, 33, 44), (11,)),
# ((22, 33, 44, 11),),
# ((22, 44), (11,), (33,)),
# ((22, 44), (11, 33)),
# ((22, 44), (33,), (11,)),
# ((22, 44), (33, 11)),
# ((22, 44, 11), (33,)),
# ((22, 44, 11, 33),),
# ((22, 44, 33), (11,)),
# ((22, 44, 33, 11),),
# ((33,), (11,), (22,), (44,)),
# ((33,), (11,), (22, 44)),
# ((33,), (11,), (44,), (22,)),
# ((33,), (11,), (44, 22)),
# ((33,), (11, 22), (44,)),
# ((33,), (11, 22, 44)),
# ((33,), (11, 44), (22,)),
# ((33,), (11, 44, 22)),
# ((33,), (22,), (11,), (44,)),
# ((33,), (22,), (11, 44)),
# ((33,), (22,), (44,), (11,)),
# ((33,), (22,), (44, 11)),
# ((33,), (22, 11), (44,)),
# ((33,), (22, 11, 44)),
# ((33,), (22, 44), (11,)),
# ((33,), (22, 44, 11)),
# ((33,), (44,), (11,), (22,)),
# ((33,), (44,), (11, 22)),
# ((33,), (44,), (22,), (11,)),
# ((33,), (44,), (22, 11)),
# ((33,), (44, 11), (22,)),
# ((33,), (44, 11, 22)),
# ((33,), (44, 22), (11,)),
# ((33,), (44, 22, 11)),
# ((33, 11), (22,), (44,)),
# ((33, 11), (22, 44)),
# ((33, 11), (44,), (22,)),
# ((33, 11), (44, 22)),
# ((33, 11, 22), (44,)),
# ((33, 11, 22, 44),),
# ((33, 11, 44), (22,)),
# ((33, 11, 44, 22),),
# ((33, 22), (11,), (44,)),
# ((33, 22), (11, 44)),
# ((33, 22), (44,), (11,)),
# ((33, 22), (44, 11)),
# ((33, 22, 11), (44,)),
# ((33, 22, 11, 44),),
# ((33, 22, 44), (11,)),
# ((33, 22, 44, 11),),
# ((33, 44), (11,), (22,)),
# ((33, 44), (11, 22)),
# ((33, 44), (22,), (11,)),
# ((33, 44), (22, 11)),
# ((33, 44, 11), (22,)),
# ((33, 44, 11, 22),),
# ((33, 44, 22), (11,)),
# ((33, 44, 22, 11),),
# ((44,), (11,), (22,), (33,)),
# ((44,), (11,), (22, 33)),
# ((44,), (11,), (33,), (22,)),
# ((44,), (11,), (33, 22)),
# ((44,), (11, 22), (33,)),
# ((44,), (11, 22, 33)),
# ((44,), (11, 33), (22,)),
# ((44,), (11, 33, 22)),
# ((44,), (22,), (11,), (33,)),
# ((44,), (22,), (11, 33)),
# ((44,), (22,), (33,), (11,)),
# ((44,), (22,), (33, 11)),
# ((44,), (22, 11), (33,)),
# ((44,), (22, 11, 33)),
# ((44,), (22, 33), (11,)),
# ((44,), (22, 33, 11)),
# ((44,), (33,), (11,), (22,)),
# ((44,), (33,), (11, 22)),
# ((44,), (33,), (22,), (11,)),
# ((44,), (33,), (22, 11)),
# ((44,), (33, 11), (22,)),
# ((44,), (33, 11, 22)),
# ((44,), (33, 22), (11,)),
# ((44,), (33, 22, 11)),
# ((44, 11), (22,), (33,)),
# ((44, 11), (22, 33)),
# ((44, 11), (33,), (22,)),
# ((44, 11), (33, 22)),
# ((44, 11, 22), (33,)),
# ((44, 11, 22, 33),),
# ((44, 11, 33), (22,)),
# ((44, 11, 33, 22),),
# ((44, 22), (11,), (33,)),
# ((44, 22), (11, 33)),
# ((44, 22), (33,), (11,)),
# ((44, 22), (33, 11)),
# ((44, 22, 11), (33,)),
# ((44, 22, 11, 33),),
# ((44, 22, 33), (11,)),
# ((44, 22, 33, 11),),
# ((44, 33), (11,), (22,)),
# ((44, 33), (11, 22)),
# ((44, 33), (22,), (11,)),
# ((44, 33), (22, 11)),
# ((44, 33, 11), (22,)),
# ((44, 33, 11, 22),),
# ((44, 33, 22), (11,)),
# ((44, 33, 22, 11),),
# ]
# self.assertEquals(sorted(permutations), expected)
# def test_permute_items_into_groups_of_all_sizes__unique__gen(self):
# # Test with 3
# items = [11, 22, 33]
# permutations = \
# permute_items_into_groups_of_all_sizes__unique__gen(
# items)
# expected = [
# ((11,), (22,), (33,)),
# ((11,), (22, 33)),
# ((11,), (33,), (22,)),
# ((11, 22), (33,)),
# ((11, 22, 33),),
# ((11, 33), (22,)),
# ((22,), (11,), (33,)),
# ((22,), (11, 33)),
# ((22,), (33,), (11,)),
# ((22, 33), (11,)),
# ((33,), (11,), (22,)),
# ((33,), (11, 22)),
# ((33,), (22,), (11,)),
# ]
# self.assertEquals(sorted(permutations), expected)
# # Test with 4
# items = [11, 22, 33, 44]
# permutations = \
# permute_items_into_groups_of_all_sizes__unique__gen(
# items)
# expected = [
# ((11,), (22,), (33,), (44,)),
# ((11,), (22,), (33, 44)),
# ((11,), (22,), (44,), (33,)),
# ((11,), (22, 33), (44,)),
# ((11,), (22, 33, 44)),
# ((11,), (22, 44), (33,)),
# ((11,), (33,), (22,), (44,)),
# ((11,), (33,), (22, 44)),
# ((11,), (33,), (44,), (22,)),
# ((11,), (33, 44), (22,)),
# ((11,), (44,), (22,), (33,)),
# ((11,), (44,), (22, 33)),
# ((11,), (44,), (33,), (22,)),
# ((11, 22), (33,), (44,)),
# ((11, 22), (33, 44)),
# ((11, 22), (44,), (33,)),
# ((11, 22, 33), (44,)),
# ((11, 22, 33, 44),),
# ((11, 22, 44), (33,)),
# ((11, 33), (22,), (44,)),
# ((11, 33), (22, 44)),
# ((11, 33), (44,), (22,)),
# ((11, 33, 44), (22,)),
# ((11, 44), (22,), (33,)),
# ((11, 44), (22, 33)),
# ((11, 44), (33,), (22,)),
# ((22,), (11,), (33,), (44,)),
# ((22,), (11,), (33, 44)),
# ((22,), (11,), (44,), (33,)),
# ((22,), (11, 33), (44,)),
# ((22,), (11, 33, 44)),
# ((22,), (11, 44), (33,)),
# ((22,), (33,), (11,), (44,)),
# ((22,), (33,), (11, 44)),
# ((22,), (33,), (44,), (11,)),
# ((22,), (33, 44), (11,)),
# ((22,), (44,), (11,), (33,)),
# ((22,), (44,), (11, 33)),
# ((22,), (44,), (33,), (11,)),
# ((22, 33), (11,), (44,)),
# ((22, 33), (11, 44)),
# ((22, 33), (44,), (11,)),
# ((22, 33, 44), (11,)),
# ((22, 44), (11,), (33,)),
# ((22, 44), (11, 33)),
# ((22, 44), (33,), (11,)),
# ((33,), (11,), (22,), (44,)),
# ((33,), (11,), (22, 44)),
# ((33,), (11,), (44,), (22,)),
# ((33,), (11, 22), (44,)),
# ((33,), (11, 22, 44)),
# ((33,), (11, 44), (22,)),
# ((33,), (22,), (11,), (44,)),
# ((33,), (22,), (11, 44)),
# ((33,), (22,), (44,), (11,)),
# ((33,), (22, 44), (11,)),
# ((33,), (44,), (11,), (22,)),
# ((33,), (44,), (11, 22)),
# ((33,), (44,), (22,), (11,)),
# ((33, 44), (11,), (22,)),
# ((33, 44), (11, 22)),
# ((33, 44), (22,), (11,)),
# ((44,), (11,), (22,), (33,)),
# ((44,), (11,), (22, 33)),
# ((44,), (11,), (33,), (22,)),
# ((44,), (11, 22), (33,)),
# ((44,), (11, 22, 33)),
# ((44,), (11, 33), (22,)),
# ((44,), (22,), (11,), (33,)),
# ((44,), (22,), (11, 33)),
# ((44,), (22,), (33,), (11,)),
# ((44,), (22, 33), (11,)),
# ((44,), (33,), (11,), (22,)),
# ((44,), (33,), (11, 22)),
# ((44,), (33,), (22,), (11,)),
# ]
# self.assertEquals(sorted(permutations), expected)
# def test_dynamic(self):
# # Produce the best fit of the given items into groups where more the
# # number of groups ADDS to the cost
# items = [11, 22, 33]
# cost_index = {
# 11: 15,
# 22: 10,
# 33: 5,
# }
# def group_cost_cb(group):
# return sum(map(lambda item: cost_index[item], group))
# attempts = []
# def attempts_cb(item_costs, system_cost, cost):
# nonlocal attempts
# attempts.append((item_costs, system_cost, cost))
# def system_cost_cb(p, total):
# return len(p) * 5
# cached_costs = {}
# permutation, \
# cost = \
# dynamic(
# items,
# group_cost_cb,
# cached_costs,
# attempts_cb=attempts_cb,
# system_cost_cb=system_cost_cb)
# expected = [
# ([((11,), 15), ((22,), 10), ((33,), 5)], 15, 45.0),
# ([((11,), 15), ((22, 33), 15)], 10, 40.0),
# ([((11,), 15), ((33,), 5), ((22,), 10)], 15, 45.0),
# ([((11, 22), 25), ((33,), 5)], 10, 40.0),
# ([((11, 22, 33), 30)], 5, 35.0),
# ([((11, 33), 20), ((22,), 10)], 10, 40.0),
# ([((22,), 10), ((11,), 15), ((33,), 5)], 15, 45.0),
# ([((22,), 10), ((11, 33), 20)], 10, 40.0),
# ([((22,), 10), ((33,), 5), ((11,), 15)], 15, 45.0),
# ([((22, 33), 15), ((11,), 15)], 10, 40.0),
# ([((33,), 5), ((11,), 15), ((22,), 10)], 15, 45.0),
# ([((33,), 5), ((11, 22), 25)], 10, 40.0),
# ([((33,), 5), ((22,), 10), ((11,), 15)], 15, 45.0),
# ]
# self.assertEquals(attempts, expected)
# # We picked the one permutation having only one group (due to the cost
# # of groups)
# self.assertEquals(permutation, ((11, 22, 33),))
# self.assertEquals(cost, 35.0)
# expected_cached_costs = {
# (11,): 15,
# (22,): 10,
# (33,): 5,
# (11, 22): 25,
# (11, 22, 33): 30,
# (11, 33): 20,
# (22, 33): 15,
# }
# self.assertEquals(cached_costs, expected_cached_costs)
# # Produce the best fit of the given items into groups where more the
# # number of groups SUBTRACTS from the cost
# attempts = []
# def attempts_cb(item_costs, system_cost, cost):
# nonlocal attempts
# attempts.append((item_costs, system_cost, cost))
# def system_cost_cb(p, total):
# return len(p) * -5
# cached_costs = {}
# permutation, \
# cost = \
# dynamic(
# items,
# group_cost_cb,
# cached_costs,
# attempts_cb=attempts_cb,
# system_cost_cb=system_cost_cb)
# expected = [
# ([((11,), 15), ((22,), 10), ((33,), 5)], -15, 15.0),
# ([((11,), 15), ((22, 33), 15)], -10, 20.0),
# ([((11,), 15), ((33,), 5), ((22,), 10)], -15, 15.0),
# ([((11, 22), 25), ((33,), 5)], -10, 20.0),
# ([((11, 22, 33), 30)], -5, 25.0),
# ([((11, 33), 20), ((22,), 10)], -10, 20.0),
# ([((22,), 10), ((11,), 15), ((33,), 5)], -15, 15.0),
# ([((22,), 10), ((11, 33), 20)], -10, 20.0),
# ([((22,), 10), ((33,), 5), ((11,), 15)], -15, 15.0),
# ([((22, 33), 15), ((11,), 15)], -10, 20.0),
# ([((33,), 5), ((11,), 15), ((22,), 10)], -15, 15.0),
# ([((33,), 5), ((11, 22), 25)], -10, 20.0),
# ([((33,), 5), ((22,), 10), ((11,), 15)], -15, 15.0),
# ]
# self.assertEquals(attempts, expected)
# # We picked the one permutation having only groups of single items (due
# # to the *negative* cost of groups)
# self.assertEquals(permutation, ((11,), (22,), (33,),))
# self.assertEquals(cost, 15.0)
# self.assertEquals(cached_costs, expected_cached_costs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment