CNF与python,如何删除重言式

时间:2017-08-28 19:13:58

标签: python artificial-intelligence

我要做AI练习。目标是在屏幕上打印CNF(n,m,k)。它是一个带有n个字母,m个子句的布尔形式,每个子句必须有k个字母。我有这样的规则,大写字母是真的,小写字母是假的。此外,我必须从条款中删除冗余和重言式。例如CNF(6,3,3)可能是这样的:

[[A,true]or[b,false]or[D,true]] and
[[B,true]or[d,false]or[a,false]] and 
[[d,false]or[A,true]or[B,true]]

你可以在这里看到我们有3个字母,正好有3个字母,字母是6个字母(6个大写,6个小)。在这里,我们没有任何冗余或重言式。冗余类似于[[A,true]或[A,true]]。重言式就像[[A,true]或[A,false]]。 现在我发布我的python代码,它工作,但我只删除冗余。所以,正如我在问题中写的那样,我需要删除重言式。 这是代码

import random
import string
import sys
import os

#This function create the list of tuple
# n = number of symbols, m = nuber of clauses, k = clause length
def createTuple(n):
    if n > 26:
        print("it's impossible to have more than 25 letters")
        sys.exit()
    # creo lista di 25 numeri e la mescolo
    my_list = [0] * (2 * n)
    for x in range(0, 2 * n):
        my_list[x] = x
    random.shuffle(my_list)

    # creo la lista di simboli minuscoli o maiuscoli
    symbols = [0] * (2 * n)
    x = 0
    for count in range(0, n):
        symbols[count] = string.ascii_lowercase[count]
    for count in range(n, 2 * n):
        symbols[count] = string.ascii_uppercase[count - n]
    symbols_rand = [0] * (2 * n)
    for count in range(0, 2 * n):
        symbols_rand[count] = symbols[my_list[count]]
    list_tuple = [0] * (2 * n)
    for counts in range(0, (2 * n)):
        if symbols_rand[counts].islower():
            list_tuple[counts] = [(symbols_rand[counts]), False]
        if symbols_rand[counts].isupper():
            list_tuple[counts] = [(symbols_rand[counts]), True]
    return list_tuple


#This function create the CNF
def createCnf(n, m, k):
    list_tuple = createTuple(n)
    stot = ""
    ki = ""
    x = 0
    i = 0
    countx = 0
    for count in range(0, m):
        ktot = "{"
        temple_tuple = createTuple(n)
        temple_tuple2 = []
        for index in range(0, k):
            #REDUNDANCY CONTROL
            i = index
            while(temple_tuple[i] == None):
               i += 1
            countx = 0
            #TAUTOLOGY CONTROL (WORKING ONLY WHEN THE TWO ELEMENTS ARE CLOSE)
            while countx < len(temple_tuple2):
                x = temple_tuple[i][0]
                if temple_tuple2[countx][0].isupper():
                    c = temple_tuple2[countx][0].lower()
                else:
                    c = temple_tuple2[countx][0].upper()

                if (x == c):
                    i += 1
                    print("tautologia trovata")
                    countx = 0
                else:
                    countx = countx + 1
            ki = temple_tuple[i]
            temple_tuple2.append(ki)
            temple_tuple[i] = None
            if index != (k - 1):
                ktot += str(ki) + " or "
            elif index == (k - 1):
                ktot += str(ki)
            # HERE TAUTOLOGIES END
        ktot += "}"
        if count != (m - 1):
            stot += ktot + " and "
        elif count == (m - 1):
            stot += ktot
    print(stot)


lista_tuple = createTuple(6)
createCnf(6,3,3)

1 个答案:

答案 0 :(得分:0)

这是答案

import random
import string
import sys
import os

 #This function create the list of tuple
 # n = number of symbols, m = nuber of clauses, k = clause length
def createTuple(n):
if n > 26:
    print("it's impossible to have more than 25 letters")
    sys.exit()
# creo lista di 25 numeri e la mescolo
my_list = [0] * (2 * n)
for x in range(0, 2 * n):
    my_list[x] = x
random.shuffle(my_list)

# creo la lista di simboli minuscoli o maiuscoli
symbols = [0] * (2 * n)
x = 0
for count in range(0, n):
    symbols[count] = string.ascii_lowercase[count]
for count in range(n, 2 * n):
    symbols[count] = string.ascii_uppercase[count - n]
symbols_rand = [0] * (2 * n)
for count in range(0, 2 * n):
    symbols_rand[count] = symbols[my_list[count]]
list_tuple = [0] * (2 * n)
for counts in range(0, (2 * n)):
    if symbols_rand[counts].islower():
        list_tuple[counts] = [(symbols_rand[counts]), False]
    if symbols_rand[counts].isupper():
        list_tuple[counts] = [(symbols_rand[counts]), True]
return list_tuple


#This function create the CNF
def createCnf(n, m, k):
list_tuple = createTuple(n)
stot = ""
ki = ""
x = 0
i = 0
countx = 0
for count in range(0, m):
    ktot = "{"
    temple_tuple = createTuple(n)
    temple_tuple2 = []
    for index in range(0, k):
        #REDUNDANCY CONTROL
        i = index
        while(temple_tuple[i] == None):
           i += 1
        countx = 0
        #TAUTOLOGY CONTROL (WORKING ONLY WHEN THE TWO ELEMENTS ARE CLOSE)
        while countx < len(temple_tuple2):
            x = temple_tuple[i][0]
            if temple_tuple2[countx][0].isupper():
                c = temple_tuple2[countx][0].lower()
            else:
                c = temple_tuple2[countx][0].upper()

            if (x == c):
                i += 1
                print("tautologia trovata")
                countx = 0
            else:
                countx = countx + 1
        ki = temple_tuple[i]
        temple_tuple2.append(ki)
        temple_tuple[i] = None
        if index != (k - 1):
            ktot += str(ki) + " or "
        elif index == (k - 1):
            ktot += str(ki)
        # HERE TAUTOLOGIES END
    ktot += "}"
    if count != (m - 1):
        stot += ktot + " and "
    elif count == (m - 1):
        stot += ktot
print(stot)


lista_tuple = createTuple(6)
createCnf(6,3,3)