简体   繁体   English

Python 中的 Z3 计数器变量数组

[英]Array of Z3 Counter variables in Python

I would like to create a Z3 array ( A ) whose elements are Z3 Int variables and each of those elements has a fixed range.我想创建一个 Z3 数组( A ),其元素是 Z3 Int 变量,并且每个元素都有一个固定的范围。 Also, I wanted to initialize the values of these elements with 0. Now let there is a simple function x1 + x2 + x3 = 6, where 0 <= x1, x2, x3 <= 4, where x1, x2, x3 are Z3 Int variables.另外,我想用 0初始化这些元素的值。现在让有一个简单的 function x1 + x2 + x3 = 6,其中 0 <= x1,x2,x3 <= 4,其中 x1,x2,x3 是 Z3整数变量。 Next, I want to increase the values at x1, x2, and x3 index positions (with the values of x1, x2, and x3 which are decided by Z3) of A by 1.接下来,我想将 A 的 x1、x2 和 x3 索引位置的值(x1、x2 和 x3 的值由 Z3 决定)增加 1。

Eg:例如:

在此处输入图像描述

My question is how to initialize a Z3 variable with its lowest possible value and use it as a counter variable?我的问题是如何用其最低值初始化 Z3 变量并将其用作计数器变量?

Note: The max.注意:最大。 and min.和分钟。 possible values of x1, x2, and x3 are always the lowest and the highest index of A , respectively. x1、x2 和 x3 的可能值始终分别是A的最低和最高索引。

General Considerations:一般注意事项:

  • Given a value assignment (x1, x2, x3) = (v1, v2, v3) , any permutation of (v1, v2, v3) is also a solution of the problem.给定一个赋值(x1, x2, x3) = (v1, v2, v3)(v1, v2, v3) v1, v2, v3) 的任何排列也是问题的解决方案。 In some circumstances, it might be convenient to break symmetries in the search space by requiring, for example, ∀ i, j.((i < j) => (x_i <= x_j)) .在某些情况下,通过要求∀ i, j.((i < j) => (x_i <= x_j))打破搜索空间中的对称性可能会很方便。

  • It is much easier to think of each location A_i as the number of all x_j assigned with value i .将每个位置A_i视为分配有值i的所有x_j数量要容易得多。

Python + z3: Python + z3:

from z3 import *

def model_to_str(m):
    fmt = "\t A:= {}; x := {};"
    A_tuple = tuple([m.eval(el) for el in A_list])
    x_tuple = tuple([m.eval(el) for el in x_list])
    return fmt.format(A_tuple, x_tuple)

###########
# PROBLEM #
###########

s = Solver()

# declarations
A_length = 5
A_list = [Int('A_{0}'.format(idx)) for idx in range(0, A_length)]
x_list = [Int('x_{0}'.format(idx)) for idx in range(0, 3)]

# A's constraints
for i, A_i in enumerate(A_list):
    s.add(A_i == Sum([If(x_j == i, 1, 0) for x_j in x_list]))
    s.add(A_i <= 2)

# x's constraints
s.add(6 == Sum(x_list))
for x_i in x_list:
    s.add(0 <= x_i)
    s.add(x_i < A_length)

# Symmetry-breaking constraints:
# (remove symmetry solutions)
#s.add(x_list[0] <= x_list[1])
#s.add(x_list[0] <= x_list[2])
#s.add(x_list[1] <= x_list[2])

# Get one solution:
if s.check() == sat:
    print("Random Solution:")
    print(model_to_str(s.model()))

# Get all solutions:
s.push()
print("\nEnumerate All Solutions:")
while s.check() == sat:
    m = s.model()
    print(model_to_str(m))
    s.add(Or([x_j != m.eval(x_j) for x_j in x_list]))
s.pop()

# Enumerate all possible assignments
print("\nTry All Possible Assignments:")
for x_0_val in range(0, A_length):
    for x_1_val in range(0, A_length):
        for x_2_val in range(0, A_length):
            values = (x_0_val, x_1_val, x_2_val)

            s.push()
            s.add([x_j == x_j_val for x_j, x_j_val in zip(x_list, values)])

            if s.check() == sat:
                print(model_to_str(s.model()))
            else:
                print("\t\tx := {} is unsat!".format(values))

            s.pop()

Output: Output:

Random Solution:
     A:= (1, 0, 1, 0, 1); x := (4, 0, 2);

Enumerate All Solutions:
     A:= (1, 0, 1, 0, 1); x := (2, 0, 4);
     A:= (1, 0, 1, 0, 1); x := (2, 4, 0);
     A:= (0, 1, 1, 1, 0); x := (2, 1, 3);
     A:= (0, 1, 1, 1, 0); x := (2, 3, 1);
     A:= (1, 0, 0, 2, 0); x := (3, 3, 0);
     A:= (1, 0, 1, 0, 1); x := (4, 0, 2);
     A:= (0, 2, 0, 0, 1); x := (4, 1, 1);
     A:= (1, 0, 1, 0, 1); x := (4, 2, 0);
     A:= (0, 1, 1, 1, 0); x := (3, 2, 1);
     A:= (1, 0, 1, 0, 1); x := (0, 2, 4);
     A:= (0, 1, 1, 1, 0); x := (1, 2, 3);
     A:= (1, 0, 1, 0, 1); x := (0, 4, 2);
     A:= (0, 1, 1, 1, 0); x := (1, 3, 2);
     A:= (0, 2, 0, 0, 1); x := (1, 4, 1);
     A:= (1, 0, 0, 2, 0); x := (0, 3, 3);
     A:= (0, 2, 0, 0, 1); x := (1, 1, 4);
     A:= (1, 0, 0, 2, 0); x := (3, 0, 3);
     A:= (0, 1, 1, 1, 0); x := (3, 1, 2);

Try All Possible Assignments:
        x := (0, 0, 0) is unsat!
        x := (0, 0, 1) is unsat!
        x := (0, 0, 2) is unsat!
        x := (0, 0, 3) is unsat!
        x := (0, 0, 4) is unsat!
        x := (0, 1, 0) is unsat!
        x := (0, 1, 1) is unsat!
        x := (0, 1, 2) is unsat!
        x := (0, 1, 3) is unsat!
        x := (0, 1, 4) is unsat!
        x := (0, 2, 0) is unsat!
        x := (0, 2, 1) is unsat!
        x := (0, 2, 2) is unsat!
        x := (0, 2, 3) is unsat!
     A:= (1, 0, 1, 0, 1); x := (0, 2, 4);
        x := (0, 3, 0) is unsat!
        x := (0, 3, 1) is unsat!
        x := (0, 3, 2) is unsat!
     A:= (1, 0, 0, 2, 0); x := (0, 3, 3);
        x := (0, 3, 4) is unsat!
        x := (0, 4, 0) is unsat!
        x := (0, 4, 1) is unsat!
     A:= (1, 0, 1, 0, 1); x := (0, 4, 2);
        x := (0, 4, 3) is unsat!
        x := (0, 4, 4) is unsat!
        x := (1, 0, 0) is unsat!
        x := (1, 0, 1) is unsat!
        x := (1, 0, 2) is unsat!
        x := (1, 0, 3) is unsat!
        x := (1, 0, 4) is unsat!
        x := (1, 1, 0) is unsat!
        x := (1, 1, 1) is unsat!
        x := (1, 1, 2) is unsat!
        x := (1, 1, 3) is unsat!
     A:= (0, 2, 0, 0, 1); x := (1, 1, 4);
        x := (1, 2, 0) is unsat!
        x := (1, 2, 1) is unsat!
        x := (1, 2, 2) is unsat!
     A:= (0, 1, 1, 1, 0); x := (1, 2, 3);
        x := (1, 2, 4) is unsat!
        x := (1, 3, 0) is unsat!
        x := (1, 3, 1) is unsat!
     A:= (0, 1, 1, 1, 0); x := (1, 3, 2);
        x := (1, 3, 3) is unsat!
        x := (1, 3, 4) is unsat!
        x := (1, 4, 0) is unsat!
     A:= (0, 2, 0, 0, 1); x := (1, 4, 1);
        x := (1, 4, 2) is unsat!
        x := (1, 4, 3) is unsat!
        x := (1, 4, 4) is unsat!
        x := (2, 0, 0) is unsat!
        x := (2, 0, 1) is unsat!
        x := (2, 0, 2) is unsat!
        x := (2, 0, 3) is unsat!
     A:= (1, 0, 1, 0, 1); x := (2, 0, 4);
        x := (2, 1, 0) is unsat!
        x := (2, 1, 1) is unsat!
        x := (2, 1, 2) is unsat!
     A:= (0, 1, 1, 1, 0); x := (2, 1, 3);
        x := (2, 1, 4) is unsat!
        x := (2, 2, 0) is unsat!
        x := (2, 2, 1) is unsat!
        x := (2, 2, 2) is unsat!
        x := (2, 2, 3) is unsat!
        x := (2, 2, 4) is unsat!
        x := (2, 3, 0) is unsat!
     A:= (0, 1, 1, 1, 0); x := (2, 3, 1);
        x := (2, 3, 2) is unsat!
        x := (2, 3, 3) is unsat!
        x := (2, 3, 4) is unsat!
     A:= (1, 0, 1, 0, 1); x := (2, 4, 0);
        x := (2, 4, 1) is unsat!
        x := (2, 4, 2) is unsat!
        x := (2, 4, 3) is unsat!
        x := (2, 4, 4) is unsat!
        x := (3, 0, 0) is unsat!
        x := (3, 0, 1) is unsat!
        x := (3, 0, 2) is unsat!
     A:= (1, 0, 0, 2, 0); x := (3, 0, 3);
        x := (3, 0, 4) is unsat!
        x := (3, 1, 0) is unsat!
        x := (3, 1, 1) is unsat!
     A:= (0, 1, 1, 1, 0); x := (3, 1, 2);
        x := (3, 1, 3) is unsat!
        x := (3, 1, 4) is unsat!
        x := (3, 2, 0) is unsat!
     A:= (0, 1, 1, 1, 0); x := (3, 2, 1);
        x := (3, 2, 2) is unsat!
        x := (3, 2, 3) is unsat!
        x := (3, 2, 4) is unsat!
     A:= (1, 0, 0, 2, 0); x := (3, 3, 0);
        x := (3, 3, 1) is unsat!
        x := (3, 3, 2) is unsat!
        x := (3, 3, 3) is unsat!
        x := (3, 3, 4) is unsat!
        x := (3, 4, 0) is unsat!
        x := (3, 4, 1) is unsat!
        x := (3, 4, 2) is unsat!
        x := (3, 4, 3) is unsat!
        x := (3, 4, 4) is unsat!
        x := (4, 0, 0) is unsat!
        x := (4, 0, 1) is unsat!
     A:= (1, 0, 1, 0, 1); x := (4, 0, 2);
        x := (4, 0, 3) is unsat!
        x := (4, 0, 4) is unsat!
        x := (4, 1, 0) is unsat!
     A:= (0, 2, 0, 0, 1); x := (4, 1, 1);
        x := (4, 1, 2) is unsat!
        x := (4, 1, 3) is unsat!
        x := (4, 1, 4) is unsat!
     A:= (1, 0, 1, 0, 1); x := (4, 2, 0);
        x := (4, 2, 1) is unsat!
        x := (4, 2, 2) is unsat!
        x := (4, 2, 3) is unsat!
        x := (4, 2, 4) is unsat!
        x := (4, 3, 0) is unsat!
        x := (4, 3, 1) is unsat!
        x := (4, 3, 2) is unsat!
        x := (4, 3, 3) is unsat!
        x := (4, 3, 4) is unsat!
        x := (4, 4, 0) is unsat!
        x := (4, 4, 1) is unsat!
        x := (4, 4, 2) is unsat!
        x := (4, 4, 3) is unsat!
        x := (4, 4, 4) is unsat!

MiniZinc + z3: MiniZinc + z3:

I want to share also this approach because it took me literally only 5 minutes to solve this problem in MiniZinc , much less than with z3py .我也想分享这种方法,因为我在MiniZinc中解决这个问题实际上只花了5 分钟,比z3py少得多。

[ Note: for this task, I am using the fzn2z3.py script from the fzn2omt project .] [注意:对于这个任务,我使用的是来自fzn2omt项目fzn2z3.py脚本。]

File counter.mzn : (general problem structure)文件counter.mzn :(一般问题结构)

include "globals.mzn";

% PARAMETERS

set of int: DOMAIN_A;
set of int: INDEX_SET_A;

set of int: DOMAIN_X = INDEX_SET_A;
set of int: INDEX_SET_X;

% VARIABLES

array [INDEX_SET_A] of var DOMAIN_A: A;
array [INDEX_SET_X] of var DOMAIN_X: x;

% CONSTRAINTS

constraint sum(x) == 6;
constraint forall(i in INDEX_SET_A) (
    A[i] = sum(j in INDEX_SET_X)(x[j] == i)
);

% SYMMETRY BREAKING CONTRAINT
constraint increasing(x);

solve satisfy;

( Note: I have enabled the symmetry breaking constraint on this problem just to reduce the number of solutions printed below.) 注意:我在这个问题上启用了对称破坏约束,只是为了减少下面打印的解决方案的数量。)

File counter.dzn : (instance-specific data)文件counter.dzn :(特定于实例的数据)

DOMAIN_A = 0..2;
INDEX_SET_A = 0..4;

INDEX_SET_X = 1..3;

Find one solution with z3 :使用z3找到一种解决方案:

~$ mzn2fzn counter.mzn counter.dzn
~$ fzn2z3.py counter.fzn 
x = array1d(1..3, [1, 1, 4]);
A = array1d(0..4, [0, 2, 0, 0, 1]);
----------

Find all possible solutions (only with optimathsat ):找到所有可能的解决方案(仅使用optimathsat ):

~$ mzn2fzn counter.mzn counter.dzn
~$ fzn2optimathsat.py --all-solutions counter.fzn
% allsat model
x = array1d(1..3, [1, 1, 4]);
A = array1d(0..4, [0, 2, 0, 0, 1]);
----------
% allsat model
x = array1d(1..3, [0, 2, 4]);
A = array1d(0..4, [1, 0, 1, 0, 1]);
----------
% allsat model
x = array1d(1..3, [0, 3, 3]);
A = array1d(0..4, [1, 0, 0, 2, 0]);
----------
% allsat model
x = array1d(1..3, [1, 2, 3]);
A = array1d(0..4, [0, 1, 1, 1, 0]);
----------
==========

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM