xubuntix
xubuntix

Reputation: 2333

Algorithm for generating list of unique tuples with specific properties

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?

Upvotes: 2

Views: 346

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57691

I think this code meets your requirements and always generates the solution with the lowest possible C. Not quite using itertools however.

def generateTuples(N, M):
  done = 0
  counters = {}
  for C in range(3, N + 3):
    for B in range(2, C):
      for A in range(1, B):
        if (counters.get('A%i' % A, 0) < M and
            counters.get('B%i' % B, 0) < M and
            counters.get('C%i' % C, 0) < M):
          yield (A, B, C)
          counters['A%i' % A] = counters.get('A%i' % A, 0) + 1
          counters['B%i' % B] = counters.get('B%i' % B, 0) + 1
          counters['C%i' % C] = counters.get('C%i' % C, 0) + 1
          done += 1
          if done >= N:
            return

for (A, B, C) in generateTuples(8, 3):
  print (A, B, C)

Upvotes: 3

Related Questions