如何在z3py中表示求和

时间:2015-05-29 03:51:33

标签: z3 smt z3py

我对z3py很新。 我试图在z3py中编写以下2个表达式

enter image description here

可以找到有关此问题的更多信息 here

我经常搜索stackoverflow并遇到了 similar question

但遗憾的是,我无法得到满意的答案。

我尝试用以下方式编写SMT中的第一个编码:

#InputGroup, BlockGroup, OutputGroup contain some integer values to represent blocks
InputGroup = [0,1,2]
BlockGroup = [2,3,4,5,6]
OutputGroup = [7,8,9]
Groups = [InputGroup, BlockGroup, OutputGroup]

NumberOfTasks = len(InputGroup)+ len(BlockGroup)+ len(OutputGroup)
M = Function('M', Intsort(), Intsort())
Task = Function('Task', Intsort(), Intsort(), Intsort())
summation1 = Int('summation1')

# each group from the Groups is represented by its index number
for r, g in enumerate(Groups): 
    for m in range(0, NumberOfTasks):
        if(m in g):
            s.add(summation1 == summation1+ M(Task(r,m)))

和SMT中的第二个表达式如下:

NumberOfInputs = len(InputGroup)
NumberOfBlocks = len(BlockGroup)
NumberOfOutputs = len(OutputGroup)
Node = Function('Node', Intsort(), Intsort(), Intsort())
f = Function('f', Intsort(), Intsort(), Intsort())

for r, g in enumerate(Groups):
    if(r != Groups.index(InputGroup) and r != Groups.index(OutputGroup)):
        for i in range(0,(NumberOfInputs+NumberOfBlocks+NumberOfOutputs)):
            summation2 = Int('summation2')
            for m in range(0, (NumberOfTasks)):
                if(m in g and i in g):
                    s.add(summation2 == summation2+ f(Node(r,i), Task(r,m)))
            s.add(summation2 == 1)

虽然我从上面的方程式得到满意的结果,但我得到的模型有点值得怀疑。 我只是想知道我是否正确表达了这一点。

1 个答案:

答案 0 :(得分:1)

我想你想改变:

# each group from the Groups is represented by its index number
for r, g in enumerate(Groups): 
    for m in range(0, NumberOfTasks):
        if(m in g):
           s.add(summation1 == summation1+ M(Task(r,m)))

这样的事情:

# each group from the Groups is represented by its index number
sumTerms = [M(Task(r,m)) 
                  for r, g in enumerate(Groups): 
                     for m in range(0, NumberOfTasks):
                          if(m in g)]
s.add(summation1 == Sum(sumTerms))

和第二个例子类似。