\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, float, graphicx, hyperref}

\title{Automata and Formal Languages}
\author{Matteo Alberici}
\date{May 2020}

\begin{document}
\maketitle
\newpage
\tableofcontents
\newpage

% ----------------------- %
% Chapter 1: Introduction
% ----------------------- %
\section{Introduction}
The theory of computation can be divided into three areas:
\begin{itemize}
    \item \textbf{Complexity Theory}
        \vspace{0.2cm} \\
        What makes problems computationally hard or easy?
    \item \textbf{Computability Theory}
        \vspace{0.2cm} \\
        What problems can(not) computers solve?
    \item \textbf{Automata Theory}
        \vspace{0.2cm} \\
        Deals with defining mathematical models of computation \\
\end{itemize}
\section{Mathematical Notions and Terminology}
\subsection{Sets}
A \textbf{set} is a group of unordered unique \textbf{elements} seen as a unit:
\begin{center}
    $S = \{1, \ ciao, \ 3\}$
\end{center}
The \textbf{cardinality} of a set is the number of its elements:
\begin{center}
    $|S| = 3$
\end{center}
The \textbf{empty set} $\emptyset$ is the set with no members. \\
A set $A$ is a \textbf{subset} of set $B$ if every element of $A$ is member of $B$:
\begin{center}
    $A \subseteq B$
\end{center}
Moreover, $A$ is a \textbf{proper subset} of $B$ if $A$ is not equal to $B$:
\begin{center}
    $A \subset B$
\end{center}
The \textbf{union} of two sets $A$ and $B$ is the set containing all the elements in $A$ and all those in $B$:
\begin{center}
    $A \cup B$
\end{center}
The \textbf{intersection} of two sets $A$ and $B$ is the set containing the elements that are in both $A$ and $B$:
\begin{center}
    $A \cap B$
\end{center}
\noindent
The \textbf{complement} of set $A$ is the set of all elements not in $A$:
\begin{center}
    $\bar{A}$
\end{center}
The \textbf{power set} of set $A$ is the set of all the subsets of $A$:
\begin{center}
    $2^A$
\end{center}
The \textbf{Cartesian product} of sets $A$ and $B$ is the set of all ordered pairs wherein the first element is a member of $A$ and the second element is a member of $B$:
\begin{center}
    $A \times B$
\end{center}
\subsection{Sequences and Tuples}
A \textbf{sequence} is an ordered list of objects:
\begin{center}
    $(1, 2, 3)$
\end{center}
A finite sequence is a \textbf{tuple}: a \textbf{k-tuple} contains $k$ elements. \\
A $2$-tuple is called an \textbf{ordered pair}.
\subsection{Functions and Relations}
A \textbf{function} is an object that sets up an input–output relationship. A function $f$ whose input and output values are $a$ and $b$ is represented as follows:
\begin{center}
    $f(a) = b$
\end{center}
A function is also called a \textbf{mapping}, thus: "$f$ maps $a$ to $b$". \\
Given a function $f$, the set of its possible inputs is called the \textbf{domain}, while the set of its possible outputs is called the \textbf{range}:
\begin{center}
    $f \ : \ D \ \rightarrow \ R$
\end{center}
A function that uses all the elements of the range is \textbf{onto} the range. \\
If the domain of a function $f$ is $A_1 \times ... \times A_k$, then the input is a k-tuple $(a_1, ..., a_k)$ where $a_i$ is an \textbf{argument} to $f$. A function $f$ with $k$ arguments is a \textbf{k-ary function}, where $k$ is the \textbf{arity} of $f$. An \textbf{unary function} has one argument, while a \textbf{binary function} has two arguments. \\
A \textbf{predicate} is a function whose range is $\{$\textcolor{ForestGreen}{true}, \ \textcolor{red}{false}$\}$. \\
A predicate whose domain is a set of k-tuples $A \times ... \times A$ is a \textbf{k-ary relation} on $A$. A $2-$ary relation is called a \textbf{binary relation}. \\
An \textbf{equivalence relation} $R$ captures the notion of two objects being equal in some feature and must satisfy the following conditions:
\begin{enumerate}
    \item $R$ is \textbf{reflexive}: $xRx$ for every $x$
    \item $R$ is \textbf{symmetric}: $xRy$ implies $yRx$ for every $x,y$
    \item $R$ is \textbf{transitive}: $xRy$ and $yRz$ imply $xRz$, for every $x,y,z$.
\end{enumerate}
\subsection{Graphs}
A \textbf{graph} is a set of \textbf{nodes} connected with \textbf{edges}. We can label nodes and edges obtaining a \textbf{labeled graph}. \\
In a \textbf{directed graph}, edges have directions. \\
A graph $G$ is a \textbf{subgraph} of a graph $H$ if the nodes and the edges of $G$ are subsets of the ones of $H$.
A \textbf{simple cycle} contains at least three nodes and repeats the first and last ones. \\
A graph is \textbf{connected} if every node has a path to the others. \\
A graph is a \textbf{tree} if it is connected and has no simple cycles.
Trees have a node designated as \textbf{root}, while the other nodes of degree $1$ are called the \textbf{leaves}. \\
A \textbf{path} is a sequence of nodes connected by edges. A \textbf{simple path} is a path with no \textbf{cycles}, meaning paths that start and end in the same node. A path wherein all the arrows point in the same direction as its steps is a \textbf{directed path}: a directed graph is \textbf{strongly connected} if a directed path connects every two nodes.
\subsection{Strings and Languages}
An \textbf{alphabet} $\Sigma$ is a nonempty finite set of \textbf{symbols}:
\begin{center}
    $\Sigma = \{a,b,c,...,z\}$
\end{center}
A \textbf{string over an alphabet} is a sequence of symbols from $\Sigma$:
\begin{center}
    $abracadabra$ is a string over $\Sigma$
\end{center}
Given a string $w$, then the \textbf{length} $|w|$ is the number of its symbols. The \textbf{empty string} $\epsilon$ is the string with length $0$. \\
Given two strings $x$ and $y$, then the \textbf{concatenation} $xy$ is obtained by appending $y$ to the end of $x$:
\begin{center}
    $aa \circ bb = aabb$,
\end{center}
where $aa$ is the \textbf{prefix} and $bb$ is the suffix. \\
A \textbf{language} $L$ is a set of strings.
\subsection{Definitions, Theorems, and Proofs}
\textbf{Definitions} describe objects and notions we make \textbf{mathematical statements} about, expressing some property. \\
A \textbf{proof} is a convincing logical argument that a statement is true, while a \textbf{theorem} is a mathematical statement proved true. Statements useful only because they assist in the proof of another are called \textbf{lemmas}. Theorems determining that related statements are true are called \textbf{corollaries}.
\subsubsection{Finding Proofs}
Truth or falsity of statements is determined trough a mathematical proof. Given the multipart statement \textit{"P iff Q"}, we have the following definitions:
\begin{itemize}
    \item \textbf{Forward direction}: if $P$ is true, then $Q$ is true - $P \Rightarrow Q$
    \item \textbf{Reverse direction}: if $Q$ is true, then $P$ is true - $Q \Leftarrow P$
    \item Finally, $P \iff Q$
\end{itemize}
If a statement states that all objects of a certain type have a particular property, we must find a \textbf{counterexample} in order to prove it wrong.
\subsubsection{Types of Proofs}
\textbf{Proof by Construction}
\vspace{0.2cm} \\
A \textbf{proof by construction} shows that a particular type of object exists. For example, we build a \textbf{k-regular graph} if every node in the graph has degree $k$.
\vspace{0.1cm} \\
\textbf{Proof by Contradiction}
\vspace{0.2cm} \\
In a \textbf{proof by contradiction}, we assume that a theorem is false and then show that it leads to a false consequence. For example, proving that the square root of $2$ is an irrational number.
\vspace{0.5cm} \\
\textbf{Proof by Induction}
\vspace{0.2cm} \\
A \textbf{proof by induction} shows that all elements of an infinite set have a specified property. For example, we show that an arithmetic expression computes a desired quantity for every assignment to its variables. \\
Given a property $P$, then the \textbf{basis} proves that $P(1)$ is true, while the \textbf{induction step} proves that if $P(1)$ is true, then so is $P(i + 1)$. The assumption that $P(i)$ is true is the \textbf{induction hypothesis}.

\newpage

\section{Boolean Logic}
\textbf{Boolean logic} is a mathematical system built around the \textbf{Boolean values} \textcolor{ForestGreen}{true} and \textcolor{red}{false}, manipulated through \textbf{Boolean operations}:
\begin{itemize}
    \item \textbf{Negation}: returns the opposite value ($NOT$ - $\neg$)
    \item \textbf{Conjunction}: returns $1$ if both values are $1$ ($AND$ - $\land$)
    \item \textbf{Disjunction}: returns $1$ if either of the values is $1$ ($OR$ - $\lor$)
    \item \textbf{Exclusive or}: returns $1$ if only one value is $1$ ($XOR$ - $\oplus$)
    \item \textbf{Equality}: returns $1$ if both values have the same value ($\iff$)
    \item \textbf{Implication}: returns $0$ iff the first operand is $1$ and the second one is $0$ ($\implies$)
\end{itemize}
\begin{figure}[H]
    \centering
    \includegraphics[width=7.5cm]{Figure 1A - Boolean Operations.png}
    \vspace{0.2cm}\\
    \includegraphics[width=7.5cm]{Figure 1B - Boolean Operations.png}
    \caption{Boolean operations results}
\end{figure}
The \textbf{distributive law} for $AND$ and $OR$ states that:
\begin{itemize}
    \item $P \land (Q \lor R) \ \iff \ (P \land Q) \lor (P \land R)$
    \item $P \lor (Q \land R) \ \iff \ (P \lor Q) \land (P \lor R)$
\end{itemize}

\newpage

\section{Regular Languages}
Theory of computation uses an idealized computer called a \textbf{computational model}.
\subsection{Finite Automata}
The simplest computational model is the \textbf{finite automaton} (\textbf{FA}). It receives a string in input and, by processing it, either \textbf{accepts} or \textbf{rejects} it.
An FA is defined by the following \textbf{5-tuple}:
\begin{center}
    $A = (Q, \Sigma, \delta, q_0, F)$, where
\end{center}
\begin{itemize}
    \item $Q$ is the finite set of states
    \item $\Sigma$ is the finite input alphabet
    \item $\delta:Q\times\Sigma\rightarrow Q$ is the transition function
    \item $q_0 \in Q$ is the initial state
    \item $F \subseteq Q$ is the accepting state
\end{itemize}
Let's analyze the following example reading string $1101$ in the machine $M$:
\begin{figure}[H]
    \centering
    \includegraphics[width=7.5cm]{Figure 2 - FA.png}
\end{figure}
\begin{enumerate}
    \item Start in the initial state $q_1$
    \item Read $1$, follow transition from $q_1$ to $q_2$
    \item Read $1$, follow transition from $q_2$ to $q_2$
    \item Read $0$, follow transition from $q_2$ to $q_3$
    \item Read $1$, follow transition from $q_3$ to $q_2$
    \item Accept because $M$ is in the accepting state $q_2$
\end{enumerate}
A language is \textbf{regular} if an FA recognises it. Given the set $A$ of all strings accepted by a machine $M$, then $A$ is the \textbf{language of machine} $M$:
\begin{center}
    $L(M) = A$
\end{center}
\newpage
\noindent
There are three \textbf{regular operations} used to study properties of regular languages:
\begin{itemize}
    \item \textbf{Union}: $A \cup B = \{x \ | \ x \in A$ or $x \in B\}$
    \item \textbf{Concatenation}: $AB = \{xy \ | \ x \in A$ and $y \in B\}$
    \item \textbf{Star}: $A* = \{x_1,x_2,...,x_k \ | \ k \geq 0$ and $x_i \in A\}$
\end{itemize}
\subsection{Nondeterminism}
In a \textbf{nondeterministic finite automaton} (\textbf{NFA}), several choices may exist for the next state:
\begin{itemize}
    \item Multiple ways to proceed, thus the machine \textbf{splits} in copies
    \item No ways to proceed, thus the copy \textbf{dies}
    \item A copy is in an accepting state, thus the string is accepted
    \item An $\varepsilon$ transition exists, thus the machine \textbf{splits immediately}
\end{itemize}
An NFA is defined by the following \textbf{5-tuple}:
\begin{center}
    $FA = (Q, \Sigma, \delta, q_0, F)$, where
\end{center}
\begin{itemize}
    \item $Q$ is the set of states
    \item $\Sigma$ is the input alphabet
    \item $\delta:Q\times\Sigma_\varepsilon\rightarrow P(Q)$ is the \textbf{transition function}
    \item $q_0 \in Q$ is the \textbf{initial state}
    \item $F \subset Q$ is the \textbf{accepting state}
\end{itemize}
\subsubsection{From NFA to FA}
There exist an NFA for each FA:
\begin{enumerate}
    \item Draw $q_0$ and the states reached from it along an $\varepsilon$ transition
    \item Draw at least one accepting state
    \item Draw all transitions
        \begin{itemize}
            \item No label: the transition leads to the \textbf{dead state} $\emptyset$
            \item $\varepsilon$ transition: the transition leads to the subset of states reached along it
        \end{itemize}
\end{enumerate}
\subsection{Regular Expressions}
\textbf{Regular expressions} describe languages and are built up using regular operations:
\begin{align*}
    &(0 \cup 1) \rightarrow (\{0\} \cup \{1\}) \rightarrow \{0,1\} = \Sigma \\
    &\Sigma 0 \rightarrow \{00,10\} \\
    &0* \rightarrow\{0*\} \rightarrow \{\varepsilon,0, 00, ...\}
\end{align*}
$R$ is a regular expression over an alphabet $\Sigma$ if it is at least one of:
\begin{enumerate}
    \item $a$, for some $a$ in $\Sigma$
    \item $\varepsilon$
    \item $\emptyset$
    \item $R_1 \cup R_2$, where $R_1$ and $R_2$ are regular expressions
    \item $R_1R_2$, where $R_1$ and $R_2$ are regular expressions
    \item $R_1*$, where $R_1$ is a regular expression
\end{enumerate}
\newpage
\section{Nonregular Languages}
To prove if a language is regular or not, we use the following procedure.
\vspace{0.2cm} \textbf{pumping lemma}, which states that if $L$ is a regular language, then there is a \textbf{pumping length} $p$ such that, if $w$ is a string in $L$ of at least length $p$, then $w$ may be divided into three pieces, $x$, $y$, and $z$, satisfying the following conditions:
\begin{enumerate}
    \item $xy^iz \in L$ for each $i \geq 0$
    \item $|y| > 0$
    \item $|xy| \leq p$
\end{enumerate}
We must choose a string $s \in L$ of at least length $p$, then show that no possible division satisfies all conditions. \\
Let's analyze an example with the following language:
\begin{center}
    $L = \{w|w = 0^n1^n$ for $n \geq0\}$
\end{center}
\begin{enumerate}
    \item Let's assume $L$ is regular
    \item A pumping length $p$ must exist such that all strings in $L$ of at least length $p$ can be pumped
    \item Choose a string $w$ that cannot be pumped:
        \begin{center}
            $w=0^p1^p \rightarrow 00...0 11...1$
        \end{center}
    \item Show that no division into $xyz$ that satisfies all conditions exists:
        \begin{enumerate}
            \item $3^{rd}$ condition: $y$ must contain only $0$s
            \item $2^{nd}$ condition: $y$ must contain at least one $0$
            \item $1^{st}$ condition: repeating $0$s results in a string $\notin L$
        \end{enumerate}
\end{enumerate}
\newpage
\section{Context-Free Languages}
A \textbf{context-free grammar} (\textbf{CFG}) is a collection of \textbf{substitution rules} which are composed of a \textbf{variable}, a symbol, and a string, which again is composed of variables or \textbf{terminals}, built as follows:
\begin{enumerate}
    \item Write the \textbf{start variable}:
    \item Find a rule starting with a known variable
    \item Replace the variable with the correspondent string
        \begin{align*}
            &A \rightarrow 0A1 &A\rightarrow0A1 \rightarrow00A11\rightarrow00B11\rightarrow00Z11 \\
            &A \rightarrow B &A\rightarrow B\rightarrow Z \\
            &B \rightarrow Z &L = \{0^nZ1^n \ | \ n\geq0\}
        \end{align*}
\end{enumerate}
A language generated by a CFG is a \textbf{context-free language} and it defined by the following \textbf{4-tuple}:
\begin{center}
    $L = (V,\Sigma, R, S)$, where
\end{center}
\begin{itemize}
    \item $V$ is the set of variables
    \item $\Sigma$ is the set of terminals
    \item $R$ is the set of rules
    \item $S \in V$ is the start variable
\end{itemize}
The following are some properties of CGFs:
\begin{enumerate}
    \item Break a language into simpler languages:
        \begin{itemize}
            \item Union:
                \begin{align*}
                    &S_1 \rightarrow 0S_11 \ | \ \varepsilon \\
                    &S_2 \rightarrow 0S_21 \ | \ 01 \\
                    &S \rightarrow S_1 \ | \ S_2
                \end{align*}
            \item Concatenation:
                \begin{align*}
                    &S_1 \rightarrow 0S_11 \ | \ \varepsilon \\
                    &S_2 \rightarrow 0S_21 \ | \ 01 \\
                    &S \rightarrow S_1S_2
                \end{align*}
        \end{itemize}
        \newpage
    \item Draw an FA and convert it to its CFG:
        \begin{enumerate}
            \item Design a variable $R_i$ for every state $Q_i$
            \item Add a rule $R_i \rightarrow aR_j$ for the $a-$transition from $Q_i$ to $Q_j$
            \item Add a rule $R_i \rightarrow \varepsilon$ if $Q_i$ is a final state
            \item Set the variable $R_0$ for the initial state $Q_0$
        \end{enumerate}
\end{enumerate}
A sequence of substitutions is called a \textbf{derivation}. A string is \textbf{derived ambiguously} if it has many derivations, thus a grammar is \textbf{ambiguous} if it generates at least one ambiguous string.
\newpage
\section{Pushdown Automata}
A \textbf{pushdown automaton} (\textbf{PDA}) is an NFA equipped with a \textbf{stack} giving it an unbounded memory needed for some non-regular languages. A PDA is defined by the following \textbf{6-tuple}:
\begin{center}
    $P = (Q, \Sigma, \Gamma, \delta, q_0, F)$, where
\end{center}
\begin{itemize}
    \item $Q$ is the set of states
    \item $\Sigma$ is the alphabet
    \item $\Gamma$ is the \textbf{stack alphabet}
    \item $\delta:Q_i\times\Sigma_\varepsilon\times\Gamma_\varepsilon\rightarrow P(Q_j\times\Gamma_{\varepsilon})$
    is the transition function, where:
        \begin{itemize}
            \item $Q_i$ is a state
            \item $\Sigma_\varepsilon$ is an input symbol
            \item $\Gamma$ is a stack symbol read
            \item $Q_j$ the state reached
            \item $\Gamma_\varepsilon$ is the new symbol on the stack
        \end{itemize}
    \item $q_0$ is the initial state
    \item $F \subseteq Q$ is the set of accepting states
\end{itemize}
The \textbf{transition labels} are written as follows:
\begin{center}
    $A,B \rightarrow C$,
\end{center}
meaning that if $A$ is read from the input string and $B$ is on top of the stack, then pop $B$ and write $C$.

\end{document}

Automata and Formal Languages/afl_cheatsheet.tex


\documentclass[10pt]{article}
\usepackage{fullpage}
\usepackage{microtype}      % microtypography
\usepackage{array}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{amsthm}

%% Header
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[C]{COMP 136 - 2020s - HW4}
\fancyfoot[C]{\thepage} % page number
\renewcommand\headrulewidth{0pt}
\pagestyle{fancy}

%% Hyperlinks always blue, no weird boxes
\usepackage[hyphens]{url}
\usepackage[colorlinks=true,allcolors=black,pdfborder={0 0 0}]{hyperref}

%%% Doc layout
\usepackage{parskip}
\usepackage{times}

%%% Write out problem statements in blue, solutions in black
\usepackage{color}
\newcommand{\officialdirections}[1]{{\color{blue} #1}}

%%% Avoid automatic section numbers (we'll provide our own)
\setcounter{secnumdepth}{0}

\begin{document}
~~\\ %% add vert space
\Large{\bf Student Name: TODO}

\Large{\bf Collaboration Statement:}

Total hours spent: TODO hours

I consulted the following resources:
\begin{itemize}
\item TODO
\end{itemize}

\tableofcontents

\newpage
\officialdirections{
\subsection*{1a: Problem Statement}
Prove the following property under a Hidden Markov Model.

	\begin{align}
	p(z_{t+1} | x_{t}, z_{t}) = p(z_{t+1} | z_{t} )
	\end{align}

}

\subsection{1a: Solution}

TODO

\newpage
\officialdirections{
\subsection*{1b: Problem Statement}
Prove the following property under a Hidden Markov Model.

	\begin{align}
	p(x_{t+1} | x_{1:t}, z_{1:t}) = p(x_{t+1} | z_t)
	\end{align}
}

\subsection{1b: Solution}

TODO

\newpage
\officialdirections{
\subsection*{2a: Problem Statement}
Write out an expression for the expected complete log likelihood:
\begin{align}
\mathbb{E}_{q(z_{1:T} |s)} \left[ \log p(z_{1:T}, x_{1:T} | \theta) \right]
\end{align}
Use the HMM probabilistic model $p(z_{1:T}, x_{1:T} | \theta)$ and the approximate posterior $q(z_{1:T} | s)$ defined above.

Your answer should be a function of the data $x$, the local sequence parameters $s$ and $r(s)$, as well as the HMM parameters $\pi, A, \phi$.
}

\subsection{2a: Solution}

TODO

\newpage
\officialdirections{
\subsection*{2b: Problem Statement}
Using your objective function from 2a above, show that for the M-step optimal update to the Bernoulli parameters $\phi_{kd}$, the optimal update is given by:
\begin{align}
\phi_{kd} = \frac{ \sum_{t=1}^T r_{tk} x_{td} }{ \sum_{t=1}^T r_{tk} }
\end{align}
}

\subsection{2b: Solution}

TODO

\officialdirections{
\subsection*{2c: Problem Statement}
Provide a short verbal summary of the update for $\phi_{kd}$. How should we interpret the numerator? The denominator?
}

\subsection{2c: Solution}

TODO

\officialdirections{
\subsection*{3a: Problem Statement}
Consider a Markov model with $K=3$ states, and the following initial probability vector and transition probability matrix:
\begin{align}
  \pi &=
  \left[ {\begin{array}{ccc}
   0.25 & 0.50 & 0.25 \\
  \end{array} } \right]
\\
  A &=
  \left[ {\begin{array}{ccc}
   0.97 & 0.01 & 0.02 \\
   0.10 & 0.80 & 0.10 \\
   0.05 & 0.04 & 0.91 \\
  \end{array} } \right]
\end{align}

What is the stationary distribution of the Markov chain?
In other words, if we sample a sequence $z_1, z_2, \ldots z_T$, for large $T \gg 0$, what is the marginal probability $p(z_T)$?

}

\end{document}


\section{The Emotiv\textsuperscript{®} EPOC+}
\begin{figure}[htbp]
	\centering
		\includegraphics[width=0.5\columnwidth]{epoc.png}
	\caption{Emotiv EPOC+ Headset}
	\label{fig:epoc}
\end{figure}

Although easily modifiable, the MusEEG package is designed to work with the Emotiv\textsuperscript{®} EPOC+. The EPOC+ is an affordable commercial-grade EEG headset that records 14 EEG channels and samples at a rate of 256Hz with 14-bit resolution, with a least significant bit value of approximately 0.51 $\mu$V. The EPOC+ headset proves to be an economically feasible alternative to a medical-grade headset as various studies confirm its viability for noncritical applications \cite{10, 11, 12, 13}.

\section{Data Acquisition}
To maximize classification accuracy, the MusEEG package is designed with a train-it-yourself structure, meaning that the end-user will have to train their own ANN model to work with their preferred set of facial or body expressions. Because of the train-it-yourself nature of the package, data was recorded for a single subject only. Six facial expressions were recorded using the Emotiv\textsuperscript{®} PRO application. 80 samples were recorded of the following facial expressions:
\begin{itemize}
\item smile
\item raise eyebrows
\item look left
\item look right
\item neutral
\item scrunch
\end{itemize}

To expedite recording times, the MusEEG package was designed to allow the user to record all samples of a single facial expression to a single .csv file. The MusEEG package then aids the user through curating and cutting the samples into individual chunks for feature extraction and classification.

\begin{figure}[htbp]
	\centering
		\includegraphics[width=1\columnwidth]{smileBigChunk_320.png}
	\caption{EEG plot of a smile sample (\textit{bigChunk})}
	\label{fig:smileBigChunk}
\end{figure}

During the curation process, the samples were examined for any discontinuities in contact quality, noise, and other artifacts. Samples that appeared to be corrupted with electrode contact discontinuities were discarded, while samples that were clean were stored in the project’s saved chunks directory. From each sample, two different sample chunks were created: a \textit{bigChunk} (1250ms, 320 samples) for facial expression recognition, and a \textit{smallChunk} (250ms, 64 samples) for facial expression/no facial expression classification. Examples of a \textit{bigChunk} and \textit{smallChunk} can be observed in (Figures~\ref{fig:smileBigChunk} and \ref{fig:smileSmallChunk}), respectively.

\begin{figure}[htbp]
	\centering
		\includegraphics[width=1\columnwidth]{smileSmallChunk_64.png}
	\caption{EEG plot of a smile sample (\textit{smallChunk})}
	\label{fig:smileSmallChunk}
\end{figure}

\section{Feature Extraction}
\subsection{Facial Expression Feature Extraction}
A feature extraction method similar to the one in \cite{14} was used. A 4-level wavelet decomposition using Daubechies order-2 (db2) mother wavelet is performed on all 14 channels of the chunk of EEG data.

One advantage of wavelet analysis over other time-frequency distribution methods (e.g. STFT) is that wavelet analysis varies the time-frequency aspect ratio, producing good frequency localization at low frequencies (long time windows) and good time localization at high frequencies (short time windows). This results in a segmentation of the time-frequency plane that will reveal transient features of the signal, which are typically not obvious during Fourier analysis \cite{14}.

Following the wavelet decomposition, the first four statistical moments (mean, variance, skewness, kurtosis) are calculated for each wavelet vector. Since four moments are calculated for each of the 5 wavelet decomposition vectors per EEG channel, a total of 14 x 4 x 5 (280) features are calculated. The data is then normalized using \textit{sklearn}'s MinMaxScaler. These features are used as an input for the classification models.

\begin{figure}[htbp]
	\centering
		\includegraphics[width=1\columnwidth]{smileWavelets.png}
	\caption{Wavelet coefficients plot for smile}
	\label{fig:smileWavelets}
\end{figure}

\subsection{Band Power Feature Extraction}
The MusEEG system is capable of sending continuous theta (4 - 8 Hz),  alpha (8 - 12 Hz), beta (12 - 30 Hz), and gamma (30  - 60 Hz) band power information at a rate of 2Hz.

\begin{figure}[htbp]
	\centering
		\includegraphics[width=0.8\columnwidth]{bandPower.png}
	\caption{EEG band power during neutral state}
	\label{fig:bandpower}
\end{figure}

Before the average band power is calculated, the power spectral density of all of the 14-channel data is calculated using Welch's periodogram, with a window duration of 0.5s. Once the power spectral density is calculated, the average band power over a certain range of frequencies is found by integrating the power spectral density over the frequency ranges.

\section{Classification}
A three-layer Artificial Neural Network (ANN) architecture was used for the classification of the EEG signals. Two models were created: a small model designed to analyze 64-sample chunks and determine whether the chunk contains a facial expression or not (\textit{smallBrain}), and a large model designed to analyze 320-sample chunks to determine what facial expression is present in the chunk (\textit{bigBrain}).

The \textit{smallBrain} model is meant to perform a quick classification of whether a facial expression is present in a 64-sample chunk. The \textit{smallBrain} model was designed with the intent of having an always-on processor for real-time implementation. By analyzing a chunk with a duration of 250ms, the \textit{smallBrain} model returns a result fairly quickly, allowing for continuous real-time classification with little latency.

\begin{figure}[htbp]
	\centering
		\includegraphics[width=1\columnwidth]{smallBrain.png}
	\caption{ANN architecture for \textit{smallBrain} model.}
	\label{fig:smallBrain}
\end{figure}

\begin{figure}[htbp]
	\centering
		\includegraphics[width=0.5\columnwidth]{smallBrainConfusion.png}
	\caption{Confusion Matrix for \textit{smallBrain} model.}
	\label{fig:smallBrainConfusion}
\end{figure}

The \textit{smallBrain} model consists of a three-layer ANN with L1 regularization methods and a sparse categorical cross entropy loss function. During testing, the model exhibited 98.75\% accuracy. Its test confusion matrix can be observed in (Figure~\ref{fig:smallBrainConfusion}) .

The \textit{bigBrain} model consists of a three-layer ANN with L1\_L2 regularization methods and a sparse categorical cross entropy loss function.  During testing, the model exhibited 87.04\% accuracy. Its test confusion matrix can be observed in (Figure~\ref{fig:bigBrainConfusion}).

\begin{figure}[htbp]
	\centering
		\includegraphics[width=1\columnwidth]{bigBrain.png}
	\caption{ANN Architecture for \textit{bigBrain} model.}
	\label{fig:bigBrain}
\end{figure}

 \begin{figure}[htbp]
	\centering
		\includegraphics[width=0.75\columnwidth]{bigBrainConfusion.png}
	\caption{Confusion Matrix for \textit{bigBrain} model.}
	\label{fig:bigBrainConfusion}
\end{figure}


\begin{document}

\section{Scripts}

In this, I'll be talking about the scripts I used. Note that this is for reference, for myself, in the future, so for anyone else reading it, it may be a bit confusing.

\subsection{Parsing a beatmap}

In the hierarchy, it'll look like this:

\begin{lstlisting}

[get_osu_from_website.py]
bm_id 	-[DL]-> osu

[osu_to_osus.py]
osu  	-[Py]-> osuho + osutp + params
osutp	: 	used to check for scroll speed changes
params	: 	beatmap metadata

[osuho_to_acd.py]
osuho	-[Py]->	acd

[get_plyrid.py]
bm_id	-[API]> plyrid

[plyrid_to_acr.py]
plyrid	-[Py]-> acr

[ac_to_acrv.py]
acr+acd	-[Py]-> acrv

[ac_to_ppshift.py]
acrv+acd-[Py]-> ppshift

\end{lstlisting}

\subsection{Feeding the map}

\begin{lstlisting}[language=Python]

[interface_neural_network.py]

# This is the overarching public function
def train(self, epochs: int, batch_size: int):
    self._load_training()
    self._train_model(epochs, batch_size)

# This converts the ppshift into a usable pandas.Dataframe
def _load_training(self):

    print("Merging " + str(len(self.training_ids)) + " files")
    print(self.training_ids)

    ppshift_list = []

    for bm_id in self.training_ids:
        ppshift_f = open(self.ppshift_dir + str(bm_id) + '.ppshift', 'r')
        # Important: The first 29 results has a 0 output, so we will cut
        # those results out with splicing.
        # This is due to the rolling Aggregation
        ppshift_str = ppshift_f.read().splitlines()[29:]
        ppshift_f.close()
        ppshift_list.extend(list(map(eval, ppshift_str)))

    self.training_df = \
    pandas.DataFrame(ppshift_list,\
                     columns=['OFF','LP','LR','LM','LI','S', \
                              'RI','RM','RR','RP','NN','LNH','LNT','MED'])

# This is the Keras Model itself, based on model 9 in Part III
def _model_c(self):

    model = keras.Sequential()

    # We make this user-friendly to adjust
    self.layer_1_nrns = 96
    self.layer_2_nrns = 48
    self.layer_3_nrns = 24

    # Layer 1
    model.add(keras.layers.Dense(\
        self.layer_1_nrns, input_shape=(12,), \
        kernel_initializer='normal', \
        activation='relu'))

    # Layer 2
    if (self.layer_2_nrns != None):
        model.add(keras.layers.Dense(
            self.layer_2_nrns,\
            ernel_regularizer=keras.regularizers.l2(0.01)))

    # Layer 3
    if (self.layer_3_nrns != None):
        model.add(keras.layers.Dense(
            self.layer_3_nrns,\
            kernel_regularizer=keras.regularizers.l2(0.01)))

    # Output Layer
    model.add(keras.layers.Dense(1, kernel_initializer='normal'))

    model.compile(loss='mean_squared_error', optimizer='adam')

    return model

# This feeds the machine
def _train_model(self, epochs: int, batch_size: int):

    # This is the dataframe, extracted from ppshift
    df = self.training_df

    # Shuffle
    df = df.sample(frac=1)
    ds_s = df.values

    in_ds_s = ds_s[:,1:13]
    out_ds_s = ds_s[:,13]

    model = self._model_c()
    model.fit(in_ds_s, out_ds_s, epochs=epochs, batch_size=batch_size)

    # Save model
    model.model.save(self.model_dir + self.model_name + '.hdf5')

    self.model = model

\end{lstlisting}

\subsection{Testing the model}

\begin{lstlisting}[language=Python]

# This is the overarching public function
def test(self):
    self._load_model()
    self._test_model(self.testing_ids, 'results_tst')
    self._test_model(self.training_ids, 'results_trn')

# This simply gets the model
def _load_model(self):

    def dummy():
        return
    model = KerasRegressor(build_fn=dummy, epochs=1, batch_size=10, verbose=1)
    model.model = ld_mdl(self.model_dir + self.model_name + '.hdf5')

    self.model = model

# This will test the model and use a custom scoring method
def _test_model(self, bm_ids, log_file_name = 'results'):

    rating_list = []
    log = []

    for bm_id in bm_ids:
        ppshift_f = open(self.ppshift_dir + str(bm_id) + '.ppshift', 'r')
        # Important: The first 29 results has a 0 output, so we will cut
        # those results out with splicing.
        # This is due to the rolling Aggregation
        ppshift_str = ppshift_f.read().splitlines()[29:]
        ppshift_f.close()

        ppshift_df = pandas.DataFrame(list(map(eval, ppshift_str)))

        ds = ppshift_df.values

        in_ds = ds[:,1:13]
        out_ds = ds[:,[0,13]]

        # We predict the results here
        out_p = self.model.predict(in_ds, verbose=0)

        out_o = pandas.DataFrame(out_ds, columns=['offset','original'])
        out_p = pandas.DataFrame(out_p, columns=['pred'])

        # We join back with the original dataframe to compare
        out = out_o.join(out_p)

        # This is where the plots created is rated
        out['delta'] = out['pred'].subtract(out['original']).abs()
        log.append(str(out['delta'].mean()) + "\t" + \
                   get_beatmap_metadata.metadata_from_id(int(bm_id))

        rating_list.append(out['delta'].mean())

    # Append stats
    log.append("mean: " + str(statistics.mean(rating_list)))
    log.append("stdev: " + str(statistics.stdev(rating_list)))
    log.append("range: " + str(min(rating_list)) + ' - ' \
               + str(max(rating_list)))

    # Export to a .txt file
    log_f = open(self.plot_dir + log_file_name + ".txt", "w+")
    log_f.write('\n'.join(log))
\end{lstlisting}
\subsection{Interface}

Interface allows you to quickly parse, feed and predict from the model, it's a bit messy but it should be easy enough to understand

\begin{lstlisting}[language=Python]

# This reads the ids and parses it from osu to ppshift
def parse_ids():
    # bm_ids = get_ids(star_rating_above=5)

    # Parsing a custom made id list
    f = open(<directory>,'r')
    bm_ids = f.read().splitlines()
    bm_ids = list(map(int, bm_ids))
    counter = 1

    for bm_id in bm_ids:
        try:
            print('[' + str(counter) + ']', end='\t')
            bm = interface_class.parse_beatmap_id(
                 beatmap_id=bm_id, soft_load_flag=True)
            bm.parse_osu()
        except:
            print (str(bm_id) + ' ERROR')
            pass
        counter += 1

# This reads maps from the directory and parses it from osu to ppshift
def parse_evals():
    file_list = [x.split('.')[0] for x in os.listdir(<directory>)]

    for file in file_list:
        bm = interface_class.parse_beatmap_file_name(\
             beatmap_file_name=file, soft_load_flag=True)
        bm.parse_osu()

# This feeds the model, the directory will be defaulted to where
# the interface_class has exported the files
def train_model(model_name: str, seed: int = None):
    nn = interface_neural_network.train_model(model_name, seed)

    # Model 9
    nn.layer_1_nrns = 96
    nn.layer_2_nrns = 48
    nn.layer_3_nrns = 24

    nn.train(50, 50)

# This gets feeds the model only the input, for prediction scoring
def test_model(model_name: str, seed: int = None):
    nn = interface_neural_network.train_model(model_name, seed)
    nn.test()

# This gets osu from a specific directory to predict
def eval_model(model_name: str):
    nn = interface_neural_network.eval_model(model_name,)
    nn.evaluate()
    return

\end{lstlisting}

\end{document}


% 20200816
\documentclass[../thesis.tex]{subfiles} %% use packages & commands as this main file
\begin{document}

Engineering with bio-components have less potential to harm nature than geo-engineering proposals.  This is because the former can be installed in cities and on rooftops; the device is isolated from natural communities.  Geo-engineering proposals however directly perturbing nature; there are risks of unintended consequences.  This study proposes a new model incorporating artificial and life history trait constraints with applications in designing an effective carbon capture device.  The model incorporates \phy\ in a system with the option to add \bac\ and harvest.  The results show the importance of optimising \phy, \bac\ and harvest parameters for maximum carbon harvest efficiency.  The results also show there is a trade-off between feasibility and efficiency; \phy\ cultures have high feasibility but low efficiency while a \pbs\ have low feasibility but high efficiency when optimised.  The choice of system is dependent on the goals of the users and whether the life history traits of \phy\ and \bac\ are controllable.

\subsection{Feasibility of \pbs s}
My model shows \bac\ lowers feasibility of \pbs s (0.3\% \PBH, 97.2\% \PBN) compare with \phy-only systems (100\% \PoH\ \& \PoN; Table \ref{t:feasDist}).  This means a random choice of \phy-\bac\ pair has a lower chance of system establishment.  Substantial work has suggested the importance of chemical mediators in establishment of a high carbon yield co-culture (also known as consortia) and about culture medium optimisations \autocite{rivas2010interactions,amin2009photolysis,fuentes2016impact}.  This study suggests an alternative view on optimising bio-mediators (life history traits).  The results show life history trait values are at least as important as (if not more) environmental factors leading to an establishment of a high carbon yield co-culture; bio-mediators have not been studied in the context of carbon capture yet.  The small feasibility (0.3\%) may explain the few success cases in the experimental tests of high carbon yield \phy-\bac\ pairs \autocite{fuentes2016impact}.  Unfortunately those experiments do not measure life history traits of their organisms of choice; the measurements count on future work for confirmation on whether the experimental life history trait values are those predicted by this model (Fig. \ref{f:harvPB}).

\subsection{Optimal harvest of \phy\ and \phy-\bac\ \pbs s}
Previous work has mixed views on whether destructive or continuous harvest is a better carbon capture approach.  Destructive harvest is proposed by many aquaculture and geo-engineering projects \autocite{lawrence2014efficiency,krause2016substantial}.  Co-culture experiments often adopt continuous harvest methods without considering destructive harvest as an alternative \autocite{kim2008anaerobic,kazamia2012mutualistic}.  My model incorporates both continuous and destructive harvest options which enables direct comparison of the two harvest methods.

The results show there is no optimal harvest for \phy-only systems (Fig. \ref{f:ydDaily}).  This is because \phy\ captures carbon at a constant speed regardless of the carbon density in the model after the system stabilizes (Fig. \ref{f:destCarbon}).  The density-independent carbon capture feature from \phy\ causes a linear accumulation of organic carbon independent from time (Fig. \ref{f:ydDaily}A).  Hence carbon yield for \phy-only systems at equivalent harvest interval/rate have almost the same numerical value; any numerical difference is likely explained by the technical error of the computational methods I used.  The results agree with geo-engineering proposals that adopting destructive harvest by convention is an effective carbon capture method because the alternative harvest method does not have significant carbon yield advantages (Fig. \ref{f:harvPo}).

Continuous harvest is the optimal harvest for \pbs s within feasibility limits (Fig. \ref{f:harvPB}).  This is because \bac\ stabilizes equilibrium carbon densities during development of \pbs s (Fig. \ref{f:destCarbon}A).  As a result longer harvest intervals in destructive harvest have no effect on the carbon content accumulation in the systems; longer waiting time lowers the average carbon yield instead (Fig. \ref{f:ydDaily}A).  Continuous harvest consistently extracts organic carbon out from the system without altering biomass content.  Thus the carbon capture ability from \phy\ is not affected by the harvest.  On the other hand, \bac l growth is limited by the availability of organic carbon; future carbon consumption from \bac\ is also limited.  With smaller \bac l biomass, more carbon can be retained as organic carbon and benefit future harvest of carbon yield.  So a high harvest rate benefits carbon yield from \pbs s (Fig. \ref{f:ydDaily}B).  The results agree with experimental observations of continuous harvest enhances carbon/product yield from \pbs s \autocite{kim2008anaerobic,choix2012enhanced1,choix2012enhanced2,leyva2014accumulation} by providing a mechanistic explanation using ecological interactions between \phy\ and \bac.

\subsection{Importance of life history traits for the organic carbon yield}
Experiments on co-cultures focus on chemical and genetic features of \phy\ and \bac\ bringing the enhanced organic carbon yield effect \autocite{seyedsayamdost2011roseobacticides,durham2015cryptic,amin2009photolysis}.  They used various \phy-\bac\ pairs with different chemical mediators to achieve the enhancement, yet there is not a general explanation on what mechanism causes the enhancement effect.  This model uses a new approach to address effect through bio-mediator (life history traits); the results show trait values are as important (if not more) as chemical ones repeatedly emphasized for the carbon yield enhancement \autocite{fuentes2016impact}.  In the model, carbon yield enhancement can be achieved by expanding the organic carbon pool.  The expansion is maximising the supply and/or minimising the consumption of organic carbon (Fig. \ref{f:harvPB}).

Maximising the carbon supply is maximising \phy\ carbon-use efficiency.  This includes increasing the \phy\ non-respired carbon fraction (increasing $\ePR$; Fig. \ref{f:harvPB}A), increasing the \phy\ carbon leakages (increasing $\eP$; Fig. \ref{f:harvPB}B) and decreasing the \phy\ density-dependent death rate (decreasing $\aP$; Fig. \ref{f:harvPB}D).  Increasing $\ePR$ and $\eP$ increases the amount of carbon leaked into the organic carbon pool and directly benefiting the carbon yield in the short-term.  Decreasing $\aP$ benefits the organic carbon pool in the long-term instead of short-term.  This is because a low $\aP$ keep more carbon as \phy\ biomass.  A high \phy\ biomass enhances photosynthesis rate (Fig. \ref{f:model}) and increases the amount of carbon available for all three processes: respiration, leakage and biomass incorporation.  An enhanced \phy\ respiration is not a concern in this study because the external carbon dioxide pool is always unlimited.  The enhanced carbon leakage from \phy\ directly benefits the organic carbon pool.  The increased carbon incorporated into biomass then forms a positive feedback loop as a long-term enhancement of carbon yield.  Thus \phy\ biomass is functionally more important than its literal carbon content in carbon capture devices.  The results provide a direction for future experimental work to confirm whether the bio-mechanisms of these \phy\ carbon-use efficiencies perform in this logic.

Minimising the carbon consumption is minimising \bac\ carbon-use efficiency.  This includes increasing \bac l respired carbon fraction (decreasing $\eBR$; Fig. \ref{f:harvPB}E), decreasing \bac l leakage carbon fraction (decreasing $\eB$; Fig. \ref{f:harvPB}F) and increasing the \bac l density-independent death rate (increasing $\mB$; Fig. \ref{f:harvPB}H).  Decreasing $\eBR$ and $\eB$ raises the carbon requirements of \bac, which suppress the maximum capacity of \bac.  Decreasing $\eBR$ means more carbon has to be released as carbon dioxide.  Decreasing $\eB$ means more carbon has to be released as organic carbon.  So less carbon can be incorporated into the \bac l biomass, and the maximum capacity of \bac\ decreased.  With less \bac, less carbon will be consumed in the long-term and relatively more carbon can be retained in the organic carbon pool for harvest.  The logic is similar to $\aP$ but in reverse.  Increasing $\mB$ has a short-term effect of increasing the organic carbon pool as well as a long-term effect of suppressing \bac l consumption of organic carbon.  Hence more carbon can be retained for harvest.  The results agree with experimental co-cultures on enhancing product yield by the use of \bac\ \autocite{fuentes2016impact,santos2014microalgal}.  It also simultaneously suggests the biological reasons leading to the enhancement of carbon yield.  The results provide a direction for future experimental confirmation on why a \bacm\ is needed for yield enhancement but the \bacm\ has to be suppressed to the maximum extent.

Two life history traits, \phy\ growth rate ($\gP$; Fig. \ref{f:harvPB}C) and \bac l clearance rate ($\gB$; Fig. \ref{f:harvPB}G), have a peak carbon yield within the range of trait values.  This shows increasing those trait values are beneficial to carbon yield enhancement until their optima; further increasing the trait values recede the carbon benefit.  Increasing $\gP$ and decreasing $\gB$ enhance carbon yields agree with the same logic above: increasing the \phy\ biomass density and/or decreasing the \bac l biomass density benefits the long-term organic carbon accumulation.

The model suggests a high $\gP$ beyond the optimal value also recede carbon yield.  This effect may be brought by the limiting living space for \phy; extremely high \phy\ growth rate would make some fraction of \phy\ biomass non-functional but also non-harvestable.  The redundant \phy\ biomass cannot expand further the organic carbon pool due to space limitations but also cannot be harvested because it is not in the organic carbon pool.  This situation may provide a reason for a lower-than-expected yield in ocean fertilisation geo-engineering proposals \autocite{boyd2008implications,gnanadesikan2008export,oschlies2010side}, where fertilisers only cause a localised algal bloom \autocite{gnanadesikan2008export,lawrence2014efficiency,trick2010iron,kwiatkowski2015atmospheric,lovelock2007ocean}.  In those areas, \phy\ are growing so fast that they cannot disperse to other areas where living space can be a limiting factor.  The results provide a direction for experimental confirmation on the mechanism behind receding yield benefit.

The model suggests that a $\gB$ lower than optimal value benefits carbon yield.  This effect is caused by the trade-off between \bac l consumption and death.  In a low $\gB$ situation, \bac l consumption ($\gB CB$) is small, so as the \bac l death ($\mB$; Eq. \ref{eq:PBH}).  An increase of $\gB$ provides more \bac l biomass to enhance future carbon yield through higher amounts of \bac l death with a minor fraction of respiration (leakage does not matter as carbon flow back to the organic carbon pool).  The increase of \bac l biomass hence is a benefit to future carbon harvest.  The benefit recedes when $\gB$ rises beyond the optimal value, which \bac l respiration is a considerable loss of carbon from the \pbs\ and hinders the yield from organic carbon pool.  The results may reinforce the experimental observations on \bac\ enhancing carbon yield in \pbs s \autocite{fuentes2016impact,santos2014microalgal} by providing a mechanistic explanation.  The results provide a direction for future ground-truthing with experimental measurements.

\end{document}


\section{Related work}
The work that we are going to study is a problem of \emph{representation/feature learning}, which is an unsupervised learning task. Representation learning regards the building of intermediate representations of data useful to solve machine learning tasks. In this project, the features that have been learned by solving the Jigsaw puzzle are repurposed to solve a classification problem. This involves also \emph{transfer learning} via \emph{fine tuning}, which is a process of updating the weights that are obtained from the training from a task to solve another task.

\subsection{Unsupervised Learning}
Most techniques that regards unsupervised learning exploit general-purpose priors such as smoothness, sharing of factors, hierarchical factors, etc. However, a general criterion to design a visual representation is not available. In the context of Computer Vision there are early works on unsupervised learning of models for classification. For example, in \cite{unsupervised_scale_invariant_learning} and \cite{unsupervised_learning_model_recognition} a probabilistic representation of objects as constellations of parts is presented. A limitation is the high computational complexity of these models. We will explain later that the Jigsaw puzzle solver also aims to build a model of both appearance and configuration of the parts.

\subsection{Self-supervised Learning}
This kind of unsupervised learning exploits labeling that comes for \textquotedblleft free" within the data. There are two types of labels that can be used:
\begin{itemize}
    \item labels that are easily accessible and are associated with a non-visual signal (such as egomotion);
    \item labels that are obtained form the structure of the data.
\end{itemize}
The paper that we are going to implement uses the latter case as they simply re-use the input images and exploits the pixel arrangement as a label.\newline
In \cite{context_prediction} Doersch \textit{et al.} train a convolutional network to classify the relative position between two patches. In contrast, in \cite{Noroozi_2016} the Jigsaw puzzle problem is solved by observing all the tiles at the same time. This allows the trained network to intersect all ambiguity sets and possibly reduce them to a singleton.
Agrawal \textit{et al.} \cite{learning_by_moving} exploits labeling provided by an odometry sensor: they trained a Siamese network to estimate ego-motion from two images frames and compare it to the ego-motion measured with the odometry sensor. The advantage is that labeling is freely available in most cases. However, since the object identity is the same in both images, the intraclass variability may be limited: concentrating on the low-level similarities between the images, some high-level structures would be lost. The Jigsaw puzzle approach ignores the similarities between the tiles and instead focuses on their differences.

\subsection{Jigsaw Puzzle}
Solving Jigsaw puzzles has been associated with learning since their inception in 1760 by John Spilsbury. Studies in Psychonomic show that Jigsaw puzzles can be used to assess visuospatial processing in humans \cite{assessing_visuospatial_processes}. Instead of using Jigsaw puzzles with this purpose, in the referring paper they want to use them to develop a visuospatial representation of objects in the context of CNNs.


\documentclass[10pt,a4paper]{article}

\usepackage[left=1.2cm,right=1.2cm,top=1.2cm,bottom=1.2cm]{geometry}
\usepackage{amsmath}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{svg}

\definecolor{darkpastelgreen}{rgb}{0.01, 0.75, 0.24}

\title{%
    ELEC 0041: Homework 1\\
    \large Design of a DC busbar
}
\author{Tomasetti Romin}

\begin{document}

\maketitle

\begin{abstract}
    This is the report for the first homework of the course ELEC0041.
    The goal is to optimize a copper busbar such that the current arriving from an input port
    is evenly distributed to 3 output ports
    \footnote{The code for this project is available at \url{https://github.com/romintomasetti/elec0041/}.}
    .
\end{abstract}

\section{Simulation setup}

In this section, the simulated geometry is briefly described.
A simple mesh convergence analysis is also conducted.
Eventually, orders of magnitude of the simulated fields are verified using simple laws.

\subsection{Geometry and modeling}

The simulated geometry is shown in~\Cref{figure:geometry}.
This geometry differs from the problem statement in that it takes the symmetry of the problem into account.
An ellipse has been added to balance the current distribution amongst the three output ports.

Note that the simulation will be carried out considering that a 2D approximation is acceptable, i.e.
perfect conductors are assumed for the connectors and 3D effects are neglected.

\begin{figure}[h]
    \centering
    \includesvg[width=0.25\textwidth]{homework-1/report/geometry.svg}
    \caption{Simulated geometry.}
    \label{figure:geometry}
\end{figure}

\subsection{Convergence of the mesh}

In this section, a simple mesh convergence analysis is conducted.
The mesh is driven by 4 parameters, namely the element size on the top and bottom copper plate borders,
on the input and output ports, and on the balancing hole.

The convergence is observed using a globally integrated quantity, namely the losses.
The left and middle output port currents are also monitored.
As can be seen in~\Cref{figure:mesh-convergence}, the quantities are nearly converged at the green cross,
which is a parametrization showcasing a good precision/computing cost trade-off.

It must be noted that even though the ellipse's center will move vertically and minor and major axes will change
during the optimization process,
the mesh parametrization will be kept unchanged for simplicity.

\subsection{Orders of magnitude}

According to Pouillet's law, the resistance is given by $R = \rho l / A$.
In this case, considering that the length $l$ is the distance between the left-most output port
and the input port, it leads to:
%
\begin{equation}
    R \approx \rho /t \approx 1.72 \cdot 10^{-8} / 2 \cdot 10^{-3} \approx 0.86 \cdot 10^{-5} \Omega,
\end{equation}
%
where $t$ is the thickness of the copper plate and $\rho$ is the annealed copper resistivity
\footnote{The annealed copper resistivity and conductivity can be found in
\url{http://hyperphysics.phy-astr.gsu.edu/hbase/Tables/rstiv.html}.}.
Then, using Ohm's law with one third of the input current, the expected scalar electric potential magnitude is
%
\begin{equation}
    V = R I = \cfrac{375}{3} \cdot 0.86 \cdot 10^{-5} = 1.075 \cdot 10^{-3} \ V.
\end{equation}
%
It can be verified in~\Cref{figure:electric-potential} that the scalar electric potential is indeed
of order $10^{-3}$.

\section{Design optimization}

In this section, the selected design is discussed.
Then, a simple optimization of the chosen design is conducted.
Eventually, the \emph{nearly-optimal} design is presented, with a brief sensitivity analysis.

\subsection{Design choice}

In order to balance the current distribution amongst the 3 output ports,
the current path from the input port to the middle output port must be lengthened.
Keeping manufacturing costs and feasibility in mind, drilling an ellipse seems to be a good option
to achieve path lengthening, though less simple to manufacture than a circle.
However, the oblong shape should help decrease the losses.

Since no specific information/constraint was given with respect to the rectangular shape
of the copper plate, it has been kept rectangular.
An improved, cheaper,
lightweight design
\footnote{The improved design will be cheaper if the increased manufacturing cost is balanced
by the reduction in price due to a lowered copper usage.}
could be thought of,
removing the parts of the plate that participate the less to current
conduction, keeping the losses at an acceptable level, via topology optimization.

\subsection{Optimization}

It was chosen to conduct an optimization campaign where the input parameters are the
ellipse center along the vertical direction, major and minor axes, and the objective
is $\left| I_{left}-I_{center}\right|$, \emph{i.e.} the current imbalance.

As a very simple approach, the \href{
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.brute.html
}{\emph{scipy.optimize.brute}} optimizer is used. It starts by a grid-based design of experiments campaign
and, from the best point, runs \href{
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fmin.html
}{\emph{scipy.optimize.fmin}} to refine the best solution.

It must be noted that a more advanced optimization campaign should also take weight, costs and losses into account,
i.e. perform a multi-objective optimization.

The optimal point found by the method (with default parameters) is given in~\Cref{table:optimal-point}.
The current imbalance is clearly neglectable.
It must be noted this optimal point must be sufficiently \emph{robust} to
cope with manufacturing tolerances. A sensitivity analysis is conducted in~\Cref{subsection:sensitivity}.

\begin{table}[h]
    \centering
    \begin{tabular}{|l|l|c|}
        \hline
        Parameter & Value & Unit \\\hline
        Ellipse half-vertical axis              ($DO_a$) & $1.05930 \cdot 10^{-2}$ & $m$              \\\hline
        Ellipse half-horizontal axis            ($DO_b$) & $6.35262 \cdot 10^{-3}$ & $m$              \\\hline
        Ellipse center along vertical direction ($DO_y$) & $2.78669 \cdot 10^{-2}$ & $m$              \\\hline
        Current imbalance                                & $6.04700 \cdot 10^{-5}$ & $A \cdot m^{-1}$ \\\hline
        Losses                                           & $239.829$               & $W \cdot m^{-2}$ \\\hline
    \end{tabular}
    \caption{Geometrical parameters of the optimal design with neglectable current imbalance.}
    \label{table:optimal-point}
\end{table}

\subsection{Sensivity near optimal point}
\label{subsection:sensitivity}

For this preliminary analysis of the busbar, a simple sensitivity analysis is chosen, namely \emph{One-at-a-time} (OAT).
Considering a machining tolerance of a tenth of a millimeter seems reasonable, since smaller tolerances
add extra costs. Considering the optimal values for the 3 geometrical parameters (see~\Cref{table:optimal-point}), a tenth of a millimeter corresponds
more or less to 0.5\%, 1\% and 1.5\% variability for $DO_y$, $DO_a$ and $DO_b$, respectively.
A OAT sensitivity analysis is shown in~\Cref{figure:sensitivity-around-optimal}.
It seems that the design is more sensitive to $DO_b$ (minor ellipse axis), though a deeper analysis is needed.
It is also clear that the machining tolerance would need to be less than a tenth of a millimeter for the design
to perfectly balance the current.

\section{Conclusion}

In this report, a preliminary design of a DC busbar was conducted, see~\Cref{figure:geometry}.
An optimal design was computed using a single objective optimization driven by the current distribution imbalance,
see~\Cref{table:optimal-point}.
A short sensitivity analysis near the optimal point with respect to the 3 design parameters was conducted in~\Cref{subsection:sensitivity}.

\pagebreak

\section{Supplementary figures}

\begin{figure}[h]
    \centering
    \includesvg[width=0.8\textwidth]{homework-1/test_mesh_convergence.svg}
    \caption{
        Convergence study.
        The mesh is driven by 4 parameters, namely the size of the elements on the top and bottom borders of the copper plate,
        on the input and output ports and on the ellipse.
        Their relative proportions are kept constant.
        The \textcolor{darkpastelgreen}{green cross} represents the chosen mesh parametrization.
    }
    \label{figure:mesh-convergence}
\end{figure}

\begin{figure}[h]
    \begin{minipage}{\textwidth}
        \setlength{\unitlength}{0.1403pt}
        \begin{picture}(0,0)
            \includegraphics[scale=0.1403]{homework-1/report/v.sym.png}
        \end{picture}%
        \begin{picture}(3042,1932)(0,0)
            \put(760.5,92){\makebox(0,0)[b]{\textcolor[rgb]{0,0,0}{{0}}}}
            \put(1521,92){\makebox(0,0)[b]{\textcolor[rgb]{0,0,0}{{0.00125}}}}
            \put(2281.5,92){\makebox(0,0)[b]{\textcolor[rgb]{0,0,0}{{0.0025}}}}
            \put(1521,142.4){\makebox(0,0)[b]{\textcolor[rgb]{0,0,0}{{Scalar electric potential [V]}}}}
            \put(2988,86){\makebox(0,0)[bl]{\textcolor[rgb]{0,0,0}{{X}}}}
            \put(2928,146){\makebox(0,0)[bl]{\textcolor[rgb]{0,0,0}{{Y}}}}
            \put(2928,86){\makebox(0,0)[bl]{\textcolor[rgb]{0,0,0}{{Z}}}}
        \end{picture}
    \end{minipage}
    \caption{Scalar electric potential in the default configuration.}
    \label{figure:electric-potential}
\end{figure}

\begin{figure}[h]
    \centering
    \includesvg[width=0.5\textwidth]{homework-1/test_sensitivity.svg}
    \caption{OAT sensitivity of the imbalance w.r.t. optimal parameter values from~\Cref{table:optimal-point}.}
    \label{figure:sensitivity-around-optimal}
\end{figure}

\end{document}


\subsection{The Ramsey-Cass-Koopmans model}

This is based on the Solow-Swan model, with an endogeneous savings rate.