\documentclass[journal,hidelinks]{IEEEtran}
\usepackage[utf8]{inputenc}
\usepackage[
  pdftitle={Assignment \#1},
  pdfauthor={Andrei Purcarus},
  pdfsubject={ECSE-526 -- Artificial Intelligence}
]{hyperref}
\usepackage{graphicx}
\usepackage[all]{hypcap}
\usepackage{cleveref}
\usepackage{indentfirst}
\usepackage[per-mode=symbol]{siunitx}

\title{ECSE-526 \\ Artificial Intelligence \\ Assignment \#1}
\author{Andrei~Purcarus,~260631911,~\IEEEmembership{McGill~University}}

\begin{document}
\sloppy

\maketitle

\begin{abstract}

An agent was designed to play the game of dynamic connect 4. To achieve this, adversarial search algorithms were analyzed and then improved to achieve good search performance. A heuristic evaluation function was also designed to evaluate game states when the search cannot find a clear win, and the trade-offs between heuristic complexity and search depth were analyzed. The result was a game-playing agent with adequate performance capable of playing a decent game of dynamic connect 4.

\end{abstract}

\section{Introduction}

\IEEEPARstart{T}{his} report describes the creation of an agent capable of playing the game of dynamic connect 4. First, we start by analyzing the minimax and alpha-beta search algorithms. Then, we describe a better search algorithm that we use in our agent. Finally, we analyze the heuristic evaluation functions that we use in our agent and discuss the trade-offs between the complexity of the evaluation function and the search depth achieved.

\section{Search}

We measured the performance of the minimax and alpha-beta search algorithms with depth cutoffs of $3$, $4$, $5$, and $6$ for each of the three states described in the assignment specifications. We used the heuristic described in \Cref{sec:heuristic}. For comparison, we also measured the performance of an ordered version of alpha-beta that reorders the moves at each node using the values of the heuristic function.

\subsection{Minimax}

The results for minimax search in the three different states are shown in \Cref{fig:state-1-minimax,fig:state-2-minimax,fig:state-3-minimax}. For each data set, we used a least squares fit to find an approximation of the form
\[
states = a * b^{depth}
\]
We used the exponential model since this closely approximates a tree with branching factor $b$. The resulting fits are shown on the graphs.

If we ignore the $a$ parameter since we know that its true value is $1$ ($states(0) = 1$), and we take the geometric mean of the $b$ parameters as our average branching factor, we get $b \approx 14.7$, so we have
\[
states = (14.7)^{depth}
\]

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-1/minimax.eps}
  \caption{Minimax search results for state (a).}
  \label{fig:state-1-minimax}
\end{figure}

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-2/minimax.eps}
  \caption{Minimax search results for state (b).}
  \label{fig:state-2-minimax}
\end{figure}

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-3/minimax.eps}
  \caption{Minimax search results for state (c).}
  \label{fig:state-3-minimax}
\end{figure}

\subsection{Alpha-Beta}

The results for alpha-beta search in the three different states are shown in \Cref{fig:state-1-alpha-beta,fig:state-2-alpha-beta,fig:state-3-alpha-beta}. For each data set, we used a least squares fit to find an approximation of the form
\[
states = a * b^{depth}
\]
We used the exponential model since this closely approximates a tree with branching factor $b$. The resulting fits are shown on the graphs.

If we ignore the $a$ parameter since we know that its true value is $1$ ($states(0) = 1$), and we take the geometric mean of the $b$ parameters as our average branching factor, we get $b \approx 5.67$, so we have
\[
states = (5.67)^{depth}
\]

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-1/alpha-beta.eps}
  \caption{Alpha-beta search results for state (a).}
  \label{fig:state-1-alpha-beta}
\end{figure}

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-2/alpha-beta.eps}
  \caption{Alpha-beta search results for state (b).}
  \label{fig:state-2-alpha-beta}
\end{figure}

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-3/alpha-beta.eps}
  \caption{Alpha-beta search results for state (c).}
  \label{fig:state-3-alpha-beta}
\end{figure}

\subsection{Ordered Alpha-Beta}

The results for an ordered alpha-beta search in the three different states are shown in \Cref{fig:state-1-ordered-alpha-beta,fig:state-2-ordered-alpha-beta,fig:state-3-ordered-alpha-beta}. For each data set, we used a least squares fit to find an approximation of the form
\[
states = a * b^{depth}
\]
We used the exponential model since this closely approximates a tree with branching factor $b$. The resulting fits are shown on the graphs.

If we ignore the $a$ parameter since we know that its true value is $1$ ($states(0) = 1$), and we take the geometric mean of the $b$ parameters as our average branching factor, we get $b \approx 3.0$, so we have
\[
states = (3.0)^{depth}
\]

Note that the $a$ parameter for this type of search deviates from the ideal. This is due to the fact that the least squares fit was not as good as for the other two types of search. A larger sample size would be needed to get a better approximation.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-1/ordered-alpha-beta.eps}
  \caption{Ordered alpha-beta search results for state (a).}
  \label{fig:state-1-ordered-alpha-beta}
\end{figure}

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-2/ordered-alpha-beta.eps}
  \caption{Ordered alpha-beta search results for state (b).}
  \label{fig:state-2-ordered-alpha-beta}
\end{figure}

\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.6\columnwidth]{state-3/ordered-alpha-beta.eps}
  \caption{Ordered alpha-beta search results for state (c).}
  \label{fig:state-3-ordered-alpha-beta}
\end{figure}

\subsection{Comparison}

Comparing the results obtained, we can see that alpha-beta pruning has a significant performance advantage over minimax, with an average branching factor $2.6$ times smaller. We can also see that move ordering using the heuristic function further diminishes this branching factor.

In theory, alpha-beta search can reach an effective branching factor as low as $\sqrt{b_{minimax}} = 3.83$. This occurs when all the moves are ordered perfectly according to their real values. We can see that in this case, move ordering using the heuristic function actually exceeded this theoretical limit. This can be accounted for by our limited sample size, which makes it hard to get a good least squares fit and thus has some error in the measure of the effective branching factor.

\section{Better Search}

In order to make the agent competitive, we used an augmented version of alpha-beta search. The first concern was the time limit imposed by competition. In order to be able to use a maximal amount of search time, we used an iterative version that increments the search depth until it runs out of time. To take advantage of the results of past depths in the next search, we sort the actions at the root after each iteration based on the values found. It is important that the sort be stable, since this ensures that an action that was more favorable at a lower depth will still be ahead of other actions that became equal at a higher depth. In this way, the best action across all depths searched is chosen.

In addition, the moves at nodes other than the root are ordered by the heuristic function before being searched. This yields better alpha-beta pruning performance. The exception is those nodes at the bottom of the tree for which the cost of sorting outweighs the benefit. A basic analysis shows that this cost is
approximately $h*b + c*b*lg(b)$ and the savings are at most $h*b^{\left \lfloor{d/2}\right \rfloor}$, where $b$ is the actual branching factor, $d$ is the distance between the node and the leaves of the tree, $h$ is the cost of a heuristic evaluation, and $c$ is the cost of a comparison and swap for sorting. This gives $d \ge 4$.

Next, a transposition table was added to keep track of states we've already seen. In order to limit the memory usage, the size was limited to $4$ million elements, and a LRU replacement strategy was used to replace old states.

Finally, to take advantage of all the time we have available, we also modified the agent to perform search on the opponent's turn. The results of this search are ignored, but the fact that search is performed allows the transposition table to be updated, which speeds up search on our turn.

For the complete implementation, see the \textbf{IterativeAlphaBeta} class in the \textit{src/search/iterative-alpha-beta.h} file.

\section{Heuristics}
\label{sec:heuristic}

To create heuristics, we used the guiding principle that a heuristic function should lead the agent to a position where it can find a winning position within its search depth. Two simple heuristics were combined to achieve this goal. These heuristics award points to connected pieces and to a dominant central position.

\subsection{Connected Pieces}

This heuristic awards $n(n-1)/2$ points for a set of $n$ connected pieces. This is important since the goal of the game is to get $4$ pieces in a row, so rewarding partial completion of the goal is likely to lead us to the goal. The corresponding class is \textbf{ConnectedPiecesV1}, which corresponds to one of the versions of this heuristic. For the other versions considered, see the \textit{src/game/heuristics/connected-pieces.h} file.

\subsection{Central Dominance}

This heuristic awards a certain number of points to each piece based on its position on the board (see \Cref{tab:central-dominance}), with central pieces getting more points. The values were chosen to give an approximate $1$ point increase as the pieces move to a more central square, with slight deviations to favor more central positions within a square. The values were also chosen to be exactly representable in binary floating point to avoid round-off errors. This heuristic gives the agent the incentive to move pieces to the center of the board, which in turn is apt to provide more opportunities for both connecting pieces or stopping the opponent from connecting pieces. In addition, this heuristic awards bonus points in the form of a multiplicative factor for a player that has at least $3$ pieces in the central positions (corresponding to the non-zero values in \Cref{tab:central-dominance}). The corresponding class is \textbf{CentralDominanceV2}, which corresponds to one of the versions of this heuristic. For the other versions considered, see the \textit{src/game/heuristics/central-dominance.h} file.

\begin{table}[!htb]
	\centering
	\caption{Central Dominance Lookup Table.}
	\label{tab:central-dominance}
	\resizebox{\columnwidth}{!}{\begin{tabular}{c|ccccccc}
		  &    1    &    2    &    3    &    4    &    5    &    6    &    7    \\ \hline
		1 & 0.0000f & 0.0000f & 0.0000f & 0.0000f & 0.0000f & 0.0000f & 0.0000f \\
		2 & 0.0000f & 0.8125f & 1.0000f & 1.1875f & 1.0000f & 0.8125f & 0.0000f \\
		3 & 0.0000f & 1.0000f & 2.0000f & 2.1875f & 2.0000f & 1.0000f & 0.0000f \\
		4 & 0.0000f & 1.1875f & 2.1875f & 2.3750f & 2.1875f & 1.1875f & 0.0000f \\
		5 & 0.0000f & 1.0000f & 2.0000f & 2.1875f & 2.0000f & 1.0000f & 0.0000f \\
		6 & 0.0000f & 0.8125f & 1.0000f & 1.1875f & 1.0000f & 0.8125f & 0.0000f \\
		7 & 0.0000f & 0.0000f & 0.0000f & 0.0000f & 0.0000f & 0.0000f & 0.0000f
	\end{tabular}}
\end{table}

\subsection{Complexity Trade-offs}

The use of a more complex evaluation function, provided this function more accurately evaluates a game state, is to be favored over a simpler one. There are several reasons for this. First, since the time taken by the search algorithm is exponential in depth and linear in evaluation function complexity, the loss of depth from having a more complex evaluation function is not likely to be significant. For example, if the evaluation function doubles in computation time and the average branching factor is $3.0$, at most the search will lose a depth of one. Second, a shallower search will use up less memory than a deeper one, though this is not as significant a concern since the memory usage for depth-first search is linear in depth. Third, a more complex evaluation function that better represents the game state is likely to lead to a better position and therefore has a better chance of winning, despite having a slightly lower search depth.

To test this trade-off, we played an agent using both \textbf{ConnectedPiecesV1} and \textbf{CentralDominanceV2} against an agent using only \textbf{ConnectedPiecesV1}. Both agents used the \textbf{IterativeAlphaBeta} search with a time limit of $\SI{1}{\second}$. To get an estimate of the speed of these agents, we ran them both on state (b) from the assignment specifications for $4$ runs and averaged the results. Agent 1 managed a depth of $10$, with $1,002,806$ states evaluated. Agent 2 managed a depth of $11$, with $1,363,925$ states evaluated. Agent 2 is therefore $\SI{36}{\percent}$ faster, and can reach an extra level of depth. However, after playing $20$ games between them, $10$ for each color, the results were $9$ wins and $1$ draw for agent 1 when it was playing as white and $9$ wins and $1$ draw when it was playing as black. Clearly, the more complicated heuristic dominated the simpler one, despite the search disadvantage. Note that a draw is defined as a repetition of moves, which indicated that the agents have reached a stalemate.

\section{Conclusions}

In conclusion, a game-playing agent was designed and analyzed. The basic alpha-beta search algorithm was augmented with several improvements, and a decent heuristic was designed for evaluating game states. The result is an agent that performs adequately in playing the game of dynamic connect 4.

\section{Acknowledgements}

High-level strategy and heuristics were discussed with Andrew Lowther and Sean Stappas.

\bibliographystyle{IEEEtran}
\bibliography{IEEEabrv,references}

\end{document}


\section{Algebra on a field}


\section{Performance with known Best BS}
In Sec~\ref{sec:op_over_infinite_plane} and~\ref{sec:op_over_infinite_plane_with_retransmission}, we study the network-level performance metrics such as packet loss rate, throughput. These metrics are actually the averaged over the entire networks, which give a global view about the quality of service in LPWAN networks but still have its limitations. With a target network level packet loss rate such as $10\%$, it is very possible that devices in the neighborhood of BS never fail while devices at the border of BS coverage actually packet loss rate higher than $10\%$.

In this section, we analyze the packet loss rate with known distance to the best BS under BS attach method and macro reception diversity, and then evaluate the maximum normalized load, in one-shot random access case, with outage probability and packet loss rate constraint.

For a typical device at the origin, we assume that the best BS station with label $y_b$ is $r_{0}$ far away from the typical device. The distribution of $r_0$ is as follows:
\begin{align}
\label{eq:pdf_modified_r_0}
f(r) = 2 \pi \lambda_b e^{\frac{2\sigma^2}{\gamma^2}}  \exp( -\lambda_b  e^{\frac{2\sigma^2}{\gamma^2}} \pi r^2 ) r
\end{align}

We now study the packet loss rate $P_{f}(r_{0})$ with macro reception diversity in this case. Network packet loss rate $P_{f}(r_{0})$ is expressed as follows:

Equivalent Radius $R_{\text{eq}}$. We consider normalized distance by $R_{\text{eq}}$.
\begin{align}
	\pi R_{\text{eq}} ^ 2 = \frac{1}{\lambda_{b}} \\
	 R_{\text{eq}}  = \frac{1}{\sqrt{\pi \lambda_{b}}}
\end{align}
\subsection{Best BS Attach}
\begin{align}
	\label{eq:link-level-packet_loss_rate}
	P_{f, b}(r_{0}) = 1- \exp(-p \lambda_{m} \pi A \theta_{T}^{\frac{2}{\gamma}} e^{\frac{2\sigma^2}{\gamma^2}}  r_{0}^2 )
%	P_{f, b}(r_{0}^{'}) = 1- \exp(-A L \theta_{T}^{\frac{2}{\gamma}} e^{\frac{2\sigma^2}{\gamma^2}}  r_{0}^{'2} )
\end{align}
From $\eqref{eq:pdf_modified_r_0}$ and $\eqref{eq:link-level-packet_loss_rate}$, the probability density function of packet loss rate $P_{f, b}(r_{0})$ is obtained as follows:
\begin{align}
	f_{P_{f,b}}(p_{f,b}) =\frac{1}{ A \theta^{\frac{2}{\gamma}} L } (1-p_f)^{ 1 / \left( A \theta^{\frac{2}{\gamma}} L\right) - 1}
\end{align}
The cumulative distribution function of $P_{f,b}$:
\begin{align}
F_{P_{f,b}}(p_{f,b}) =1 - (1-p_f)^{ 1 / \left( A \theta^{\frac{2}{\gamma}} L\right) }
\end{align}
According to the outage probability definition, $P_{\text{outage}, b}$ is as follows:
\begin{align}
	P_{\text{outage}, b} = (1-p_f)^{ 1 / \left( A \theta^{\frac{2}{\gamma}} L \right) }
\end{align}

\subsection{Macro Reception Diversity}
Recall that in case of macro reception diversity,
\begin{align}
\label{eq:definition_pf1}
P_{f, m}(r_{0}) &= \mathbb{P}\left\lbrace  \theta_{y_b} < \theta_{T}, \theta_{y_b} < \theta_{T} \text{where} \right\rbrace \nonumber\\
&= ( 1-p_s( r_{0} ) ) \mathbb{E} \left[  \prod_{r_i \in \Phi_{b}} (1-p_{s}(r_i)) \right], \text{with } r_i \in \left[ r_{0}, +\infty\right],
\end{align}

where $r_i$ refers to the distance between the device and BS with label $i$. Note that $r_i$ should be not less than distance to the best BS denoted by $r_{0}$, whose probability density function is given in $\eqref{eq:pdf_nearest_distance}$. Expression $\eqref{eq:definition_pf1}$ is actually the Probability Generating FunctionaL (PGFL) of Point point process $\Phi_{b}$ for Base Station, which states for some function $f(x)$ that $\mathbb{E}\left[ \prod_{x \in \Phi}f(x) \right] = \exp(-\lambda(\int_{\mathbb{R}^2}(1-f(x))dx))$. Thus, term in $\mathbb{E} \left[  \prod_{r_i \in \Phi_{b}} (1-p_{s}(r_i)) \right]$ in $\eqref{eq:definition_pf1}$ can be expressed as follows:
\begin{align}
\mathbb{E} \left[  \prod_{r_i \in \Phi_{b}} (1-p_{s}(r_i)) \right] &= \exp\left\lbrace -2\pi \lambda_{b} \int_{r_{0}}^{+\infty} p_{s}(r)rdr \right\rbrace \nonumber\\
&= \exp\left\lbrace  \int_{r_{0}}^{+\infty}  \exp(-p \lambda_{m} \pi A \theta_{T}^{\frac{2}{\gamma}} e^{\frac{2\sigma^2}{\gamma^2}}  r^2) 2 \pi \lambda_b e^{\frac{2\sigma^2}{\gamma^2}}  rdr  \right\rbrace \nonumber \\
&= \exp\left\lbrace -\pi \lambda_{b} e^{\frac{2\sigma^2}{\gamma^2}}  \int_{r_{0}^{2}}^{+\infty}  \exp(-p \lambda_{m} \pi A \theta_{T}^{\frac{2}{\gamma}} e^{\frac{2\sigma^2}{\gamma^2}}  r^2 ) dr^2  \right\rbrace \nonumber\\
&= \exp\left\lbrace -\frac{1}{ A \theta_{T}^{\frac{2}{\gamma}}  L }  \exp(-p \lambda_{m} \pi A \theta_{T}^{\frac{2}{\gamma}} e^{  \frac{2\sigma^2}{\gamma^2}  } r_{0}^2)  \right\rbrace
\end{align}

Thus,
\begin{align}
P_{f}(r_{0}) = ( 1- \exp(-p \lambda_{m} \pi A \theta_{T}^{\frac{2}{\gamma}} e^{\frac{2\sigma^2}{\gamma^2}}  r_{0}^2 ) )\exp\left\lbrace \!\!\!-\frac{1}{ A \theta_{T}^{\frac{2}{\gamma}}  L }  \!\exp(-p \lambda_{m} \pi A \theta_{T}^{\frac{2}{\gamma}} e^{  \frac{2\sigma^2}{\gamma^2}  } r_{0}^2)  \!\!\!\right\rbrace
\end{align}

Outage probability:
\begin{align}
	P_{\text{outage}} &= \int_{0}^{+\infty} \mathds{1}_{\left[ P_{f, m} > P_{f}^{\text{max}}\right] }f(r) dr \nonumber \\
	&= \int_{R}^{+\infty} f(r) dr \nonumber \\
	&= 1 - \int_{ 0 }^{ R } f(r) dr \nonumber \\
	&=  \exp( -\lambda_b  e^{\frac{2\sigma^2}{\gamma^2}} \pi R^2 ),
\end{align}
where $R$ is the minimum distance between typical device and BS $y_0$ satisfying condition $P_{f, m} > P_{f}^{\text{max}}$.
\qsong{To be further simplified...}


% Generated by GrindEQ Word-to-LaTeX 2008
% ========== UNREGISTERED! ========== Please register! ==========
% LaTeX/AMS-LaTeX

\documentclass[a4paper]{article}
\usepackage{anysize}
\marginsize{1cm}{1cm}{1cm}{1cm}

\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[dvips]{graphicx}
\usepackage{listings}
\lstset{language=haskell}
\lstset{commentstyle=\textit}
\lstset{mathescape=true}
%\lstset{labelstep=1}
%\lstset{backgroundcolor=,framerulecolor=}
\lstset{backgroundcolor=,rulecolor=}
\linespread{2.0}

\begin{document}

%%% remove comment delimiter ('%') and select language if required
%\selectlanguage{spanish}

\noindent
\section{A Concrete Implementation}

\noindent We now give a first implementation of these constructs. Our first goal is to write a heap and a reference that allow us to manipulate the heap in a stateful fashion. Since our heap is strongly typed, our first need is a collection of heterogeneous values where values may be added to the list; this kind of collection can be easily implemented with heterogeneous lists accessed with compile-time Church Numerals. Our implementation is based on that of \dots

\noindent Note that we never use ``regular'' lists in our system. For this reason we have decided to borrow the notation where $::$ is the list constructor since we will not need it elsewhere. Also, in this context we will use the same symbols twice: once for values and once for types; we advise the reader to be always aware of the context in which these operators are used since the context will make it clear which one of the symbols is currently in use. We believe that having two distinct sets of symbols in the end would reduce the readability of our code and so we only used distinct symbols in the actual implementation.

\noindent A heterogeneous list can be built using two type constructors, one for the empty heterogeneous list and one for adding a new head to an existing heterogeneous list:

\begin{lstlisting}
Nil=Nil
h::tl=h::tl
\end{lstlisting}

We characterize heterogeneous lists not by their type (that would be impossible since heterogeneous lists all have very different types), but rather with the $HList $inductive predicate:

\begin{lstlisting}
HList Nil
HList tl $\Rightarrow$ HList (h::tl)
\end{lstlisting}

This means that the empty heterogeneous list is a heterogeneous list, and by adding an element of any type $h$ to a heterogeneous list we obtain a new heterogeneous list.

\noindent Heterogeneous lists are accessed by index. To make accesses type safe we need to ensure that no integer that would result in an impossible access (index out of range) is ever used to access a list. For this reason we implement compile-time Church Numerals at the type level, and we use those to access our heterogeneous lists:

\begin{lstlisting}
Z=Z
S n=S n
\end{lstlisting}

Just like we did for heterogeneous lists we now add a $CNum $predicate to characterize all Church Numerals inductively:

\begin{lstlisting}
CNum Z
CNum n $\Rightarrow$ CNum (S n)
\end{lstlisting}

The only real difference between Church Numerals and heterogeneous lists is that while the heterogeneous list carries inside its values a series of values of different types, a Church Numeral is always empty, that is beyond a certain number of applications of its $S$ constructor it does nothing. For this reason we will never be interested in the actual value of Church Numerals, and we will only use unnamed instances of Church Numerals like in the following example:

\begin{lstlisting}
CNum n $\Rightarrow$ f : n $\to$ Nil
f (_:n)=Nil
\end{lstlisting}

Accessing a heterogeneous list is based on the $HLookup$ predicate:

\begin{lstlisting}
CNum n $\Rightarrow$ HLookup l n
\end{lstlisting}

This predicate contains a type function that tells us the type of the selected field:

\begin{lstlisting}
HAt l n : *
\end{lstlisting}

Plus two functions to read and write the n-th element of the list:

\begin{lstlisting}
hRead : l $\to$ n $\to$ HAt l n
hWrite :HAt l n $\to$ l $\to$ n $\to$ l
\end{lstlisting}

The $HLookup$ predicate is instanced inductively on the second parameter. First we instance $HLookup$ for accesses to the first item of the list (index $0$):

\begin{lstlisting}
HLookup (h::tl) Z
HAt (h::tl) Z=h
hRead (h::tl) _ = h
hWrite v (h::tl) _ =v::tl
\end{lstlisting}

Now we instance $HLookup$ for all other accesses:

\begin{lstlisting}
CNum n $\Rightarrow$ HLookup (h::tl) (S n)
HAt (h::tl) (S n)=HAt tl n
hRead (h::tl) _=hRead tl (_:n)
hWrite v (h::tl) _=h::(hWrite v tl (_:n))
\end{lstlisting}

Notice two interesting aspects about this code: first it is impossible to read anything on the empty heterogeneous list; second, it is impossible to access a heterogeneous list with an index that would be too big. As an example, consider the type of the expression:

\begin{lstlisting}
hRead (0::1::Nil)(S S S Z) : $\alpha$
\end{lstlisting}

From the definition of the $hRead$ function we know that:

\begin{lstlisting}
$\alpha$ 	= HAt (Int::Int::Nil) (S S S Z)
	= HAt (Int:Nil)(S S Z)
	= HAt Nil (S Z)
\end{lstlisting}

But the last term is stuck since there are no instances of the $HAt $type functions (being there no instances of the $HLookup$ predicate) where$ l=Nil$.

\noindent A straightforward choice for a reference type is clearly inspired by the state monad: a reference will contain a pair of functions, one for evaluating the reference from the heap and one for setting a new value for this reference on the heap:

\begin{lstlisting}
Reference h $\alpha$ = Reference (h $\to$ ($\alpha$ $\times$ h)) ($\alpha$ $\to$ h $\to$ (Unit $\times$ h))
\end{lstlisting}

The reference operations are stateful, that is the heap might be changed (its new value is returned by both the get and the set functions) even when we simply evaluate the reference. These two functions have been chosen for the simplicity with which they can be converted into statements of the state monad.

\noindent Now that we have both a candidate for heaps and references we can instance the $Heap$ predicate. We will use heterogeneous lists as typed heaps and $Reference$ as the type for our references. We start by inductively instancing the allocation operators:

\begin{lstlisting}
Heap Nil Reference
  New Nil $\alpha$ = $\alpha$ :: Nil
  new Nil x = x :: Nil
  delete (x::Nil) = Nil
\end{lstlisting}

and

\begin{lstlisting}
Heap (h::tl) Reference
  New (h::tl)$\alpha$ =h::(New $\alpha$  tl)new (h::tl)x=h::(new x tl)delete (h::tl)=h::(delete tl) \]
\end{lstlisting}

The remaining functions needed for one heap have the same implementation for both the empty heap and a non-empty heap. For this reason we just assume that $HList h\Rightarrow Heap h$.

\noindent The evaluation operator simply returns the get function:

\begin{lstlisting}
eval (Reference get set)=ST get
\end{lstlisting}

The assignment operator invokes the set function with the given parameter and returns the resulting curried function:

\begin{lstlisting}
(Reference get set) := v = ST(set v)
\end{lstlisting}

The allocation operator $(\gg +)$ generates a temporary reference that refers to a newly allocated value in a larger heap. A function is invoked to act on this reference to generate a statement that will return the final value of the computation done on this new reference. We define this operator by steps; since the $(\gg +)$ operator requires manipulating the length of a heterogeneous list, we begin by giving the $HLength$ type function (which is part of the $HList$ type predicate):

\begin{lstlisting}
HLength l : *
HLength Nil=Z
HLength (h::tl)=S (HLength tl)
\end{lstlisting}

The first step of the actual computation is to build the new reference to access the new item, which can be looked up with an index equal to the length of the input heap:

\begin{lstlisting}
(>>+) : $\alpha$ $\to$ (Reference (New h $\alpha$) $\alpha$ $\to$ ST (New h $\alpha$) $\beta$) $\to$ ST h $\beta$
 v >>+ k =
    let $r_{new}$ = Reference ($\lambda$h.(hRead h (_:HLength h),h))
                         ($\lambda$v.$\lambda$h.((),hWrite v h (_:HLength h)))
\end{lstlisting}

Notice how we invoke the read and write functions ($hRead$ and $hWrite$) with index equals to the length of the type of the original heap $h$. The second step consists in obtaining the result by invoking the $k$ function:

\begin{lstlisting}
    let (ST res)=k $r_{new}$
\end{lstlisting}

Finally we convert this result into the desired form by temporarily adding the new value $v$ to the heap, performing the computation and then removing the value:

\begin{lstlisting}
    in ST($\lambda$h.downcast(res(new h v))  where downcast (x,h)=(x,delete h)
\end{lstlisting}

\noindent

\end{document}

% == UNREGISTERED! == GrindEQ Word-to-LaTeX 2008 ==


\section{Evaluating the performance of the proposed method}
\label{sec:rul_eval}

In this section we evaluate the performance of the proposed method. The architecture of the \gls{mlp} to be used throughout this section is described in Table \ref{table:proposed_nn}. The results presented here were obtained after performing $10$ different runs where each network was trained for $100$ epochs at each run and tested in each subset of the \gls{cmaps} dataset.

For the first experiment, the combinations of window-size $n_w$, window-stride $n_s$ and early \gls{rul} $R_e$ are presented in Table \ref{table:data_params_de}.

\begin{table}[!htb]
\centering
\begin{tabular}{l r r r l}
	\hline
	 Dataset & $n_w$ &  $n_s$ & $R_e$\\
  	\hline
  	FD001 & 30 & 1 & 128\\
  	FD002 & 20 & 2 & 134\\
  	FD003 & 30 & 1 & 128\\
  	FD004 & 18 & 2 & 134\\
  	\hline
\end{tabular}
\caption{Data-related parameters for each subset as obtained by \gls{de}.}
\label{table:data_params_de}
\end{table}

The obtained results for $f(\nu)$ using the above setting are depicted in Table \ref{table:results_ann_de}. Notice that the performances obtained for datasets FD001 and FD002 improved with respect to those presented in Table \ref{table:tested_architectures_100} due to the fact of using a better $\nu$.

\begin{table}[!htb]
\centering
\begin{tabular}{l | r r r r | r r r r}
	\hline
	& \multicolumn{4}{| c}{RMSE} & \multicolumn{4}{| c}{RHS} \\
	Data Subset & min & max & avg & STD & min & max & avg & STD\\
  	\hline
  	FD001 & 14.87 & 15.08 & 14.94 & 0.06 &3.65 & 3.99 & 3.86 & 0.10\\
  	FD002 & 28.67 & 31.60 & 29.54 & 0.91 & 49.15 & 92.52 & 61 & 13.52\\
  	FD003 & 14.55 & 16.21 & 15.05 & 0.52 & 3.88 & 4.54 & 4.17 & 0.22\\
  	FD004 & 32.54 & 37.703 & 34.08 & 1.43 & 48.35 & 79.33 & 59.60 & 10.26\\
  	\hline
\end{tabular}

\caption{Scores for each dataset using the data-related parameters obtained by \gls{de}.}
\label{table:results_ann_de}
\end{table}

Next, the possibility of using a single set of data-related parameters for all the subsets is explored. For this experiment $n_w$ is fixed using the maximum allowable window size for all datasets, for \gls{cmaps} $n_w=18$. The data-related chosen parameters proposed for this experiment are $\nu=(18, 2, 18)$, the results $f(\nu)$ obtained are displayed in Table \ref{table:results_ann_1}.

\begin{table}[!htb]
\centering
\begin{tabular}{l | r r r r | r r r r}
	\hline
	& \multicolumn{4}{| c}{RMSE} & \multicolumn{4}{| c}{RHS} \\
	Data Subset & min & max & avg & STD & min & max & avg & STD\\
  	\hline
  	FD001 & 17.58 & 18.56 & 17.82 & 0.29 & 8.26 & 9.18 & 8.59 & 0.28\\
  	FD002 & 29.48 & 31.92 & 30.15 & 0.75 & 65.83 & 104.37 & 75.73 & 11.54\\
  	FD003 & 17.35 & 19.99 & 18.20 & 0.72 & 6.25 & 16.09 & 8.27 & 2.86\\
  	FD004 & 32.91 & 37.28 & 34.45 & 1.41 & 48.90 & 77.93 & 59.60 & 9.65\\
  	\hline
\end{tabular}
\caption{Scores for each dataset using the single set of data-related parameters.}
\label{table:results_ann_1}
\end{table}

As can be observed, performance is decreased for subsets FD001/FD003, this indicates that larger window sizes are beneficial for this regression problem. Figures \ref{fig:scores_rmse} and \ref{fig:scores_rhs} show a comparison of how the scores are affected for each dataset by changing the data-related parameters to make use of 2 and 1 sets of them.

\begin{figure}[!htb]
\centering
\includegraphics[width=0.62\textwidth]{../img/rmse_comparisson.png}
\caption{Comparison of \gls{rmse} results for different sets of data-related parameters.}
\label{fig:scores_rmse}
\end{figure}

\begin{figure}[!htb]
\centering
\includegraphics[width=0.62\textwidth]{../img/rhs_comparisson.png}
\caption{Comparison of \gls{rhs} results for different sets of data-related parameters.}
\label{fig:scores_rhs}
\end{figure}

\subsection{Comparison with other approaches}

In this section the performance of the proposed method is compared against other state-of-the-art methods. Most of the presented methods in this section have only reported results on the test set FD001 in terms of $e_{rms}$, the results are displayed in Table \ref{table:results_comparison}. The $e_{rms}$ value of the proposed method in Table \ref{table:results_comparison} is the mean value of 10 independent runs. The remainder of the values are identical to those reported in their respective original papers.

\begin{table}[!htb]
\centering
\begin{tabular}{l | r r r r | r r r r}
	\hline
	Method & $e_{rms}$ \\
  	\hline
  	%ESN trained by Kalman Filter \citep{Peng2012} & 63.45\\
  	Support Vector Machine Classifier \citep{Louen2013} & 29.82\\
  	Time Window Neural Network \citep{Lim2016} & 15.16\\
  	Multi-objective deep belief networks ensemble \citep{Zhang2016} & 15.04\\
  	Deep Convolutional Neural Network \citep{Babu2016} & 18.45\\
  	\textbf{Proposed method with $n_w = 30$, $n_s=1$ and $R_e = 128$} & 14.87\\
  	\hline
\end{tabular}
\caption{Performance comparisons of the proposed method and the latest related papers on the \gls{cmaps} dataset.}
\label{table:results_comparison}
\end{table}

Based on the results, the proposed method performs better than the compared methods. Two methods come close to the performance of the presented approach in this paper, namely the time window \gls{ann} \citep{Lim2016} and the Networks Ensemble \citep{Zhang2016}. While the performance of both methods comes close to the results presented in this paper, the presented approach is computationally more efficient. Furthermore, the framework proposed in here is simple, robust, generic and light-weight, features we believe are important to highlight when comparing the proposed method against other state-of-the-art approaches.


\section{Modelling heterogeneous preferences with mixed logit}


\chapter{Milestone 5}
\section{Berendsen Thermostat Teststrategy}
\begin{comment}
-basic idea is weather or not the implementation follows the exponential damping from the 2 equation for a small system with known velocities
\end{comment}
%equations
\begin{equation}
	\lambda = \sqrt{1 + \bigg(\frac{T_{0}}{T} -1\bigg)\frac{\Delta t}{\tau}}
\end{equation}

\begin{equation}
	T(t) = T_{0} + (T_{1}-T_{0})e^{-t/\tau}
\end{equation}

\section{Simulation Time}
\begin{comment}
simulation should be roughly of complexity N2
may need too approximate with a function
\end{comment}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale=1]{Figure/plotAtomTimes.png}
	\end{center}
	\caption[Simulationtime]{Simulationtime from 8 to 192 Atoms }
	\label{PlotAtomTimes}
\end{figure}


% !Mode:: "TeX:UTF-8"
\lipsum[2]
\begin{itemize}
    \item minimizes the discomfort to the hands, or
    \item maximizes the outgoing velocity of the ball.
\end{itemize}
We focus exclusively on the second definition.
\begin{itemize}
    \item the initial velocity and rotation of the ball,
    \item the initial velocity and rotation of the bat,
    \item the relative position and orientation of the bat and ball, and
    \item the force over time that the hitter hands applies on the handle.
\end{itemize}
\lipsum[3]
\begin{itemize}
    \item the angular velocity of the bat,
    \item the velocity of the ball, and
    \item the position of impact along the bat.
\end{itemize}
\lipsum[4]
\emph{center of percussion} [Brody 1986], \lipsum[5]
\begin{Theorem}\label{thm:latex}
    \LaTeX
\end{Theorem}
\begin{Lemma} \label{thm:tex}
    \TeX .
\end{Lemma}
\begin{proof}
    The proof of theorem.
\end{proof}

\subsection{Other Assumptions}
\lipsum[6]
\begin{itemize}
    \item
    \item
    \item
    \item
\end{itemize}
\lipsum[7]


% Begin = Header
\section{Graphs produced during the runtime of the simulation}

The following subsections present the basic data measured and collected during the runtime of the simulation.

\input{{0}/graphs.tex}
% End = Header

% Begin = Fluid mass
\subsection{Fluid Mass}

\figref{fig:Fluid mass} displays the total mass of the fluid as a function of time. The graph is an important tool to indicate possible bugs in the code / simulation set-up. Questions one might ask while looking at the graph could be:

\begin{itemize}
\item If a constant mass is expected, is the line straight?
\item If a certain inflow of the mass is expected, does the curve have the appropriate slope?
\end{itemize}
% End = Fluid mass

% Begin = Pressure difference
\subsection{Pressure difference}\label{sec:Pressure difference}

Compressible fluid dynamics solver, namely Lattice Boltzmann method, is used in the simulation. \figref{fig:Pressure difference} therefore indicates the minimum, maximum and the average pressure difference between an ideal incompressible fluid and the fluid used in the simulation.

The compressibility of the fluid can be limited by slowing down the computation which on the other hand elongates the computational time. The computational speed and the compressibility of the fluid have to be therefore balanced.
% End = Pressure difference

% Begin = Fluid speed
\subsection{Fluid speed}

\figref{fig:Fluid speed} indicates the minimum, maximum and the average speed of the fluid as a function of time.

The second $y$ axis located at the right shows the speed measured in Lattice Boltzmann lattice units [LU]. The maximal [LU] speed must be lower than ca. 0.3 to maintain stability of the Lattice Boltzmann method. The maximal [LU] speed should be lower than 0.05 to be able to consider the flow as incompressible. See \secref{sec:Pressure difference} for further information on compressibility.
% End = Fluid speed

% Begin = Shear rate
\subsection{Shear rate}\label{sec:Shear rate}

\figref{fig:Shear rate} shows the minimum, maximum and the average shear rate of the flow as a function of time. Shear rate is the driving force for the orientation of fibres. The higher the shear rate the higher change in orientation the fibres undergo. Vice-versa the lower shear rate indicates that fibres do not change their orientation that much.
% End = Shear rate

% Begin = Apparent viscosity
\subsection{Apparent viscosity}\label{sec:Apparent viscosity}

\figref{fig:Apparent viscosity} shows the average apparent dynamic viscosity of the fluid. The second $y$ axis located at the right shows the apparent viscosity measured in Lattice Boltzmann lattice units [LU].

The [LU] viscosity indicates correctness of the flow. For a single relaxation time flows (see XXX), the flow becomes inaccurate for values larger than 1/6. For large values of the [LU] viscosity the flow becomes unstable. In case of multiple relaxation time, the flow stays stable even for high values of the viscosity. The correctness of the flow is also significantly improved by the multiple relaxation time.

The change of the simulation set-up in such a way to decrease the average apparent viscosity increases the stability and the correctness of the flow at the expense of rescaled time and thus elongated computational time.

The value of the [LU] apparent viscosity is especially important in the time regions where the shear rate of the flow is high (see \secref{sec:Shear rate}), i.e. where the immersed fibres undergo high change in orientation.
% End = Apparent viscosity

% Begin = Fraction of fluid having certain viscosity
\subsection{Fraction of fluid having certain viscosity}

\figref{fig:Fraction of fluid having certain viscosity} shows fraction of fluid having certain apparent viscosity (see \secref{sec:Apparent viscosity}) as a function of time.

When a sufficient fraction of fluid exceeds a certain level the fluid can be treated as solid, i.e. not flowing or flowing as a solid body. In such a case the simulation is often assumed to be finished.
% End = Fraction of fluid having certain viscosity

% Begin = Dissipated energy during collisions per particle
\subsection{Dissipated energy during collisions per particle}

blabla
% End = Dissipated energy during collisions per particle

% Begin = Kinetic energy of particles
\subsection{Kinetic energy of particles}

\figref{fig:Kinetic energy of particles} presents the total kinetic energy of all rigid particles present in the simulation as a function of time. The kinetic energy is computed in the following way:

\begin{equation}
	\frac{1}{2} m u^2 + \frac{1}{2} I \omega^2
\end{equation}
% End = Kinetic energy of particles

% Begin = Number of collisions per particle
\subsection{Number of collisions per particle}

blublu
% End = Number of collisions per particle

% Begin = Number of particles
\subsection{Number of particles}

blibli
% End = Number of particles

% Begin = Particle volume fraction
\subsection{Particle volume fraction}

blaublau
% End = Particle volume fraction

% Begin =
% End =

% Begin =
% End =

% Begin =
% End =

% Begin =
% End =