\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{listings}
\usepackage{hyperref}

\setlength\parindent{0pt}

\begin{document}
    \title{HPCSE II - Exercise 4}
    \author{Anian Ruoss}
    \maketitle

    \section*{Task 1}
    \label{sec:Task1}

    As in Task 2 of Part 2 of Homework 3, we use the head2DSolver to model
    the temperature distribution on the steel sheets.
    For every torch we want to determine the optimal beam width, beam
    intensity and x- and y-coordinates meaning that we have 16 parameters in
    total for the 4 robotic torches.
    For all parameters we know the upper and the lower bounds given by:
    \begin{itemize}
        \item $x \in [0.0, 0.5]$ for torches 1 and 2
        \item $x \in [0.5, 1.0]$ for torches 3 and 4
        \item $y \in [0.0, 1.0]$ for all torches
        \item $\text{beam intensity} \in [0.4, 0.6]$ for all torches
        \item $\text{beam width} \in [0.04, 0.06]$ for all torches
    \end{itemize}
    Since we do not have any additional information about the parameter
    distributions, we model all parameters as being uniformly distributed
    within their respective bounds.
    We employ Korali's CMA-ES solver to maximize the posterior
    distribution of the parameters (even though we don't really have
    informative prior distributions, but the posterior was given in the
    template) to find the most likely parameter values and we display the
    results in Listing~\ref{lst:Task1}.

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Korali
    output when maximizing the posterior distribution of the parameters for
    the model with four robotic torches.}, label={lst:Task1}]
    [Korali] Starting CMAES. Parameters: 17, Seed: 0xFFFFFFFFFFFFFFFF
    ...
    [Korali] Finished - Reason: Object variable changes < 1.00e-06
    [Korali] Parameter 'Sigma' Value: 0.937023
    [Korali] Parameter 'torch_1_x' Value: 0.242895
    [Korali] Parameter 'torch_1_y' Value: 0.240862
    [Korali] Parameter 'torch_1_intensity' Value: 0.505957
    [Korali] Parameter 'torch_1_width' Value: 0.046153
    [Korali] Parameter 'torch_2_x' Value: 0.251464
    [Korali] Parameter 'torch_2_y' Value: 0.741347
    [Korali] Parameter 'torch_2_intensity' Value: 0.479662
    [Korali] Parameter 'torch_2_width' Value: 0.051501
    [Korali] Parameter 'torch_3_x' Value: 0.757994
    [Korali] Parameter 'torch_3_y' Value: 0.254470
    [Korali] Parameter 'torch_3_intensity' Value: 0.474448
    [Korali] Parameter 'torch_3_width' Value: 0.054619
    [Korali] Parameter 'torch_4_x' Value: 0.760434
    [Korali] Parameter 'torch_4_y' Value: 0.770577
    [Korali] Parameter 'torch_4_intensity' Value: 0.475872
    [Korali] Parameter 'torch_4_width' Value: 0.059926
    [Korali] Total Elapsed Time: 234.156253s
    \end{lstlisting}

    Korali performs the optimization in roughly 4 minutes.
    We know from the lecture that CMA-ES is embarrassingly parallel since we
    can compute and evaluate every sample independently, which can be
    achieved by parallelizing the two for-loops at lines 11 and 14.
    Typically the evaluation is a lot more costly than random
    number generation, which is why we should already observe a considerable
    speedup from only parallelizing the loop at line 14.

    \section*{Task 2}
    \label{sec:Task2}

    We run the single tasking engine and display its output in
    Listing~\ref{lst:Task2Single}.

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Output from
    executing the single tasking engine.}, label={lst:Task2Single}]
    Processing 240 Samples each with 2 Parameter(s)...
    Verification Passed
    Total Running Time: 29.717s
    \end{lstlisting}

    \subsection*{a)}
    \label{subsec:Task2a}

    Since all samples are well-known at the beginning, they can be distributed
    evenly among all ranks and gathered back to one rank once the evaluations
    are completed.
    We implement this divide-and-conquer strategy with UPC++ and MPI and
    display the results obtained from running the implementations with 24
    ranks on an Euler compute node in listings~\ref{lst:Task2aUPCXX}
    and~\ref{lst:Task2aMPI} respectively.

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Output from
    executing the UPC++ tasking engine with the divide-and-conquer strategy.},
    label={lst:Task2aUPCXX}]
    Verification Passed
    Total time:           1.37665
    Average time:         1.17792
    Load imbalance ratio: 0.144362
    \end{lstlisting}

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Output from
    executing the MPI tasking engine with the divide-and-conquer strategy.},
    label={lst:Task2aMPI}]
    Verification Passed
    Total time:           1.31242
    Average time:         1.17451
    Load imbalance ratio: 0.10508
    \end{lstlisting}

    We observe a speedup of $\approx 21.5$ for UPC++ and $\approx 22.5$ for
    MPI and thus we report efficiencies of $\approx 90\%$ for UPC++ and
    $\approx 94.5\%$ for MPI.\@
    Both implementations suffer from a relatively high load imbalance ratio
    ($\approx 0.145$ for UPC++ and $\approx 0.105$ for MPI) which results
    from the fluctuation in evaluation times and the static distribution
    schedule.
    The MPI approach is practically identical to that employed for the UPC++
    code but the collective operations in MPI allow for a much cleaner
    implementation (especially using \textit{MPI\_Scatter} since it eliminates
    the need to care about the global sample index of sample that is processed
    locally on a given rank).
    In general, MPI feels more natural since it requires very explicit
    communication.

    \subsection*{b)}
    \label{subsec:Task2b}

    To solve the load imbalance problem observed in Task 2a) we implement
    the producer-consumer strategy which takes advantage of the fact that the
    evaluation times differ and distributes workloads
    according to rank availability and not according to a fixed scheme.
    We display the results obtained from running the UPC++ and MPI
    implementations with 24 ranks on an Euler compute node in
    listings~\ref{lst:Task2bUPCXX} and~\ref{lst:Task2bMPI} respectively.

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Output from
    executing the UPC++ tasking engine with the producer-consumer strategy.},
    label={lst:Task2bUPCXX}]
    Processing 240 Samples each with 2 Parameter(s)...
    Verification Passed
    Total time:           1.32383
    Average time:         1.25486
    Load imbalance ratio: 0.0520984
    \end{lstlisting}

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Output from
    executing the MPI tasking engine with the producer-consumer strategy.},
    label={lst:Task2bMPI}]
    Verification Passed
    Total time:           1.32354
    Average time:         1.24246
    Load imbalance ratio: 0.0612577
    \end{lstlisting}

    We observe for both UPC++ and MPI that the load imbalance ratio drops
    significantly compared to the divide-and-conquer strategy, although more
    drastically for UPC++.
    For UPC++ we observe that the total time decreases slightly compared to
    the divide-and-conquer strategy (speedup: $\approx 22.5$, efficiency:
    $\approx 93.5\%$), whereas for the MPI implementation the total running
    time increases marginally (speedup: $\approx 22.5$, efficiency:
    $\approx 93.5\%$).
    We take this as evidence that the UPC++ implementation has successfully
    solved the load imbalance problem.
    Even though the load imbalance has also dropped for MPI, the
    producer-consumer strategy requires a lot more communication which explains
    the slightly higher running time.
    The MPI approach differs slightly from the UPC++ approach:
    \begin{itemize}
        \item UPC++ employs a queue of consumers which contain a future among
        other data.
        The producer iterates over the queue and checks whether a RPC has
        completed before distributing another sample to the idling rank.
        \item The MPI implementation does not require a queue as the
        producer just sends samples and listens for results until all
        samples have been evaluated.
        Unlike the UPC++ implementation, we need to explicitly tell every
        rank that the evaluation has completed once all samples have been
        processed.
    \end{itemize}
    In general one can say that UPC++ is more suited for the implementation
    of the producer-consumer problem.
    Nevertheless, the MPI approach feels cleaner as its communication is more
    explicit\footnote{From \emph{The Zen of Python, by Tim Peters}:
    ``Explicit is better than implicit.``.}.

    \section*{Task 3}
    \label{sec:Task3}

    We run the single tasking engine and display its output in
    Listing~\ref{lst:Task3Single}.

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Output from
    executing the single tasking engine.}, label={lst:Task3Single}]
    Processing 240 Samples (24 initially available), each with 2 Parameter(s)...
    Verification Passed
    Total Running Time: 29.458s
    \end{lstlisting}

    Since not all samples are available at the beginning of the generation
    it does not make sense to have more ranks than initially available
    samples and we enforce this constraint with an assert.
    Apart from that our approaches are similar to those from Task 2b) and we
    display the results obtained from running the UPC++ and MPI
    implementations with 24 ranks on an Euler compute node in
    listings~\ref{lst:Task3UPCXX} and~\ref{lst:Task3MPI} respectively.

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Output from
    executing the UPC++ tasking engine .},
    label={lst:Task3UPCXX}]
    Processing 240 Samples (24 initially available), each with 2 Parameter(s)...
    Verification Passed
    Total Running Time: 1.355s
    \end{lstlisting}

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Output from
    executing the MPI tasking engine .},
    label={lst:Task3MPI}]
    Verification Passed
    Total Running Time: 1.311s
    \end{lstlisting}

    We observe speedups of $\approx 21.5$ for UPC++ and $\approx 22.5$ for
    MPI and correspondingly efficiencies of $\approx 90.5\%$ for UPC++ and
    $\approx 93.5\%$ for MPI.\@
    For the UPC++ implementation we faced the challenge that
    \mbox{\textit{getSample()}} and \mbox{\textit{updateEvaluation()}}
    have to be called from the root rank and that
    \mbox{\textit{updateEvaluation()}} has to be called after the evaluation
    has completed on a consumer rank.
    This problem can be elegantly solved with the \textit{then()} method
    from UPC++.
    Our MPI implementation is basically equivalent to that of Task 2b) and
    thus we refer to Task 2b) for a discussion of the differences between
    the UPC++ and MPI approaches.
    Using the $then()$ function is definitely more elegant than sending data
    back and forth as is required for MPI.\@

    \section*{Task 4}
    \label{sec:Task4}

    To exploit full parallelism we increase the population size to 23 since
    we have 23 consumer ranks and 1 producer rank.
    We run Korali with the single conduit and display its output in
    Listing~\ref{lst:Task4Single}.

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Korali
    output for the single conduit when running the code from Task 1 with
    population size 23.}, label={lst:Task4Single}]
    [Korali] Starting CMAES. Parameters: 17, Seed: 0xFFFFFFFFFFFFFFFF
    ...
    [Korali] Finished - Reason: Object variable changes < 1.00e-06
    [Korali] Parameter 'Sigma' Value: 0.937782
    [Korali] Parameter 'torch_1_x' Value: 0.251375
    [Korali] Parameter 'torch_1_y' Value: 0.741905
    [Korali] Parameter 'torch_1_intensity' Value: 0.414076
    [Korali] Parameter 'torch_1_width' Value: 0.060000
    [Korali] Parameter 'torch_2_x' Value: 0.242771
    [Korali] Parameter 'torch_2_y' Value: 0.240310
    [Korali] Parameter 'torch_2_intensity' Value: 0.435900
    [Korali] Parameter 'torch_2_width' Value: 0.053473
    [Korali] Parameter 'torch_3_x' Value: 0.760406
    [Korali] Parameter 'torch_3_y' Value: 0.770098
    [Korali] Parameter 'torch_3_intensity' Value: 0.590264
    [Korali] Parameter 'torch_3_width' Value: 0.048075
    [Korali] Parameter 'torch_4_x' Value: 0.756812
    [Korali] Parameter 'torch_4_y' Value: 0.254229
    [Korali] Parameter 'torch_4_intensity' Value: 0.501190
    [Korali] Parameter 'torch_4_width' Value: 0.051603
    [Korali] Total Elapsed Time: 441.061222s
    \end{lstlisting}

    Our UPC++ and MPI approaches are very similar to those of tasks 2b) and
    3 and we display the results obtained from running the UPC++ and MPI
    implementations with 24 ranks on an Euler compute node in
    listings~\ref{lst:Task4UPCXX} and~\ref{lst:Task4MPI}.

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Korali
    output for the UPC++ conduit when running the code from Task 1 with
    population size 23.}, label={lst:Task4UPCXX}]
    [Korali] Starting CMAES. Parameters: 17, Seed: 0xFFFFFFFFFFFFFFFF
    ...
    [Korali] Finished - Reason: Object variable changes < 1.00e-06
    [Korali] Parameter 'Sigma' Value: 0.937782
    [Korali] Parameter 'torch_1_x' Value: 0.251375
    [Korali] Parameter 'torch_1_y' Value: 0.741905
    [Korali] Parameter 'torch_1_intensity' Value: 0.414076
    [Korali] Parameter 'torch_1_width' Value: 0.060000
    [Korali] Parameter 'torch_2_x' Value: 0.242771
    [Korali] Parameter 'torch_2_y' Value: 0.240310
    [Korali] Parameter 'torch_2_intensity' Value: 0.435900
    [Korali] Parameter 'torch_2_width' Value: 0.053473
    [Korali] Parameter 'torch_3_x' Value: 0.760406
    [Korali] Parameter 'torch_3_y' Value: 0.770098
    [Korali] Parameter 'torch_3_intensity' Value: 0.590264
    [Korali] Parameter 'torch_3_width' Value: 0.048075
    [Korali] Parameter 'torch_4_x' Value: 0.756812
    [Korali] Parameter 'torch_4_y' Value: 0.254229
    [Korali] Parameter 'torch_4_intensity' Value: 0.501190
    [Korali] Parameter 'torch_4_width' Value: 0.051603
    [Korali] Total Elapsed Time: 21.553523s
    \end{lstlisting}

    \begin{lstlisting}[basicstyle=\tiny, frame=single, caption={Korali
    output for the MPI conduit when running the code from Task 1 with
    population size 23.}, label={lst:Task4MPI}]
    [Korali] Starting CMAES. Parameters: 17, Seed: 0xFFFFFFFFFFFFFFFF
    ...
    [Korali] Finished - Reason: Object variable changes < 1.00e-06
    [Korali] Parameter 'Sigma' Value: 0.937782
    [Korali] Parameter 'torch_1_x' Value: 0.251375
    [Korali] Parameter 'torch_1_y' Value: 0.741905
    [Korali] Parameter 'torch_1_intensity' Value: 0.414076
    [Korali] Parameter 'torch_1_width' Value: 0.060000
    [Korali] Parameter 'torch_2_x' Value: 0.242771
    [Korali] Parameter 'torch_2_y' Value: 0.240310
    [Korali] Parameter 'torch_2_intensity' Value: 0.435900
    [Korali] Parameter 'torch_2_width' Value: 0.053473
    [Korali] Parameter 'torch_3_x' Value: 0.760406
    [Korali] Parameter 'torch_3_y' Value: 0.770098
    [Korali] Parameter 'torch_3_intensity' Value: 0.590264
    [Korali] Parameter 'torch_3_width' Value: 0.048075
    [Korali] Parameter 'torch_4_x' Value: 0.756812
    [Korali] Parameter 'torch_4_y' Value: 0.254229
    [Korali] Parameter 'torch_4_intensity' Value: 0.501190
    [Korali] Parameter 'torch_4_width' Value: 0.051603
    [Korali] Total Elapsed Time: 22.081863s
    \end{lstlisting}

    We observe that the results are identical for all three conduits and
    that UPC++ achieves a speedup of $\approx 20.5$ with efficiency
    $\approx 85.5\%$ whereas MPI achieves a speedup of $\approx 20$ with
    efficiency $\approx 83\%$.
    Both implementations thus exceed the requirement of being at least 10x
    faster while producing results similar to those of the single conduit,
    implying that the CEO of the company will give us a raise.
    As mentioned above, both approaches are similar to those of tasks 2b)
    and 3 and can thus be easily adapted to the current task.
    However, for UPC++ we faced the new challenge that the number of
    parameters is not given as a constant.
    Consequently, we cannot use the \textit{Consumer} struct from Task 3 as
    its sample array would have to be allocated dynamically, which in turn
    means that \textit{Consumer} is not trivially copyable anymore.
    With UPC++ we can solve this problem by simply using the local pointer of
    the global sampleArrayPointer to access the sample data from every
    consumer rank.
    The MPI implementation for this task is basically identical to that of
    tasks 2b) and 3.
    For this reason, we refer to Task 2b) for a discussion on the difference
    between the implementations.
    As for Task 3, it was relatively easy to adapt the implementations to
    the current task for both frameworks.
    The most difficult part of the MPI conduit implementation is the
    integration with Korali which is described below:

    \begin{itemize}
        \item load the required modules and set the environment variables
        \begin{lstlisting}[basicstyle=\footnotesize]
    module load new
    module load gcc/6.3.0
    module load intel/2018.1
    module load impi/2018.1.163

    export UPCXX_GASNET_CONDUIT=smp
    export UPCXX_THREADMODE=seq
    export UPCXX_CODEMODE=O3
    export KORALI_CONDUIT=single
        \end{lstlisting}
        \item replace single.cpp with mpi.cpp
        \item replace the Makefile in the conduits directory
        with this
        \href{https://github.com/anianruoss/HPCSE-II/blob/master/exercise04/task1/conduits/Makefile_MPI}{Makefile}
        \item replace the Makefile in the task1 directory with this
        \href{https://github.com/anianruoss/HPCSE-II/blob/master/exercise04/task1/Makefile_MPI}{Makefile}
    \end{itemize}

\end{document}


\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage[left=2cm,right=2cm,bottom=2.5cm,top=2.5cm]{geometry}
\usepackage{fancyhdr}
\usepackage{subcaption}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage{float}
\usepackage{breqn}
\usepackage{soul}
\setlength{\headheight}{14.5pt}

%Swap bullets for long dash in itemize
	\renewcommand\labelitemi{---}
	\renewcommand\qedsymbol{\(\blacksquare\)}

% Typesets derivatives nicely using roman d. Adapts size automatically. Stolen from https://tex.stackexchange.com/questions/135944/commath-and-ifinner/135985#135985
% \od{y}{x} is ordinary derivative, \pd{f}{x} is partial derivative. See commath documentation for others
	\usepackage{amsmath}
	\newcommand{\dif}{\mathop{}\!\mathrm{d}}
	\newcommand{\Dif}{\mathop{}\!\mathrm{D}}

	\makeatletter
	\newcommand{\spx}[1]{%
		\if\relax\detokenize{#1}\relax
		\expandafter\@gobble
		\else
		\expandafter\@firstofone
		\fi
		{^{#1}}%
	}
	\makeatother

	\newcommand\pd[3][]{\frac{\partial\spx{#1}#2}{\partial#3\spx{#1}}}
	\newcommand\tpd[3][]{\tfrac{\partial\spx{#1}#2}{\partial#3\spx{#1}}}
	\newcommand\dpd[3][]{\dfrac{\partial\spx{#1}#2}{\partial#3\spx{#1}}}

	\newcommand{\md}[6]{\frac{\partial\spx{#2}#1}{\partial#3\spx{#4}\partial#5\spx{#6}}}
	\newcommand{\tmd}[6]{\tfrac{\partial\spx{#2}#1}{\partial#3\spx{#4}\partial#5\spx{#6}}}
	\newcommand{\dmd}[6]{\dfrac{\partial\spx{#2}#1}{\partial#3\spx{#4}\partial#5\spx{#6}}}

	\newcommand{\od}[3][]{\frac{\dif\spx{#1}#2}{\dif#3\spx{#1}}}
	\newcommand{\tod}[3][]{\tfrac{\dif\spx{#1}#2}{\dif#3\spx{#1}}}
	\newcommand{\dod}[3][]{\dfrac{\dif\spx{#1}#2}{\dif#3\spx{#1}}}

	\newcommand{\genericdel}[4]{%
		\ifcase#3\relax
		\ifx#1.\else#1\fi#4\ifx#2.\else#2\fi\or
		\bigl#1#4\bigr#2\or
		\Bigl#1#4\Bigr#2\or
		\biggl#1#4\biggr#2\or
		\Biggl#1#4\Biggr#2\else
		\left#1#4\right#2\fi
	}
	\newcommand{\del}[2][-1]{\genericdel(){#1}{#2}}
	\newcommand{\set}[2][-1]{\genericdel\{\}{#1}{#2}}
	\let\cbr\set
	\newcommand{\sbr}[2][-1]{\genericdel[]{#1}{#2}}
	\let\intoo\del
	\let\intcc\sbr
	\newcommand{\intoc}[2][-1]{\genericdel(]{#1}{#2}}
	\newcommand{\intco}[2][-1]{\genericdel[){#1}{#2}}
	\newcommand{\eval}[2][-1]{\genericdel.|{#1}{#2}}
	\newcommand{\envert}[2][-1]{\genericdel||{#1}{#2}}
	\let\abs\envert
	\newcommand{\sVert}[1][0]{%
		\ifcase#1\relax
		\rvert\or\bigr|\or\Bigr|\or\biggr|\or\Biggr
		\fi
	}
	\newcommand{\enVert}[2][-1]{\genericdel\|\|{#1}{#2}}
	\let\norm\enVert
	\newcommand{\fullfunction}[5]{%
		\begin{array}{@{}r@{}r@{}c@{}l@{}}
			#1 \colon & #2 & {}\longrightarrow{} & #3 \\
			& #4 & {}\longmapsto{}     & #5
		\end{array}
	}

\title{Presentation Sketch Script}
\author{B. Han, T.M. Hodgson, M. Holden \& M. Puza}

% Define new commands for typesetting here
\newcommand{\R}{\mathbb{R}}
\renewcommand{\L}{\mathcal{L}}
\renewcommand{\P}{\mathbb{P}}
%\renewcommand{\epsilon}{\varepsilon}
\newcommand{\e}{\mathrm{e}}
\newcommand{\grad}{\nabla}
\newcommand{\E}{\mathbb{E}}

\begin{document}
\maketitle
\section{Introduction}
``Recent Advances in Langevin Monte Carlo'' \\
The aim of MCMC is to be able to accurately sample from an unknown probability distribution. When do we not know a distribution?
 \begin{itemize}
     \item Bayesian Statistics, only have some data and an incomplete idea of what the distribution looks like
     \item Molecular Dynamics
 \end{itemize}
 All MCMC methods aim to sample as accurately and efficiently as possible. We will focus on distributions of the form
 \[\pi(x) = \mathcal{Z}^{-1} \e^{-U(x)}\]
 This distribution goes by many names as it crops up in many areas:
 \begin{itemize}
     \item \st{Statisticians know it as a `log-linear model'} \textbf{REALLY? -- MH}
     \item Mathematicians often call it a `Gibbs measure'
     \item Physicists know it as the `Boltzmann distribution' or `canonical ensemble'
 \end{itemize}
 \st{In fact, the Hammersley-Clifford theorem states that any measure that satisfies a Markov property can be expressed as a Gibbs measure}. So although we only look at distributions of this form, it is not actually as much of a restriction as it initially appears. Borrowing the terminology from MD, we call \(U\) the potential function (\(\mathcal{Z}\) is a normalising constant). We know potential, not normaliser. Let's look pursue this molecular dynamics link to gain some intuition. How does this distribution arise?

 In MD, \(U\) describes the potential energy of a particle, for this reason we call \(U\) a potential well. (Picture of potential well). If we let a particle move in this well, this is what we see (animation?). The particle moves in the well, up and down each side.

 The equation of motion for a particle moving like this (Brownian dynamics) is the SDE
 \[\dif X_t = -\grad U(X_t) \dif t +\sqrt{2}\dif W_t\]
 the (overdamped) Langevin equation. A natural question to ask is where is this particle on average? It spends most of the time in the bottom of the well (can see that just by watching), sometimes makes it quite high up the sides. Shock horror, it is exactly given by \(\pi\)! This is what we call the invariant measure of the SDE (kind of).

\section{Algorithms}
 So back to statistics: the problem of statistical sampling has just been reduced to watching particles in wells! Bonzer! ... Except not quite. Just like in the ordinary differential equation case, one can't solve exactly in many cases, so we must discretise. Like in ODEs we use the Euler method!

 Applying this to the Langevin equation gives an iterative scheme to approximate the continuous diffusion (this is the MC from which MCMC gets its name).
 \[X_{n+1} = X_n - h\grad U(X_n)  + \sqrt{2h}Z_{n+1}\]
 Let's see what this looks like (trace plot of approximation? Or vis in well with a higher stepsize) This is only exact when the step size tends to zero, which we obviously can't do. This means that the invariant measure (the average positions of the particles) may not be the same as the continuous case. Or we may not even be able to find one! (Chain is transient/not ergodic)

 Example:

 Most extreme case: \(h=1, U(x)=x^2/2\). Obvious invariant measure is \(N(0,1)\), but chain (immediately) converges to \(N(0,2)\).

 So we have to be careful with stepsize. Also if the potential is superlinear (sides of the well are too steep), the iterative scheme diverges, even though the underlying diffusion is ergodic.

 Can we fix this? Yeeees, with Metropolis rejection (explain? Idea is to just reject some of the samples with some probability - show vis with rejects). Doing so means that whenever the underlying diffusion is exp ergodic?, the chain also will be (in the limit -- not actually useful - show vis.). Still runs in to problems when the potential is superlinear.

 So this is the direction that most research has taken since its initial proposal in 1996. We have been looking at a different method that avoids Metropolis rejection -- TAMING!

 \section{Taming the Beast}
 We have seen in the visualisation that \texttt{ULA} and \texttt{MALA} don't quite work. The issue is the gradient of the potential well. Taming swaps the gradient for a function that is `close' to the gradient, but preserves the invariant measure of the diffusion.
\[\dif X_t = -\grad U(X_t) \dif t +\sqrt{2}\dif W_t \Longrightarrow \dif X_t = -T(X_t)\dif t +\sqrt{2}\dif W_t \]
Different choices of \(T\) are available, we consider
\[T=\frac{\grad U }{1+h\grad U}\]
What does this do? It reduces the effect the gradient has on the iteration. Visualise.

Stiffness effects motivates coordinatewise? Show how taming deals with stiff problems in 2d. (Impacts ability in ill-conditioned problems).

One final modification we wish to mention is to use a different method of discretisation: Higher order (Stochastic Runge-Kutta) \texttt{tHOLA}, Leimkuhler-Matthews (Non-Markovian) \texttt{LM}. We don't have time to go into these methods here but ask at the end if you're interested in how they work.
\section{Horse Racing}
So we have given a heuristic (qualitative) motivation of how these algorithms are different. Can we quantify each methods effectiveness on different potentials/distributions?

The existing results on \texttt{tULA} only looked at first and second moments Brosse et al.. This is sufficient only for Gaussian distributions, which aren't even superlinear. We wish to get some stronger results in terms of metrics.

Shitty boxplots \(\longrightarrow\) Histograms? Wasserstein? KL? Computer time? Pick two or three plots that best illustrate errors, or use vis.

We also have some theoretical non-asymptotic error bounds in these metrics.

\section{Conclusion}
What algorithm is best? Probably problem dependent, depends what you want out of the alorithm. Taming will give you an answer, which may be inaccurate -- but at least it is an answer.
\subsection*{Next Steps}
There are many many methods we haven't considered: HMC, mMALA, SGLD. Discuss SGLD? So hot right now NeurIPS.
\subsubsection*{Future Work}
We'd like to test these methods on real data, or even just simulated data, when we don't know the gradient analytically. This is much more relevant for the end user than a theoretical bound on convergence. Especially wish to test on `big data' as many (all?) MCMC methods suffer the curse of dimensionality. Lower dimensions may not show off the benefits of taming, may only become relevant in high dimensions. Also for algorithms with very complex iteration steps, they may lend themselves to parallelisation  better than simpler algorithms.

Finally, we've made all the code used for testing available on github, as well as the visualisation.

\begin{center}Any questions? PLease say no\end{center}
\end{document}


\documentclass{article}
\usepackage[elliptic]{qed}

\begin{document}

\section*{Numerical fallback}

The Legendre relation is a relation among the elliptic integrals of the first
($\ellipk$) and second ($\ellipe$) kinds, given by
%
\begin{qed}
    \ellipe(k^2) \ellipk(1-k^2) + \ellipe(1 - k^2) \ellipk(k^2)
    - \ellipk(k^2) \ellipk(1 - k^2) = \frac{\pi}{2}
\end{qed}
%
Unfortunately, \textsf{sympy} doesn't know how to automatically simplify it,
so \QED cannot analytically determine its validity. Nevertheless,
when we typeset it, we still got a passing badge. That's because \QED falls
back to numerical evaluation automatically.

\end{document}


\documentclass[executivepaper]{extarticle}
\usepackage[margin=3.0cm]{geometry}
\usepackage{fancyhdr}
\usepackage{extsizes}
\usepackage{enumerate}
\usepackage{enumitem}
\usepackage{mathtools}

\begin{document}

\vspace*{-40mm}

\begin{center}

\textbf{Modern Class Notes}

\end{center}

\section*{1/12/16}

\vspace{-5.5mm}

\noindent \rule{2cm}{0.5pt}

A group is an object that tries to describe the symmetry of something \\

Going to study:

\begin{enumerate}

\item Permutation groups

\item Matrix groups

\item Cosets and Legrange's Theorem

\item And other constructs

\end{enumerate}

Review of some discrete math stuff: \\

\begin{enumerate}

\item A function $f: x \rightarrow y$ (where X and Y are sets) is an assignment of an element of Y for each element of X

\item Fact: let $f: x \rightarrow y$ be a function. Then:

\begin{enumerate}

\item $f$ is invertible $\iff f$ is one to one

\end{enumerate}

\end{enumerate}

Ex)Let x={1, 2, 3,...,n}\\

$\pi(1)=2$

$\pi(2)=3$

$\pi(n-1)=n$

$\pi(n)=1$\\

A permutation is a one-to-one and onto function from a finite set to itself \\

At least 3 ways of recording a permutation: \\

\begin{enumerate}

\item Two line notation

\item One line notation (works for permutations of order up to 9)

\item Cycle notation: start with a number and write down where it goes and then repeat this process for the rest of the numbers

\item

\end{enumerate}

\end{document}


% Created: Enze Chen, June 2017
% Last edited: Enze Chen, February 2018
%
% Chapter 4 of the MSE 142 coursereader. This chapter discusses quantum tunneling using the finite potential step/barrier. It derives the math behind boundary conditions and tunneling probability. Interference from multiple boundaries is also discussed. There is an emphasis on applications to really drive the point home that this phenomenon is cool and more ubiquitous than we might initially think!

% Uncomment the following three lines and last line to individually compile this chapter
%\documentclass[12pt, english]{book}
%\usepackage{142crstyle}
%\begin{document}

\chapter{Quantum Tunneling} \label{ch:tunnel}
%{ \doublespacing
Imagine you're tossing a tennis ball against the wall. Classical mechanics says that the ball will bounce back at you, or, should you throw it very hard, it might get stuck in the wall. But what if the ball somehow \emph{goes through} the wall and emerges on the other side, leaving both unscathed? This is the basis for \textbf{quantum tunneling}, a phenomenon where a particle passes through a potential barrier that it classically could not surmount. As bizarre as this may seem, quantum mechanics says there is a non-zero probability for tunneling to occur, and this phenomenon is actually leveraged for a variety of physical applications. Tunneling at the quantum scale occurs in physical systems ranging from photons\footnote{See D. D. Coon \href{http://aapt.scitation.org/doi/10.1119/1.1972893}{\emph{American Journal of Physics}} \textbf{34}, 240 (1966).} to water\footnote{See \href{https://physics.aps.org/articles/v9/43}{this focus article} from \emph{Physics} \textbf{9}, 43 about the work of A. I. Kolesnikov et al, \href{https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.116.167802}{\emph{Physical Review Letters}} \textbf{116}, 167802 (2016).} and we will spend this chapter exploring the nature of this truly quantum mechanical behavior.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Finite potential step}
To facilitate our analysis of this behavior, we need to first devise a simple model for a quantum mechanical system, which we have shown here in Figure~\ref{fig:tunnel-step}. On the left half of the figure in region I, where $x < 0$, we have a free particle ($V=0$) traveling to the right with energy $E$. At $x=0$, the particle encounters a \textbf{potential step} with some \emph{finite} potential $V=V_0$ that extends for all $x>0$ into region II.

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.42\linewidth]{tunnel-step}
	\caption{A simple model to demonstrate quantum tunneling. A free particle with energy $E$ is traveling to the right and at $x=0$ is incident on a finite potential step with magnitude $V_0$.}
	\label{fig:tunnel-step}
\end{figure}

Note that we have not said anything yet about the relationship between $E$ and $V_0$ (the vertical position of the particle in Figure~\ref{fig:tunnel-step} was arbitrarily drawn). Since the finite potential step is time-independent, we will try to understand how quantum mechanical particles interact with such a potential by solving the time-independent \Sch\ equation. In region I, we only have the free particle, so we can write down the \Sch\ equation as
\begin{equation}
	-\frac{\hbar^2}{2m} \dv[2]{\Psi}{x} = E\Psi \label{eq:reg-1-se}
\end{equation}
In region II, we have
\begin{equation}
	-\frac{\hbar^2}{2m} \dv[2]{\Psi}{x} = (E-V_0)\Psi \label{eq:reg-2-se}
\end{equation}
where the energy of the particle is now with reference to the step's potential. We've seen these kinds of equations before and know that the general solution are sines and cosines, as was the case with the particle in a box. Here we write the general solutions in a slightly different, but equivalent way:
\begin{tcolorbox}[title = Traveling wave solutions] \vspace{-2ex}
	\begin{align}
		\Psi_{\text{I}} &= Ae^{ik_1x} + Be^{-ik_1x} \label{eq:reg-1-wf} \\
		\Psi_{\text{II}} &= Ce^{ik_2x} + De^{-ik_2x} \label{eq:reg-2-wf}
	\end{align}
\end{tcolorbox}
where $A$, $B$, $C$, and $D$ are unknown coefficients. Whoa! We know by Euler's formula that complex exponentials can be reformulated as sinusoidal functions, so these solutions at least seem plausible on the surface. To be sure we did our math right, let's plug Equation~\ref{eq:reg-1-wf} into Equation~\ref{eq:reg-1-se} to verify that it is a valid solution.
\begin{align*}
	-\frac{\hbar^2}{2m} \dv[2]{\Psi_{\text{I}}}{x} &= E\Psi_{\text{I}} \\
	-\frac{\hbar^2}{2m} \dv[2]{x}(Ae^{ik_1x} + Be^{-ik_1x}) &= E(Ae^{ik_1x} + Be^{-ik_1x}) \\
	-\frac{\hbar^2}{2m} \dv{x}(Aik_1e^{ik_1x} - Bik_1e^{-ik_1x}) &= E(Ae^{ik_1x} + Be^{-ik_1x}) \\
	\frac{\hbar^2}{2m} \left(Ak_1^2e^{ik_1x} + Bk_1^2e^{-ik_1x}\right) &= E(Ae^{ik_1x} + Be^{-ik_1x}) \\
	k_1^2\left(Ae^{ik_1x} + Be^{-ik_1x}\right) &= \frac{2mE}{\hbar^2}(Ae^{ik_1x} + Be^{-ik_1x})
\end{align*}

So we see that
\begin{equation}
	k_1 = \sqrt{\frac{2mE}{\hbar^2}} \label{eq:reg-1-k}
\end{equation}
which is the same expression we found previously and shows that Equation~\ref{eq:reg-1-wf} is a valid solution. In a similar vein, we see that the magnitude of the wave vector in region II is given by
\begin{equation}
	k_2 = \sqrt{\frac{2m(E-V_0)}{\hbar^2}} \label{eq:reg-2-k}
\end{equation}

Note that if $E > V_0$, then $k_2$ is real and we have sinusoidal behavior in region II. This makes sense because a particle with higher energy than the potential step should be able to surmount it and continue exhibiting wave-like behavior. However, if $E < V_0$, then $k_2$ becomes imaginary, and the wave-like solutions given in Equation~\ref{eq:reg-2-wf} turn into exponentially growing and decaying functions. Thus it seems like even though the particle does not have sufficient energy to surmount the potential step, there is still something happening in region II \emph{inside} the step. We will come back to this point shortly. \par

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Forward and backward waves}
Now, the general solutions given by Equation~\ref{eq:reg-1-wf} and~\ref{eq:reg-2-wf} are perfectly acceptable mathematically, but we argued in class that there is also a useful physical interpretation of these solutions. Recall that the de Broglie relation relates the wave vector $k$ to the momentum. Now we see that these wave function solutions not only resemble the plane wave solutions that we started with, but they also have a superposition of positive and negative wave vectors which determine the direction of propagation of the wave. We therefore argue that we can associate $A$ with the amplitude of the \textbf{incident wave} on the step (since the exponent has a positive $k_1$) and $B$ with the amplitude of the \textbf{reflected wave} (arising from a reflection at the interface). Similarly, we will associate $C$ with the amplitude of the \textbf{transmitted wave} through the step and $D$ with the amplitude of the wave incident on the step from the right. \par

In the example shown in Figure~\ref{fig:tunnel-step}, because we are only considering a wave incident on the step from the left, the coefficient $D$ must be zero. For the case when $E > V_0$, we still have a complex exponential function, so the wave function hasn't changed much. But for the case when $E < V_0$, the wave function in region II is only an exponentially damped function, which as we discussed in class is analogous to an evanescent wave in wave optics.\footnote{Indeed, evanescent wave coupling causes \href{https://en.wikipedia.org/wiki/Total_internal_reflection\#Frustrated_total_internal_reflection}{``frustrated'' total internal reflection}, which is a very similar behavior to quantum tunneling.} To see why this is the case, we can define a constant
\begin{equation}
	k' = \sqrt{\frac{2m(V_0-E)}{\hbar^2}} \label{eq:reg-2-kp}
\end{equation}
such that
\begin{equation*}
	k_2 = \sqrt{\frac{2m(E-V_0)}{\hbar^2}} = i\sqrt{\frac{2m(V_0-E)}{\hbar^2}} = ik'
\end{equation*}

This allows us to rewrite Equation~\ref{eq:reg-2-wf} as
\begin{equation}
	\Psi_{\text{II}} = Ce^{ik_2x} = Ce^{-k'x} \label{eq:reg-2-wfp}
\end{equation}

and the exponential decay function emerges. Our goal now is to extract the reflection coefficient $R$ and transmission coefficient $T$. We define these two quantities as follows:
\begin{equation*}
	\boxed{R = \abs{\frac{B}{A}}^2 \quad \text{and} \quad T = \frac{k_2}{k_1}\abs{\frac{C}{A}}^2}
\end{equation*}
% TODO: check with Aaron about this
Similar to the statistical interpretation of the wave function, we have taken the modulus squared to represent the probability of an event occurring. \par

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Continuity condition}
In order to relate the coefficients from the wave function in region I with those from the wave function in region II, we have to be precise and specify the behavior at the boundary of the two regions (i.e. at $x=0$). Let us consider what the conditions are for continuity of the wave function and its first derivative in a region where the potential is discontinuous (as it is at $x=0$). Starting from the time-independent \Sch\ equation, we integrate over an infinitesimal region $(-\epsilon, \epsilon)$ about $x=0$:
\begin{align*}
	-\frac{\hbar^2}{2m} \dv[2]{\Psi}{x} + V(x)\Psi &= E\Psi \\
	\dv[2]{\Psi}{x} &= \frac{2m}{\hbar^2}(V(x) - E)\Psi \\
	\int_{-\epsilon}^{\epsilon} \dv[2]{\Psi(x)}{x} \dd{x} &= \frac{2m}{\hbar^2} \int_{-\epsilon}^{\epsilon} (V(x)-E)\Psi(x) \dd{x}
\end{align*}

Now as $\epsilon$ approaches zero, the right hand side approaches zero if $V(x)$ and $\Psi(x)$ are not infinite (and there are good reasons why $\Psi$, from which one calculates the probability density function, cannot go to infinity).\footnote{Though $\Psi(x)$ cannot be infinite at the boundary, $V(x)$ still can. We'll save this for the next chapter when we discuss the Kronig-Penney model.} We therefore conclude, after performing the straightforward integral on the left hand side, that
\begin{tcolorbox}[title = Continuity of the wave function] \vspace{-2ex}
	\begin{equation}
		\dv{\Psi}{x} \bigg|_{\epsilon} - \dv{\Psi}{x} \bigg|_{-\epsilon} = 0 \label{eq:cont-dx}
	\end{equation}
\end{tcolorbox}

This means that across the discontinuity in the potential, the first derivative of the wave function (and by extension the wave function itself) must be continuous.\footnote{Continuity of the wave function is an important point, so if you need another explanation to internalize the ideas, \href{https://www.quora.com/Why-does-the-wave-function-have-to-be-continuous}{Quora} actually gives a pretty good answer.}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Tunneling probability}
Equation~\ref{eq:cont-dx} allows us to obtain the desired transmission and reflection coefficients as follows. At the boundary, we require that
\begin{align*}
	\Psi_{\text{I}}(0) &= \Psi_{\text{II}}(0) \\
	\Psi_{\text{I}}'(0) &= \Psi_{\text{II}}'(0)
\end{align*}
which directly translates into
\begin{align}
A + B &= C \label{eq:abc1} \\
Ak_1 - Bk_1 &= Ck_2 \label{eq:abc2}
\end{align}

We can solve the above system of equations in many ways, such as multiplying Equation~\ref{eq:abc1} by $k_1$ and adding the result to Equation~\ref{eq:abc2}. Doing so gives
\begin{equation}
	\frac{C}{A} = \frac{2k_1}{k_1+k_2} \label{eq:c-a}
\end{equation}
and a similar method gives
\begin{equation}
	\frac{B}{A} = \frac{k_1 - k_2}{k_1 + k_2} \label{eq:b-a}
\end{equation}

Finally, this gives
\begin{tcolorbox}[title = $T$ and $R$ for finite potential step] \vspace{-2ex}
	\begin{align}
		T = \frac{k_2}{k_1}\abs{\frac{C}{A}}^2 &= \frac{4k_1k_2}{(k_1+k_2)^2} \label{eq:t-step} \\
		R = \abs{\frac{B}{A}}^2 &= \frac{(k_1-k_2)^2}{(k_1+k_2)^2} \label{eq:r-step}
	\end{align}
\end{tcolorbox}
If you have taken a course in optics or acoustics these equations should look familiar---they are similar to the results for the transmission and reflection coefficients for an acoustic wave or light wave incident on a boundary. Now we're beginning to see how quantum mechanics predicts transmission with a non-zero probability. Note here that the way we have defined $T$ and $R$ also gives us the identity $T + R = 1$. \par

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Finite potential barrier} \label{sec:tunnel-barrier}
Having successfully managed the previous example, we're now ready to tackle the slightly more complicated case (and the one that we've been waiting for!) of quantum tunneling through a finite potential barrier of width $L$ and height $V_0$ (Figure~\ref{fig:tunnel-barrier}). We will carry out the following calculations in particular for the case $E < V_0$, which is the most interesting and surprising case.

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.6\linewidth]{tunnel-barrier}
	\caption{In this example, a free particle with energy $E$ is traveling to the right and at $x=0$ is incident on a finite potential barrier with width $L$ and height $V_0$. The potential is otherwise zero in front of and behind the barrier.}
	\label{fig:tunnel-barrier}
\end{figure}

With three regions, we can now write down the solutions to the time-independent \Sch\ equation as
\begin{align}
	\Psi_{\text{I}} &= Ae^{ikx} + Be^{-ikx} \label{eq:barr-1} \\
	\Psi_{\text{II}} &= Ce^{k'x} + De^{-k'x} \label{eq:barr-2} \\
	\Psi_{\text{III}} &= Fe^{ikx} \label{eq:barr-3}
\end{align}

Here, we have defined $k = \sqrt{\frac{2m}{\hbar^2}(E)}$ and $k'=\sqrt{\frac{2m}{\hbar^2}(V_0-E)}$ such that $k'$ is a real number. As before, we note that Equation~\ref{eq:barr-2} models exponential growth and decay inside the barrier. We also argue that a particle incident only from the left means that the only term in region III is the transmitted wave with amplitude $F$ (Equation~\ref{eq:barr-3}). Now our goal is to extract the transmission coefficient $T = \abs{\frac{F}{A}}^2$.\footnote{Whereas previously we had an extra $k_2/k_1$ term, in this case the momentum of the wave in region I and region III are the same! It might seem weird, but tunneling doesn't cause the wave to actually lose energy.} In this case, the transmitted wave with amplitude $F$ encodes within it all the possible ways in which the wave can transmit through the barrier, including multiple reflections inside the barrier. All of these complicated possibilities are accounted for simply by enforcing the continuity equations at $x=0$ and $x=L$. This gives us the following set of equations:
\begin{align*}
	A + B &= C + D \\
	A - B &= (C - D)\frac{k'}{ik} \\
	Ce^{k'L} + De^{k'L} &= Fe^{ikL} \\
	Ce^{k'L} - De^{k'L} &= Fe^{ikL}\frac{ik}{k'}
\end{align*}

We will not go through the gory details here of solving this system of equations as it involves messy algebra and nothing more of substance, but feel free to consult Appendix~\ref{sec:tunnel-deriv} for all the steps. After combining these equations and eliminating variables appropriately, we obtain the following equation relating $F$ to $A$:
\begin{equation}
	4Aikk'e^{-ikL} = F \left[(k'+ik)^2e^{-k'L} - (k'-ik)^2e^{k'L}\right] \label{eq:tunnel-fa}
\end{equation}

In the limit where $k'L \gg 1$ (i.e. the barrier width is much larger than the effective wavelength of the particle inside the barrier), one can simplify Equation~\ref{eq:tunnel-fa} to obtain
\begin{equation}
	T = \abs{\frac{F}{A}}^2 = \frac{16k^2k^{\prime 2}e^{-2k'L}}{(k^{\prime 2}+k^2)^2} \label{eq:tunnel-prob}
\end{equation}

Since this expression for $T$ is dominated by the exponential term, it asymptotically approaches the very simple equation
\begin{tcolorbox}[title = Tunneling probability approximation when $E < V_0$] \vspace{-2ex}
	\begin{equation}
		T \approx e^{-2k'L} \label{eq:tunnel-approx}
	\end{equation}
\end{tcolorbox}

The above equation is a very useful approximate solution for the tunneling probability that is often applied in practice. For completeness, we will also give the exact solution for the tunneling probability below.
\begin{tcolorbox}[title = Tunneling probability exact solution when $E < V_0$] \vspace{-2ex}
	\begin{equation}
		T = \abs{\frac{F}{A}}^2 = \frac{1}{1 + \left(\frac{k^2+k^{\prime 2}}{2kk'}\right)^2 \sinh^2(k'L)} \label{eq:tunnel-prob-full}
	\end{equation}
\end{tcolorbox}

The exact solution has a maximum value of 1 when $k'L=0$ and rapidly decreases to 0 as $k'L$ increases. However, $T$ is still non-zero, which means there is a chance for the particle to tunnel through the barrier and emerge on the other side! This is shown schematically in Figure~\ref{fig:tunnel-wave}.\footnote{What we've plotted is the actual wave function amplitude. How would the probability density differ?} As discussed in class, for $L \approx \SI{1}{\nano\meter}$ and for typical electron-volt-scale potential barriers, the transmission probability is close to 1, indicating how important these kinds of effects become at the nanoscale. \par

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.48\linewidth]{tunnel-wave}
	\caption{An illustration of the amplitude of the wave function in the three regions. It exponentially decays through the barrier but emerges from the right side with a lower amplitude and the same frequency (energy) as the wave function in the left region. A non-zero value of the wave function on the right indicates that the particle may exist past the barrier.}
	\label{fig:tunnel-wave}
\end{figure}

Furthermore, note how the wave function in region III differs from the wave function in region I. The amplitude is much smaller, which makes sense as there is a much lower probability that we will find the particle on the right side of the barrier. However, should tunneling occur, we can apply the conservation of energy to see that the particle has the same energy as before, and hence the frequency of the wave function is the same. It's easy to get probability and energy mixed up, but make sure you can draw the distinction.\footnote{An analogy to classical mechanics: A ball with a certain amount of kinetic energy that rolls up and over a hill will have the same amount of kinetic energy when it arrives back to ground level (assuming no friction).} \par

Though we did not derive it here, it is interesting to look at the transmission probability for the case when $E > V_0$, which is given by Equation~\ref{eq:tunnel-prob-res} and plotted in Figure~\ref{fig:tunnel-prob} as a function of energy.
\begin{equation}
	\boxed{T = \frac{1}{1 + \left(\frac{k^2-k_2^2}{2kk_2}\right)^2 \sin^2(k_2L)}} \label{eq:tunnel-prob-res}
\end{equation}

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.35\linewidth]{tunnel-prob}
	\caption{Tunneling probability as a function of energy for the finite potential barrier when $E > V_0$. At characteristic energies, the tunneling probability reaches 1 due to resonance of the wave function in the region of the barrier.}
	\label{fig:tunnel-prob}
\end{figure}

In this case, the wave function displays sinusoidal behavior in region II instead of exponential behavior, and we have made the appropriate substitution as $k_2=ik'$. It may come as no surprise that if the energy of the particle was high enough, then the transmission probability approaches 1 (i.e. it's as if the barrier wasn't even there). However, as seen in Figure~\ref{fig:tunnel-prob}, total transmission is also achievable at lower values of $E$, which correspond to tunneling \textbf{resonance} when the wavelength of the particle coincides with the width of the barrier. This leads to a constructive interference effect analogous to what we observed in the double-slit experiment.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section[Application: Devices]{Application: Optical and electronic devices}
Once the theory behind quantum tunneling was formally developed, scientists suddenly realized just how ubiquitous the phenomenon was. It turns out quantum tunneling is necessary to explain the radioactive decay of nuclei, the fusion reactions inside stars, and even the mechanisms behind photosynthesis! Furthermore, scientists have leveraged quantum tunneling to create many new nanoscale devices, two of which will be discussed here.

\subsection{Quantum cascade laser}
The first device we will survey here, the \textbf{quantum cascade laser} (QCL), actually combines quantum tunneling with the quantum wells that we analyzed in the previous chapter. First demonstrated in 1994 using a mixture of GaInAs/AlInAs, QCLs are semi-conductor lasers comprised of a stack of quantum well heterostructures (Figure~\ref{fig:qcl}).

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.55\linewidth]{qcl}
	\caption{Schematic of the quantum cascade laser, where periodic layers of varying thicknesses confine electrons into different energy levels. A population inversion causes electron transitions that lead to photon emission and subsequent tunneling of the electron to the next period in the structure. Reproduced from J. Faist et al. \href{http://science.sciencemag.org/content/264/5158/553}{\emph{Science}} \textbf{264}, 5158 (1994).}
	\label{fig:qcl}
\end{figure}

Instead of having a bulk structure, QCLs use a periodic series of layers of different materials and thicknesses to achieve a superlattice. This superlattice is capable of confining electrons into different discrete sub-bands across all of the quantum wells. When a \textbf{population inversion} is achieved, where more electrons are in an excited state than in the ground state, electrons will drop in energy and emit photons, which is how all lasers operate in general. What makes the QCL unique, in addition to its highly tunable layers (e.g. grown with molecular beam epitaxy), is that once an electron has transitioned to a lower energy level in one period of the structure, it can then tunnel to the next period in the heterostructure and undergo more transitions, thereby releasing more photons in a \emph{cascading} effect. The ability for electrons to tunnel through barriers and emit multiple photons leads to higher power output for QCLs than normal lasers, which could lead to advances in spectroscopy, sensing, and signaling.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Scanning tunneling microscope}
Our second nanoscale device, the \textbf{scanning tunneling microscope} (STM), is a more popular tool that you might have previously heard about. It was developed in 1981 by Gerd Binnig and Heinrich Rohrer at IBM, and a schematic detailing the principles of operation is shown in Figure~\ref{fig:stm}.

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.5\linewidth]{stm}
	\caption{Schematic for how the scanning tunneling microscope operates. As the tip moves across the surface, the control unit (CU) applies an appropriate voltage $V_p$ to a piezoelectric arm $P_z$ and modulates its height such that the same tunneling current $J_T$ is maintained. As a result, the tip moves up and down to trace a material's topography, which is imaged at angstrom-scale resolution. Reproduced from G. Binnig et al. \href{https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.49.57}{\emph{Phys. Rev. Lett.}} \textbf{49}, 57 (1982).}
	\label{fig:stm}
\end{figure}

In order to detect small changes on a material's surface, the STM leverages \textbf{piezoelectricity} to control the three arms that guide a sharp tip that sits less than \SI{1}{\nano\meter} away from the surface.\footnote{For those unfamiliar with piezoelectricity, it is briefly explained by \href{http://hyperphysics.phy-astr.gsu.edu/hbase/Solids/piezo.html}{Rod Nave} at Georgia State University.} As $P_x$ and $P_y$ raster scan the tip across the surface, a control unit applies a small voltage ($\sim\SI{10}{\milli\volt}$) to cause electrons to tunnel between the tip and the surface. It measures the tunneling current $J_T$ that's produced and adjusts the height of the third arm $P_z$ to maintain the same current. The piezoelectric arms are so sensitive to changes in $J_T$ that the STM is capable of imaging the surface topography with a resolution of \SI{1}{\angstrom} (\SI{e-10}{\meter}) or less! Not only are scientists able to image \emph{individual atoms} with this technique, but they have been able to manipulate the atoms as well, forming unique nanostructures like the quantum corral in Figure~\ref{fig:corral} and even a stop-motion animated short film by the folks at IBM Research.\footnote{A video of ``A Boy and His Atom'' and more information can be found on the \href{http://www.research.ibm.com/articles/madewithatoms.shtml\#fbid=9xTkVKSpT3k}{IBM Research website}.}

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.4\linewidth]{corral}
	\caption{A quantum corral constructed by arranging 48 Fe atoms in a ring on Cu(111) surface using a STM. Reproduced from M. F. Crommie et al. \href{http://science.sciencemag.org/content/262/5131/218}{\emph{Science}} \textbf{262}, 5131 (1993).}
	\label{fig:corral}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\subsection{Resonant tunneling diode}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Summary}
To recap, in this chapter we analyzed the peculiar phenomenon of quantum tunneling, whereby a particle incident on a finite potential barrier has a non-zero probability of penetrating the barrier and emerging on the other side. This holds true even if the particle carried less energy than the barrier's potential, which is prohibited by the laws of classical mechanics. In that case, we argued that the wave function of the particle exhibits exponential behavior inside the barrier and it must maintain continuity with the wave function on the outside. This allowed us to derive the transmission probability as a function of the particle's wave vector and the barrier width, and we typically use Equation~\ref{eq:tunnel-approx} to approximate this probability. Tunneling is a vital mechanism for many natural processes and scientists have leveraged its power to create novel nanoscale devices like the quantum cascade laser and scanning tunneling microscope. In the next chapter, we will solve the \Sch\ equation for yet another model, one that consists of periodic potential barriers that are infinitely tall and infinitely narrow.

%} % for doublespacing
%\end{document}


\documentclass[12pt]{article}
\usepackage{verbatim}
\usepackage{amssymb,amsmath}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{epsfig}
\begin{comment}
\setlength{\textwidth}{6.50in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textheight}{8.5in}
\setlength{\topmargin}{-.25in}
\end{comment}
%\newtheorem{theorem}{Theorem}
\newtheorem{corollary}{Corollary}[section]
\newtheorem{lemma}[corollary]{Lemma}
\newtheorem{proposition}[corollary]{Proposition}
\newtheorem{theorem}[corollary]{Theorem}
%\newtheorem{conjecture}[corollary]{Conjecture}
\newcommand{\Prob} {{\bf P}}
\newcommand{\Z}{{\mathbb Z}}
\newcommand{\E}{{\bf E}}
\newcommand{\Es}{{\rm Es}}
\newcommand{\Cp}{{\rm cap}}
\newcommand{\R}{{\mathbb{R}}}
\newcommand{\C}{{\mathbb C}}
\newcommand{\rad}{{\rm rad}}
\newcommand{\hdim}{{\rm dim}_h}
\newcommand{\bound}{\partial_O}
\newcommand{\dist}{{\rm dist}}
\newcommand{\x}{{\bf x}}
\newcommand{\y}{{\bf y}}
\newcommand{\sphere}{{\cal S}}
\newcommand{\Var}{{\rm Var}}
\newcommand{\Q}{{\bf Q}}
\newcommand{\add}{+}
\newcommand{\bdim}{{\rm dim}_b}
\newcommand{\lf}{\lfloor}
\newcommand{\rf}{\rfloor}

\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\bea}{\begin{eqnarray}}
\newcommand{\eea}{\end{eqnarray}}
\newcommand{\beann}{\begin{eqnarray*}}
\newcommand{\eeann}{\end{eqnarray*}}

\def\reff#1{(\ref{#1})}

\def \dHalf  {{\cal H}}
\def \dhulls {{\cal A}}
\def \dhcap {{\rm dhcap}}
\def \Im {{\rm Im}}
\def \Re {{\rm Re}}
\def \p {\partial}
\def \edges {{\cal E}}
\def \Half {{\mathbb H}}
\def \reals {{\mathbb R}}
\def \interior {\rm int}
\def \LEW{LEW}
\def \sledim{\hat d}
\def \Disk {{\mathbb D}}
\def \Square {{\cal S}}
\def \diam {{\rm diam}}
\def \state {{\cal X}}
\def \eset {\emptyset}

\def \saws {{\cal R}}
\def \paths {{\cal K}}
\def \loop {{\cal L}}
\def \um  {{\bar m}}
\def \soup {{\cal C}}
\def \tree {{\cal T}}
\def \sgn {{\rm sgn}}
\def \looper {{\cal J}}
\def \strip{{\cal S}}

\def \scaleexp{{\rho}}

\def \energy {{\cal E}}
\def \hcap {{\rm hcap}}
\def \rect{{\cal R}}
\def \distsub {{\Upsilon}}
\def \distsubtwo {{\Theta}}

\def \dyad {{\cal D}}

\def \F {{\mathcal F}}

\def \walk {{\mathcal W}}
\def \hwalk {{\mathcal H}}
\def \bridge{{\mathcal B}}
\def \ibridge{{\mathcal I}}

\def \acat {{\oplus}}
\def \bcat {{\otimes}}

\def \Define {\noindent {\bf Definition.} }
\def  \Pf  {\noindent {\bf Proof.} }
\def \freemass {{\cal C}}
\newenvironment{remark}[1][Remark]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}
\newenvironment{definition}[1][Definition]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}
\newenvironment{example}[1][Example]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}
\newenvironment{conjecture}[1][Conjecture]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}

\begin{document}

\section{SLE partition functions}

For a domain $D$ and points $z,w$ on its boundary, we let
$H_D(z,w)$ denote the total mass of the SAW's in $D$ from $z$ to $w$.
This is a rather vague definition. Our goal is to argue that it
should be conformally covariant in the following sense. If $\Phi$ is
a conformal map on $D$, then
\bea
H_D (z,w)  =
[\Phi_A^\prime(z) \Phi_A^\prime(w)]^{5/8}
H_{\Phi(D)}(\Phi(z),\Phi(w))
\label{eqzz}
\eea
This formula should not be taken too literally. In particular, there
are lattice effects to be taken into account.

Let
\beann
Z(0,n) = \sum_{\omega:0 \rightarrow n, \omega \subset \Half} \beta^{|\omega|}
\eeann
The sum is over all self-avoiding walks that start at $(0,0)$, end
at $(n,0)$  and stay in the upper half plane except for their endpoints.
We assume that there is an exponent $\scaleexp$ such that
the limit
\beann
c=\lim_{n \rightarrow \infty} Z(0,n) n^\rho
\eeann
exists. Now let $x >0$. Let $[nx]$ denote the integer closest to $x$.
We have
\beann
\lim_{n \rightarrow \infty} Z(0,[nx]) n^\scaleexp =
\lim_{n \rightarrow \infty}  Z(0,[nx]) [nx]^\scaleexp
{n^\scaleexp \over [nx]^\scaleexp}
= {c \over x^\scaleexp}
\eeann
This shows that
\bea
H_\Half(0,x) = \frac{1}{x^\scaleexp} \, H_\Half(0,1)
\label{scaling}
\eea

Let $A \subset \Half$ be such that $\Half \setminus A$ is a simply connected
domain which is bounded away from $0$. The scaling limit of the
self-avoiding walk is conjectured to be $SLE_{8/3}$, a
chordal restriction measure.
The probability that a SAW $\gamma$ in $\Half$ from $0$ to $\infty$ does not
enter $A$ is given by
\beann
P(\gamma \cap A = \emptyset) = [\phi_A^\prime(0)]^{5/8}
\eeann
where $\phi_A$ is a conformal map from $\Half \setminus A$ to $\Half$
which sends $\infty$ to itself and has derivative $1$ at $\infty$.
(This means $\phi(z) \asymp z$ as $z \rightarrow \infty$.)
If we assume that the scaling limit of the probability mesasure
for the SAW between two points is conformally invariant,
then we can find the probability that a SAW in $\Half$ from $0$ to a
real $x$ hits $A$. Denote this probability by
$P_{\Half,0,x}(\gamma \cap A =\emptyset)$.
Let $\psi_x$ be a Moibius transformation of
$\Half$ to itself which fixes $0$ and sends $x$ to $\infty$.
Then
\beann
P_{\Half,0,x}(\gamma \cap A =\emptyset)=  [\phi_{\psi_x(A)}^\prime(0)]^{5/8}
\eeann
We can express this probability as a ratio of partition functions
\beann
P_{\Half,0,x}(\gamma \cap A =\emptyset) = {H_{\Half \setminus A} (0,x)
\over H_\Half(0,x) }
\eeann
So we have the following transformation result for partition functions
\beann
H_{\Half \setminus A} (0,x)  = [\phi_{\psi_x(A)}^\prime(0)]^{5/8} H_\Half(0,x)
\eeann

Recall that $\phi_{\psi_x(A)}$ is a conformal map that takes
$\Half \setminus \psi_x(A)$ to $\Half$ and fixes $0$ and $\infty$.
Letting
\beann
\phi_{x,A}= \psi_x^{-1} \circ \phi_{\psi_x(A)} \circ \psi_x,
\eeann
$\phi_{x,A}$ maps $\Half \setminus A$ to $\Half$ and fixes $0$
and $x$.
Since $\psi_x(0)=0$, $\phi_{\psi_x(A)}(0)=0$, and
$(\psi_x^{-1})^\prime(0)=1/\psi_x^\prime(0)$,
we have that
\beann
\phi_{x,A}^\prime(0) = \phi_{\psi(A)}^\prime(0)
\eeann
So we have
\bea
H_{\Half \setminus A} (0,x)  = [\phi_{x,A}^\prime(0)]^{5/8} H_\Half(0,x)
\label{trans}
\eea
Of course there is more that one conformal map of $\Half \setminus A$
to $\Half$ that fixes $0$ and $x$. The particular map we defined has
$\phi_{x,A}^\prime(x)=1$.
A general conformal map $\Phi_{x,A}$ of $\Half \setminus A$
to $\Half$ that fixes $0$ and $x$ can be written as
$\Phi_{x,A} = \chi \circ \phi_{x,A}$
where $\chi$ is a Moibius transformation of $\Half$ to itself that
fixes $0$ and $x$.
Such transformations are given by
\beann
\chi(z) = {z \over c(z-x)+1}
\eeann
where $c \in \reals$.
A short computation shows $\chi^\prime(0) \chi^\prime(x)=1$.
Since
\beann
\Phi_{x,A}^\prime(0) &=& \chi^\prime(0) \phi_{x,A}^\prime(0) \\
\Phi_{x,A}^\prime(x) &=& \chi^\prime(x) \phi_{x,A}^\prime(x) = \chi^\prime(x)
\eeann
we find
\beann
\phi_{x,A}^\prime(0) = \Phi_{x,A}^\prime(0)  \Phi_{x,A}^\prime(x)
\eeann
So \reff{trans} can be written as
\bea
H_{\Half \setminus A} (0,x)  =
[\Phi_{x,A}^\prime(0) \Phi_{x,A}^\prime(x)]^{5/8} H_\Half(0,x)
\label{eqaa}
\eea
where $\Phi_{x,A}$ is any conformal map of $\Half \setminus A$ to $\Half$
which fixes $0$ and $x$.

Now let $\Phi_A$ be a conformal map that maps $\Half \setminus A$
onto $\Half$ and fixes $0$ but not $x$. Let $\lambda=\Phi_A(x)/x$.
Then $\Phi_{x,A} = \lambda^{-1} \Phi_A$ fixes $x$, and so \reff{eqaa}
implies
\beann
H_{\Half \setminus A} (0,x)  =
[\Phi_A^\prime(0) \Phi_A^\prime(x)]^{5/8} \lambda^{-5/4} H_\Half(0,x)
\label{eqbb}
\eeann
Using \reff{scaling} we can rewrite this as
\bea
H_{\Half \setminus A} (0,x)  =
[\Phi_A^\prime(0) \Phi_A^\prime(x)]^{5/8} \lambda^{\rho-5/4}
H_\Half(0,\Phi_A(x))
\eea

Until now we have been assuming the conformal map $\Phi_A$ fixes $0$.
By adding a real constant to the map we can relax this constraint.
Since $H_\Half(x,y) = H_\Half(y-x)$, we conclude that for any
conformal map of $\Half \setminus A$ to $\Half$,
\bea
H_{\Half \setminus A} (0,x)  =
[\Phi_A^\prime(0) \Phi_A^\prime(x)]^{5/8} \lambda^{\rho-5/4}
H_\Half(\Phi_A(0),\Phi_A(x))
\eea
If $\rho=5/4$ this estabilishes \reff{eqzz} for a limited
class of domains.

\end{document}
\end


\documentclass{article}

\usepackage{amsmath,amssymb,amsthm,graphicx}
\usepackage{fullpage}
\usepackage{caption,subcaption}
\newtheorem{prob}{Problem}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}{Lemma}[section]

\newenvironment{problem}
{
\renewcommand{\labelenumi}{(\alph{enumi})}
\renewcommand{\labelenumii}{\roman{enumii}.}
\begin{prob}
}
{
\end{prob}
 }
 \newcommand{\N}{\mathbb{N}}
 \newcommand{\Z}{\mathbb{Z}}
 \newcommand{\Q}{\mathbb{Q}}
 \newcommand{\R}{\mathbb{R}}
 \newcommand{\C}{\mathbb{C}}
 \newcommand{\bM}{\begin{bmatrix}}
 \newcommand{\eM}{\end{bmatrix}}
 \newcommand{\Ni}{\N\cup\{\infty\}}
  \newcommand{\rmD}[1]{\mathrm{d}#1}
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\title{Optimal Path Planning for Robotic Arms in Household Assistance}
\author{Vikram Sunder and Zachary Greenberg}

\begin{document}

\maketitle

\section{Abstract}
We investigated and implemented motion planning algorithms for a robotic arm. The algorithm computes a shortest path in a given metric space.  To find the optimal path, we considered various metrics on a graph in Configuration Space. For example we considered the Euclidean distance in work space, energy needed to transition between states, and the energy required to hold an object in the gripper. Currently we have a small mobile robot with a five degrees of freedom arm. The overall objective is for the robot to help with small household tasks. We simulated the motion of the arm in Matlab to evaluate the various paths.

\section{Introduction}
One of the main problems with bringing robots into the home is the cost of hight precision parts. Currently the cost of quality robotic arms is far beyond the price range of the average household. While they might be more accessible in the future, we want to experiment with a lower cost robotic arm currently available. The trade off here is decreased power and precession. Our robot has a small 5 degree of freedom arm, that we hope to use to pick up plates/cups or open doors. However, to maximize the set of objects we can manipulate and the duration of time that we can carry out interactions we need minimize the length of individual interactions and the energy required to carry them out.  The longer our arm takes to manipulate an object the more power it will consume from its highly limited power source.  Likewise the more torque required from the servos will translate to increased current draw and minimize the weight of the objects we can manipulate.  As such we must optimize our trajectories to meet these goals.

\section{The Robot}
Our robot uses Willow Garage's Turtlebot as a base. On top of this we have attached a PhantomX Reactor robotic arm. This arm has 5 degrees of freedom. It can rotate about the base to select a particular plane of motion.  There are then three degrees of freedom in this particular plane.  The last degree of freedom is a wrist joint. \ref{fig:DHparam} for the Denavit-Hartenberg parameters for the robot. \\
\begin{figure}[hb]
\centering
\begin{tabular}{|r|c|c|c|c|}
\hline
&$\alpha$ & a & d & $\theta$\\
\hline
\hline
1& $\frac{\pi}{2}$ & 0 & 26.5 & $\theta_0$\\
\hline
2& 0 & 150 & 0 & $\frac{\pi}{2}+\theta_1$\\
\hline
3& 0 & 150 & 0 & $\theta_2$\\
\hline
4& $\frac{\pi}{2}$& 0 & 0 & $\frac{\pi}{2}+\theta_3$\\
\hline
5& 0& 0& 116.525 & $\theta_4$\\
\hline
\end{tabular}
\caption{Denavit Hartenberg Paramaters}
\label{fig:DHparam}
\end{figure} \\
For path planning there are a few convenient things about the forward kinematics for this robot. The first is that the final angle, $\theta_4$ doesn't affect the final position of the robot. Therefore when searching configuration space, any algorithm doesn't need to search changes in $\theta_4$. Similarly $\theta_0$ only affects what plane the robot needs to be in. In a configuration space only constrained by angle limits, it is a simple computation to figure out what plane the goal point is in. Then $\theta_0$ can be calculated to point the arm in that plane. Thus we only have to search the space defined by $\theta_1,\theta_2,\theta_3$. This cuts the search space from a five dimensional space to a three dimensional space. It also solves one workspace variable, which leaves only $x,z,\alpha$ where $\alpha$ is the pitch of the end effector.
\section{Path Planning}
Instead of computing the inverse kinematics for the arm, we decided to search the configuration space using a shortest path algorithm and use the forward kinematics to stop. From above we saw that only 3 angles had to be searched over. From there we discretized the space by placing nodes at even intervals along all three dimensions. Let a tick be the length of the intervals in radians. Then we connected any two nodes that differed by one tick in some coordinate. This created a graph to search over. Currently we are using a tick size of $\frac{\pi}{100}$ radians, but eventually a tick will correspond to the precision of the servos. This tick size gave us enough precision to find smooth paths, without making the graph too big.\\
We considered several search algorithms for the graph and eventually settled on $A^*$. We looked into $D*$, but since we chose not to consider any moving obstacles, we did not have to recompute the paths as we searched. This meant that the added complexity of $D*$ wasn't helpful. $D*$ also isn't any faster then $A*$ to find the initial path. The other search algorithm we considered was RRT. RRT is fast, but does guarantee an optimal path. The advantage of RRT is its ability to search high-dimensional non-convex spaces.  Given the number of degrees of freedom available to us this is not advantageous or necessary. Once we optimized our $A^*$ code for speed, it was fast enough to search the entire space. This combined with the simplicity of $A^*$ made it the best choice.\\
The next thing that we needed to do was find a consistent and admissible heuristic. The simplest metric we could use was the Euclidean distance in the work space between the nodes. The standard heuristic for this space is the euclidean distance to the target. When we changed the metric, we wanted an easy way to determine a heuristic. So for every new metric we added the cost to the euclidean distance. Let $c(v,w)$ be the added cost function. Let $d(v,w)$ be the euclidean distance between the nodes. So the weight of an edge $(u,v)$ is $c(v,w)+d(v,w)$. Then let $h(v)$ be the distance from $v$ to the target, $g$. So for an edge $(u,v)$ we have:
\begin{align*}
h(v) =& d(v,g)\\
\leq& d(v,w) + d(w,g) & \text{[triangle inequality]}\\
\leq& d(v,w) + c(v,w) + d(w,g) & [c(v,w)>0]\\
\leq& w(\{u,v\}) +h(g)
\end{align*}
So as long as our additional cost is always positive, the standard distance heuristic is always consistent.

\section{Metrics }
\subsection{Euclidean Work Space}
The first metric we tried was looking at the euclidean distance in the work space. So if $FK$ was the function that maps angles to work space, the cost between nodes $u$ and $v$ is $d_{e}(FK(u),FK(v))$. In path planning in addition to specifying a position we might want to specify an orientation of the arm. For this arm the orientation we care about is the sum of $\theta_1,\theta_2,\theta_3$, which we will call $\alpha$.
So we will add to the distance between nodes $|\Delta\alpha|$ and add $|\alpha- \alpha_{target}|$ to the heuristic. Everything is still consistent so $A^*$ still works. This gives two different metrics, the basic distance, which considers orientation, and the no-alpha metric, which does not. See Figure {fig:basicPaths} to see the difference in the paths found for each metric.\\
\begin{figure}[htb]
\centering
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[scale=.5]{PathPics/Basic_Path.jpg}
\caption{Orientation Path}
\end{subfigure}%
~
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[scale=.5]{PathPics/NoAlpha_Path.jpg}
\caption{No Orientation Path}
\end{subfigure}

\caption{Euclidean Distance Paths}
\label{fig:basicPaths}
\end{figure}\\

\subsection{State Maintenance Method 1}

	From here, so long as we add positive weights to the existing costs of edges in our graph our heuristic will remain consistent and admissible.  Power is a major constraint for our robot. The amount of force the arm can output is highly limited, as is the amount of power available to the robot to carry out its various actions and functions.  As such we want to make all our motions as energy efficient as possible.  To do this we used the fact that the torque output of every actuator correlates to its energy consumption.  Therefore we will attempt to minimize the sum of the total torque of each state on a given path.  Let J be the Jacobian of the forward kinematic map of our robotic arm.  The Jacobian transpose will provide us with a linear map from forces applied by the arm to the output torques of the individual joints.  Thus we can calculate the torques required to generate a force at the end effector to counteract the force of gravity on object we are manipulating.   The cost of the edge between two configuration states $u$ and $v$ is as follows:

\[	d(u,v) =  \left\|(FK(u)-FK(v))\right\|^2 + \left\|J^T_v\bM 0 \\ 0 \\ mg \eM\right\|^2\]
Figure \ref{fig:EnergyPaths1} shows the different paths obtained by considering this energy.
\begin{figure}[htb]
\centering
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[scale=.3]{PathPics/Wrench_Path.jpg}
\caption{Orientation Path}
\end{subfigure}%
~
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[scale=.3]{PathPics/Wrench_NoAlpha_Path.jpg}
\caption{No Orientation Path}
\end{subfigure}

\caption{Paths When Considering Energy To Hold Up an Object}
\label{fig:EnergyPaths1}
\end{figure}\\
\subsection{State Maintenance Method 2}

	One of the issues with this is that it does not take into account the transition from one state to the next.  To do this we use the ratio of the torque at the new state to the torque at the old state.  This causes a transition to a state that requires more torque to have a higher cost and a transition to a lower state have lower cost.  Our cost function here where u is the initial state and v is the next state is as follows:

	 \[	d(u,v) =  \left\|(FK(u)-FK(v))\right\|^2 + \frac{\left\|J^T_v\bM 0 \\ 0 \\ mg \eM\right\|^2}{\left\|J^T_u\bM 0 \\ 0 \\ mg \eM\right\|^2}\]

Figure \ref{fig:EnergyPaths2} compares taking the ratio of these torques to the original formulation, of looking at the torque needed to hold the next state. You can see that the ratio of torques resulted in a shorter, smoother path to the goal.\\
\begin{figure}[htb]
\centering
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[scale=.3]{PathPics/Wrench_Path.jpg}
\caption{Path When Only Considering The Next State}
\end{subfigure}%
~
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[scale=.3]{PathPics/Wrench_Ratio_Path.jpg}
\caption{Path for Ratio of Torques}
\end{subfigure}

\caption{Paths When Considering Energy To Hold Up an Object}
\label{fig:EnergyPaths2}
\end{figure}\\

\subsection{Kinetic Energy}
The final set of metrics we considered take into account the kinetic energy needed to move from state to state. To simplify this problem we assume the arm is massless except for a mass in the end effector. \\
Once again the Jacobian $J$, comes in handy as a map from from angular velocity to linear velocity. Let $\dot{\theta}$ be the difference in angles between the states $(u,v)$ divided by a time-step $h$. Then the linear kinetic energy of the object the arm is carrying is:
 \[E_{linear} = \frac{1}{2}m \|J_u\dot{\theta}\|^2\]. \\
For angular kinetic energy we approximated the actual value. We assumed that each angle contributes equally to angular kinetic energy and rotates about the same point. This is a significant simplification, but it saves on computation time and complexity. In order to calculate the actual rotation energy, we would need to compute the locations of each joint, not just the end effector in the search, which is at least three times slower. With this approximation our angular kinetic energy is:
\[E_{angular} = \frac{1}{2}m \left\|\frac{\Delta\theta_1+\Delta\theta_2+\Delta\theta_3)}{h}\right\|^2\]
The final metric we considered was the total kinetic energy between points, which is the sum of the previous two metrics.\\
Figure \ref{fig:KineticPaths} shows the paths for these three metrics. In this path you can see that the total kinetic energy path is identical to the linear kinetic energy path. This is most likely due to the level of approximation in our angular kinetic energy calculations.\\
\begin{figure}[htb]
\centering
\begin{subfigure}[b]{0.33\textwidth}
\centering
\includegraphics[scale=.33]{PathPics/Energy_Linear_Path.jpg}
\caption{Linear Kinetic Engery Path}
\end{subfigure}%
~
\begin{subfigure}[b]{0.33\textwidth}
\centering
\includegraphics[scale=.33]{PathPics/Energy_Angular_Path.jpg}
\caption{Angular Kinetic Energy Path}
\end{subfigure}%
~
\begin{subfigure}[b]{0.33\textwidth}
\centering
\includegraphics[scale=.33]{PathPics/Energy_Kinetic_Path.jpg}
\caption{Total Kinetic Energy Path}
\end{subfigure}

\caption{Paths When Considering Kinetic Energy}
\label{fig:EnergyPaths2}
\end{figure}\\
\section{Conclusion}
Overall the smoothest path we found was paths that accounted for the total/linear kinetic energy. In this case we specified position and orientation and still found a smooth path to the goal. Using just Euclidean distance if we don't care about orientation the algorithm finds a smooth path. But when trying to find a path with orientation as well it moves into position and then struggles to change orientation without moving too far away from the target. By considering the energies the algorithm can find paths that change orientation on the way even if takes more distance. \\
In practice specifying orientation of the arm will be important. In order to pick up objects, the arm must grip them in a specific way and then place them the right way up. If the path planning can't specify an angle it won't be able to do this. Therefore either including kinetic energy or the cost of holding a position is important for getting smooth paths. Which of these two methods to use depends on the weight of the object you are holding and how long it will stay at each state.
\section{Future Work}
In the future there are several other metrics to consider. One thing to do is to remove the approximations on angular kinetic energy to get a better optimization. Another metric to consider would be the combination of the Kinetic Energy with the cost to hold up an object. These two costs would need to be scaled appropriately to determine which was more important. In general we can continue to work on developing a more accurate physics model of the arm to accurately determine the cost of individual motions. \\
Another way to improve the smoothness of the paths found is to make the tick size smaller. This increases the size of the grid, but lets the robot check more paths. Unfortunately the complexity of the problem is cubic in the grid size, so even small increases in size quickly become too expensive to compute. This is especially true, if the cost of each edge is doing a lot of complicated forward kinematics for each joint and energy calculations.\\
Outside of our simulation, the robot will need to navigate in a house to perform tasks. So it will need to plan a path to move before, it plans a path for the arm. Choosing where to move is another optimization problem in and of itself. There is a shell in the workspace of the arm, where it is more maneuverable and can reach more orientations. Ideally a complete path planning algorithm would move the robot so that the target is in that zone. Alternatively we can relax the constraints on arm position and use the addition of the mobile base to enhance possible manipulations\\
Finally this algorithm needs to move beyond simulation and on to an actual robot. In practice it might be apparent that certain paths are less prone to drifting and accumulating error. This would provided a decisive case for one metric over the other.
\end{document}


\paragraph{}
The several algorithms that are being used warrant having some high-level pseudocode written in order to better explain their operation in the programming, those that where omitted are either highly implementation specific or basic equations.

\setcounter{algorithm}{1}

\begin{algorithm}[H]
\caption{Calculation of All Forces}
\begin{algorithmic}[1]
\FOR{$Body_A \leftarrow 1$ \TO Bodies}
  \FOR{$Body_B \leftarrow (Body_A-1)$ \TO Bodies}
    \IF{$Body_B \neq Body_A$}
      \STATE $Matrix_F[Body_A][Body_B] \leftarrow calculateForce(Body_A, Body_B, x)$
      \STATE $Matrix_F[Body_B][Body_A] \leftarrow calculateForce(Body_A, Body_B, y)$
    \ENDIF
  \ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[H]
\caption{Summation of All Forces}
\begin{algorithmic}[1]
\FOR{$Body_A \leftarrow 1$ \TO Bodies}
  \FOR{$Body_B \leftarrow 1$ \TO Bodies}
    \IF{$Body_B \neq Body_A$}
      \IF{$Body_B < BodyA$}
        \STATE $Body[xAccess]_F~-\leftarrow Matrix_F[Body_B][Body_A]$
        \STATE $Body[yAccess]_F~+\leftarrow Matrix_F[Body_A][Body_B]$
      \ELSE
        \STATE $Body[xAccess]_F~+\leftarrow Matrix_F[Body_A][Body_B]$
        \STATE $Body[yAccess]_F~-\leftarrow Matrix_F[Body_B][Body_A]$
      \ENDIF
    \ENDIF
  \ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[H]
\caption{Integration of New Position}
\begin{algorithmic}[1]
\IF{$ScenarioChange$}
  \STATE $CalculateForceMatrix()$
  \STATE $SumAllForcesToBodies()$
  \STATE
  \FOR{$bC \leftarrow 1$ \TO $Bodies$}
    \STATE \COMMENT{Acceleration}
    \STATE $Body[bC]_{ax} \leftarrow Body[bC]_{Fx}/Body[bC]_{m})$
    \STATE $Body[bC]_{ay} \leftarrow Body[bC]_{Fy}/Body[bC]_{m})$
  \ENDFOR
  \STATE
  \STATE $ScenarioChange \leftarrow false$
\ENDIF
\STATE
\FOR{$bC \leftarrow 1$ \TO $Bodies$}
  \STATE \COMMENT{Half-Velocity}
  \STATE $Body[bC]_{vx} ~+\leftarrow 0.5(Body[bC]_{ax}\times\Delta{t})$
  \STATE $Body[bC]_{vy} ~+\leftarrow 0.5(Body[bC]_{ay}\times\Delta{t})$
  \STATE \COMMENT{Position}
  \STATE $Body[bC]_{rx} ~+\leftarrow Body[bC]_{vx}\times\Delta{t}$
  \STATE $Body[bC]_{ry} ~+\leftarrow Body[bC]_{vy}\times\Delta{t}$
\ENDFOR
\STATE
\STATE $CheckCollisions()$
\STATE
\STATE $CalculateForceMatrix()$
\STATE $SumAllForcesToBodies()$
\STATE
\FOR{$bC \leftarrow 1$ \TO $Bodies$}
  \STATE \COMMENT{Acceleration}
  \STATE $Body[bC]_{ax} \leftarrow Body[bC]_{Fx}/Body[bC]_{m})$
  \STATE $Body[bC]_{ay} \leftarrow Body[bC]_{Fy}/Body[bC]_{m})$
  \STATE \COMMENT{Half-Velocity}
  \STATE $Body[bC]_{vx} ~+\leftarrow 0.5(Body[bC]_{ax}\times\Delta{t})$
  \STATE $Body[bC]_{vy} ~+\leftarrow 0.5(Body[bC]_{ay}\times\Delta{t})$
\ENDFOR
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[H]
\caption{Collision Detection and Simulation}
\begin{algorithmic}[1]
\FOR{$bC_A \leftarrow 1$ \TO $Bodies$}
  \FOR{$bC_B \leftarrow (bC_A-1)$ \TO $Bodies$}
    \IF{$bC_B \neq bC_A$}
      \STATE $Distance \leftarrow calculateDistance(bC_A, bC_B)$
      \IF{$Distance < (Body[bC_A]_r+Body[bC_B]_r)$)}
        \STATE $p_{x} \leftarrow ((Body[bC_A]_m \cdot Body[bC_A]_{vx}) + (Body[bC_B]_m \cdot Body[bC_B]_{vx}))$
        \STATE $p_{y} \leftarrow ((Body[bC_A]_m \cdot Body[bC_A]_{vy}) + (Body[bC_B]_m \cdot Body[bC_B]_{vy}))$
        \STATE
        \STATE $Body[bC_A]_m \leftarrow (Body[bC_A]_m+Body[bC_B]_m)$
        \STATE $Body[bC_A]_{vx} \leftarrow (p_{x}/Body[bC_A]_m)$
        \STATE $Body[bC_A]_{vy} \leftarrow (p_{y}/Body[bC_A]_m)$
        \STATE
        \STATE $DeleteBody(bC_B)$
      \ENDIF
    \ENDIF
  \ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}