简体   繁体   中英

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. 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. 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.

Eg:

在此处输入图像描述

My question is how to initialize a Z3 variable with its lowest possible value and use it as a counter variable?

Note: The max. and min. possible values of x1, x2, and x3 are always the lowest and the highest index of A , respectively.

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. 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)) .

  • It is much easier to think of each location A_i as the number of all x_j assigned with value i .

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:

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:

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 .

[ Note: for this task, I am using the fzn2z3.py script from the fzn2omt project .]

File counter.mzn : (general problem structure)

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)

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

INDEX_SET_X = 1..3;

Find one solution with 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 ):

~$ 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]);
----------
==========

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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