summaryrefslogtreecommitdiff
path: root/tex/thesis/contribution/contribution.tex
blob: 7d12299c78bf016dca222d93cd66aee4836fd8ee (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
\floatname{algorithm}{Listing}

\newcommand\stable{\mathsf{stable}}
\newcommand\eval{\mathsf{\textsc{eval}}}
\newcommand\stabilise{\mathsf{\textsc{stabilise}}}
\newcommand\solve{\mathsf{\textsc{solve}}}
\newcommand\system{\mathsf{system}}
\newcommand\invalidate{\mathsf{\textsc{invalidate}}}
\newcommand\fixpoint{\mathsf{\textsc{fixpoint}}}
\newcommand\strategy{\mathsf{\textsc{strategy}}}
\algblockx[Globals]{Globals}{EndGlobals}{\textbf{Globals:\\}}{} 
\algblockx[Assume]{Assumptions}{EndAssumptions}{\textbf{Assume:\\}}{} 


\chapter{Contribution} \label{chap:contribution}

The main theoretical contribution of this paper is an improvement on a
$\max$-strategy improvement algorithm for solving fixpoint equations
over the integers with monotonic
operators\cite{Gawlitza:2007:PFC:1762174.1762203}. The original
algorithm is presented in Section \ref{section:basic-algorithm}. We
employ the ideas of Seidl, et al. to design an algorithm which runs in
considerably less time (in the best case, and in most practical cases)
than the existing solver.

In this chapter we will begin by presenting the Work-list Depth First
Search (W-DFS) fixpoint algorithm developed by Seidl, et
al.\cite{DBLP:tr/trier/MI96-11}. We will then present a modification
to the algorithm to allow it to perform $\max$-strategy iteration
rather than fixpoint iteration. The chapter will then conclude with
our Local Demand-driven Strategy Improvement (LDSI) algorithm.

The existing algorithm as presented in Section
\ref{section:basic-algorithm} consists of two iterative operations:
fixpoint iteration and max-strategy iteration. Each of these
operations consists of naively ``evaluating'' the system repeatedly
until a further evaluation yields no change. It is shown by Gawlitza,
et al. that these iterations must converge in a finite number of
steps\cite{Gawlitza:2007:PFC:1762174.1762203}, but in practice this
naive approach performs many more operations than are necessary, in
many cases merely re-calculating results which are already known.

By making use of some data-dependencies within the equation systems it
is possible to reduce the amount of work that is to be done quite
considerably.

In order to aid our explanation of these algorithms we will now define
a few terms and notations. All variables are taken from the set $X$
and all values from the set $\D$.

\begin{definition}
  \textbf{Variable Assignments:} $X \to \D$. A function from a
  variable to a value in our domain. An underlined value
  (eg. $\underline{\infty}$) indicates a variable assignment mapping
  everything to that value. Variable assignments may be combined with
  $\oplus$ in the following way:
  \begin{align*}
    \rho \oplus \varrho = \left\{\begin{array}{lc}
        \varrho(x) & x \in \mathsf{domain}(\varrho) \\
        \rho(x) & \mbox{otherwise}
      \end{array}\right.
  \end{align*}
\end{definition}

\begin{definition}
  \textbf{Expressions:} For the purposes of this discussion we will
  consider expressions, $e \in E$, as $e : (X \to \D) \to \D$, a
  mapping from a variable assignment to the expression's value in that
  assignment.
  
  The subset of expressions of the form $\max(e_1, e_2,
  ... e_n)$, with $e_1, e_2, ..., e_n \in E$ are referred to as
  \emph{$\max$-expressions}, denoted by $E_{\max} \subset E$.
\end{definition}

\begin{definition}
  \textbf{Equation System:} $\{ x = e_x \mid x \in X, e_x \in E
  \}$. The values $x \in X$ are called ``variables'' while the values
  $e_x \in E$ are called ``right-hand-sides''.

  An equation system can also be considered as a function $\varepsilon
  : (X \to D) \to (X \to D)$; $\varepsilon[\rho](x) = e_x(\rho)$.
\end{definition}

\begin{definition}
  \textbf{Dependencies:} A variable or expression $x$ is said to
  \emph{depend on} $y$ if a change to the value of $y$ induces a
  change in the value of $x$. If $x$ depends on $y$ then $y$ is said
  to \emph{influence} $x$.
\end{definition}

\begin{definition}
  \textbf{Local:} A solver is said be local if, for some $e_x \in E$,
  the evaluation of $e_x$ only requires the evaluation of other
  variables which $e_x$ may depend on.
\end{definition}

\section{Fixpoint Iteration}
\subsection{Kleene Iteration}

A simple approach to fixpoint iteration over monotonic equations is to
simply iterate over the system repeatedly until a reevaluation results
in no change to the values of any variables. This approach will always
reach the least/greatest solution if there is one to be found, but it
will often perform many more evaluations than are necessary. This
algorithm is presented in Listing \ref{algo:kleene}.

\begin{algorithm}[H]
  \begin{algorithmic}
    \Assumptions
      \begin{tabularx}{0.9\textwidth}{rX}
        $\rho $:&$ X \to \D$, a variable assignment \\
        $\varepsilon $:&$ (X \to \D) \to (X \to \D)$, an equation system
      \end{tabularx}
    \EndAssumptions

    \State $n = 0$
    \State $\rho_0 = \underline{\infty}$
    \Repeat
      \State $\rho_{n+1} = \varepsilon[ \rho_{n} ]$
      \State $n = n + 1$
    \Until {$\rho_{n-1} = \rho_n$}
    \State \Return $\rho_n$
  \end{algorithmic}
  \caption{The Kleene iteration algorithm for solving fixpoint
    equations for their greatest solutions.}
  \label{algo:kleene}
\end{algorithm}

For each iteration the entire system is evaluated, irrespective of
whether it could possibly have changed value. This results in a
considerable inefficiency in practice, requiring the evaluation of
$O(n^3)$ right-hand-sides, and thus an approach which can evaluate
smaller portions of the system in each iteration would be a
significant improvement.

\subsection{W-DFS algorithm}

The W-DFS algorithm presented by Seidl, et al. takes into account some
form of data-dependencies as it solves the system. This gives it the
ability to leave portions of the system unevaluated when it is certain
that those values have not changed.

\begin{algorithm}[H]
  \begin{algorithmic}
    \Globals
    \begin{tabularx}{0.9\textwidth}{rX}
      $D : X \to \D$ & a mapping from variables to their current
      values, starting at $\{ x \mapsto \infty | \forall x \in X \}$
      \\

      I & A mapping from a variable to the variables which \emph{may}
      depend on it in their evaluation \\

      stable & The set of all variables whose values have stabilised
      \\ 

      system & The equation system, a mapping from a variable to its
      associated function \\
    \end{tabularx}
    \EndGlobals
  \end{algorithmic}

  \begin{algorithmic}
    \Function {eval} {$x$, $y$}
    \Comment{Evaluate $y$ for its value and note that when $y$
      changes, $x$ must be re-evaluated}
      \State $\solve(y)$
      \State $I[y] = I[y] \cup \{x\}$
      \State \Return $D[y]$
    \EndFunction 
  \end{algorithmic}

  \begin{algorithmic}
    \Function {solve} {$x$}
    \Comment{Solve a specific variable and place its value in $D$}
    \If {$x \not \in \stable$}
      \State $f = \system[x]$
      \State $\stable = \stable \cup \{x\}$
      \State $v = f( \lambda y . \eval(x, y) )$
      \If {$v \ne D[x]$}
        \State $D = \{ x \mapsto v \} \oplus D$
        \State $W = I[x]$
        \State $I(x) = \emptyset$
        \State $\stable = \stable \backslash W$
        \For {$v \in W$}
          \State $\solve(v)$
        \EndFor
      \EndIf
    \EndIf
    \EndFunction
  \end{algorithmic}

  \caption{The W-DFS alluded to in \cite{DBLP:tr/trier/MI96-11} and
    presented in \cite{fixpoint-slides}, modified to find
    greatest-fixpoints of monotonic fixpoint equations}
  \label{algo:w-dfs}
\end{algorithm}

The W-DFS algorithm over-approximates the dependencies for each
variable, keeping a map of which variables \emph{may} depend on other
variables.

The particular variation of W-DFS presented here is designed to return
the \emph{greatest} fixpoint of an equation system consisting of only
\emph{monotonic} expressions.



\section{$\max$-strategy Iteration}

The $\max$-strategy iteration can be viewed as an accelerated fixpoint
problem. We are attempting to find a strategy, $\sigma: E_{\max} \to
E$ that will result in the greatest value for each $e \in
E_{\max}$. Therefore if we consider our ``variables'' to be
$\max$-expressions and our ``values'' to be their subexpressions then we
can solve for the best $\max$-strategy using a similar approach
 and our
``comparison''

Because $\max$-strategy iteration is so similar to a standard fixpoint
problem it is possible

\subsection{Naive approach}

\begin{algorithm}[H]
  \begin{algorithmic}
    \Assumptions
      \begin{tabularx}{0.9\textwidth}{rX}
        $\sigma $:&$ E_{\max} \to E$, a $\max$ strategy \\

        $\varepsilon $:&$ (X \to \D) \to (X \to \D)$, an equation
        system \\

        $\rho $:&$ (X \to D)$, a variable assignment \\

        $P_{\max} $:&$ ((E_{\max} \to E_{\max}), (X \to \D)) \to
        (E_{\max} \to E_{\max})$, a $\max$-strategy improvement
        operator
      \end{tabularx}
    \EndAssumptions

    \State $n = 0$
    \State $\sigma_0 = \lambda x . -\infty$
    \State $\rho_0 = \underline{-\infty}$
    \Repeat
      \State $\sigma_{n+1} = P_{\max}(\sigma, \rho)$
      \State $\rho_{n+1} = \sigma(\varepsilon)[ \rho_{n} ]$
      \State $n = n + 1$
    \Until {$\sigma_{n-1} = \sigma_n$}
    \State \Return $\sigma_n$
  \end{algorithmic}
  \caption{The naive approach to strategy iteration}
  \label{algo:naive-strategy}
\end{algorithm}


\subsection{Adapted W-DFS algorithm}

This, then, allows us to use the W-DFS algorithm to re-evaluate only
those parts of the strategy which have changed. Listing
\ref{algo:w-dfs-max} presents this variation on W-DFS.

\begin{algorithm}[H]
  \begin{algorithmic}
    \Globals
    \begin{tabularx}{0.9\textwidth}{rX}
      $\sigma$ & A mapping from $\max$-expressions to their current
      sub-expressions, starting by mapping to the first
      sub-expression \\
      I & A mapping from a $\max$-expression to the sub-expressions
      which depend on it in their evaluation \\
      stable & The set of all $\max$-expressions whose strategies have
      stabilised \\
      system & The equation system, a mapping from a variable to its
      associated function \\
      bestStrategy & A function $(E_{\max}, (X \to D)) \to E$ mapping
      from an expression and a variable \\& assignment to the greatest
      subexpression in that context
    \end{tabularx}
    \EndGlobals

    \Function {eval} {$x$, $y$}
    \Comment{Evaluate $y$ for its value and note that when $y$
      changes, $x$ must be re-evaluated}
      \State $\solve(y)$
      \State $I[y] = I[y] \cup \{x\}$
      \State \Return $\sigma[y]$
    \EndFunction

    \Function {solve} {$x$}
    \Comment{Solve a specific expression and place its value in $\sigma$}
    \If {$x \not \in \stable$}
      \State $f = \system[x]$
      \State $\stable = \stable \cup \{x\}$
      \State $v = bestStrategy(f, \lambda y . \eval(x, y))$
      \If {$v \ne \sigma[x]$}
        \State $\sigma = \{ x \mapsto v\} \oplus \sigma$
        \State $W = I[x]$
        \State $I(x) = \emptyset$
        \State $\stable = \stable \backslash W$
        \For {$v \in W$}
        \State $\solve(v)$
        \EndFor
      \EndIf
    \EndIf
    \EndFunction
  \end{algorithmic}

  \caption{W-DFS, this time modified to find the best $\max$-strategy.}
  \label{algo:w-dfs-max}
\end{algorithm}


\section{Local Demand-driven Strategy Improvement (LDSI)}

W-DFS can be used to speed up both the $\max$-strategy iteration and
the fixpoint iteration as two independent processes, but each
fixpoint-iteration step still requires at least one complete
evaluation of the equation system per $\max$-strategy improvement
step. Ideally we would be able to adapt the W-DFS algorithm so that
the fixpoint-iteration and $\max$-strategy iteration steps could
provide some information to each other about what values have changed
so that at each step only a subset of the entire system would have to
be evaluated.

The new \emph{Local Demand-driven Strategy Improvement} algorithm,
\emph{LDSI}, seeks to do this. By adding an ``invalidate''
function to both W-DFS instances we provide an interface for the two
sides of the algorithm to indicate which values have changed. The
fixpoint-iteration operation can then be provided with a ``stabilise''
function to solve a partially-unstable system.

This essentially results in a $\max$-strategy iteration which, at each
strategy-improvement step, invalidates a portion of the current
fixpoint iteration. The fixpoint iteration then re-stabilises itself
by evaluating what values have been changed before returning control
to the $\max$-strategy iteration to change the system again. The back
and forth continues in this way until the $\max$-strategy can not
improve the strategy any further.



This algorithm is presented in two parts. Listings
\ref{algo:ldsi:fixpoint:globals},
\ref{algo:ldsi:fixpoint:eval},
\ref{algo:ldsi:fixpoint:invalidate},
\ref{algo:ldsi:fixpoint:solve} and
\ref{algo:ldsi:fixpoint:stabilise} present the
fixpoint-iteration portion of the algorithm, while Listing
\ref{algo:combined-max} presents the $\max$-strategy portion.  The
correctness of this new algorithm is argued in
\ref{sec:ldsi-correctness}.

\begin{algorithm}[H]
  \begin{algorithmic}
    \Globals
    \begin{tabularx}{0.9\textwidth}{rX}
      $V$ & $\{X \to \D\}$ - a mapping from variables to values,
      starting at $\{ x \mapsto \infty \}$ \\

      $I_{X,X}$ & $\{X \to X\}$ - a mapping from a variable to the
      variables it influences \\

      $I_{\max,\max}$ & $\{E_{\max} \to E_{\max}\}$ - a mapping from a
      $\max$-expression to the $\max$-expressions it influences \\

      $I_{X,\max}$ & $\{X \to E_{\max}\}$ - a mapping from a variable
      to the $\max$-expressions it influences \\

      $U_{X}$ & The set of all variables whose values have not
      stabilised to a final fixpoint value (unstable variables) \\

      $U_{\max}$ & The set of all $\max$ expressions whose strategies
      have not yet stabilised to their final strategy (unstable
      $\max$-expressions) \\

      $\varepsilon$ & The equation system, a mapping from a variable
      to its associated function \\
    \end{tabularx}
    \EndGlobals
  \end{algorithmic}
  \caption{Global variables used by the LDSI algorithm}
  \label{algo:ldsi:fixpoint:globals}
\end{algorithm}




\begin{algorithm}[H]
  \begin{algorithmic}
    \Function {evalfixpoint} {$x$, $y$}
      \State $\solve \fixpoint(y)$
      \State $I_{X,X}[y] = I_{X,X}[y] \cup \{x\}$
      \State \Return $D[y]$
    \EndFunction
  \end{algorithmic}
  \caption{Utility function used to track fixpoint variable dependencies.}
  \label{algo:ldsi:fixpoint:eval}
\end{algorithm}

\begin{algorithm}[H]
  \begin{algorithmic}
    \Function {invalidatefixpoint} {$x$}
    \If {$x \not \in U_X$}
      \State $U_X = U_X \cup \{x\}$
      \State $D[x] = \infty$
      \State $W = I[x]$
      \State $I[x] = \emptyset$
      \For {$v \in W$}
        \State $\invalidate \fixpoint(v)$
      \EndFor
    \EndIf
    \EndFunction
  \end{algorithmic}
  \caption{Fixpoint subroutine called from the $\max$-strategy
    iteration portion to invalidate fixpoint variables}
  \label{algo:ldsi:fixpoint:invalidate}
\end{algorithm}

\begin{algorithm}[H]
  \begin{algorithmic}
    \Function {solvefixpoint} {$x$}
    \Comment{Solve a specific expression and place its value in $D$}
    \If {$x \in U_X$}
      \State $f = \system[x]$
      \State $U_X = U_X \backslash \{x\}$
      \State $v = f( \lambda y . \eval(x, y) )$
      \If {$v \ne D[x]$}
        \State $D = \{ x \mapsto v \} \oplus D$
        \State $W = I_{X,X}[x]$
        \State $I_{X,X}[x] = \emptyset$
        \State $\invalidate \strategy(x)$
        \State $\stable = \stable \backslash W$
        \For {$v \in W$}
          \State $\solve \fixpoint(v)$
        \EndFor
      \EndIf
    \EndIf
    \EndFunction
  \end{algorithmic}
  \caption{The subroutine of the fixpoint iteration responsible for
    solving for each variable}
  \label{algo:ldsi:fixpoint:solve}
\end{algorithm}

After an evaluation of the $\solve \fixpoint$ procedure, the variable
supplied as its argument will have been stabilised within the current
$\max$-strategy.

After an evaluation of the $\stabilise \fixpoint$ procedure all
currently ``unstable'' variables in the current fixpoint will be
solved. This correlates to performing an entire fixpoint-iteration for
the greatest-fixpoint of the unstable portion of the equation
system. This is meant to be used to perform a sufficient
fixpoint-iteration to allow for the next $\max$-strategy improvement
step, but this relies on the $\max$-strategy solver to correctly
invalidate the fixpoint-iteration portion of the algorithm.


\begin{algorithm}[H]
  \begin{algorithmic}
    \Function {evalstrategy} {$x$, $y$}
    \Comment{Evaluate $y$ for its value and note that when $y$
      changes, $x$ must be re-evaluated}
      \State $\solve(y)$
      \State $I[y] = I[y] \cup \{x\}$
      \State \Return $\sigma[y]$
    \EndFunction

    \Function {invalidatestrategy} {$x \in X$} \Comment{$x$ is a
        \emph{variable}}
      \If {$x \not \in U_X$}
        \State $U_X = U_X \cup \{x\}$
        \State $W = I[x]$
        \State $I[x] = \emptyset$
        \For {$v \in W$}
          \State $\invalidate \fixpoint(v)$
        \EndFor
      \EndIf
    \EndFunction

    \Function {solvestrategy} {$x$}
    \Comment{Solve a specific variable and place its value in $\sigma$}
    \If {$x \not \in \stable$}
      \State $f = \system[x]$
      \State $\stable = \stable \cup \{x\}$
      \State $v = bestStrategy(f, \sigma(), \lambda y . \eval(x, y))$
      \If {$v \ne \sigma[x]$}
        \State $\sigma = \{ x \mapsto v \} \oplus \sigma$
        \State $W = I[x]$
        \State $I(x) = \emptyset$
        \State $\invalidate \fixpoint(\mathsf{lookupVarFor}(x))$
        \State $\stable = \stable \backslash W$
        \For {$v \in W$}
          \State $\solve(v)$
        \EndFor
      \EndIf
    \EndIf
    \EndFunction
  \end{algorithmic}

  \caption{The $\max$-strategy portion of the Combined W-DFS.}
  \label{algo:combined-max}
\end{algorithm}


\subsection{Correctness} \label{sec:combined-correctness}

This algorithm relies on the correctness of the underlying W-DFS
algorithm. This algorithm was presented in
\cite{DBLP:tr/trier/MI96-11}.

If we assume that W-DFS is correct then we only have to prove that the
combination algorithm is correct. For this it is sufficient to show
that the invalidate calls in both directions preserve the correctness
of the original algorithm.

// TODO finish this.
General idea:
\begin{itemize}
  \item
    Invalidate calls from fixpoint $\to$ max strategy are correct if
    the calls the other way are, because it completely re-solves the
    equations
  \item
    Invalidate calls from max strategy $\to$ fixpoint are correct
    because they effectively ``reset'' that part of the system,
    creating it to be entirely re-calculated.
\end{itemize}