开发者

Quine-McCluskey algorithm in Python

开发者 https://www.devze.com 2023-04-08 15:45 出处:网络
I\'m trying to write the Quine-McCluskey algorithm in python, but I开发者_C百科 wanted to see if there were any versions out there that I might use instead. A google search showed few useful results.

I'm trying to write the Quine-McCluskey algorithm in python, but I开发者_C百科 wanted to see if there were any versions out there that I might use instead. A google search showed few useful results. I'm looking for 4x4 map reduction, not 2x2 or 3x3. Any ideas or references?


def combine(m, n):
    a = len(m)
    c = ''
    count = 0
    for i in range(a): 
        if(m[i] == n[i]):
            c += m[i]
        elif(m[i] != n[i]):
            c += '-'
            count += 1

    if(count > 1): 
        return None
    else:            
        return c


def find_prime_implicants(data):
    newList = list(data)
    size = len(newList)
    IM = []
    im = []
    im2 = []
    mark = [0]*size
    m = 0
    for i in range(size):
        for j in range(i+1, size):
            c = combine( str(newList[i]), str(newList[j]) )
            if c != None:
                im.append(str(c))
                mark[i] = 1
                mark[j] = 1
            else:
                continue

    mark2 = [0]*len(im)
    for p in range(len(im)):
        for n in range(p+1, len(im)):
            if( p != n and mark2[n] == 0):
                if( im[p] == im[n]):
                    mark2[n] = 1


    for r in range(len(im)):
        if(mark2[r] == 0):
            im2.append(im[r])

    for q in range(size):
        if( mark[q] == 0 ):
            IM.append( str(newList[q]) )
            m = m+1

    if(m == size or size == 1):
        return IM
    else:
        return IM + find_prime_implicants(im2)


minterms = set(['1101', '1100', '1110', '1111', '1010', '0011', '0111', '0110'])

minterms2 = set(['0000', '0100', '1000', '0101', '1100', '0111', '1011', '1111'])

minterms3 = set(['0001', '0011', '0100', '0110', '1011', '0000', '1000', '1010', '1100', '1101'])

print 'PI(s):', find_prime_implicants(minterms)

print 'PI2(s):', find_prime_implicants(minterms2)

print 'PI3(s):', find_prime_implicants(minterms3)


In the Wikipedia of which you gave the link, there are some "External links" at the bottom, among which are these, interesting relatively to your project:

  • " Python Implementation by Robert Dick "

    Wouldn't this fulfil your need ?

  • " A series of two articles describing the algorithm(s) implemented in R: first article and second article. The R implementation is exhaustive and it offers complete and exact solutions. It processes up to 20 input variables. "

    You could use the rpy Python interface to R language to run the R code of the Quine-McCluskey algorithm. Note that there is a rewrite of rpy : rpy2

    Also, why not, write yourself a new Python script, using the enhancement of the algorithm done by Adrian Duşa in 2007 , lying in the second article ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号