isl_basic_map_remove_redundancies: sort constraints
[isl.git] / doc / implementation.tex
blob096e125324b4500f28ac5514224f6205b200fa2d
1 \section{Sets and Relations}
3 \begin{definition}[Polyhedral Set]
4 A {\em polyhedral set}\index{polyhedral set} $S$ is a finite union of basic sets
5 $S = \bigcup_i S_i$, each of which can be represented using affine
6 constraints
7 $$
8 S_i : \Z^n \to 2^{\Z^d} : \vec s \mapsto
9 S_i(\vec s) =
10 \{\, \vec x \in \Z^d \mid \exists \vec z \in \Z^e :
11 A \vec x + B \vec s + D \vec z + \vec c \geq \vec 0 \,\}
14 with $A \in \Z^{m \times d}$,
15 $B \in \Z^{m \times n}$,
16 $D \in \Z^{m \times e}$
17 and $\vec c \in \Z^m$.
18 \end{definition}
20 \begin{definition}[Parameter Domain of a Set]
21 Let $S \in \Z^n \to 2^{\Z^d}$ be a set.
22 The {\em parameter domain} of $S$ is the set
23 $$\pdom S \coloneqq \{\, \vec s \in \Z^n \mid S(\vec s) \ne \emptyset \,\}.$$
24 \end{definition}
26 \begin{definition}[Polyhedral Relation]
27 A {\em polyhedral relation}\index{polyhedral relation}
28 $R$ is a finite union of basic relations
29 $R = \bigcup_i R_i$ of type
30 $\Z^n \to 2^{\Z^{d_1+d_2}}$,
31 each of which can be represented using affine
32 constraints
34 R_i = \vec s \mapsto
35 R_i(\vec s) =
36 \{\, \vec x_1 \to \vec x_2 \in \Z^{d_1} \times \Z^{d_2}
37 \mid \exists \vec z \in \Z^e :
38 A_1 \vec x_1 + A_2 \vec x_2 + B \vec s + D \vec z + \vec c \geq \vec 0 \,\}
41 with $A_i \in \Z^{m \times d_i}$,
42 $B \in \Z^{m \times n}$,
43 $D \in \Z^{m \times e}$
44 and $\vec c \in \Z^m$.
45 \end{definition}
47 \begin{definition}[Parameter Domain of a Relation]
48 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation.
49 The {\em parameter domain} of $R$ is the set
50 $$\pdom R \coloneqq \{\, \vec s \in \Z^n \mid R(\vec s) \ne \emptyset \,\}.$$
51 \end{definition}
53 \begin{definition}[Domain of a Relation]
54 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation.
55 The {\em domain} of $R$ is the polyhedral set
56 $$\domain R \coloneqq \vec s \mapsto
57 \{\, \vec x_1 \in \Z^{d_1} \mid \exists \vec x_2 \in \Z^{d_2} :
58 (\vec x_1, \vec x_2) \in R(\vec s) \,\}
61 \end{definition}
63 \begin{definition}[Range of a Relation]
64 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation.
65 The {\em range} of $R$ is the polyhedral set
67 \range R \coloneqq \vec s \mapsto
68 \{\, \vec x_2 \in \Z^{d_2} \mid \exists \vec x_1 \in \Z^{d_1} :
69 (\vec x_1, \vec x_2) \in R(\vec s) \,\}
72 \end{definition}
74 \begin{definition}[Composition of Relations]
75 Let $R \in \Z^n \to 2^{\Z^{d_1+d_2}}$ and
76 $S \in \Z^n \to 2^{\Z^{d_2+d_3}}$ be two relations,
77 then the composition of
78 $R$ and $S$ is defined as
80 S \circ R \coloneqq
81 \vec s \mapsto
82 \{\, \vec x_1 \to \vec x_3 \in \Z^{d_1} \times \Z^{d_3}
83 \mid \exists \vec x_2 \in \Z^{d_2} :
84 \vec x_1 \to \vec x_2 \in R(\vec s) \wedge
85 \vec x_2 \to \vec x_3 \in S(\vec s)
86 \,\}
89 \end{definition}
91 \begin{definition}[Difference Set of a Relation]
92 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation.
93 The difference set ($\Delta \, R$) of $R$ is the set
94 of differences between image elements and the corresponding
95 domain elements,
97 \diff R \coloneqq
98 \vec s \mapsto
99 \{\, \vec \delta \in \Z^{d} \mid \exists \vec x \to \vec y \in R :
100 \vec \delta = \vec y - \vec x
101 \,\}
103 \end{definition}
105 \section{Simple Hull}\label{s:simple hull}
107 It is sometimes useful to have a single
108 basic set or basic relation that contains a given set or relation.
109 For rational sets, the obvious choice would be to compute the
110 (rational) convex hull. For integer sets, the obvious choice
111 would be the integer hull.
112 However, {\tt isl} currently does not support an integer hull operation
113 and even if it did, it would be fairly expensive to compute.
114 The convex hull operation is supported, but it is also fairly
115 expensive to compute given only an implicit representation.
117 Usually, it is not required to compute the exact integer hull,
118 and an overapproximation of this hull is sufficient.
119 The ``simple hull'' of a set is such an overapproximation
120 and it is defined as the (inclusion-wise) smallest basic set
121 that is described by constraints that are translates of
122 the constraints in the input set.
123 This means that the simple hull is relatively cheap to compute
124 and that the number of constraints in the simple hull is no
125 larger than the number of constraints in the input.
126 \begin{definition}[Simple Hull of a Set]
127 The {\em simple hull} of a set
128 $S = \bigcup_{1 \le i \le v} S_i$, with
130 S : \Z^n \to 2^{\Z^d} : \vec s \mapsto
131 S(\vec s) =
132 \left\{\, \vec x \in \Z^d \mid \exists \vec z \in \Z^e :
133 \bigvee_{1 \le i \le v}
134 A_i \vec x + B_i \vec s + D_i \vec z + \vec c_i \geq \vec 0 \,\right\}
136 is the set
138 H : \Z^n \to 2^{\Z^d} : \vec s \mapsto
139 S(\vec s) =
140 \left\{\, \vec x \in \Z^d \mid \exists \vec z \in \Z^e :
141 \bigwedge_{1 \le i \le v}
142 A_i \vec x + B_i \vec s + D_i \vec z + \vec c_i + \vec K_i \geq \vec 0
143 \,\right\}
146 with $\vec K_i$ the (component-wise) smallest non-negative integer vectors
147 such that $S \subseteq H$.
148 \end{definition}
149 The $\vec K_i$ can be obtained by solving a number of
150 LP problems, one for each element of each $\vec K_i$.
151 If any LP problem is unbounded, then the corresponding constraint
152 is dropped.
154 \section{Parametric Integer Programming}
156 \subsection{Introduction}\label{s:intro}
158 Parametric integer programming \parencite{Feautrier88parametric}
159 is used to solve many problems within the context of the polyhedral model.
160 Here, we are mainly interested in dependence analysis \parencite{Fea91}
161 and in computing a unique representation for existentially quantified
162 variables. The latter operation has been used for counting elements
163 in sets involving such variables
164 \parencite{BouletRe98,Verdoolaege2005experiences} and lies at the core
165 of the internal representation of {\tt isl}.
167 Parametric integer programming was first implemented in \texttt{PipLib}.
168 An alternative method for parametric integer programming
169 was later implemented in {\tt barvinok} \cite{barvinok-0.22}.
170 This method is not based on Feautrier's algorithm, but on rational
171 generating functions \cite{Woods2003short} and was inspired by the
172 ``digging'' technique of \textcite{DeLoera2004Three} for solving
173 non-parametric integer programming problems.
175 In the following sections, we briefly recall the dual simplex
176 method combined with Gomory cuts and describe some extensions
177 and optimizations. The main algorithm is applied to a matrix
178 data structure known as a tableau. In case of parametric problems,
179 there are two tableaus, one for the main problem and one for
180 the constraints on the parameters, known as the context tableau.
181 The handling of the context tableau is described in \autoref{s:context}.
183 \subsection{The Dual Simplex Method}
185 Tableaus can be represented in several slightly different ways.
186 In {\tt isl}, the dual simplex method uses the same representation
187 as that used by its incremental LP solver based on the \emph{primal}
188 simplex method. The implementation of this LP solver is based
189 on that of {\tt Simplify} \parencite{Detlefs2005simplify}, which, in turn,
190 was derived from the work of \textcite{Nelson1980phd}.
191 In the original \parencite{Nelson1980phd}, the tableau was implemented
192 as a sparse matrix, but neither {\tt Simplify} nor the current
193 implementation of {\tt isl} does so.
195 Given some affine constraints on the variables,
196 $A \vec x + \vec b \ge \vec 0$, the tableau represents the relationship
197 between the variables $\vec x$ and non-negative variables
198 $\vec y = A \vec x + \vec b$ corresponding to the constraints.
199 The initial tableau contains $\begin{pmatrix}
200 \vec b & A
201 \end{pmatrix}$ and expresses the constraints $\vec y$ in the rows in terms
202 of the variables $\vec x$ in the columns. The main operation defined
203 on a tableau exchanges a column and a row variable and is called a pivot.
204 During this process, some coefficients may become rational.
205 As in the \texttt{PipLib} implementation,
206 {\tt isl} maintains a shared denominator per row.
207 The sample value of a tableau is one where each column variable is assigned
208 zero and each row variable is assigned the constant term of the row.
209 This sample value represents a valid solution if each constraint variable
210 is assigned a non-negative value, i.e., if the constant terms of
211 rows corresponding to constraints are all non-negative.
213 The dual simplex method starts from an initial sample value that
214 may be invalid, but that is known to be (lexicographically) no
215 greater than any solution, and gradually increments this sample value
216 through pivoting until a valid solution is obtained.
217 In particular, each pivot exchanges a row variable
218 $r = -n + \sum_i a_i \, c_i$ with negative
219 sample value $-n$ with a column variable $c_j$
220 such that $a_j > 0$. Since $c_j = (n + r - \sum_{i\ne j} a_i \, c_i)/a_j$,
221 the new row variable will have a positive sample value $n$.
222 If no such column can be found, then the problem is infeasible.
223 By always choosing the column that leads to the (lexicographically)
224 smallest increment in the variables $\vec x$,
225 the first solution found is guaranteed to be the (lexicographically)
226 minimal solution \cite{Feautrier88parametric}.
227 In order to be able to determine the smallest increment, the tableau
228 is (implicitly) extended with extra rows defining the original
229 variables in terms of the column variables.
230 If we assume that all variables are non-negative, then we know
231 that the zero vector is no greater than the minimal solution and
232 then the initial extended tableau looks as follows.
234 \begin{tikzpicture}
235 \matrix (m) [matrix of math nodes]
237 & {} & 1 & \vec c \\
238 \vec x && |(top)| \vec 0 & I \\
239 \vec r && \vec b & |(bottom)|A \\
241 \begin{pgfonlayer}{background}
242 \node (core) [inner sep=0pt,fill=black!20,right delimiter=),left delimiter=(,fit=(top)(bottom)] {};
243 \end{pgfonlayer}
244 \end{tikzpicture}
246 Each column in this extended tableau is lexicographically positive
247 and will remain so because of the column choice explained above.
248 It is then clear that the value of $\vec x$ will increase in each step.
249 Note that there is no need to store the extra rows explicitly.
250 If a given $x_i$ is a column variable, then the corresponding row
251 is the unit vector $e_i$. If, on the other hand, it is a row variable,
252 then the row already appears somewhere else in the tableau.
254 In case of parametric problems, the sign of the constant term
255 may depend on the parameters. Each time the constant term of a constraint row
256 changes, we therefore need to check whether the new term can attain
257 negative and/or positive values over the current set of possible
258 parameter values, i.e., the context.
259 If all these terms can only attain non-negative values, the current
260 state of the tableau represents a solution. If one of the terms
261 can only attain non-positive values and is not identically zero,
262 the corresponding row can be pivoted.
263 Otherwise, we pick one of the terms that can attain both positive
264 and negative values and split the context into a part where
265 it only attains non-negative values and a part where it only attains
266 negative values.
268 \subsection{Gomory Cuts}
270 The solution found by the dual simplex method may have
271 non-integral coordinates. If so, some rational solutions
272 (including the current sample value), can be cut off by
273 applying a (parametric) Gomory cut.
274 Let $r = b(\vec p) + \sp {\vec a} {\vec c}$ be the row
275 corresponding to the first non-integral coordinate of $\vec x$,
276 with $b(\vec p)$ the constant term, an affine expression in the
277 parameters $\vec p$, i.e., $b(\vec p) = \sp {\vec f} {\vec p} + g$.
278 Note that only row variables can attain
279 non-integral values as the sample value of the column variables is zero.
280 Consider the expression
281 $b(\vec p) - \ceil{b(\vec p)} + \sp {\fract{\vec a}} {\vec c}$,
282 with $\ceil\cdot$ the ceiling function and $\fract\cdot$ the
283 fractional part. This expression is negative at the sample value
284 since $\vec c = \vec 0$ and $r = b(\vec p)$ is fractional, i.e.,
285 $\ceil{b(\vec p)} > b(\vec p)$. On the other hand, for each integral
286 value of $r$ and $\vec c \ge 0$, the expression is non-negative
287 because $b(\vec p) - \ceil{b(\vec p)} > -1$.
288 Imposing this expression to be non-negative therefore does not
289 invalidate any integral solutions, while it does cut away the current
290 fractional sample value. To be able to formulate this constraint,
291 a new variable $q = \floor{-b(\vec p)} = - \ceil{b(\vec p)}$ is added
292 to the context. This integral variable is uniquely defined by the constraints
293 $0 \le -d \, b(\vec p) - d \, q \le d - 1$, with $d$ the common
294 denominator of $\vec f$ and $g$. In practice, the variable
295 $q' = \floor{\sp {\fract{-f}} {\vec p} + \fract{-g}}$ is used instead
296 and the coefficients of the new constraint are adjusted accordingly.
297 The sign of the constant term of this new constraint need not be determined
298 as it is non-positive by construction.
299 When several of these extra context variables are added, it is important
300 to avoid adding duplicates.
301 Recent versions of {\tt PipLib} also check for such duplicates.
303 \subsection{Negative Unknowns and Maximization}
305 There are two places in the above algorithm where the unknowns $\vec x$
306 are assumed to be non-negative: the initial tableau starts from
307 sample value $\vec x = \vec 0$ and $\vec c$ is assumed to be non-negative
308 during the construction of Gomory cuts.
309 To deal with negative unknowns, \textcite[Appendix A.2]{Fea91}
310 proposed to use a ``big parameter'', say $M$, that is taken to be
311 an arbitrarily large positive number. Instead of looking for the
312 lexicographically minimal value of $\vec x$, we search instead
313 for the lexicographically minimal value of $\vec x' = \vec M + \vec x$.
314 The sample value $\vec x' = \vec 0$ of the initial tableau then
315 corresponds to $\vec x = -\vec M$, which is clearly not greater than
316 any potential solution. The sign of the constant term of a row
317 is determined lexicographically, with the coefficient of $M$ considered
318 first. That is, if the coefficient of $M$ is not zero, then its sign
319 is the sign of the entire term. Otherwise, the sign is determined
320 by the remaining affine expression in the parameters.
321 If the original problem has a bounded optimum, then the final sample
322 value will be of the form $\vec M + \vec v$ and the optimal value
323 of the original problem is then $\vec v$.
324 Maximization problems can be handled in a similar way by computing
325 the minimum of $\vec M - \vec x$.
327 When the optimum is unbounded, the optimal value computed for
328 the original problem will involve the big parameter.
329 In the original implementation of {\tt PipLib}, the big parameter could
330 even appear in some of the extra variables $\vec q$ created during
331 the application of a Gomory cut. The final result could then contain
332 implicit conditions on the big parameter through conditions on such
333 $\vec q$ variables. This problem was resolved in later versions
334 of {\tt PipLib} by taking $M$ to be divisible by any positive number.
335 The big parameter can then never appear in any $\vec q$ because
336 $\fract {\alpha M } = 0$. It should be noted, though, that an unbounded
337 problem usually (but not always)
338 indicates an incorrect formulation of the problem.
340 The original version of {\tt PipLib} required the user to ``manually''
341 add a big parameter, perform the reformulation and interpret the result
342 \parencite{Feautrier02}. Recent versions allow the user to simply
343 specify that the unknowns may be negative or that the maximum should
344 be computed and then these transformations are performed internally.
345 Although there are some application, e.g.,
346 that of \textcite{Feautrier92multi},
347 where it is useful to have explicit control over the big parameter,
348 negative unknowns and maximization are by far the most common applications
349 of the big parameter and we believe that the user should not be bothered
350 with such implementation issues.
351 The current version of {\tt isl} therefore does not
352 provide any interface for specifying big parameters. Instead, the user
353 can specify whether a maximum needs to be computed and no assumptions
354 are made on the sign of the unknowns. Instead, the sign of the unknowns
355 is checked internally and a big parameter is automatically introduced when
356 needed. For compatibility with {\tt PipLib}, the {\tt isl\_pip} tool
357 does explicitly add non-negativity constraints on the unknowns unless
358 the \verb+Urs_unknowns+ option is specified.
359 Currently, there is also no way in {\tt isl} of expressing a big
360 parameter in the output. Even though
361 {\tt isl} makes the same divisibility assumption on the big parameter
362 as recent versions of {\tt PipLib}, it will therefore eventually
363 produce an error if the problem turns out to be unbounded.
365 \subsection{Preprocessing}
367 In this section, we describe some transformations that are
368 or can be applied in advance to reduce the running time
369 of the actual dual simplex method with Gomory cuts.
371 \subsubsection{Feasibility Check and Detection of Equalities}
373 Experience with the original {\tt PipLib} has shown that Gomory cuts
374 do not perform very well on problems that are (non-obviously) empty,
375 i.e., problems with rational solutions, but no integer solutions.
376 In {\tt isl}, we therefore first perform a feasibility check on
377 the original problem considered as a non-parametric problem
378 over the combined space of unknowns and parameters.
379 In fact, we do not simply check the feasibility, but we also
380 check for implicit equalities among the integer points by computing
381 the integer affine hull. The algorithm used is the same as that
382 described in \autoref{s:GBR} below.
383 Computing the affine hull is fairly expensive, but it can
384 bring huge benefits if any equalities can be found or if the problem
385 turns out to be empty.
387 \subsubsection{Constraint Simplification}
389 If the coefficients of the unknown and parameters in a constraint
390 have a common factor, then this factor should be removed, possibly
391 rounding down the constant term. For example, the constraint
392 $2 x - 5 \ge 0$ should be simplified to $x - 3 \ge 0$.
393 {\tt isl} performs such simplifications on all sets and relations.
394 Recent versions of {\tt PipLib} also perform this simplification
395 on the input.
397 \subsubsection{Exploiting Equalities}\label{s:equalities}
399 If there are any (explicit) equalities in the input description,
400 {\tt PipLib} converts each into a pair of inequalities.
401 It is also possible to write $r$ equalities as $r+1$ inequalities
402 \parencite{Feautrier02}, but it is even better to \emph{exploit} the
403 equalities to reduce the dimensionality of the problem.
404 Given an equality involving at least one unknown, we pivot
405 the row corresponding to the equality with the column corresponding
406 to the last unknown with non-zero coefficient. The new column variable
407 can then be removed completely because it is identically zero,
408 thereby reducing the dimensionality of the problem by one.
409 The last unknown is chosen to ensure that the columns of the initial
410 tableau remain lexicographically positive. In particular, if
411 the equality is of the form $b + \sum_{i \le j} a_i \, x_i = 0$ with
412 $a_j \ne 0$, then the (implicit) top rows of the initial tableau
413 are changed as follows
415 \begin{tikzpicture}
416 \matrix [matrix of math nodes]
418 & {} & |(top)| 0 & I_1 & |(j)| & \\
419 j && 0 & & 1 & \\
420 && 0 & & & |(bottom)|I_2 \\
422 \node[overlay,above=2mm of j,anchor=south]{j};
423 \begin{pgfonlayer}{background}
424 \node (m) [inner sep=0pt,fill=black!20,right delimiter=),left delimiter=(,fit=(top)(bottom)] {};
425 \end{pgfonlayer}
426 \begin{scope}[xshift=4cm]
427 \matrix [matrix of math nodes]
429 & {} & |(top)| 0 & I_1 & \\
430 j && |(left)| -b/a_j & -a_i/a_j & \\
431 && 0 & & |(bottom)|I_2 \\
433 \begin{pgfonlayer}{background}
434 \node (m2) [inner sep=0pt,fill=black!20,right delimiter=),left delimiter=(,fit=(top)(bottom)(left)] {};
435 \end{pgfonlayer}
436 \end{scope}
437 \draw [shorten >=7mm,-to,thick,decorate,
438 decoration={snake,amplitude=.4mm,segment length=2mm,
439 pre=moveto,pre length=5mm,post length=8mm}]
440 (m) -- (m2);
441 \end{tikzpicture}
443 Currently, {\tt isl} also eliminates equalities involving only parameters
444 in a similar way, provided at least one of the coefficients is equal to one.
445 The application of parameter compression (see below)
446 would obviate the need for removing parametric equalities.
448 \subsubsection{Offline Symmetry Detection}\label{s:offline}
450 Some problems, notably those of \textcite{Bygde2010licentiate},
451 have a collection of constraints, say
452 $b_i(\vec p) + \sp {\vec a} {\vec x} \ge 0$,
453 that only differ in their (parametric) constant terms.
454 These constant terms will be non-negative on different parts
455 of the context and this context may have to be split for each
456 of the constraints. In the worst case, the basic algorithm may
457 have to consider all possible orderings of the constant terms.
458 Instead, {\tt isl} introduces a new parameter, say $u$, and
459 replaces the collection of constraints by the single
460 constraint $u + \sp {\vec a} {\vec x} \ge 0$ along with
461 context constraints $u \le b_i(\vec p)$.
462 Any solution to the new system is also a solution
463 to the original system since
464 $\sp {\vec a} {\vec x} \ge -u \ge -b_i(\vec p)$.
465 Conversely, $m = \min_i b_i(\vec p)$ satisfies the constraints
466 on $u$ and therefore extends a solution to the new system.
467 It can also be plugged into a new solution.
468 See \autoref{s:post} for how this substitution is currently performed
469 in {\tt isl}.
470 The method described in this section can only detect symmetries
471 that are explicitly available in the input.
472 See \autoref{s:online} for the detection
473 and exploitation of symmetries that appear during the course of
474 the dual simplex method.
476 \subsubsection{Parameter Compression}\label{s:compression}
478 It may in some cases be apparent from the equalities in the problem
479 description that there can only be a solution for a sublattice
480 of the parameters. In such cases ``parameter compression''
481 \parencite{Meister2004PhD,Meister2008} can be used to replace
482 the parameters by alternative ``dense'' parameters.
483 For example, if there is a constraint $2x = n$, then the system
484 will only have solutions for even values of $n$ and $n$ can be replaced
485 by $2n'$. Similarly, the parameters $n$ and $m$ in a system with
486 the constraint $2n = 3m$ can be replaced by a single parameter $n'$
487 with $n=3n'$ and $m=2n'$.
488 It is also possible to perform a similar compression on the unknowns,
489 but it would be more complicated as the compression would have to
490 preserve the lexicographical order. Moreover, due to our handling
491 of equalities described above there should be
492 no need for such variable compression.
493 Although parameter compression has been implemented in {\tt isl},
494 it is currently not yet used during parametric integer programming.
496 \subsection{Postprocessing}\label{s:post}
498 The output of {\tt PipLib} is a quast (quasi-affine selection tree).
499 Each internal node in this tree corresponds to a split of the context
500 based on a parametric constant term in the main tableau with indeterminate
501 sign. Each of these nodes may introduce extra variables in the context
502 corresponding to integer divisions. Each leaf of the tree prescribes
503 the solution in that part of the context that satisfies all the conditions
504 on the path leading to the leaf.
505 Such a quast is a very economical way of representing the solution, but
506 it would not be suitable as the (only) internal representation of
507 sets and relations in {\tt isl}. Instead, {\tt isl} represents
508 the constraints of a set or relation in disjunctive normal form.
509 The result of a parametric integer programming problem is then also
510 converted to this internal representation. Unfortunately, the conversion
511 to disjunctive normal form can lead to an explosion of the size
512 of the representation.
513 In some cases, this overhead would have to be paid anyway in subsequent
514 operations, but in other cases, especially for outside users that just
515 want to solve parametric integer programming problems, we would like
516 to avoid this overhead in future. That is, we are planning on introducing
517 quasts or a related representation as one of several possible internal
518 representations and on allowing the output of {\tt isl\_pip} to optionally
519 be printed as a quast.
521 Currently, {\tt isl} also does not have an internal representation
522 for expressions such as $\min_i b_i(\vec p)$ from the offline
523 symmetry detection of \autoref{s:offline}.
524 Assume that one of these expressions has $n$ bounds $b_i(\vec p)$.
525 If the expression
526 does not appear in the affine expression describing the solution,
527 but only in the constraints, and if moreover, the expression
528 only appears with a positive coefficient, i.e.,
529 $\min_i b_i(\vec p) \ge f_j(\vec p)$, then each of these constraints
530 can simply be reduplicated $n$ times, once for each of the bounds.
531 Otherwise, a conversion to disjunctive normal form
532 leads to $n$ cases, each described as $u = b_i(\vec p)$ with constraints
533 $b_i(\vec p) \le b_j(\vec p)$ for $j > i$
535 $b_i(\vec p) < b_j(\vec p)$ for $j < i$.
536 Note that even though this conversion leads to a size increase
537 by a factor of $n$, not detecting the symmetry could lead to
538 an increase by a factor of $n!$ if all possible orderings end up being
539 considered.
541 \subsection{Context Tableau}\label{s:context}
543 The main operation that a context tableau needs to provide is a test
544 on the sign of an affine expression over the elements of the context.
545 This sign can be determined by solving two integer linear feasibility
546 problems, one with a constraint added to the context that enforces
547 the expression to be non-negative and one where the expression is
548 negative. As already mentioned by \textcite{Feautrier88parametric},
549 any integer linear feasibility solver could be used, but the {\tt PipLib}
550 implementation uses a recursive call to the dual simplex with Gomory
551 cuts algorithm to determine the feasibility of a context.
552 In {\tt isl}, two ways of handling the context have been implemented,
553 one that performs the recursive call and one, used by default, that
554 uses generalized basis reduction.
555 We start with some optimizations that are shared between the two
556 implementations and then discuss additional details of each of them.
558 \subsubsection{Maintaining Witnesses}\label{s:witness}
560 A common feature of both integer linear feasibility solvers is that
561 they will not only say whether a set is empty or not, but if the set
562 is non-empty, they will also provide a \emph{witness} for this result,
563 i.e., a point that belongs to the set. By maintaining a list of such
564 witnesses, we can avoid many feasibility tests during the determination
565 of the signs of affine expressions. In particular, if the expression
566 evaluates to a positive number on some of these points and to a negative
567 number on some others, then no feasibility test needs to be performed.
568 If all the evaluations are non-negative, we only need to check for the
569 possibility of a negative value and similarly in case of all
570 non-positive evaluations. Finally, in the rare case that all points
571 evaluate to zero or at the start, when no points have been collected yet,
572 one or two feasibility tests need to be performed depending on the result
573 of the first test.
575 When a new constraint is added to the context, the points that
576 violate the constraint are temporarily removed. They are reconsidered
577 when we backtrack over the addition of the constraint, as they will
578 satisfy the negation of the constraint. It is only when we backtrack
579 over the addition of the points that they are finally removed completely.
580 When an extra integer division is added to the context,
581 the new coordinates of the
582 witnesses can easily be computed by evaluating the integer division.
583 The idea of keeping track of witnesses was first used in {\tt barvinok}.
585 \subsubsection{Choice of Constant Term on which to Split}
587 Recall that if there are no rows with a non-positive constant term,
588 but there are rows with an indeterminate sign, then the context
589 needs to be split along the constant term of one of these rows.
590 If there is more than one such row, then we need to choose which row
591 to split on first. {\tt PipLib} uses a heuristic based on the (absolute)
592 sizes of the coefficients. In particular, it takes the largest coefficient
593 of each row and then selects the row where this largest coefficient is smaller
594 than those of the other rows.
596 In {\tt isl}, we take that row for which non-negativity of its constant
597 term implies non-negativity of as many of the constant terms of the other
598 rows as possible. The intuition behind this heuristic is that on the
599 positive side, we will have fewer negative and indeterminate signs,
600 while on the negative side, we need to perform a pivot, which may
601 affect any number of rows meaning that the effect on the signs
602 is difficult to predict. This heuristic is of course much more
603 expensive to evaluate than the heuristic used by {\tt PipLib}.
604 More extensive tests are needed to evaluate whether the heuristic is worthwhile.
606 \subsubsection{Dual Simplex + Gomory Cuts}
608 When a new constraint is added to the context, the first steps
609 of the dual simplex method applied to this new context will be the same
610 or at least very similar to those taken on the original context, i.e.,
611 before the constraint was added. In {\tt isl}, we therefore apply
612 the dual simplex method incrementally on the context and backtrack
613 to a previous state when a constraint is removed again.
614 An initial implementation that was never made public would also
615 keep the Gomory cuts, but the current implementation backtracks
616 to before the point where Gomory cuts are added before adding
617 an extra constraint to the context.
618 Keeping the Gomory cuts has the advantage that the sample value
619 is always an integer point and that this point may also satisfy
620 the new constraint. However, due to the technique of maintaining
621 witnesses explained above,
622 we would not perform a feasibility test in such cases and then
623 the previously added cuts may be redundant, possibly resulting
624 in an accumulation of a large number of cuts.
626 If the parameters may be negative, then the same big parameter trick
627 used in the main tableau is applied to the context. This big parameter
628 is of course unrelated to the big parameter from the main tableau.
629 Note that it is not a requirement for this parameter to be ``big'',
630 but it does allow for some code reuse in {\tt isl}.
631 In {\tt PipLib}, the extra parameter is not ``big'', but this may be because
632 the big parameter of the main tableau also appears
633 in the context tableau.
635 Finally, it was reported by \textcite{Galea2009personal}, who
636 worked on a parametric integer programming implementation
637 in {\tt PPL} \parencite{PPL},
638 that it is beneficial to add cuts for \emph{all} rational coordinates
639 in the context tableau. Based on this report,
640 the initial {\tt isl} implementation was adapted accordingly.
642 \subsubsection{Generalized Basis Reduction}\label{s:GBR}
644 The default algorithm used in {\tt isl} for feasibility checking
645 is generalized basis reduction \parencite{Cook1991implementation}.
646 This algorithm is also used in the {\tt barvinok} implementation.
647 The algorithm is fairly robust, but it has some overhead.
648 We therefore try to avoid calling the algorithm in easy cases.
649 In particular, we incrementally keep track of points for which
650 the entire unit hypercube positioned at that point lies in the context.
651 This set is described by translates of the constraints of the context
652 and if (rationally) non-empty, any rational point
653 in the set can be rounded up to yield an integer point in the context.
655 A restriction of the algorithm is that it only works on bounded sets.
656 The affine hull of the recession cone therefore needs to be projected
657 out first. As soon as the algorithm is invoked, we then also
658 incrementally keep track of this recession cone. The reduced basis
659 found by one call of the algorithm is also reused as initial basis
660 for the next call.
662 Some problems lead to the
663 introduction of many integer divisions. Within a given context,
664 some of these integer divisions may be equal to each other, even
665 if the expressions are not identical, or they may be equal to some
666 affine combination of other variables.
667 To detect such cases, we compute the affine hull of the context
668 each time a new integer division is added. The algorithm used
669 for computing this affine hull is that of \textcite{Karr1976affine},
670 while the points used in this algorithm are obtained by performing
671 integer feasibility checks on that part of the context outside
672 the current approximation of the affine hull.
673 The list of witnesses is used to construct an initial approximation
674 of the hull, while any extra points found during the construction
675 of the hull is added to this list.
676 Any equality found in this way that expresses an integer division
677 as an \emph{integer} affine combination of other variables is
678 propagated to the main tableau, where it is used to eliminate that
679 integer division.
681 \subsection{Experiments}
683 \autoref{t:comparison} compares the execution times of {\tt isl}
684 (with both types of context tableau)
685 on some more difficult instances to those of other tools,
686 run on an Intel Xeon W3520 @ 2.66GHz.
687 These instances are available in the \lstinline{testsets/pip} directory
688 of the {\tt isl} distribution.
689 Easier problems such as the
690 test cases distributed with {\tt Pip\-Lib} can be solved so quickly
691 that we would only be measuring overhead such as input/output and conversions
692 and not the running time of the actual algorithm.
693 We compare the following versions:
694 {\tt piplib-1.4.0-5-g0132fd9},
695 {\tt barvinok-0.32.1-73-gc5d7751},
696 {\tt isl-0.05.1-82-g3a37260}
697 and {\tt PPL} version 0.11.2.
699 The first test case is the following dependence analysis problem
700 originating from the Phideo project \parencite{Verhaegh1995PhD}
701 that was communicated to us by Bart Kienhuis:
702 \begin{lstlisting}[flexiblecolumns=true,breaklines=true]{}
703 lexmax { [j1,j2] -> [i1,i2,i3,i4,i5,i6,i7,i8,i9,i10] : 1 <= i1,j1 <= 8 and 1 <= i2,i3,i4,i5,i6,i7,i8,i9,i10 <= 2 and 1 <= j2 <= 128 and i1-1 = j1-1 and i2-1+2*i3-2+4*i4-4+8*i5-8+16*i6-16+32*i7-32+64*i8-64+128*i9-128+256*i10-256=3*j2-3+66 };
704 \end{lstlisting}
705 This problem was the main inspiration
706 for some of the optimizations in \autoref{s:GBR}.
707 The second group of test cases are projections used during counting.
708 The first nine of these come from \textcite{Seghir2006minimizing}.
709 The remaining two come from \textcite{Verdoolaege2005experiences} and
710 were used to drive the first, Gomory cuts based, implementation
711 in {\tt isl}.
712 The third and final group of test cases are borrowed from
713 \textcite{Bygde2010licentiate} and inspired the offline symmetry detection
714 of \autoref{s:offline}. Without symmetry detection, the running times
715 are 11s and 5.9s.
716 All running times of {\tt barvinok} and {\tt isl} include a conversion
717 to disjunctive normal form. Without this conversion, the final two
718 cases can be solved in 0.07s and 0.21s.
719 The {\tt PipLib} implementation has some fixed limits and will
720 sometimes report the problem to be too complex (TC), while on some other
721 problems it will run out of memory (OOM).
722 The {\tt barvinok} implementation does not support problems
723 with a non-trivial lineality space (line) nor maximization problems (max).
724 The Gomory cuts based {\tt isl} implementation was terminated after 1000
725 minutes on the first problem. The gbr version introduces some
726 overhead on some of the easier problems, but is overall the clear winner.
728 \begin{table}
729 \begin{center}
730 \begin{tabular}{lrrrrr}
731 & {\tt PipLib} & {\tt barvinok} & {\tt isl} cut & {\tt isl} gbr & {\tt PPL} \\
732 \hline
733 \hline
734 % bart.pip
735 Phideo & TC & 793m & $>$999m & 2.7s & 372m \\
736 \hline
737 e1 & 0.33s & 3.5s & 0.08s & 0.11s & 0.18s \\
738 e3 & 0.14s & 0.13s & 0.10s & 0.10s & 0.17s \\
739 e4 & 0.24s & 9.1s & 0.09s & 0.11s & 0.70s \\
740 e5 & 0.12s & 6.0s & 0.06s & 0.14s & 0.17s \\
741 e6 & 0.10s & 6.8s & 0.17s & 0.08s & 0.21s \\
742 e7 & 0.03s & 0.27s & 0.04s & 0.04s & 0.03s \\
743 e8 & 0.03s & 0.18s & 0.03s & 0.04s & 0.01s \\
744 e9 & OOM & 70m & 2.6s & 0.94s & 22s \\
745 vd & 0.04s & 0.10s & 0.03s & 0.03s & 0.03s \\
746 bouleti & 0.25s & line & 0.06s & 0.06s & 0.15s \\
747 difficult & OOM & 1.3s & 1.7s & 0.33s & 1.4s \\
748 \hline
749 cnt/sum & TC & max & 2.2s & 2.2s & OOM \\
750 jcomplex & TC & max & 3.7s & 3.9s & OOM \\
751 \end{tabular}
752 \caption{Comparison of Execution Times}
753 \label{t:comparison}
754 \end{center}
755 \end{table}
757 \subsection{Online Symmetry Detection}\label{s:online}
759 Manual experiments on small instances of the problems of
760 \textcite{Bygde2010licentiate} and an analysis of the results
761 by the approximate MPA method developed by \textcite{Bygde2010licentiate}
762 have revealed that these problems contain many more symmetries
763 than can be detected using the offline method of \autoref{s:offline}.
764 In this section, we present an online detection mechanism that has
765 not been implemented yet, but that has shown promising results
766 in manual applications.
768 Let us first consider what happens when we do not perform offline
769 symmetry detection. At some point, one of the
770 $b_i(\vec p) + \sp {\vec a} {\vec x} \ge 0$ constraints,
771 say the $j$th constraint, appears as a column
772 variable, say $c_1$, while the other constraints are represented
773 as rows of the form $b_i(\vec p) - b_j(\vec p) + c$.
774 The context is then split according to the relative order of
775 $b_j(\vec p)$ and one of the remaining $b_i(\vec p)$.
776 The offline method avoids this split by replacing all $b_i(\vec p)$
777 by a single newly introduced parameter that represents the minimum
778 of these $b_i(\vec p)$.
779 In the online method the split is similarly avoided by the introduction
780 of a new parameter. In particular, a new parameter is introduced
781 that represents
782 $\left| b_j(\vec p) - b_i(\vec p) \right|_+ =
783 \max(b_j(\vec p) - b_i(\vec p), 0)$.
785 In general, let $r = b(\vec p) + \sp {\vec a} {\vec c}$ be a row
786 of the tableau such that the sign of $b(\vec p)$ is indeterminate
787 and such that exactly one of the elements of $\vec a$ is a $1$,
788 while all remaining elements are non-positive.
789 That is, $r = b(\vec p) + c_j - f$ with $f = -\sum_{i\ne j} a_i c_i \ge 0$.
790 We introduce a new parameter $t$ with
791 context constraints $t \ge -b(\vec p)$ and $t \ge 0$ and replace
792 the column variable $c_j$ by $c' + t$. The row $r$ is now equal
793 to $b(\vec p) + t + c' - f$. The constant term of this row is always
794 non-negative because any negative value of $b(\vec p)$ is compensated
795 by $t \ge -b(\vec p)$ while and non-negative value remains non-negative
796 because $t \ge 0$.
798 We need to show that this transformation does not eliminate any valid
799 solutions and that it does not introduce any spurious solutions.
800 Given a valid solution for the original problem, we need to find
801 a non-negative value of $c'$ satisfying the constraints.
802 If $b(\vec p) \ge 0$, we can take $t = 0$ so that
803 $c' = c_j - t = c_j \ge 0$.
804 If $b(\vec p) < 0$, we can take $t = -b(\vec p)$.
805 Since $r = b(\vec p) + c_j - f \ge 0$ and $f \ge 0$, we have
806 $c' = c_j + b(\vec p) \ge 0$.
807 Note that these choices amount to plugging in
808 $t = \left|-b(\vec p)\right|_+ = \max(-b(\vec p), 0)$.
809 Conversely, given a solution to the new problem, we need to find
810 a non-negative value of $c_j$, but this is easy since $c_j = c' + t$
811 and both of these are non-negative.
813 Plugging in $t = \max(-b(\vec p), 0)$ can be performed as in
814 \autoref{s:post}, but, as in the case of offline symmetry detection,
815 it may be better to provide a direct representation for such
816 expressions in the internal representation of sets and relations
817 or at least in a quast-like output format.
819 \section{Coalescing}\label{s:coalescing}
821 See \textcite{Verdoolaege2015impact} for details on integer set coalescing.
823 \section{Transitive Closure}
825 \subsection{Introduction}
827 \begin{definition}[Power of a Relation]
828 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation and
829 $k \in \Z_{\ge 1}$
830 a positive number, then power $k$ of relation $R$ is defined as
831 \begin{equation}
832 \label{eq:transitive:power}
833 R^k \coloneqq
834 \begin{cases}
835 R & \text{if $k = 1$}
837 R \circ R^{k-1} & \text{if $k \ge 2$}
839 \end{cases}
840 \end{equation}
841 \end{definition}
843 \begin{definition}[Transitive Closure of a Relation]
844 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation,
845 then the transitive closure $R^+$ of $R$ is the union
846 of all positive powers of $R$,
848 R^+ \coloneqq \bigcup_{k \ge 1} R^k
851 \end{definition}
852 Alternatively, the transitive closure may be defined
853 inductively as
854 \begin{equation}
855 \label{eq:transitive:inductive}
856 R^+ \coloneqq R \cup \left(R \circ R^+\right)
858 \end{equation}
860 Since the transitive closure of a polyhedral relation
861 may no longer be a polyhedral relation \parencite{Kelly1996closure},
862 we can, in the general case, only compute an approximation
863 of the transitive closure.
864 Whereas \textcite{Kelly1996closure} compute underapproximations,
865 we, like \textcite{Beletska2009}, compute overapproximations.
866 That is, given a relation $R$, we will compute a relation $T$
867 such that $R^+ \subseteq T$. Of course, we want this approximation
868 to be as close as possible to the actual transitive closure
869 $R^+$ and we want to detect the cases where the approximation is
870 exact, i.e., where $T = R^+$.
872 For computing an approximation of the transitive closure of $R$,
873 we follow the same general strategy as \textcite{Beletska2009}
874 and first compute an approximation of $R^k$ for $k \ge 1$ and then project
875 out the parameter $k$ from the resulting relation.
877 \begin{example}
878 As a trivial example, consider the relation
879 $R = \{\, x \to x + 1 \,\}$. The $k$th power of this map
880 for arbitrary $k$ is
882 R^k = k \mapsto \{\, x \to x + k \mid k \ge 1 \,\}
885 The transitive closure is then
887 \begin{aligned}
888 R^+ & = \{\, x \to y \mid \exists k \in \Z_{\ge 1} : y = x + k \,\}
890 & = \{\, x \to y \mid y \ge x + 1 \,\}
892 \end{aligned}
894 \end{example}
896 \subsection{Computing an Approximation of $R^k$}
897 \label{s:power}
899 There are some special cases where the computation of $R^k$ is very easy.
900 One such case is that where $R$ does not compose with itself,
901 i.e., $R \circ R = \emptyset$ or $\domain R \cap \range R = \emptyset$.
902 In this case, $R^k$ is only non-empty for $k=1$ where it is equal
903 to $R$ itself.
905 In general, it is impossible to construct a closed form
906 of $R^k$ as a polyhedral relation.
907 We will therefore need to make some approximations.
908 As a first approximations, we will consider each of the basic
909 relations in $R$ as simply adding one or more offsets to a domain element
910 to arrive at an image element and ignore the fact that some of these
911 offsets may only be applied to some of the domain elements.
912 That is, we will only consider the difference set $\Delta\,R$ of the relation.
913 In particular, we will first construct a collection $P$ of paths
914 that move through
915 a total of $k$ offsets and then intersect domain and range of this
916 collection with those of $R$.
917 That is,
918 \begin{equation}
919 \label{eq:transitive:approx}
920 K = P \cap \left(\domain R \to \range R\right)
922 \end{equation}
923 with
924 \begin{equation}
925 \label{eq:transitive:path}
926 P = \vec s \mapsto \{\, \vec x \to \vec y \mid
927 \exists k_i \in \Z_{\ge 0}, \vec\delta_i \in k_i \, \Delta_i(\vec s) :
928 \vec y = \vec x + \sum_i \vec\delta_i
929 \wedge
930 \sum_i k_i = k > 0
931 \,\}
932 \end{equation}
933 and with $\Delta_i$ the basic sets that compose
934 the difference set $\Delta\,R$.
935 Note that the number of basic sets $\Delta_i$ need not be
936 the same as the number of basic relations in $R$.
937 Also note that since addition is commutative, it does not
938 matter in which order we add the offsets and so we are allowed
939 to group them as we did in \eqref{eq:transitive:path}.
941 If all the $\Delta_i$s are singleton sets
942 $\Delta_i = \{\, \vec \delta_i \,\}$ with $\vec \delta_i \in \Z^d$,
943 then \eqref{eq:transitive:path} simplifies to
944 \begin{equation}
945 \label{eq:transitive:singleton}
946 P = \{\, \vec x \to \vec y \mid
947 \exists k_i \in \Z_{\ge 0} :
948 \vec y = \vec x + \sum_i k_i \, \vec \delta_i
949 \wedge
950 \sum_i k_i = k > 0
951 \,\}
952 \end{equation}
953 and then the approximation computed in \eqref{eq:transitive:approx}
954 is essentially the same as that of \textcite{Beletska2009}.
955 If some of the $\Delta_i$s are not singleton sets or if
956 some of $\vec \delta_i$s are parametric, then we need
957 to resort to further approximations.
959 To ease both the exposition and the implementation, we will for
960 the remainder of this section work with extended offsets
961 $\Delta_i' = \Delta_i \times \{\, 1 \,\}$.
962 That is, each offset is extended with an extra coordinate that is
963 set equal to one. The paths constructed by summing such extended
964 offsets have the length encoded as the difference of their
965 final coordinates. The path $P'$ can then be decomposed into
966 paths $P_i'$, one for each $\Delta_i$,
967 \begin{equation}
968 \label{eq:transitive:decompose}
969 P' = \left(
970 (P_m' \cup \identity) \circ \cdots \circ
971 (P_2' \cup \identity) \circ
972 (P_1' \cup \identity)
973 \right) \cap
974 \{\,
975 \vec x' \to \vec y' \mid y_{d+1} - x_{d+1} = k > 0
976 \,\}
978 \end{equation}
979 with
981 P_i' = \vec s \mapsto \{\, \vec x' \to \vec y' \mid
982 \exists k \in \Z_{\ge 1}, \vec \delta \in k \, \Delta_i'(\vec s) :
983 \vec y' = \vec x' + \vec \delta
984 \,\}
987 Note that each $P_i'$ contains paths of length at least one.
988 We therefore need to take the union with the identity relation
989 when composing the $P_i'$s to allow for paths that do not contain
990 any offsets from one or more $\Delta_i'$.
991 The path that consists of only identity relations is removed
992 by imposing the constraint $y_{d+1} - x_{d+1} > 0$.
993 Taking the union with the identity relation means that
994 that the relations we compose in \eqref{eq:transitive:decompose}
995 each consist of two basic relations. If there are $m$
996 disjuncts in the input relation, then a direct application
997 of the composition operation may therefore result in a relation
998 with $2^m$ disjuncts, which is prohibitively expensive.
999 It is therefore crucial to apply coalescing (\autoref{s:coalescing})
1000 after each composition.
1002 Let us now consider how to compute an overapproximation of $P_i'$.
1003 Those that correspond to singleton $\Delta_i$s are grouped together
1004 and handled as in \eqref{eq:transitive:singleton}.
1005 Note that this is just an optimization. The procedure described
1006 below would produce results that are at least as accurate.
1007 For simplicity, we first assume that no constraint in $\Delta_i'$
1008 involves any existentially quantified variables.
1009 We will return to existentially quantified variables at the end
1010 of this section.
1011 Without existentially quantified variables, we can classify
1012 the constraints of $\Delta_i'$ as follows
1013 \begin{enumerate}
1014 \item non-parametric constraints
1015 \begin{equation}
1016 \label{eq:transitive:non-parametric}
1017 A_1 \vec x + \vec c_1 \geq \vec 0
1018 \end{equation}
1019 \item purely parametric constraints
1020 \begin{equation}
1021 \label{eq:transitive:parametric}
1022 B_2 \vec s + \vec c_2 \geq \vec 0
1023 \end{equation}
1024 \item negative mixed constraints
1025 \begin{equation}
1026 \label{eq:transitive:mixed}
1027 A_3 \vec x + B_3 \vec s + \vec c_3 \geq \vec 0
1028 \end{equation}
1029 such that for each row $j$ and for all $\vec s$,
1031 \Delta_i'(\vec s) \cap
1032 \{\, \vec \delta' \mid B_{3,j} \vec s + c_{3,j} > 0 \,\}
1033 = \emptyset
1035 \item positive mixed constraints
1037 A_4 \vec x + B_4 \vec s + \vec c_4 \geq \vec 0
1039 such that for each row $j$, there is at least one $\vec s$ such that
1041 \Delta_i'(\vec s) \cap
1042 \{\, \vec \delta' \mid B_{4,j} \vec s + c_{4,j} > 0 \,\}
1043 \ne \emptyset
1045 \end{enumerate}
1046 We will use the following approximation $Q_i$ for $P_i'$:
1047 \begin{equation}
1048 \label{eq:transitive:Q}
1049 \begin{aligned}
1050 Q_i = \vec s \mapsto
1051 \{\,
1052 \vec x' \to \vec y'
1053 \mid {} & \exists k \in \Z_{\ge 1}, \vec f \in \Z^d :
1054 \vec y' = \vec x' + (\vec f, k)
1055 \wedge {}
1058 A_1 \vec f + k \vec c_1 \geq \vec 0
1059 \wedge
1060 B_2 \vec s + \vec c_2 \geq \vec 0
1061 \wedge
1062 A_3 \vec f + B_3 \vec s + \vec c_3 \geq \vec 0
1063 \,\}
1065 \end{aligned}
1066 \end{equation}
1067 To prove that $Q_i$ is indeed an overapproximation of $P_i'$,
1068 we need to show that for every $\vec s \in \Z^n$, for every
1069 $k \in \Z_{\ge 1}$ and for every $\vec f \in k \, \Delta_i(\vec s)$
1070 we have that
1071 $(\vec f, k)$ satisfies the constraints in \eqref{eq:transitive:Q}.
1072 If $\Delta_i(\vec s)$ is non-empty, then $\vec s$ must satisfy
1073 the constraints in \eqref{eq:transitive:parametric}.
1074 Each element $(\vec f, k) \in k \, \Delta_i'(\vec s)$ is a sum
1075 of $k$ elements $(\vec f_j, 1)$ in $\Delta_i'(\vec s)$.
1076 Each of these elements satisfies the constraints in
1077 \eqref{eq:transitive:non-parametric}, i.e.,
1079 \left[
1080 \begin{matrix}
1081 A_1 & \vec c_1
1082 \end{matrix}
1083 \right]
1084 \left[
1085 \begin{matrix}
1086 \vec f_j \\ 1
1087 \end{matrix}
1088 \right]
1089 \ge \vec 0
1092 The sum of these elements therefore satisfies the same set of inequalities,
1093 i.e., $A_1 \vec f + k \vec c_1 \geq \vec 0$.
1094 Finally, the constraints in \eqref{eq:transitive:mixed} are such
1095 that for any $\vec s$ in the parameter domain of $\Delta$,
1096 we have $-\vec r(\vec s) \coloneqq B_3 \vec s + \vec c_3 \le \vec 0$,
1097 i.e., $A_3 \vec f_j \ge \vec r(\vec s) \ge \vec 0$
1098 and therefore also $A_3 \vec f \ge \vec r(\vec s)$.
1099 Note that if there are no mixed constraints and if the
1100 rational relaxation of $\Delta_i(\vec s)$, i.e.,
1101 $\{\, \vec x \in \Q^d \mid A_1 \vec x + \vec c_1 \ge \vec 0\,\}$,
1102 has integer vertices, then the approximation is exact, i.e.,
1103 $Q_i = P_i'$. In this case, the vertices of $\Delta'_i(\vec s)$
1104 generate the rational cone
1105 $\{\, \vec x' \in \Q^{d+1} \mid \left[
1106 \begin{matrix}
1107 A_1 & \vec c_1
1108 \end{matrix}
1109 \right] \vec x' \,\}$ and therefore $\Delta'_i(\vec s)$ is
1110 a Hilbert basis of this cone \parencite[Theorem~16.4]{Schrijver1986}.
1112 Note however that, as pointed out by \textcite{DeSmet2010personal},
1113 if there \emph{are} any mixed constraints, then the above procedure may
1114 not compute the most accurate affine approximation of
1115 $k \, \Delta_i(\vec s)$ with $k \ge 1$.
1116 In particular, we only consider the negative mixed constraints that
1117 happen to appear in the description of $\Delta_i(\vec s)$, while we
1118 should instead consider \emph{all} valid such constraints.
1119 It is also sufficient to consider those constraints because any
1120 constraint that is valid for $k \, \Delta_i(\vec s)$ is also
1121 valid for $1 \, \Delta_i(\vec s) = \Delta_i(\vec s)$.
1122 Take therefore any constraint
1123 $\spv a x + \spv b s + c \ge 0$ valid for $\Delta_i(\vec s)$.
1124 This constraint is also valid for $k \, \Delta_i(\vec s)$ iff
1125 $k \, \spv a x + \spv b s + c \ge 0$.
1126 If $\spv b s + c$ can attain any positive value, then $\spv a x$
1127 may be negative for some elements of $\Delta_i(\vec s)$.
1128 We then have $k \, \spv a x < \spv a x$ for $k > 1$ and so the constraint
1129 is not valid for $k \, \Delta_i(\vec s)$.
1130 We therefore need to impose $\spv b s + c \le 0$ for all values
1131 of $\vec s$ such that $\Delta_i(\vec s)$ is non-empty, i.e.,
1132 $\vec b$ and $c$ need to be such that $- \spv b s - c \ge 0$ is a valid
1133 constraint of $\Delta_i(\vec s)$. That is, $(\vec b, c)$ are the opposites
1134 of the coefficients of a valid constraint of $\Delta_i(\vec s)$.
1135 The approximation of $k \, \Delta_i(\vec s)$ can therefore be obtained
1136 using three applications of Farkas' lemma. The first obtains the coefficients
1137 of constraints valid for $\Delta_i(\vec s)$. The second obtains
1138 the coefficients of constraints valid for the projection of $\Delta_i(\vec s)$
1139 onto the parameters. The opposite of the second set is then computed
1140 and intersected with the first set. The result is the set of coefficients
1141 of constraints valid for $k \, \Delta_i(\vec s)$. A final application
1142 of Farkas' lemma is needed to obtain the approximation of
1143 $k \, \Delta_i(\vec s)$ itself.
1145 \begin{example}
1146 Consider the relation
1148 n \to \{\, (x, y) \to (1 + x, 1 - n + y) \mid n \ge 2 \,\}
1151 The difference set of this relation is
1153 \Delta = n \to \{\, (1, 1 - n) \mid n \ge 2 \,\}
1156 Using our approach, we would only consider the mixed constraint
1157 $y - 1 + n \ge 0$, leading to the following approximation of the
1158 transitive closure:
1160 n \to \{\, (x, y) \to (o_0, o_1) \mid n \ge 2 \wedge o_1 \le 1 - n + y \wedge o_0 \ge 1 + x \,\}
1163 If, instead, we apply Farkas's lemma to $\Delta$, i.e.,
1164 \begin{verbatim}
1165 D := [n] -> { [1, 1 - n] : n >= 2 };
1166 CD := coefficients D;
1168 \end{verbatim}
1169 we obtain
1170 \begin{verbatim}
1171 { rat: coefficients[[c_cst, c_n] -> [i2, i3]] : i3 <= c_n and
1172 i3 <= c_cst + 2c_n + i2 }
1173 \end{verbatim}
1174 The pure-parametric constraints valid for $\Delta$,
1175 \begin{verbatim}
1176 P := { [a,b] -> [] }(D);
1177 CP := coefficients P;
1179 \end{verbatim}
1181 \begin{verbatim}
1182 { rat: coefficients[[c_cst, c_n] -> []] : c_n >= 0 and 2c_n >= -c_cst }
1183 \end{verbatim}
1184 Negating these coefficients and intersecting with \verb+CD+,
1185 \begin{verbatim}
1186 NCP := { rat: coefficients[[a,b] -> []]
1187 -> coefficients[[-a,-b] -> []] }(CP);
1188 CK := wrap((unwrap CD) * (dom (unwrap NCP)));
1190 \end{verbatim}
1191 we obtain
1192 \begin{verbatim}
1193 { rat: [[c_cst, c_n] -> [i2, i3]] : i3 <= c_n and
1194 i3 <= c_cst + 2c_n + i2 and c_n <= 0 and 2c_n <= -c_cst }
1195 \end{verbatim}
1196 The approximation for $k\,\Delta$,
1197 \begin{verbatim}
1198 K := solutions CK;
1200 \end{verbatim}
1201 is then
1202 \begin{verbatim}
1203 [n] -> { rat: [i0, i1] : i1 <= -i0 and i0 >= 1 and i1 <= 2 - n - i0 }
1204 \end{verbatim}
1205 Finally, the computed approximation for $R^+$,
1206 \begin{verbatim}
1207 T := unwrap({ [dx,dy] -> [[x,y] -> [x+dx,y+dy]] }(K));
1208 R := [n] -> { [x,y] -> [x+1,y+1-n] : n >= 2 };
1209 T := T * ((dom R) -> (ran R));
1211 \end{verbatim}
1213 \begin{verbatim}
1214 [n] -> { [x, y] -> [o0, o1] : o1 <= x + y - o0 and
1215 o0 >= 1 + x and o1 <= 2 - n + x + y - o0 and n >= 2 }
1216 \end{verbatim}
1217 \end{example}
1219 Existentially quantified variables can be handled by
1220 classifying them into variables that are uniquely
1221 determined by the parameters, variables that are independent
1222 of the parameters and others. The first set can be treated
1223 as parameters and the second as variables. Constraints involving
1224 the other existentially quantified variables are removed.
1226 \begin{example}
1227 Consider the relation
1230 n \to \{\, x \to y \mid \exists \, \alpha_0, \alpha_1: 7\alpha_0 = -2 + n \wedge 5\alpha_1 = -1 - x + y \wedge y \ge 6 + x \,\}
1233 The difference set of this relation is
1235 \Delta = \Delta \, R =
1236 n \to \{\, x \mid \exists \, \alpha_0, \alpha_1: 7\alpha_0 = -2 + n \wedge 5\alpha_1 = -1 + x \wedge x \ge 6 \,\}
1239 The existentially quantified variables can be defined in terms
1240 of the parameters and variables as
1242 \alpha_0 = \floor{\frac{-2 + n}7}
1243 \qquad
1244 \text{and}
1245 \qquad
1246 \alpha_1 = \floor{\frac{-1 + x}5}
1249 $\alpha_0$ can therefore be treated as a parameter,
1250 while $\alpha_1$ can be treated as a variable.
1251 This in turn means that $7\alpha_0 = -2 + n$ can be treated as
1252 a purely parametric constraint, while the other two constraints are
1253 non-parametric.
1254 The corresponding $Q$~\eqref{eq:transitive:Q} is therefore
1256 \begin{aligned}
1257 n \to \{\, (x,z) \to (y,w) \mid
1258 \exists\, \alpha_0, \alpha_1, k, f : {} &
1259 k \ge 1 \wedge
1260 y = x + f \wedge
1261 w = z + k \wedge {} \\
1263 7\alpha_0 = -2 + n \wedge
1264 5\alpha_1 = -k + x \wedge
1265 x \ge 6 k
1266 \,\}
1268 \end{aligned}
1270 Projecting out the final coordinates encoding the length of the paths,
1271 results in the exact transitive closure
1273 R^+ =
1274 n \to \{\, x \to y \mid \exists \, \alpha_0, \alpha_1: 7\alpha_1 = -2 + n \wedge 6\alpha_0 \ge -x + y \wedge 5\alpha_0 \le -1 - x + y \,\}
1277 \end{example}
1279 The fact that we ignore some impure constraints clearly leads
1280 to a loss of accuracy. In some cases, some of this loss can be recovered
1281 by not considering the parameters in a special way.
1282 That is, instead of considering the set
1284 \Delta = \diff R =
1285 \vec s \mapsto
1286 \{\, \vec \delta \in \Z^{d} \mid \exists \vec x \to \vec y \in R :
1287 \vec \delta = \vec y - \vec x
1288 \,\}
1290 we consider the set
1292 \Delta' = \diff R' =
1293 \{\, \vec \delta \in \Z^{n+d} \mid \exists
1294 (\vec s, \vec x) \to (\vec s, \vec y) \in R' :
1295 \vec \delta = (\vec s - \vec s, \vec y - \vec x)
1296 \,\}
1299 The first $n$ coordinates of every element in $\Delta'$ are zero.
1300 Projecting out these zero coordinates from $\Delta'$ is equivalent
1301 to projecting out the parameters in $\Delta$.
1302 The result is obviously a superset of $\Delta$, but all its constraints
1303 are of type \eqref{eq:transitive:non-parametric} and they can therefore
1304 all be used in the construction of $Q_i$.
1306 \begin{example}
1307 Consider the relation
1309 % [n] -> { [x, y] -> [1 + x, 1 - n + y] | n >= 2 }
1310 R = n \to \{\, (x, y) \to (1 + x, 1 - n + y) \mid n \ge 2 \,\}
1313 We have
1315 \diff R = n \to \{\, (1, 1 - n) \mid n \ge 2 \,\}
1317 and so, by treating the parameters in a special way, we obtain
1318 the following approximation for $R^+$:
1320 n \to \{\, (x, y) \to (x', y') \mid n \ge 2 \wedge y' \le 1 - n + y \wedge x' \ge 1 + x \,\}
1323 If we consider instead
1325 R' = \{\, (n, x, y) \to (n, 1 + x, 1 - n + y) \mid n \ge 2 \,\}
1327 then
1329 \diff R' = \{\, (0, 1, y) \mid y \le -1 \,\}
1331 and we obtain the approximation
1333 n \to \{\, (x, y) \to (x', y') \mid n \ge 2 \wedge x' \ge 1 + x \wedge y' \le x + y - x' \,\}
1336 If we consider both $\diff R$ and $\diff R'$, then we obtain
1338 n \to \{\, (x, y) \to (x', y') \mid n \ge 2 \wedge y' \le 1 - n + y \wedge x' \ge 1 + x \wedge y' \le x + y - x' \,\}
1341 Note, however, that this is not the most accurate affine approximation that
1342 can be obtained. That would be
1344 n \to \{\, (x, y) \to (x', y') \mid y' \le 2 - n + x + y - x' \wedge n \ge 2 \wedge x' \ge 1 + x \,\}
1347 \end{example}
1349 \subsection{Checking Exactness}
1351 The approximation $T$ for the transitive closure $R^+$ can be obtained
1352 by projecting out the parameter $k$ from the approximation $K$
1353 \eqref{eq:transitive:approx} of the power $R^k$.
1354 Since $K$ is an overapproximation of $R^k$, $T$ will also be an
1355 overapproximation of $R^+$.
1356 To check whether the results are exact, we need to consider two
1357 cases depending on whether $R$ is {\em cyclic}, where $R$ is defined
1358 to be cyclic if $R^+$ maps any element to itself, i.e.,
1359 $R^+ \cap \identity \ne \emptyset$.
1360 If $R$ is acyclic, then the inductive definition of
1361 \eqref{eq:transitive:inductive} is equivalent to its completion,
1362 i.e.,
1364 R^+ = R \cup \left(R \circ R^+\right)
1366 is a defining property.
1367 Since $T$ is known to be an overapproximation, we only need to check
1368 whether
1370 T \subseteq R \cup \left(R \circ T\right)
1373 This is essentially Theorem~5 of \textcite{Kelly1996closure}.
1374 The only difference is that they only consider lexicographically
1375 forward relations, a special case of acyclic relations.
1377 If, on the other hand, $R$ is cyclic, then we have to resort
1378 to checking whether the approximation $K$ of the power is exact.
1379 Note that $T$ may be exact even if $K$ is not exact, so the check
1380 is sound, but incomplete.
1381 To check exactness of the power, we simply need to check
1382 \eqref{eq:transitive:power}. Since again $K$ is known
1383 to be an overapproximation, we only need to check whether
1385 \begin{aligned}
1386 K'|_{y_{d+1} - x_{d+1} = 1} & \subseteq R'
1388 K'|_{y_{d+1} - x_{d+1} \ge 2} & \subseteq R' \circ K'|_{y_{d+1} - x_{d+1} \ge 1}
1390 \end{aligned}
1392 where $R' = \{\, \vec x' \to \vec y' \mid \vec x \to \vec y \in R
1393 \wedge y_{d+1} - x_{d+1} = 1\,\}$, i.e., $R$ extended with path
1394 lengths equal to 1.
1396 All that remains is to explain how to check the cyclicity of $R$.
1397 Note that the exactness on the power is always sound, even
1398 in the acyclic case, so we only need to be careful that we find
1399 all cyclic cases. Now, if $R$ is cyclic, i.e.,
1400 $R^+ \cap \identity \ne \emptyset$, then, since $T$ is
1401 an overapproximation of $R^+$, also
1402 $T \cap \identity \ne \emptyset$. This in turn means
1403 that $\Delta \, K'$ contains a point whose first $d$ coordinates
1404 are zero and whose final coordinate is positive.
1405 In the implementation we currently perform this test on $P'$ instead of $K'$.
1406 Note that if $R^+$ is acyclic and $T$ is not, then the approximation
1407 is clearly not exact and the approximation of the power $K$
1408 will not be exact either.
1410 \subsection{Decomposing $R$ into strongly connected components}
1412 If the input relation $R$ is a union of several basic relations
1413 that can be partially ordered
1414 then the accuracy of the approximation may be improved by computing
1415 an approximation of each strongly connected components separately.
1416 For example, if $R = R_1 \cup R_2$ and $R_1 \circ R_2 = \emptyset$,
1417 then we know that any path that passes through $R_2$ cannot later
1418 pass through $R_1$, i.e.,
1419 \begin{equation}
1420 \label{eq:transitive:components}
1421 R^+ = R_1^+ \cup R_2^+ \cup \left(R_2^+ \circ R_1^+\right)
1423 \end{equation}
1424 We can therefore compute (approximations of) transitive closures
1425 of $R_1$ and $R_2$ separately.
1426 Note, however, that the condition $R_1 \circ R_2 = \emptyset$
1427 is actually too strong.
1428 If $R_1 \circ R_2$ is a subset of $R_2 \circ R_1$
1429 then we can reorder the segments
1430 in any path that moves through both $R_1$ and $R_2$ to
1431 first move through $R_1$ and then through $R_2$.
1433 This idea can be generalized to relations that are unions
1434 of more than two basic relations by constructing the
1435 strongly connected components in the graph with as vertices
1436 the basic relations and an edge between two basic relations
1437 $R_i$ and $R_j$ if $R_i$ needs to follow $R_j$ in some paths.
1438 That is, there is an edge from $R_i$ to $R_j$ iff
1439 \begin{equation}
1440 \label{eq:transitive:edge}
1441 R_i \circ R_j
1442 \not\subseteq
1443 R_j \circ R_i
1445 \end{equation}
1446 The components can be obtained from the graph by applying
1447 Tarjan's algorithm \parencite{Tarjan1972}.
1449 In practice, we compute the (extended) powers $K_i'$ of each component
1450 separately and then compose them as in \eqref{eq:transitive:decompose}.
1451 Note, however, that in this case the order in which we apply them is
1452 important and should correspond to a topological ordering of the
1453 strongly connected components. Simply applying Tarjan's
1454 algorithm will produce topologically sorted strongly connected components.
1455 The graph on which Tarjan's algorithm is applied is constructed on-the-fly.
1456 That is, whenever the algorithm checks if there is an edge between
1457 two vertices, we evaluate \eqref{eq:transitive:edge}.
1458 The exactness check is performed on each component separately.
1459 If the approximation turns out to be inexact for any of the components,
1460 then the entire result is marked inexact and the exactness check
1461 is skipped on the components that still need to be handled.
1463 It should be noted that \eqref{eq:transitive:components}
1464 is only valid for exact transitive closures.
1465 If overapproximations are computed in the right hand side, then the result will
1466 still be an overapproximation of the left hand side, but this result
1467 may not be transitively closed. If we only separate components based
1468 on the condition $R_i \circ R_j = \emptyset$, then there is no problem,
1469 as this condition will still hold on the computed approximations
1470 of the transitive closures. If, however, we have exploited
1471 \eqref{eq:transitive:edge} during the decomposition and if the
1472 result turns out not to be exact, then we check whether
1473 the result is transitively closed. If not, we recompute
1474 the transitive closure, skipping the decomposition.
1475 Note that testing for transitive closedness on the result may
1476 be fairly expensive, so we may want to make this check
1477 configurable.
1479 \begin{figure}
1480 \begin{center}
1481 \begin{tikzpicture}[x=0.5cm,y=0.5cm,>=stealth,shorten >=1pt]
1482 \foreach \x in {1,...,10}{
1483 \foreach \y in {1,...,10}{
1484 \draw[->] (\x,\y) -- (\x,\y+1);
1487 \foreach \x in {1,...,20}{
1488 \foreach \y in {5,...,15}{
1489 \draw[->] (\x,\y) -- (\x+1,\y);
1492 \end{tikzpicture}
1493 \end{center}
1494 \caption{The relation from \autoref{ex:closure4}}
1495 \label{f:closure4}
1496 \end{figure}
1497 \begin{example}
1498 \label{ex:closure4}
1499 Consider the relation in example {\tt closure4} that comes with
1500 the Omega calculator~\parencite{Omega_calc}, $R = R_1 \cup R_2$,
1501 with
1503 \begin{aligned}
1504 R_1 & = \{\, (x,y) \to (x,y+1) \mid 1 \le x,y \le 10 \,\}
1506 R_2 & = \{\, (x,y) \to (x+1,y) \mid 1 \le x \le 20 \wedge 5 \le y \le 15 \,\}
1508 \end{aligned}
1510 This relation is shown graphically in \autoref{f:closure4}.
1511 We have
1513 \begin{aligned}
1514 R_1 \circ R_2 &=
1515 \{\, (x,y) \to (x+1,y+1) \mid 1 \le x \le 9 \wedge 5 \le y \le 10 \,\}
1517 R_2 \circ R_1 &=
1518 \{\, (x,y) \to (x+1,y+1) \mid 1 \le x \le 10 \wedge 4 \le y \le 10 \,\}
1520 \end{aligned}
1522 Clearly, $R_1 \circ R_2 \subseteq R_2 \circ R_1$ and so
1524 \left(
1525 R_1 \cup R_2
1526 \right)^+
1528 \left(R_2^+ \circ R_1^+\right)
1529 \cup R_1^+
1530 \cup R_2^+
1533 \end{example}
1535 \begin{figure}
1536 \newcounter{n}
1537 \newcounter{t1}
1538 \newcounter{t2}
1539 \newcounter{t3}
1540 \newcounter{t4}
1541 \begin{center}
1542 \begin{tikzpicture}[>=stealth,shorten >=1pt]
1543 \setcounter{n}{7}
1544 \foreach \i in {1,...,\value{n}}{
1545 \foreach \j in {1,...,\value{n}}{
1546 \setcounter{t1}{2 * \j - 4 - \i + 1}
1547 \setcounter{t2}{\value{n} - 3 - \i + 1}
1548 \setcounter{t3}{2 * \i - 1 - \j + 1}
1549 \setcounter{t4}{\value{n} - \j + 1}
1550 \ifnum\value{t1}>0\ifnum\value{t2}>0
1551 \ifnum\value{t3}>0\ifnum\value{t4}>0
1552 \draw[thick,->] (\i,\j) to[out=20] (\i+3,\j);
1553 \fi\fi\fi\fi
1554 \setcounter{t1}{2 * \j - 1 - \i + 1}
1555 \setcounter{t2}{\value{n} - \i + 1}
1556 \setcounter{t3}{2 * \i - 4 - \j + 1}
1557 \setcounter{t4}{\value{n} - 3 - \j + 1}
1558 \ifnum\value{t1}>0\ifnum\value{t2}>0
1559 \ifnum\value{t3}>0\ifnum\value{t4}>0
1560 \draw[thick,->] (\i,\j) to[in=-20,out=20] (\i,\j+3);
1561 \fi\fi\fi\fi
1562 \setcounter{t1}{2 * \j - 1 - \i + 1}
1563 \setcounter{t2}{\value{n} - 1 - \i + 1}
1564 \setcounter{t3}{2 * \i - 1 - \j + 1}
1565 \setcounter{t4}{\value{n} - 1 - \j + 1}
1566 \ifnum\value{t1}>0\ifnum\value{t2}>0
1567 \ifnum\value{t3}>0\ifnum\value{t4}>0
1568 \draw[thick,->] (\i,\j) to (\i+1,\j+1);
1569 \fi\fi\fi\fi
1572 \end{tikzpicture}
1573 \end{center}
1574 \caption{The relation from \autoref{ex:decomposition}}
1575 \label{f:decomposition}
1576 \end{figure}
1577 \begin{example}
1578 \label{ex:decomposition}
1579 Consider the relation on the right of \textcite[Figure~2]{Beletska2009},
1580 reproduced in \autoref{f:decomposition}.
1581 The relation can be described as $R = R_1 \cup R_2 \cup R_3$,
1582 with
1584 \begin{aligned}
1585 R_1 &= n \mapsto \{\, (i,j) \to (i+3,j) \mid
1586 i \le 2 j - 4 \wedge
1587 i \le n - 3 \wedge
1588 j \le 2 i - 1 \wedge
1589 j \le n \,\}
1591 R_2 &= n \mapsto \{\, (i,j) \to (i,j+3) \mid
1592 i \le 2 j - 1 \wedge
1593 i \le n \wedge
1594 j \le 2 i - 4 \wedge
1595 j \le n - 3 \,\}
1597 R_3 &= n \mapsto \{\, (i,j) \to (i+1,j+1) \mid
1598 i \le 2 j - 1 \wedge
1599 i \le n - 1 \wedge
1600 j \le 2 i - 1 \wedge
1601 j \le n - 1\,\}
1603 \end{aligned}
1605 The figure shows this relation for $n = 7$.
1606 Both
1607 $R_3 \circ R_1 \subseteq R_1 \circ R_3$
1609 $R_3 \circ R_2 \subseteq R_2 \circ R_3$,
1610 which the reader can verify using the {\tt iscc} calculator:
1611 \begin{verbatim}
1612 R1 := [n] -> { [i,j] -> [i+3,j] : i <= 2 j - 4 and i <= n - 3 and
1613 j <= 2 i - 1 and j <= n };
1614 R2 := [n] -> { [i,j] -> [i,j+3] : i <= 2 j - 1 and i <= n and
1615 j <= 2 i - 4 and j <= n - 3 };
1616 R3 := [n] -> { [i,j] -> [i+1,j+1] : i <= 2 j - 1 and i <= n - 1 and
1617 j <= 2 i - 1 and j <= n - 1 };
1618 (R1 . R3) - (R3 . R1);
1619 (R2 . R3) - (R3 . R2);
1620 \end{verbatim}
1621 $R_3$ can therefore be moved forward in any path.
1622 For the other two basic relations, we have both
1623 $R_2 \circ R_1 \not\subseteq R_1 \circ R_2$
1625 $R_1 \circ R_2 \not\subseteq R_2 \circ R_1$
1626 and so $R_1$ and $R_2$ form a strongly connected component.
1627 By computing the power of $R_3$ and $R_1 \cup R_2$ separately
1628 and composing the results, the power of $R$ can be computed exactly
1629 using \eqref{eq:transitive:singleton}.
1630 As explained by \textcite{Beletska2009}, applying the same formula
1631 to $R$ directly, without a decomposition, would result in
1632 an overapproximation of the power.
1633 \end{example}
1635 \subsection{Partitioning the domains and ranges of $R$}
1637 The algorithm of \autoref{s:power} assumes that the input relation $R$
1638 can be treated as a union of translations.
1639 This is a reasonable assumption if $R$ maps elements of a given
1640 abstract domain to the same domain.
1641 However, if $R$ is a union of relations that map between different
1642 domains, then this assumption no longer holds.
1643 In particular, when an entire dependence graph is encoded
1644 in a single relation, as is done by, e.g.,
1645 \textcite[Section~6.1]{Barthou2000MSE}, then it does not make
1646 sense to look at differences between iterations of different domains.
1647 Now, arguably, a modified Floyd-Warshall algorithm should
1648 be applied to the dependence graph, as advocated by
1649 \textcite{Kelly1996closure}, with the transitive closure operation
1650 only being applied to relations from a given domain to itself.
1651 However, it is also possible to detect disjoint domains and ranges
1652 and to apply Floyd-Warshall internally.
1654 \LinesNumbered
1655 \begin{algorithm}
1656 \caption{The modified Floyd-Warshall algorithm of
1657 \protect\textcite{Kelly1996closure}}
1658 \label{a:Floyd}
1659 \SetKwInput{Input}{Input}
1660 \SetKwInput{Output}{Output}
1661 \Input{Relations $R_{pq}$, $0 \le p, q < n$}
1662 \Output{Updated relations $R_{pq}$ such that each relation
1663 $R_{pq}$ contains all indirect paths from $p$ to $q$ in the input graph}
1665 \BlankLine
1666 \SetAlgoVlined
1667 \DontPrintSemicolon
1669 \For{$r \in [0, n-1]$}{
1670 $R_{rr} \coloneqq R_{rr}^+$ \nllabel{l:Floyd:closure}\;
1671 \For{$p \in [0, n-1]$}{
1672 \For{$q \in [0, n-1]$}{
1673 \If{$p \ne r$ or $q \ne r$}{
1674 $R_{pq} \coloneqq R_{pq} \cup \left(R_{rq} \circ R_{pr}\right)
1675 \cup \left(R_{rq} \circ R_{rr} \circ R_{pr}\right)$
1676 \nllabel{l:Floyd:update}
1681 \end{algorithm}
1683 Let the input relation $R$ be a union of $m$ basic relations $R_i$.
1684 Let $D_{2i}$ be the domains of $R_i$ and $D_{2i+1}$ the ranges of $R_i$.
1685 The first step is to group overlapping $D_j$ until a partition is
1686 obtained. If the resulting partition consists of a single part,
1687 then we continue with the algorithm of \autoref{s:power}.
1688 Otherwise, we apply Floyd-Warshall on the graph with as vertices
1689 the parts of the partition and as edges the $R_i$ attached to
1690 the appropriate pairs of vertices.
1691 In particular, let there be $n$ parts $P_k$ in the partition.
1692 We construct $n^2$ relations
1694 R_{pq} \coloneqq \bigcup_{i \text{ s.t. } \domain R_i \subseteq P_p \wedge
1695 \range R_i \subseteq P_q} R_i
1698 apply \autoref{a:Floyd} and return the union of all resulting
1699 $R_{pq}$ as the transitive closure of $R$.
1700 Each iteration of the $r$-loop in \autoref{a:Floyd} updates
1701 all relations $R_{pq}$ to include paths that go from $p$ to $r$,
1702 possibly stay there for a while, and then go from $r$ to $q$.
1703 Note that paths that ``stay in $r$'' include all paths that
1704 pass through earlier vertices since $R_{rr}$ itself has been updated
1705 accordingly in previous iterations of the outer loop.
1706 In principle, it would be sufficient to use the $R_{pr}$
1707 and $R_{rq}$ computed in the previous iteration of the
1708 $r$-loop in Line~\ref{l:Floyd:update}.
1709 However, from an implementation perspective, it is easier
1710 to allow either or both of these to have been updated
1711 in the same iteration of the $r$-loop.
1712 This may result in duplicate paths, but these can usually
1713 be removed by coalescing (\autoref{s:coalescing}) the result of the union
1714 in Line~\ref{l:Floyd:update}, which should be done in any case.
1715 The transitive closure in Line~\ref{l:Floyd:closure}
1716 is performed using a recursive call. This recursive call
1717 includes the partitioning step, but the resulting partition will
1718 usually be a singleton.
1719 The result of the recursive call will either be exact or an
1720 overapproximation. The final result of Floyd-Warshall is therefore
1721 also exact or an overapproximation.
1723 \begin{figure}
1724 \begin{center}
1725 \begin{tikzpicture}[x=1cm,y=1cm,>=stealth,shorten >=3pt]
1726 \foreach \x/\y in {0/0,1/1,3/2} {
1727 \fill (\x,\y) circle (2pt);
1729 \foreach \x/\y in {0/1,2/2,3/3} {
1730 \draw (\x,\y) circle (2pt);
1732 \draw[->] (0,0) -- (0,1);
1733 \draw[->] (0,1) -- (1,1);
1734 \draw[->] (2,2) -- (3,2);
1735 \draw[->] (3,2) -- (3,3);
1736 \draw[->,dashed] (2,2) -- (3,3);
1737 \draw[->,dotted] (0,0) -- (1,1);
1738 \end{tikzpicture}
1739 \end{center}
1740 \caption{The relation (solid arrows) on the right of Figure~1 of
1741 \protect\textcite{Beletska2009} and its transitive closure}
1742 \label{f:COCOA:1}
1743 \end{figure}
1744 \begin{example}
1745 Consider the relation on the right of Figure~1 of
1746 \textcite{Beletska2009},
1747 reproduced in \autoref{f:COCOA:1}.
1748 This relation can be described as
1750 \begin{aligned}
1751 \{\, (x, y) \to (x_2, y_2) \mid {} & (3y = 2x \wedge x_2 = x \wedge 3y_2 = 3 + 2x \wedge x \ge 0 \wedge x \le 3) \vee {} \\
1752 & (x_2 = 1 + x \wedge y_2 = y \wedge x \ge 0 \wedge 3y \ge 2 + 2x \wedge x \le 2 \wedge 3y \le 3 + 2x) \,\}
1754 \end{aligned}
1756 Note that the domain of the upward relation overlaps with the range
1757 of the rightward relation and vice versa, but that the domain
1758 of neither relation overlaps with its own range or the domain of
1759 the other relation.
1760 The domains and ranges can therefore be partitioned into two parts,
1761 $P_0$ and $P_1$, shown as the white and black dots in \autoref{f:COCOA:1},
1762 respectively.
1763 Initially, we have
1765 \begin{aligned}
1766 R_{00} & = \emptyset
1768 R_{01} & =
1769 \{\, (x, y) \to (x+1, y) \mid
1770 (x \ge 0 \wedge 3y \ge 2 + 2x \wedge x \le 2 \wedge 3y \le 3 + 2x) \,\}
1772 R_{10} & =
1773 \{\, (x, y) \to (x_2, y_2) \mid (3y = 2x \wedge x_2 = x \wedge 3y_2 = 3 + 2x \wedge x \ge 0 \wedge x \le 3) \,\}
1775 R_{11} & = \emptyset
1777 \end{aligned}
1779 In the first iteration, $R_{00}$ remains the same ($\emptyset^+ = \emptyset$).
1780 $R_{01}$ and $R_{10}$ are therefore also unaffected, but
1781 $R_{11}$ is updated to include $R_{01} \circ R_{10}$, i.e.,
1782 the dashed arrow in the figure.
1783 This new $R_{11}$ is obviously transitively closed, so it is not
1784 changed in the second iteration and it does not have an effect
1785 on $R_{01}$ and $R_{10}$. However, $R_{00}$ is updated to
1786 include $R_{10} \circ R_{01}$, i.e., the dotted arrow in the figure.
1787 The transitive closure of the original relation is then equal to
1788 $R_{00} \cup R_{01} \cup R_{10} \cup R_{11}$.
1789 \end{example}
1791 \subsection{Incremental Computation}
1792 \label{s:incremental}
1794 In some cases it is possible and useful to compute the transitive closure
1795 of union of basic relations incrementally. In particular,
1796 if $R$ is a union of $m$ basic maps,
1798 R = \bigcup_j R_j
1801 then we can pick some $R_i$ and compute the transitive closure of $R$ as
1802 \begin{equation}
1803 \label{eq:transitive:incremental}
1804 R^+ = R_i^+ \cup
1805 \left(
1806 \bigcup_{j \ne i}
1807 R_i^* \circ R_j \circ R_i^*
1808 \right)^+
1810 \end{equation}
1811 For this approach to be successful, it is crucial that each
1812 of the disjuncts in the argument of the second transitive
1813 closure in \eqref{eq:transitive:incremental} be representable
1814 as a single basic relation, i.e., without a union.
1815 If this condition holds, then by using \eqref{eq:transitive:incremental},
1816 the number of disjuncts in the argument of the transitive closure
1817 can be reduced by one.
1818 Now, $R_i^* = R_i^+ \cup \identity$, but in some cases it is possible
1819 to relax the constraints of $R_i^+$ to include part of the identity relation,
1820 say on domain $D$. We will use the notation
1821 ${\cal C}(R_i,D) = R_i^+ \cup \identity_D$ to represent
1822 this relaxed version of $R^+$.
1823 \textcite{Kelly1996closure} use the notation $R_i^?$.
1824 ${\cal C}(R_i,D)$ can be computed by allowing $k$ to attain
1825 the value $0$ in \eqref{eq:transitive:Q} and by using
1827 P \cap \left(D \to D\right)
1829 instead of \eqref{eq:transitive:approx}.
1830 Typically, $D$ will be a strict superset of both $\domain R_i$
1831 and $\range R_i$. We therefore need to check that domain
1832 and range of the transitive closure are part of ${\cal C}(R_i,D)$,
1833 i.e., the part that results from the paths of positive length ($k \ge 1$),
1834 are equal to the domain and range of $R_i$.
1835 If not, then the incremental approach cannot be applied for
1836 the given choice of $R_i$ and $D$.
1838 In order to be able to replace $R^*$ by ${\cal C}(R_i,D)$
1839 in \eqref{eq:transitive:incremental}, $D$ should be chosen
1840 to include both $\domain R$ and $\range R$, i.e., such
1841 that $\identity_D \circ R_j \circ \identity_D = R_j$ for all $j\ne i$.
1842 \textcite{Kelly1996closure} say that they use
1843 $D = \domain R_i \cup \range R_i$, but presumably they mean that
1844 they use $D = \domain R \cup \range R$.
1845 Now, this expression of $D$ contains a union, so it not directly usable.
1846 \textcite{Kelly1996closure} do not explain how they avoid this union.
1847 Apparently, in their implementation,
1848 they are using the convex hull of $\domain R \cup \range R$
1849 or at least an approximation of this convex hull.
1850 We use the simple hull (\autoref{s:simple hull}) of $\domain R \cup \range R$.
1852 It is also possible to use a domain $D$ that does {\em not\/}
1853 include $\domain R \cup \range R$, but then we have to
1854 compose with ${\cal C}(R_i,D)$ more selectively.
1855 In particular, if we have
1856 \begin{equation}
1857 \label{eq:transitive:right}
1858 \text{for each $j \ne i$ either }
1859 \domain R_j \subseteq D \text{ or } \domain R_j \cap \range R_i = \emptyset
1860 \end{equation}
1861 and, similarly,
1862 \begin{equation}
1863 \label{eq:transitive:left}
1864 \text{for each $j \ne i$ either }
1865 \range R_j \subseteq D \text{ or } \range R_j \cap \domain R_i = \emptyset
1866 \end{equation}
1867 then we can refine \eqref{eq:transitive:incremental} to
1869 R_i^+ \cup
1870 \left(
1871 \left(
1872 \bigcup_{\shortstack{$\scriptstyle\domain R_j \subseteq D $\\
1873 $\scriptstyle\range R_j \subseteq D$}}
1874 {\cal C} \circ R_j \circ {\cal C}
1875 \right)
1876 \cup
1877 \left(
1878 \bigcup_{\shortstack{$\scriptstyle\domain R_j \cap \range R_i = \emptyset$\\
1879 $\scriptstyle\range R_j \subseteq D$}}
1880 \!\!\!\!\!
1881 {\cal C} \circ R_j
1882 \right)
1883 \cup
1884 \left(
1885 \bigcup_{\shortstack{$\scriptstyle\domain R_j \subseteq D $\\
1886 $\scriptstyle\range R_j \cap \domain R_i = \emptyset$}}
1887 \!\!\!\!\!
1888 R_j \circ {\cal C}
1889 \right)
1890 \cup
1891 \left(
1892 \bigcup_{\shortstack{$\scriptstyle\domain R_j \cap \range R_i = \emptyset$\\
1893 $\scriptstyle\range R_j \cap \domain R_i = \emptyset$}}
1894 \!\!\!\!\!
1896 \right)
1897 \right)^+
1900 If only property~\eqref{eq:transitive:right} holds,
1901 we can use
1903 R_i^+ \cup
1904 \left(
1905 \left(
1906 R_i^+ \cup \identity
1907 \right)
1908 \circ
1909 \left(
1910 \left(
1911 \bigcup_{\shortstack{$\scriptstyle\domain R_j \subseteq D $}}
1912 R_j \circ {\cal C}
1913 \right)
1914 \cup
1915 \left(
1916 \bigcup_{\shortstack{$\scriptstyle\domain R_j \cap \range R_i = \emptyset$}}
1917 \!\!\!\!\!
1919 \right)
1920 \right)^+
1921 \right)
1924 while if only property~\eqref{eq:transitive:left} holds,
1925 we can use
1927 R_i^+ \cup
1928 \left(
1929 \left(
1930 \left(
1931 \bigcup_{\shortstack{$\scriptstyle\range R_j \subseteq D $}}
1932 {\cal C} \circ R_j
1933 \right)
1934 \cup
1935 \left(
1936 \bigcup_{\shortstack{$\scriptstyle\range R_j \cap \domain R_i = \emptyset$}}
1937 \!\!\!\!\!
1939 \right)
1940 \right)^+
1941 \circ
1942 \left(
1943 R_i^+ \cup \identity
1944 \right)
1945 \right)
1949 It should be noted that if we want the result of the incremental
1950 approach to be transitively closed, then we can only apply it
1951 if all of the transitive closure operations involved are exact.
1952 If, say, the second transitive closure in \eqref{eq:transitive:incremental}
1953 contains extra elements, then the result does not necessarily contain
1954 the composition of these extra elements with powers of $R_i$.
1956 \subsection{An {\tt Omega}-like implementation}
1958 While the main algorithm of \textcite{Kelly1996closure} is
1959 designed to compute and underapproximation of the transitive closure,
1960 the authors mention that they could also compute overapproximations.
1961 In this section, we describe our implementation of an algorithm
1962 that is based on their ideas.
1963 Note that the {\tt Omega} library computes underapproximations
1964 \parencite[Section 6.4]{Omega_lib}.
1966 The main tool is Equation~(2) of \textcite{Kelly1996closure}.
1967 The input relation $R$ is first overapproximated by a ``d-form'' relation
1969 \{\, \vec i \to \vec j \mid \exists \vec \alpha :
1970 \vec L \le \vec j - \vec i \le \vec U
1971 \wedge
1972 (\forall p : j_p - i_p = M_p \alpha_p)
1973 \,\}
1976 where $p$ ranges over the dimensions and $\vec L$, $\vec U$ and
1977 $\vec M$ are constant integer vectors. The elements of $\vec U$
1978 may be $\infty$, meaning that there is no upper bound corresponding
1979 to that element, and similarly for $\vec L$.
1980 Such an overapproximation can be obtained by computing strides,
1981 lower and upper bounds on the difference set $\Delta \, R$.
1982 The transitive closure of such a ``d-form'' relation is
1983 \begin{equation}
1984 \label{eq:omega}
1985 \{\, \vec i \to \vec j \mid \exists \vec \alpha, k :
1986 k \ge 1 \wedge
1987 k \, \vec L \le \vec j - \vec i \le k \, \vec U
1988 \wedge
1989 (\forall p : j_p - i_p = M_p \alpha_p)
1990 \,\}
1992 \end{equation}
1993 The domain and range of this transitive closure are then
1994 intersected with those of the input relation.
1995 This is a special case of the algorithm in \autoref{s:power}.
1997 In their algorithm for computing lower bounds, the authors
1998 use the above algorithm as a substep on the disjuncts in the relation.
1999 At the end, they say
2000 \begin{quote}
2001 If an upper bound is required, it can be calculated in a manner
2002 similar to that of a single conjunct [sic] relation.
2003 \end{quote}
2004 Presumably, the authors mean that a ``d-form'' approximation
2005 of the whole input relation should be used.
2006 However, the accuracy can be improved by also trying to
2007 apply the incremental technique from the same paper,
2008 which is explained in more detail in \autoref{s:incremental}.
2009 In this case, ${\cal C}(R_i,D)$ can be obtained by
2010 allowing the value zero for $k$ in \eqref{eq:omega},
2011 i.e., by computing
2013 \{\, \vec i \to \vec j \mid \exists \vec \alpha, k :
2014 k \ge 0 \wedge
2015 k \, \vec L \le \vec j - \vec i \le k \, \vec U
2016 \wedge
2017 (\forall p : j_p - i_p = M_p \alpha_p)
2018 \,\}
2021 In our implementation we take as $D$ the simple hull
2022 (\autoref{s:simple hull}) of $\domain R \cup \range R$.
2023 To determine whether it is safe to use ${\cal C}(R_i,D)$,
2024 we check the following conditions, as proposed by
2025 \textcite{Kelly1996closure}:
2026 ${\cal C}(R_i,D) - R_i^+$ is not a union and for each $j \ne i$
2027 the condition
2029 \left({\cal C}(R_i,D) - R_i^+\right)
2030 \circ
2032 \circ
2033 \left({\cal C}(R_i,D) - R_i^+\right)
2037 holds.