SAT

求解所有满足的解

def sat_all(props, f):
    """Get all solutions of given proposition set props that satisfy f

    Arguments:
        props {BoolRef} -- Proposition list
        f {Boolref} -- logical express that consist of props
    """
    solver = Solver()
    solver.add(f)
    result = []
    while solver.check() == sat:
        m = solver.model()
        result.append(m)
        block = []
        # 对结果中命题进行判断
        for prop in props:
            if m.eval(prop, True): #使用eval获取结果(true/false),
                new_prop = prop #如果为true,则不做改变
                # TODO: Fill your code here
            else:
                new_prop = Not(prop)#如果为false,则进行取反
            block.append(new_prop)#将每个判断后的命题添加到列表中

        # TODO: Fill your code here
        new_prop = And(f, Not(And(block))) #对列表中所有命题合取,然后再取反,并且与原命题合取,形成新的约束
        solver.add(new_prop) #添加新约束
    print("the given proposition: ", f)
    print("the number of solutions: ", len(result))

    def print_model(m):
        print(sorted([(d, m[d]) for d in m], key=lambda x: str(x[0])))
    for m in result:
        print_model(m)


# If you complete the function. Try to use it for below props.
sat_all([P, Q], Or(P, Q))
sat_all([P], Implies(P, P))
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页