使用Python处理CNF,如何删除重言式

我有一项人工智能练习要做。目标是在屏幕上打印一个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]or[A,true]]这样的情况。重言式是像[[A,true]or[A,false]]这样的情况。现在我发布我的Python代码,它能工作,但只删除了冗余。正如我在问题中所写,我需要删除重言式。这里是代码

import randomimport stringimport sysimport os#这个函数创建元组列表# n = 符号数量, m = 子句数量, k = 子句长度def createTuple(n):    if n > 26:        print("不可能有超过25个字母")        sys.exit()    # 创建并打乱25个数字的列表    my_list = [0] * (2 * n)    for x in range(0, 2 * n):        my_list[x] = x    random.shuffle(my_list)    # 创建小写或大写符号的列表    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#这个函数创建CNFdef 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):            #冗余控制            i = index            while(temple_tuple[i] == None):               i += 1            countx = 0            #重言式控制(仅在两个元素相邻时有效)            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("发现重言式")                    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)            # 重言式控制结束        ktot += "}"        if count != (m - 1):            stot += ktot + " and "        elif count == (m - 1):            stot += ktot    print(stot)lista_tuple = createTuple(6)createCnf(6,3,3)

回答:

这是答案

import randomimport stringimport sysimport os #这个函数创建元组列表 # n = 符号数量, m = 子句数量, k = 子句长度def createTuple(n):if n > 26:    print("不可能有超过25个字母")    sys.exit()# 创建并打乱25个数字的列表my_list = [0] * (2 * n)for x in range(0, 2 * n):    my_list[x] = xrandom.shuffle(my_list)# 创建小写或大写符号的列表symbols = [0] * (2 * n)x = 0for 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#这个函数创建CNFdef createCnf(n, m, k):list_tuple = createTuple(n)stot = ""ki = ""x = 0i = 0countx = 0for count in range(0, m):    ktot = "{"    temple_tuple = createTuple(n)    temple_tuple2 = []    for index in range(0, k):        #冗余控制        i = index        while(temple_tuple[i] == None):           i += 1        countx = 0        #重言式控制(仅在两个元素相邻时有效)        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("发现重言式")                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)        # 重言式控制结束    ktot += "}"    if count != (m - 1):        stot += ktot + " and "    elif count == (m - 1):        stot += ktotprint(stot)lista_tuple = createTuple(6)createCnf(6,3,3)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注