I need to label N objects with unique tuples (A, B, C), where A < B < C and the maximal number of identical As is M. The same for Bs and Cs each. Among all solutions one with the lowest highest value of C is searched for. (This last sentence means: if one of two solutions has a highest C of 4 and the other of 5, then the first one is to right answer.)
Example:
M = 1
N = 4
# As Bs Cs
objects = [(1, 2, 3),
(2, 3, 4),
(3, 4, 5),
(4, 5, 6)]
M = 2
N = 4
objects = [(1, 2, 3),
(1, 2, 4),
(2, 3, 4),
(2, 3, 5)]
# or e.g
objects = [(1, 2, 3),
(2, 3, 4),
(2, 4, 5),
(3, 4, 5)]
M = 3
N = 8
objects = [(1, 2, 3),
(2, 3, 4),
(2, 3, 5),
(2, 4, 5),
(3, 4, 5),
(3, 4, 6),
(3, 5, 6),
(4, 5, 6)]
The program I came up with is a complicated if else monster:
import sys
# useage: labelme.py <N> <M>
class ObjectListTree(object):
"""Create many possible paths.
Store the parent in each node.
The last nodes are appended to the class wide endnodes.
"""
endnodes = []
def __init__(self, parent, label, counter, n, M, N):
self.parent = parent
self.M = M
self.N = N
self.label = label
self.counter = counter
self.n = n
if n < N:
self.inc_a()
self.inc_b()
self.inc_c()
else:
ObjectListTree.endnodes.append(self)
def inc_a(self):
if self.label[0]+1 < self.label[1]:
if self.counter[1] < self.M:
if self.counter[2] < self.M:
self.plus_1()
else:
self.plus_1_3()
else:
if self.counter[2] < self.M:
self.plus_1_2()
else:
self.plus_all()
elif self.label[1]+1 < self.label[2]:
if self.counter[2] < self.M:
self.plus_1_2()
else:
self.plus_all()
else:
self.plus_all()
def inc_b(self):
if self.counter[0] == self.M:
return
if self.label[1]+1 < self.label[2] and self.counter[2] < self.M:
self.plus_2()
else:
self.plus_2_3()
def inc_c(self):
if self.counter[0] == self.M or self.counter[1] == self.M:
return
else:
self.plus_3()
def plus_all(self):
ObjectListTree(self, (self.label[0]+1, self.label[1]+1, self.label[2]+1),
counter = [1, 1, 1,],
n = self.n+1, N=self.N, M=self.M)
def plus_1_2(self):
ObjectListTree(self, (self.label[0]+1, self.label[1]+1, self.label[2]),
counter = [1, 1, self.counter[2]+1,],
n = self.n+1, N=self.N, M=self.M)
def plus_1_3(self):
ObjectListTree(self, (self.label[0]+1, self.label[1], self.label[2]+1),
counter = [1, self.counter[1]+1, 1,],
n = self.n+1, N=self.N, M=self.M)
def plus_1(self):
ObjectListTree(self, (self.label[0]+1, self.label[1], self.label[2]),
counter = [1, self.counter[1]+1, self.counter[2]+1,],
n = self.n+1, N=self.N, M=self.M)
def plus_2(self):
ObjectListTree(self, (self.label[0], self.label[1]+1, self.label[2]),
counter = [self.counter[0]+1, 1, self.counter[2]+1,],
n = self.n+1, N=self.N, M=self.M)
def plus_2_3(self):
ObjectListTree(self, (self.label[0], self.label[1]+1, self.label[2]+1),
counter = [self.counter[0]+1, 1, 1,],
n = self.n+1, N=self.N, M=self.M)
def plus_3(self):
ObjectListTree(self, (self.label[0], self.label[1], self.label[2]+1),
counter = [self.counter[0]+1, self.counter[1]+1, 1,],
n = self.n+1, N=self.N, M=self.M)
tree = ObjectListTree(parent=None, label=(1, 2, 3), counter = [1,1,1,], n=1, N=int(sys.argv[1]), M=int(sys.argv[2]))
best_path = tree.endnodes[0]
for n in tree.endnodes:
if n.label[2] < best_path.label[2]:
best_path = n
objects = []
while best_path:
objects.append(best_path.label)
best_path = best_path.parent
objects.reverse()
print objects
But I have the feeling that this should actually be something simple like a combination of two or three functions from the itertools module wrapped in a set or something. Can anyone see a simple solution?
I think this code meets your requirements and always generates the solution with the lowest possible C. Not quite using itertools however.