5.3 Tailoring Problem Classes
5.3.1 Flattening Parameterised Subexpressions
The main challenge of flattening parameterised expressions is to handle subexpressions that are quantified by parameters. If an expression is quantified by a parameterised quantifying variable, we need to generate a quantified number of auxiliary variables during flattening. Thus, whenever a parameterised subexpression is flattened, anarrayof auxiliary variables is introduced, whose size is derived by the domain of the corresponding quantifiers. This information is retrieved from the quantification attributes of the quantified subexpression.
Flattening Parameterised Quantifications by Example
To illustrate flattening of a parameterised expression we consider each step in a simple example that contains a parameterised existential quantification:
given n : i n t ( 1 . . )
f i n d x : matrix indexed by[i n t ( 1 . .n) ] of i n t( 1 . .n)
such t h a t
e x i s t s i:i n t ( 1 . .n) . x[i]=i
The quantified expression,‘x[i]=i’, is a Boolean expression that has the quantifier attributes
which is added to the problem class:
f i n d auxArray : matrix indexed by [i n t ( 1 . .n) ] of bool
The auxiliary array is then linked to the flattened expression‘x[i]=i’:
f o r a l l i:i n t ( 1 . .n) . auxArray[i] <=> (x[i] = i)
Finally, the auxiliary variable array represents the original expression‘x[i]=i’:
e x i s t s i:i n t ( 1 . .n) . auxArray[i]
In summary, flattening yields the flat problem class:
given n : i n t ( 1 . . )
f i n d x : matrix indexed by[i n t ( 1 . .n) ] of i n t ( 1 . .n)
f i n d auxArray : matrix indexed by [i n t ( 1 . .n) ] of bool
such t h a t
f o r a l l i:i n t ( 1 . .n) . auxArray[i] <=> (x[i] = i) ,
e x i s t s i:i n t ( 1 . .n) . auxArray[i]
Algorithm 5.1Excerpt of FLATTEN CLASS(E,flatten2Aux) for flattening problem classes
(and instances), based on FLATTEN CSE (Alg. 4.2) from instance level. Extensions are given inred font.
Require: E: expression tree,flatten2Aux: Boolean flattened to aux var
1: if¬(all ofE’s children are leaves)then
2: for allei∈children(E)do 3: if ¬(ei.isLeaf)then
4: Stringei ←toString(ei)
5: if hashMap.contains(Stringei) then
6: aux←hashMap.get(Stringei)
7: else
8: aux← FLATTEN CLASS(ei,S,true)
9: hashMap.add(Stringei,aux)
10: E.replaceChildWith(ei,aux)
11: ifflatten2Aux then
12: if Eis quantified by parameterised expression then
13: AuxArray=createNewVarArray(E.lb,E.ub,E.qt.length);vars.add(‘AuxArray’)
14: constraints.add(‘{‘∀x∈x.dom’|x∈E.qt.vars}.AuxArray[E.qt.index] =E’)
15: return AuxArray[E.qt.index]
16: else
17: Aux←createNewVariable(E.lb,E.ub);auxVars.add(Aux);ctBuffer.add(‘Aux=E’)
An Algorithm for Flattening Problem Classes
The only difference between instance flattening and class flattening is the generation of aux- iliary variables and flat constraints: if a subexpressions is quantified over a parameterised expression, anarrayof auxiliary variables is created, otherwise just a single auxiliary vari- able. Therefore, the instance algorithm can simply be extended so as to generate auxiliary variables for parameterised quantified expressions.
We summarise the flattening process for problem-class expressions in Alg. 5.1, where ex- tensions to FLATTEN/ FLATTEN CSEare given inred font: It proceeds in three steps: first, an auxiliary variablearrayis generated (line 13). Second, the array is linked to the corre- sponding flattened expression (line 14). Third, the auxiliary array is returned in such a way that it is referenced correspondingly to the flattened expression (line 15).
Note, that only those nodes are flattened that cannot be evaluated at instance level, i.e. nodes that are of category decision variable (see Sec. 2.2). Furthermore, since quantifi- cations cannot be unrolled at class level, flattening typically processes far less nodes than instance flattening. In the following, each flattening step is further explained.
1. Generating the Auxiliary Array if node E is quantified, it is flattened to an auxil-
iary variable array (line 13). Note that, for convenience, we use 1-dimensional arrays to represent auxiliary variables. The length of the array is determined from the lengths of the quantifying variables’ domains: if quantified node E is quantified by k quantifying variablesvi that each range over the domainlbi..ubi, thenlengthof the array,l, is
l= i
%
1..k
ubi−lbi + 1 (5.1)
As an example, in Fig. 5.4, ‘aux0’ represents an expression that is quantified byi∈(1..n)
andj ∈(1..m)and hence has length(n−1 + 1)∗(m−1 + 1) =n∗m.
2. Linking the Auxiliary Array to the Flat Expression After creating the auxiliary
variable array, the to-be-flattened nodeE is linked (or ‘reified’) withAuxArray (line 14). The challenge in linking is to dereference the auxiliary array correctly, which needs to take the following into account: the range of each quantifying variable and its associated domain as well as the constant value, from which the target solver starts initialising its arrays. As an example for the latter, in solver MINION, arrays are always dereferenced starting from 0. In FlatZinc, however, arrays are dereferenced starting from 1. We denote this constant value witht. Ifˆ E is quantified byk quantifying variablesvi that range over lbi..ubi, then
the linking constraint defined as:
∀v1 ∈(lb1..ub1).∀v2 ∈(lb2..ub2). . . .∀vk∈(lbk..ubk).
E =AuxArray[v1+ i $ 2..k .(vi−ˆt)∗ j % 1..k−1 (ubj −lbj+ 1)] (5.2)
As an example, consider the quantified expression from Fig. 5.4:
f o r a l l i: i n t ( 1 . .n) .
e x i s t s j: i n t ( 1 . .m) .
x[i]=y[j]
Now we consider flattening this expression to two different targets: first to FlatZinc, where
ˆ
t = 1, and then to solver MINION, whereˆt= 0. When flattening to FlatZinc, the tailoring
middle-end produces the intermediate representation:
f o r a l l i: i n t ( 1 . .n) .
f o r a l l j: i n t ( 1 . .m) .
(x[i]=y[j] ) <=> auxArray[i+(j−1)∗n]
When flattening to MINION, the tailoring middle-end produces:
f o r a l l i: i n t ( 1 . .n) .
f o r a l l j: i n t ( 1 . .m) .
(x[i−1]=y[j−1]) <=> auxArray[ (i−1)+(j−1)∗(n) ]
Note, that every ‘i’ and ‘j’ has been replaced by ‘i−1’ and ‘j−1’ in order to adapt the
index to the target solvers array-dereference start valueˆt, since both index domains initially
ranged from‘1..n’ and‘1..m’, respectively.
Theorem 5.3.1. Thetime complexityof flattening with FLATTEN CLASSlies inO(ˆkm).
Proof. FLATTEN CLASS (Alg. 5.1) is an extension of Alg. 4.2 ( FLATTEN CSE ) that
lies inO(ˆkm)wherekˆis the maximal number of subexpressions any expression has in the instance/class. FLATTEN CLASSadditionally flattens quantified expressions, which adds the following operations:
1. Checking if an expression is quantified (line 12) requires an atomic check of expres- sionE that is independent of the number of its subexpressions and hence constant. This check is performed for all mu subexpressions, where in the worst case, when
the class has no CS,mu =m. Hence, in summary, the operation lies inO(m).
2. Generating the auxiliary array (line 13): this operation first requires the computation of the lengthlof the auxiliary array, which depends onv (see Eq. 5.1, the the num- ber of variables that quantify the subexpression, which is constant (the number of quantifying variables cannot be scaled). Second, an array of lengthl is constructed.
Since we assume atomic operations in the implementation, both operations require constant time, which is performed formu subexpressions. In summary, generating
an auxiliary variable lies inO(m), since in the worst case,mu =m.
3. Linking the auxiliary array to the flat expression (line 14) involves adding a constraint to the class. This requires to construct a quantification and to compute the index expression from Eq. 5.2 for the auxiliary array where both operations depend on the number of quantifying variablesv, which is constant. Again, since we assume atomic operations in the implementation, both operations are dependent only on the number of subexpressions in the to-be-flattened expression, k. The operations are performed formu subexpressions, hence, in summary, adding the linking constraint
lies inO(ˆkm), since in the worst case,mu =m, whereˆkis the maximal number of
subexpressions of any expression in the class.
In summary, the operations added to flatten classes addO(m) +O(m) +O(ˆkm), which, if
added to the overall runtime of CSE-flattening at instance level, results inO(ˆkm).