\documentclass[aps,prl,reprint]{revtex4-2}
\usepackage{gensymb}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{dsfont}
\usepackage{relsize}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{hyperref}
\hypersetup{colorlinks=true, citecolor=blue, urlcolor=blue, linkcolor=blue}

\begin{document}

% Use the \preprint command to place your local institutional report
% number in the upper righthand corner of the title page in preprint mode.
% Multiple \preprint commands are allowed.
% Use the 'preprintnumbers' class option to override journal defaults
% to display numbers if necessary
%\preprint{}

%Title of paper
\title{Coupled Electric Oscillators}

% repeat the \author .. \affiliation  etc. as needed
% \email, \thanks, \homepage, \altaffiliation all apply to the current
% author. Explanatory text should go in the []'s, actual e-mail
% address or url should go in the {}'s for \email and \homepage.
% Please use the appropriate macro foreach each type of information

% \affiliation command applies to all authors since the last
% \affiliation command. The \affiliation command should follow the
% other information
% \affiliation can be followed by \email, \homepage, \thanks as well.
\author{Trevor Smith, Alex Storrer}
\email[]{smith.tr@northeastern.edu}
\homepage[]{https://github.com/trevorm4x/}
%\thanks{}
%\altaffiliation{}
\affiliation{Northeastern University}

\date{\today}

\begin{abstract}

	In this lab, the behavior of single, uncoupled, and coupled LRC circuits
	were evaluated with simple relationships between L, R, C, and voltage
	as well as complex differential equations. The latter were simplified
	based on the unique initial conditions of this experiment. This behavior
	was further linked to a mechanical analogue, an elastic pendulum system.
	For two inductors, nearly identical behavior of the single RLC circuit was
	observed.  The oscillating decay of the single LRC
	circuits  were  similar  for  the  two  inductors, where A-inductor
	and B-inductor were calculated to have decay  natural
	frequencies  of  14587.05$\pm$ 0.03  rad/sec and 14578.44$\pm$ 0.03 rad/s, and
	decay constants of 649.9$\pm$ 0.1 s−1 and 651.3$\pm$ 0.1 s−1.  From these
	values the inductances were calculated to be 0.100±0.003 H for both of
	the inductors. When the A and B circuits were coupled via a coupling
	capacitor, each circuit produced a signal with a beat frequency of
	170 $\pm$ 1 Hz. By using equations modeling a coupled LRC circuit, the
	coupling capacitance was calculated to be 4.1 $\pm$ 0.1 nF, which is one
	standard deviation away from its measured value of 3.9 $\pm$ 0.1 nF.

\end{abstract}

\maketitle

% body of paper here - Use proper section commands
% References should be done using the \cite, \ref, and \label commands
\section{Introduction}
% The Introduction should contain 1 or 2 paragraphs.
% Briefly state the physics underlying the experiment
% (what is being tested and why).
In many circuits, a voltage is continuously applied to several components in series
and/or in parallel. Three essential components used in circuits are resistors,
capacitors, and inductors. These components operate in distinct ways, which can be
seen by the way we use inductance, resistance, and capacitance to calculate voltage.

$$ V = L \cdot \frac{d^2q}{dt^2} $$
$$ V = R \cdot \frac{dq}{dt} $$
$$ V = \frac{1}{C} \cdot q $$

Where V is voltage, L is inductance, R is resistance, C is capacitance, and q is
charge. In other words, capacitors do work proportional to amount of charge buildup,
resistors do work proportional to the flow of charge or current, and inductors do
work proporional to the inflection of charge, or the change in current. \\

\begin{figure}[h]
\includegraphics[width=0.4\textwidth]{../Images/l6_LRC.png}
\caption{\label{figA}}
\end{figure}

An LRC circuit, seen in \ref{figA}, combines these three components to produce an
oscillating response
to any change in voltage, for example the turning on and off of an applied voltage.
These oscillations could be compared to the oscillation of a spring-mass system,
and indeed the differential equations describing both the voltage of an LRC circuit
and the height of an oscillating spring/mass system in gravity look identical. \\

This peculiar behavior can be multiplied when two such systems interact with each
other, behaving as what's known as ``coupled oscillators". The beauty of differential
equations is that a mathematical model can be used to describe such motion, even
if the differential equation itself is unsolvable unless there are specific initial
parameters. In an unsolvable case, a computer program can simulate the system one step at a
time using Euler's method, or we can simply perform an experiment and let nature
describe the motion. In this lab, we will be looking at a coupled electric oscillator
that is operating in a normal mode, which is to say a mode where the coupled oscillators
behave like a single harmonic oscillator, or a solvable case.

\section{Apparatus}
% List equipment components (manufacturer, model
% numbers and brief specifications).

The apparatus consisted of the following.
\begin{itemize}
	\item Coupled electronic oscillator circuit box
	\item Fixed capacitor, variable capacitor
	\item Two large inductor coils, labelled 'A' and 'B'
	\item Oscilloscope, Tektronix TDS210
	\item Digital Multimeter, Extech EX430A
	\item OpenChoice Desktop, Oscilloscope Software
	\item Jupyter, Python compiler
	\item LaTeX, document preparation software
\end{itemize}

\section{Measure Inductance}

\subsection{Procedure}
% Briefly describe the experimental procedures (in your own words, but don’t overdo it)
% Discuss calibrations, etc., if required
% Include necessary equations and put them on their own line (number them, e.g. “Eq. (3)”)
% Include plots showing relevant results (label each figure, e.g. “Fig. 3”, with caption).
% Describe what you found (describe what the plot illustrates)
In this phase of the lab we measure the basic parameters of our system, described
in \ref{setup}, including $L_A$, $L_B$, $R_L_A$, $R_L_B$, full decay natural
frequency $\omega_0$, damping constant $\gamma$, time constant $\tau$,
quality factor $Q$ and the standard capacitor. These values, save for the capacitance,
are calculated by analyzing the voltage oscillations of the circuit. Values for $R_L_A$
and $R_L_B$ are also measured with an ohmmeter, and the capacitance is measured
with a digital multimeter (DMM). \\

\begin{figure}[h]
\includegraphics[width=0.4\textwidth]{../Images/l6_setup.png}
\caption{\label{setup} Test setup for the coupled electric oscillators.}
\end{figure}

The standard capacitor and one inductor was installed in the A-circuit. A +5 VDC source
was connected to the box, and the A-output was connected to channel 1 of an oscilloscope.
The trigger out was connected to the scope trigger, and the viewing window of the scope
was adjusted so that the decaying oscillation occurring when the square wave turns off
was visible. \\

The decaying waveform is then stored digitally and analyzed. By using eq.
\ref{LRC Oscillation} to make curve fits, the basic parameters of the system
can be obtained. These fits were first performed on a single period of oscillation,
in order to get an estimate without the added complexity of the exponential. Then,
using the parameters for the sine wave as a starting point, the full equation describing
the oscillation was obtained.

\begin{equation}
q(t) = q_{0}e^{-\gamma t/2}cos(\omega_0t)
\label{LRC Oscillation}
\end{equation}

Where q is charge as a function of time, $\gamma$ is the decay constant,
$\omega_0$ is the natural frequency, and t is time. This equation,
analogous to a mass-spring system being stretched and released,
describes the flow of charge between the capacitor and inductor in
the LRC circuit. When the `spring is released', or the 555 pulse
square wave switches off, the capacitor immediately discharges.
However, the inductor counters such a quick change in current,
and produces a current in the opposite direction, cancelling out a
portion of the flow of charge from the capacitor. This has the
effect of recharging the capacitor again, which, when the flow
of current from the inductor is stopped, will again quickly
discharge. The length of wire between the two circuit components,
in this case, acts as a resistor and steadily gives off energy
in the form of heat as a proportion of the current at a given
moment. The end result is this beautiful equation
describing a sine wave undergoing exponential decay. \\

This analysis was repeated with inductor `B', inserted into the same circuit
and replacing inductor `A'.

\subsection{Results}

First, the capacitance of the standard capacitor was measured. The DMM has an
uncertainty of 3\% associated with all measurements, and yielded a capacitance
measurement of 47 $\pm$ 1 $\mu$F. \\

A single period of the oscillation was recorded and analyzed for each inductor,
shown in \ref{single_period}.

\begin{figure}[h]
\includegraphics[width=0.4\textwidth]{../Images/l6_channel_A_one_period_fit.png}
\includegraphics[width=0.4\textwidth]{../Images/l6_channel_B_one_period_fit.png}
\caption{\label{single_period} Single period of oscillation and fit for A (above) and B
(below). The differing fit parameters for phase and the sign of the frequency is not
relevant in this case. }
\end{figure}

\newpage
The calculated frequencies for A and B, respectively, are 14660 $\pm$ 30 Hz and
14460 $\pm$ 30 Hz, where the uncertainty is calculated from the covariance
matrix of the fit. Using \ref{LCalc} and the values above, $L_A$ and $L_B$ were
calculated to be 0.099 $\pm$ 0.003 H and 0.101 $\pm$ 0.003 H. It should be noted
that the uncertainty in the fit was well below one part in 100, meaning that
nearly all the uncertainty in the inductance measurements were due to the DMM.

\begin{equation}
	L = \frac{1}{C\omega_0^2}
	\label{LCalc}
\end{equation}

These fit parameters were then used as starting parameters for the more complicated
curve fit needed when exponential decay is included, as shown in \ref{full}.

\begin{figure}[h]
\includegraphics[width=0.4\textwidth]{../Images/l6_channel_A_full_fit.png}
\includegraphics[width=0.4\textwidth]{../Images/l6_channel_B_full_fit.png}
\caption{\label{full} Several periods of oscillations, with fit corresponding
to eq. \ref{LRC Oscillation}. }
\end{figure}

While the frequencies for A and B when viewing a longer decay
were extremely precise, 14587.05 $\pm$ 0.03 and 14578.44 $\pm$ 0.03
respectively, the proportion of uncertainty in our calculated
L values were exactly the same, caused again by uncertainty in the
measured capacitance. These values did, however, agree better than
before, at $0.100\ \pm\ 0.003$ H for both inductors. \\

The damping coefficient $\gamma$ was calculated at 650 /s and 651 /s for
A and B respectively. This damping coefficient $\gamma$ is related to the
resistance R, which generates heat proportional to the flow of charge,
and inductance L which generates a flow of charge opposite to the
change in current, by eq. \ref{LRG}. We can then rearrange the equation
so that we can use our calculated inductance and frequency to calculate
resistance.

\begin{equation}
	\gamma = \frac{R}{L} \rightarrow R = L\gamma
	\label{LRG}
\end{equation}

The resistance for inductor `A' and `B' were both found to be
$65\ \pm\ 2\ \Omega$. Again, calculations were precise enough
to be able to distinguish a difference in the damping coefficient
itself for the two inductors, but the error propagated from the
DMM swamps that difference. This difference is small when considering
the discrepancy between these calculated values for resistance and
those measured directly with an ohmmeter. These measured resistances
for inductor `A' and `B' were 17.4 $\pm$ 0.2 $\Omega$ and
17.2 $\pm$ 0.2 $\Omega$ respectively, with the error again coming from
the DMM's 3\% precision. \\

This difference can be attributed to a difference in exactly what
is being measured. In the case of the DMM, the inductors were isolated
from the circuit and measured separately, whereas the decay constant
is caused by all resistance that exists in the circuit, including the
wire and all contacts. Therefore, it is not surprising that the
resistance related to the time constant is much higher. \\

In theory, this resistance does not have any effect on the frequency
of the circuit, as $\omega_0$ is clearly a function of only L and C. \\

Finally, two more important values were derived from values already
discussed, $\tau$, another to describe damping, called the time constant,
and $Q$, or the quality factor, which also describes the decay
in an oscillating system. The equations used for these values are given
by \ref{tau} and \ref{Q} respectively.
All constants in these equations have already been defined in this report.

\begin{equation}
	\tau = \frac{2}{\gamma}
	\label{tau}
\end{equation}

\begin{equation}
	Q = \frac{\omega_0}{\gamma}
	\label{Q}
\end{equation}

The quality factor for circuits containing each inductor `A' and `B' were
again found to be equal after considering uncertainty due to the DMM,
at 22.4 $\pm$ 0.7. This value corresponds to the amount of decay that
occurs over a single period. \\

The time constant for circuits containing each inductor `A' and `B'
calculated with great precision, as they do not depend on any measurements
from the DMM. These values were 0.0030773 $\pm$ 0.0000006 s and
0.0030709 $\pm$ 0.0000006 s respectively. These uncertainties are
$6.0\ \cdot\ 10^{-7}$.

\newpage
\section{Uncoupled LRC Oscillators}

In the following sections, a system with two LRC circuits will be
analyzed, starting with two uncoupled LRC oscillators.
The B inductor and adjustable capacitor were then added to the
B-circuit, and the B-circuit output was connected to channel
2 on the scope. By switching the coupling capacitor
\textbf{\emph{off}}, these two LRC circuits were able to operate
independently, but excited by the same emf pulse.\\

The waveforms were viewed on the oscilloscope, and the B-capacitor
was adjusted until both waveforms had the same period. These
waveforms were stored and are shown in fig. \ref{same}.

\begin{figure}[h]
\includegraphics[width=0.4\textwidth]{../Images/l6_uncoupled_full_window.png}
\caption{\label{same} Two uncoupled LRC circuits powered by the same pulse
generator. }
\end{figure}

The variable capacitor was then removed from the circuit and measured
with the DMM at 47 $\pm$ 1 nF. Considering that this value is exactly
the same as the standard capacitor, and that the two inductances
were the same within uncertainty, this makes sense. We would expect
that, if all other parameters describing the oscillation are
more or less equal, then in order to equalize the resulting waves
then this parameter would need to be equal as well.

\section{Coupled LRC Oscillator}

In the final phase of this experiment, two coupled LRC oscillators
are analyzed. The complexity of this system is drastically higher
than a single oscillator, and as such the circuit has been designed
in such a way to simplify the interaction of the two LRC circuits
in terms of their phase. In the ``normal" configuration,
the amplitude of on oscillators are either the same or exactly
opposite, corresponding to a phase difference of 0 or 180 degrees.
In this configuration, the A-circuit begins at a maximum and
the B-circuit begins at zero, corresponding to a 90 degrees phase
difference. This is achieved by switching ``on" the coupling in
our box, but switching ``off" the trigger connection to the
B-circuit. \\

\subsection{Observing the ``Beat"}

\subsubsection{Procedure}

In this portion of the lab, we will observe the resulting output
of the coupled LRC circuits and attempt to explain its behavior.\\

The variable capacitor was replaced in the B-circuit, and
the box was set to the above stated parameters.  As in the first
section, the window of the scope was moved to observe only the
falling edge of the excitation and the ensuing behavior of the
two coupled circuits. The variable capacitor was tuned to reduce
the minima between the resulting waves closest to zero as possible.\\

\subsubsection{Results}

The resulting waves were saved and analyzed digitally, and can be seen
in \ref{waves}.

\begin{figure}[h]
\includegraphics[width=0.48\textwidth]{../Images/l6_coupled_same_graph.png}
\caption{\label{waves} Two coupled LRC circuits, operating at a 90 degree
phase difference, and their sum.}
\end{figure}

We can see that the sum of the two channels produces a behavior very similar
to the uncoupled or individual LRC circuits, but each coupled circuit, when
measured individually, expresses a ``beat" pattern.

\subsubsection{Discussion}

It is worth it to attempt to explain the behavior of the circuit before
moving on. It seems clear
that each time the A-capacitor discharges, most of the energy goes into
the A-inductor and bounces back in the typical oscillating fashion
seen above, but some of that energy goes into the B-circuit, producing a
pattern of oscillation there. With each oscillation, more energy is moved
into the B-circuit, but not as much energy is taken out of the B-circuit
by its own oscillations, so the energy in the B-circuit steadily grows.
At a certain point, the energy leaving the A-circuit is less than the energy
leaving the B-circuit, and the pattern reverses, with the B-circuit
losing energy to the A-circuit until the A-circuit reaches another maxima. \\

However, because the energy lost by a given circuit is more or less the same
as the energy gained by the other one, the overall pattern of the sum of
energy in the circuit is the same as a single oscillator. Therefore it is
simply a translation of energy between two locations that produces this
``beat". \\

\subsection{Mechanical Analog}

This behavior is still a little inscrutable, and therefore it is
helpful to observe a mechanical analog which is more intuitively understood.
This analog is a spring-mass system that is allowed to move in two dimensions,
both up and down as well as side-to-side. A diagram describing this setup
can be found in fig. \ref{elastic pendulum}.

\begin{figure}[h]
\includegraphics[width=0.48\textwidth]{../Images/l6_ElasticPendulum.jpg}
\caption{\label{elastic pendulum} Diagram of elastic pendulum coupled
mechanical oscillator.}
\end{figure}

This system was setup in the lab, and a demonstration was made. The motion
was initiated by simply adding tension to the string in the downward direction
and releasing. What was observed was a component of the motion, very small at
first, that was an angular, pendulum motion rather than a spring motion,
which grew slowly. At one point, this pendulum motion was the only motion
observed, and the system by all means appeared to be a pendulum, for
about one period. However, at the end of this period, some small spring
motion was seen, which grew until the spring motion was the only motion
observed and there was no angular momentum, as in the initial condition.
This behavior then repeated several times. \\

This behavior is exactly like what was observed in the coupled LRC circuits,
but the distinction between the two forms of energy was more profound.
Instead of two waves that look similar, which are both described by the same
equations and the same parameters, the mechanical demonstration clearly
distinguished the exchange between the two forms of motion, angular and spring.
We can more clearly understand why energy is exchanged between these two
forms by thinking through it one step at a time, and we can draw parallels
to the coupled LRC system.\\

First, the spring is drawn back and released, close to directly below the
spring's mounting point. However, the mass is bound to be slightly off
center in some direction. There is very little pendulum motion in that
direction, but the accelerating spring gives a hard yank towards the
mounting point. This yank is harder than regular string tension would be,
if this was a regular pendulum, and the mass is kicked further out on
the other side of the mounting point. The same thing happens for some time,
until the spring is no longer providing a `yank' to the mass because it
is moving entirely in the pendulum mode with no motion toward or away from
the spring mounting point. However, this simple harmonic motion can't be
maintained because a spring does not provide the true tension force that
is needed to produce continuous motion, and will stretch rather than provide
a rigid restoring force. This stretching removes energy from the pendulum
system, which will no longer sweep as far out. That pattern continues until
the energy from the pendulum has been brought to zero, and we observe
a spring motion only. \\

\subsection{Fitting the ``Beat"}

In this section, numerical analysis is conducted on the digitized waveforms
in order to calculate the coupling capacitance. First, analysis is
simplified by fitting the inverse exponential to the sum of the two waves,
and then dividing each wave by the exponential to remove the effect of
the decay. This can be see in fig. \ref{nodecay}.

\begin{figure}[h]
\includegraphics[width=0.48\textwidth]{../Images/l6_channels_sans_decay.png}
\caption{\label{nodecay} The resulting A and B channel waves after removing
the effect of the decay. A cutoff was chosen subjectively based on where the
noise was greater than the signal. }
\end{figure}

By using two approaches to curve fitting the beats, the beat frequency and
uncertainty were calculated. The first method involved taking the absolute
value of the signal, and then extracting every peak. A simpler Hilbert
transform approach was considered but the packet was not well defined enough
for this purpose. These peaks were then fit to a cosine wave, providing a
beat frequency for each wave.

\begin{figure}[h]
\includegraphics[width=0.48\textwidth]{../Images/l6_channel_A_peaks_fit.png}
\includegraphics[width=0.48\textwidth]{../Images/l6_channel_B_peaks_fit.png}
\caption{\label{peaks_fits} Illustration of the peak fitting method of
curve fitting the beats for both A and B channel waves, with Channel A shown
above, and Channel B shown below.}
\end{figure}

The beat frequency for channel A was calculated at 168.34 Hz, and
the beat frequency for channel B was calculated at 171.77 Hz. \\

The second method for finding this beat frequency utilizes Fourier analysis.
The beat frequency is simply the difference between two dominant frequencies
at work in the signal, and these two frequencies will be obvious in the
frequency domain, even if they are very difficult to compute in the time
domain. The FFT of each wave was computed, with significant zero padding for
greater resolution and the hamming window to mitigate spectral leakage,
and the peak frequency from the two peaks was found.
The beat frequency is simply defined as the difference between the two peak
frequencies. The FFT analysis can be seen in fig. \ref{peaks}.

\begin{figure}[h]
\includegraphics[width=0.48\textwidth]{../Images/l6_channel_A_fft.png}
\includegraphics[width=0.48\textwidth]{../Images/l6_channel_B_fft.png}
\caption{\label{peaks} Illustration of the FFT method of curve fitting
the beats for both A (above) and B (below) channel waves.}
\end{figure}

The peaks for channel A were located at 2153.06 and 2323.01 Hz, corresponding
to a beat frequency of 168 Hz.
The peaks for channel B were located at 2153.01 and 2324.20 Hz, corresponding
to a beat frequency of 171 Hz. \\

These beat frequency measurements for each wave were then averaged
to produce a final calculated value, with the uncertainty taken as
the standard deviation across each of the two measurements. These
final values were 170 $\pm$ 1 Hz for channel A and 170.9 $\pm$ 0.9
for channel B.

\subsection{Conclusion}

A derivation in appendix D details the differential equations describing
the coupled LRC circuit without damping, assuming that the electrical
components used are identical.
The solution to the differential equations are shown below as

\begin{equation}
q_+(t) = q_{+_0}sin(\omega_{+}t)
\label{qplus}
\end{equation}

\begin{equation}
q_-(t) = q_{-_0}sin(\omega_{-}t)
\label{qplus}
\end{equation}

where q$_+$ = q$_1$ + q$_2$, q$_-$ = q$_1$ - q$_2$. q$_1$ and q$_2$ are
the charges on the non-coupling capacitors in the coupled LRC circuits, and
$\omega_-$ and $\omega_+$ are the frequencies of the oscillations of these
capacitor charges. From the derivation in Appendix D, the frequencies
$\omega_-$ and $\omega_+$ are given by

\begin{equation}
\omega_+ = \frac{1}{2\pi\sqrt{LC}}
\label{omegaplus}
\end{equation}

\begin{equation}
\omega_- = \frac{1}{2\pi\sqrt{L(C+C_{coup}}}
\label{omegaminus}
\end{equation}

where L is the inductance of the two inductors, C is the capacitance of the
two identical capacitors and C$_{coup}$ is the coupling capacitance.  \\

This system has two normal modes, as described earlier. When q$_1$ = q$_2$
at t = 0, $q_{-0}=0$ the two LRC circuits operate in phase with each other.
When q$_1$ = -q$_2$ at t = 0, $q_{+0} = 0$ the LRC circuits oscillate
completely out of phase.\\

In the case of this experiment, q$_1$ = q$_0$ and q$_2$ = 0 at t = 0, because
A-capacitor has been fully charged by the square wave impulse, and B-capacitor
has been completely bypassed. For a coupled LRC circuit with these initial
conditions the system behaves as a combination of the two normal modes.

\begin{equation}
q = q_0(sin(\omega_{+}t)+sin(\omega_{-}t))
\label{Qeqn}
\end{equation}

For weak coupling, (C$_{coup}$  $<$ C), $\omega_{+}$ $\approx$ $\omega_{-}$,
and the normal modes are close in frequency. This gives rise to an apparent
``beat" in the charges on the two capacitors. By rewriting \ref{Qeqn} using
a trigonometric identity, this beat phenomena can more clearly be understood.

\begin{equation}
q = 2q_0sin[(\frac{\omega_{+}+\omega_{-}}{2})t]cos[(\frac{\omega_{+}-\omega_{-}}{2})t]
\label{Qeqn2}
\end{equation}

Equation \ref{Qeqn2} is a characteristic equation describing beats,
or packets of waves. From Equation \ref{Qeqn2} the wave amplitude is
determined by the sine term whereas the beat behavior is determined
by the cosine term. Thus for the initial conditions q$_1$ = q$_0$ and
q$_2$ = 0 at t = 0, with weak coupling, beats will be observed with a
frequency of $\omega_{+}$ $-$ $\omega_{-}$. These frequencies can
be substituted to produce the beat frequency as a function of
L, C, and $C_{coup}$.

\begin{equation}
f_{beat} = \frac{1}{2\pi\sqrt{LC}}-\frac{1}{2\pi\sqrt{L(C+2C_{coup})}}
\label{fbeat}
\end{equation}

This equation can be rewritten to solve for the coupling capacitance.

\begin{equation}
C_{coup} = \frac{1}{2}[\frac{1}{L(\frac{1}{\sqrt{LC}}-2\pi f_{beat})^2}-C]
\label{ccoup}
\end{equation}

Since the indicators and capacitors are identical within
uncertainty, average values of L = 0.1 $\pm$ 0.003 H and C = 4.7 $\pm$ 0.1
nF were used. From these values, along with a beat frequency
f$_{beat}$ = 170 $\pm$1 Hz, the coupling capacitance C$_{coup}$ was
calculated from Equation \ref{ccoup} to be 3.9 $\pm$0.1 nF.\\

This calculated value agrees with the assertion that the system had
weak coupling, C$_{coup}$  $<$ C,and is very similar to the measured
value of 4.1 $\pm$0.1 nF. \\

On a final note, the decision to analyze the falling edge of the pulse
generator should be commented on. The difference in behavior between
the rising edge and falling edge can be simply described as the falling
edge has more oscillations. The reason for this is found in the equation
for a single LRC circuit, shown below.

\begin{equation}
	L \frac{d^2q}{dt^2} + R\frac{dq}{dt} + \frac{1}{C} q=0
\label{ccoup}
\end{equation}
With a non-zero voltage, the current will be much greater, meaning the
resistance will dissipate proportionally more energy, resulting in
more damping and fewer oscillations.

\section{Summary}

In this lab, the behavior of single, uncoupled, and coupled LRC circuits
were evaluated with simple relationships between L, R, C, and voltage
as well as complex differential equations. The latter were simplified
based on the unique initial conditions of this experiment. This behavior
was further linked to a mechanical analogue, a harmonic spring system. \\

In the first phase of the lab, basic quantities describing the circuit
and electrical components were measured directly, and calculated using
curve fits which relate to solutions to the differential equations describing
LRC oscillations. These values are summarized below, in Table I. In these
calculations, it was observed that the precision of the DMM was responsible
for virtually all of the uncertainty in our measurements. \\

In the second phase of the lab, the two circuits were coupled via a
capacitor, which adds immense complexity to the differential equations
describing the flow of charge. It was, however, possible to relate the
beat frequency, or the difference in the fundamental frequency of the
two circuits A and B, to the capacitance of the coupling capacitor $C_coup$
using some assumptions related to the initial conditions of the circuits
and building on more accessible solutions to these governing equations
known as `normal' operating modes. \\

It was possible to find this beat frequency by doing analysis on the
signals from each channel in the time and frequency domain. By
dividing the effect of damping out of the signals, then fitting
the amplitude of the peaks, the changing amplitude of each wave was matched
with a beat frequency. Separately, by simply finding the fundamental
frequency of the two added waves which produce the beat using the FFT,
the beat frequency could be found more directly. Both methods were
considered to produce the final measure of beat frequency, 170 $\pm$ 1 Hz
for each channel A and B. This agreement makes sense considering the
beat is caused by the difference in each circuits own operating frequency
and the other's. \\

Using these measurements of beat frequency, the final value for $C_{coup}$
was calculated at 4.1 $\pm$ 0.1 nF, which is in close agreement with the
measured value of 3.9 $\pm$ 0.1 nF. Given the amount of calculations needed,
this value is satisfyingly close at 1 sigma. Some of this error may
be due to discrepancies in the A and B circuits. If they are not perfectly
identical, then the assumptions behind our equations are not accurate and
the effective capacitance and resistance of the circuit, for example,
will be different than expected.

\newpage
\begin{widetext}
\begin{center}
\begin{table}[h]
\renewcommand{\arraystretch}{1.35}
\setlength{\tabcolsep}{10pt}
\caption{\label{DONE BITCH}Measured and calculated values from analysis of the two inductors in the individual LRC circuit.}
\begin{tabular}{|c|c|c|}
%\hline
\toprule
Inductor Label & 'A' & 'B'\\
\colrule
Full Decay Natural Frequency, $\omega_0$ & 14587.05  $\pm$ 0.03 rad/s & 14578.44 $\pm$ 0.03 rad/s \\
\colrule
Damping Constant, $\gamma$ & 649.9 $\pm$ 0.1  $s^{-1}$ & 651.3 $\pm$ 0.1  $s^{-1}$  \\
\colrule
Single Period Inductance, L & 0.099 $\pm$ 0.009 H &  0.101 $\pm$ 0.009 H\\
\colrule
Full Decay Inductance, L & 0.100 $\pm$ 0.003 H & 0.100 $\pm$ 0.003 H \\
\colrule
Calculated Resistance, R & 65 $\pm$ 2 $\Omega$ & 65 $\pm$ 2 $\Omega$ \\
\colrule
Measured Resistance, R & 17.4 $\pm$ 0.5 $\Omega$ & 17.2 $\pm$ 0.5 $\Omega$ \\
\colrule
Time Constant, $\tau$ & 3077.3 $\pm$ 0.6 $\mu$s & 3070.9 $\pm$ 0.6 $\mu$s\\
\colrule
Quality Factor, Q & 22.4 $\pm$ 0.3 & 22.4 $\pm$ 0.3\\
%\hline
\botrule
\end{tabular}
\end{table}

\begin{table}[h]
\renewcommand{\arraystretch}{1.35}
\setlength{\tabcolsep}{10pt}
\caption{\label{DONE BITCH 2}Measured calculated values from the analysis of the coupled LRC circuits.}
\begin{tabular}{|c|c|c|c|}
%\hline
\toprule
Property & Calculated Value & Measured Value & Deviation \\
\colrule
LRC Capacitence & Standard / 'A': 47.1 $\pm$ 1 nF & Variable / 'B': 47.1 $\pm$ 1 nF & 0$\sigma$\\
\colrule
Beat Frequency & Circuit 'A' Beats: 170 $\pm$ 1 Hz & Circuit 'B' Beats: 170.9 $\pm$ .9 Hz & - \\
\colrule
Coupling Capacitance &  4.1 $\pm$0.1 nF & 3.9 $\pm$0.1 nF & 1$\sigma$\\

%\hline
\botrule
\end{tabular}
\end{table}
\end{center}
\end{widetext}

\newpage
\begin{thebibliography}{9}
%

\bibitem{Feynman} \\
S. Strogatz, \textit{“Infinite Powers: How Calculus Reveals the Secrets of the Universe”}, (Houghton Mifflin Harcourt, New York, 2019).
%
\\\\\\\\
\bibitem{Ficks}
Oregon Graduate Institute, Diffusion Theory: Fick's Laws: \\
\href{https://omlc.org/classroom/ece532/class5/ficks1.html}{https://omlc.org/classroom/ece532/class5/ficks1.html}

\bibitem{Heat}
Stanford, Heat Equation: \\
\href{https://web.stanford.edu/class/math220b/handouts/heateqn.pdf}{https://web.stanford.edu/class/math220b/handouts/heateqn.pdf}

\bibitem{NS}
NASA, Navier-Stokes Equations: \\
\href{https://www.grc.nasa.gov/www/k-12/airplane/nseqs.html}{https://www.grc.nasa.gov/www/k-12/airplane/nseqs.html}
%
\bibitem{Lenz}
Wikipedia, Lenz's Law: \\
\href{https://en.wikipedia.org/wiki/Lenz27s_law}{https://en.wikipedia.org/wiki/Lenz27s_law}

\end{thebibliography}

\end{document}
%
% ****** End of file apstemplate.tex ******


\documentclass[main.tex]{subfiles}
\begin{document}

\section*{Fri Jan 10 2020}

The energy of the states \(\ket{n}\) is well defined, but there are still issues to sort out: for instance, the expectation value of the electric field is zero \emph{at each point},
%
\begin{align}
\bra{n} E_x(z, t) \ket{n} \propto \bra{n} \hat{a} \ket{n} + \bra{n} \hat{a}^\dag  \ket{n} = 0
\,.
\end{align}

However, the expectation value of the \emph{square} of the electric field is nonzero:
%
\begin{align}
\expval{E^2_{x} (z, t) } = 2 \mathcal{E}_{0}^2 \sin^2(kz) \qty(n+ \frac{1}{2})
\,.
\end{align}

This is in accordance with the uncertainty principle, since the operator \(\hat{n} \) does not commute with the electric field operator \(\hat{E}_{x}\). We can write the undetermination relation
%
\begin{align}
\Delta n \Delta E \geq \frac{1}{2} \mathcal{E}_{0} \abs{\sin(kz)} \abs{\expval{\hat{a}^\dag - \hat{a}}}
\,.
\end{align}

We expect to be able to find a notion of phase such that there is a number-phase uncertainty relation, similarly to the time-energy uncertainty relation.

The time evolution of the electric field operator is given by
%
\begin{align}
E_{x} = \mathcal{E}_{0} \qty(\hat{a} e^{-i \omega t}  + \hat{a}^\dag e^{i \omega t} )\sin(kz)
\,,
\end{align}
%
and we define the quadrature operators:
%
\begin{align}
X_{1} = \frac{1}{2} \qty(\hat{a} + \hat{a}^\dag) \qquad \text{and} \qquad
X_{2} = \frac{1}{2i} \qty(\hat{a} - \hat{a}^\dag)
\,.
\end{align}

We have effectively decomposed the electric field into two oscillating parts, out of phase with each other by \SI{90}{\degree}. We have the commutator \([\hat{X}_{1}, \hat{X}_{2}] = i/2\).
Even for the vacuum state the fluctuations of these operators are nonzero.

We will now distinguish two different kinds of radiation: blackbody radiation and coherent (laser-like) radiation.

For the first case, we know that the distribution of the energy levels is given by the Boltzmann distribution,
%
\begin{align}
P(n) = \frac{1}{Z} \exp(- \frac{E_n}{k_B T})
\,,
\end{align}
%
where \(Z\) is a normalization factor.
We now give an intuitive justification.

Let us suppose that we have a system of four particles, with three quanta of energy, which we  write as \(\Delta E\). How can this energy be distributed?

\begin{table}[h]
\centering
\begin{tabular}{ccccc|c}
0 & \(\Delta E\) & \(2 \Delta E\)& \(3 \Delta E\)& \(4 \Delta E\)  & Possibilities\\
3 & 0 & 0 & 1 & 0 & 4 \\
2 & 1 & 1 & 0 & 0 & 12  \\
1 & 3 & 0 & 0 & 0 & 4
\end{tabular}
\end{table}

So, for \(0 \Delta E\) we have \(12+24+4 = 40\) total possibilities, for \(1 \Delta E\) we have \(12 +12 = 24\) total possibilities, for \(2 \Delta E\) we have \(12\) possibilities, for \(3 \Delta E\) we have \(4\) possibilities. The total is then 80.

This kiind of looks like an exponential decrease, I guess if we were to do a more precise calculation we would get an exponential exactly.

In a quantum setting, we will have a density matrix looking like
%
\begin{align}
\rho = \frac{1}{\tr \exp(- \hat{H} / k_B T)} \exp(- \hat{H} / k_B T)
\,;
\end{align}
%
which gives a familiar result:
%
\begin{align}
\rho = \sum _{n} \frac{\exp(- E_n / k_B T)}{Z} \dyad{n}
\,.
\end{align}

The average number of photons can be found to be given by
%
\begin{align}
\expval{n} = \frac{1}{\exp(\hbar \omega / k_B T) - 1}
\,.
\end{align}

In the limits \(\hbar \omega / k_B T\) going to either infinity or zero we get \(\expval{n} \rightarrow \hbar \omega / k_B T\) or its inverse.

We can write the relation
%
\begin{align}
\exp(- \hbar \omega /k_B T) = \frac{\overline{n}}{1 + \overline{n}
}
\,,
\end{align}
%
where \(\overline{n} = \expval{n}\). Then we will have
%
\begin{align}
\rho = \frac{1}{1 + \overline{n}} \sum _{n} \qty(\frac{\overline{n}}{1 + \overline{n}})^{n} \dyad{n}
\,.
\end{align}

It can be shown that
%
\begin{align}
\expval{\hat{n}^2 } = \overline{n}  +2 \overline{n}^2
\,,
\end{align}
%
which implies
%
\begin{align}
\Delta n = \qty(\overline{n} + \overline{n}^2)^{1/2}
\,,
\end{align}
%
so we have \(\Delta n \sim \overline{n} + \frac{1}{2}\) asymptotically. Therefore, there never is a well-defined number of photons in the box.

We can write an expression for the average energy density \(U(\omega )\):
%
\begin{align}
U(\omega ) = \frac{\hbar \omega^3}{\pi^2c^3} \frac{1}{\exp(\hbar \omega / k_B T) - 1}
\,.
\end{align}

The average energy of these photons is given by \(\hbar
 \omega \overline{n}\).

From these expressions we can recover Wien's law and Stefan-Boltzmann's law.

How do we represent a plane wave in a QFT?
If we want a nonzero electric field we need a superposition of number states differing by \(\pm 1\).

Another way to put it is: are there eigenstates \(\ket{\alpha }\) of the annihilation operator \(\hat{a}\)?

They will look like
%
\begin{align}
\ket{\alpha } = C_0 \sum _{n} \frac{\alpha^{n}}{\sqrt{n!}} \ket{n}
\,.
\end{align}

By normalization, \(C_0 = \exp( - \abs{\alpha }^2 / 2)\). This gives us a coherent state, which like we wanted has a nonzero expected electric field. It looks precisely like a plane wave:
%
\begin{align}
\expval{\hat{E}_{x}}_{\alpha } = i \sqrt{\frac{\hbar \omega }{2 \epsilon_0 V }} \qty(\alpha \exp(i \vec{k} \cdot \vec{x} - i \omega t) - \alpha^{*} \exp(-i \vec{k} \cdot \vec{x} + i \omega t))
\,.
\end{align}

We find also the expectation value of the \emph{square} of the electric field
%
\begin{align}
\expval{E^2_{x} }_{\alpha } = \frac{\hbar \omega }{2 \epsilon_0 V} \qty(1 + 4 \abs{\alpha }^2 \sin^2 (\omega t - \vec{k} \cdot \vec{r} - \theta ))
\,,
\end{align}
%
where \(\alpha = \abs{\alpha } \exp(i \theta )\).

This means that not even in the vacuum state we can have a zero expected electric field. The vectors \(\ket{\alpha }\) are \emph{over-complete}, since they are bidimensional while a one-dimensional continuous basis would be enough to span the Hilbert space.

The average of the number of photons \(\hat{n}\) for an eigestate \(\ket{\alpha }\) is quickly calculated to be \(\abs{\alpha }^2\): then we can see that \(\abs{\alpha }^2 = \overline{n}\).

So, with these states we have \(\expval{\hat{n}^2}_{\alpha } = \overline{n}^2 + \overline{n}\). Therefore \(\Delta n = \sqrt{\overline{n}}\). The probability of detecting \(n\) photons is given by \(\abs{\braket{n}{\alpha }}^2\):
%
\begin{align}
P_{\alpha }(n) = \exp(- \overline{n} ) \frac{\overline{n}^{n}}{n!}
\,,
\end{align}
%
a Poissonian distribution. If the number of photons gets large then the Poissonian approaches a Gaussian.

\end{document}


\documentclass[a4paper]{article}

\usepackage{graphicx}
\usepackage{float}
\usepackage{amsmath}
\usepackage[margin=3cm]{geometry}
\usepackage{titlesec}
\usepackage{hyperref}

\graphicspath{{images/}}
\newcommand{\sectionbreak}{\clearpage}

%opening
\title{koda: Deep Learning-Enhanced Pipeline for Keyword Detection in Document Images}
\author{Matteo Pellegrino, Federico Terzi}

\begin{document}

\maketitle

\begin{abstract}
This paper proposes a pipeline, KODA, suitable for keyword highlighting in document images. At its core, KODA uses a combination of edge detection, hough transform, OCR and warping to achieve the result. The study describes the custom, deep learning-based, approach for edge detection, motivated by the problems of traditional techniques and explaining the model training process, along with examples. Moreover, document detection and warping algorithms are detailed, as well as the keyword highlighting process.

\vspace{1cm}
All the code can be found in the repository: \url{https://github.com/federico-terzi/koda}
\end{abstract}

\begingroup\let\clearpage\relax
\vspace{1cm}
\tableofcontents \endgroup

\section{Introduction}
Document detection in images is a well-known problem, whose most common use-case is document scanning using mobile devices, achieving results comparable with commercial scanners. Although many apps already solve this problem, only a few offer the possibility to interactively search the document for specific keywords, a feature which could be beneficial to many people. For example, those with alimentary disorders could greatly benefit from an application which, from a simple restaurant menu or a product label picture, could show all the contained allergens conveniently.

Our proposed pipeline, KODA, is specifically designed to detect documents from images, extract all the interesting keywords and, eventually, highlight them in the original image, so that the user can conveniently see them.

The paper is organized as follows: the first section, Architecture, describes the general structure and components of KODA. In the Edge Detection section, starting from the limitations of traditional techniques, a deep learning-based approach is proposed, along with a thorough comparison. Thereafter, the Corners Detection section describes the document detection algorithm itself, exploiting Hough transformations. Then, in the Document analysis section, warping, OCR and highlighting techniques are described. Finally, the concluding remarks are drawn in the last section.

\section{Architecture}

KODA architecture was inspired by a Dropbox article \footnote{https://blogs.dropbox.com/tech/2016/08/fast-and-accurate-document-detection-for-scanning/} which defines the steps to solve a problem similar to the one this project is based on.

The process is divided into two macro tasks (Fig. \ref{fig:pipeline}): image processing and keywords detection: The former detects a document from an image and retrieve all the contained words. The latter find user specified keywords, highlighting them in the document image.

\begin{figure}[!htbp]
	\centering
	\includegraphics[width=0.8\linewidth]{Pipeline.png}
	\caption{KODA Pipeline scheme with sub-tasks. The computationally expensive processing is done once per input image, then the user can search for keywords and retrieves the output image multiple times repeatedly.}
	\label{fig:pipeline}
\end{figure}

\begin{figure}[!htbp]
	\includegraphics[width=\linewidth]{ipt.png}
	\caption{Image processing tasks example. Note: edges detection returns a resized image (256x256)}
	\label{fig:imageprocessing}
\end{figure}

\section{Edge Detection}

Above all the possible approaches, the pipeline proposed in this paper heavily
relies on edges to detect documents inside images. Therefore, in order to obtain
good results, it is crucial to employ a solid \textit{edge detection} algorithm.

\subsection{First attempts with Canny}

Our initial attempts to detect document edges involved the well known \textit{Canny edge detector} \footnote{J. Canny, "A Computational Approach to Edge Detection," in IEEE Transactions on Pattern Analysis and Machine Intelligence, vol. PAMI-8, no. 6, pp. 679-698, Nov. 1986.
	doi: 10.1109/TPAMI.1986.4767851}.
Although applying the filter directly does detect the sheet edges, it presents many artifacts
due to the text printed on the document itself (as seen in Fig. \ref{fig:canny_gaussian} (B)).

In order to mitigate the problem, a \textit{Gaussian filter} is applied before the edge detection, obtaining a clear highlighting of the interesting edges (as seen in Fig.  \ref{fig:canny_gaussian} (C)).

\begin{figure}[!htb]
	\includegraphics[width=\linewidth]{canny_gaussian.png}
	\caption{Application of Canny edge detector to the original image without/with a Gaussian Filter}
	\label{fig:canny_gaussian}
\end{figure}

Despite the result, after many observations it became clear that this approach was not robust enough to
work in all real-world scenarios. In particular, Canny's parameter tuning turned out to be a major
problem, as it can be seen in Fig \ref{fig:canny_comparison}, where a combination of parameters,
despite producing good results in some scenarios, proved itself incapable of generalizing well in others.

\begin{figure}[htb!]
	\includegraphics[width=\linewidth]{canny_comparison.png}
	\caption{Comparison of different thresholds for Canny edge detector}
	\label{fig:canny_comparison}
\end{figure}

\subsection{Deep Learning approach}

In order to make the pipeline able to deal with real-world images, it is crucial to develop an
edge detection mechanism capable of generalizing better in heterogeneous situations.
For this reason, a \textit{Deep Learning}-based approach was explored with the aim of creating a model
capable of distinguishing document sheet edges from the rest of the image.

Due to the lack of a suitable dataset with labelled document edges, a custom one was created.
In particular, 250 images containing document sheets were taken under different lighting conditions,
backgrounds and positions. Thereafter, every image was labelled with the 4 corner coordinates of the document.

For this particular task, \textit{Keras}, an open-source Python library for Deep Learning \footnote{https://keras.io/}, was chosen to build the model. Many experiments were made to find the right architecture, most of them
exploiting \textit{convolutional neural networks}. Above all of them, KODA uses \textit{U-Net}
\footnote{O. Ronneberger, P. Fischer, T. Brox, "U-Net: Convolutional Networks for Biomedical Image Segmentation", CoRR, abs/1505.04597, 2015}, a popular architecture for \textit{object segmentation}, and in particular an open-source implementation for Keras \footnote{https://github.com/zhixuhao/unet}.

The model takes a 3-dimensional input image (RGB) and outputs a 2-d map of the edges. Although the shape
of those matrices can vary, KODA uses a 256 pixels side for both input and output, as it provides a reasonable compromise between size and resolution. Fig. \ref{fig:label_edge} illustrates the way a
sample is fed to the network. In particular, the input image is resized to a 256x256 RGB image and the
output label is a gray scale map of the expected document edges.

\begin{figure}[htb!]
	\includegraphics[width=\linewidth]{label_edge.png}
	\caption{Example of the way a sample is fed to the network.}
	\label{fig:label_edge}
\end{figure}

Due to the high number of required samples, the dataset itself is insufficient to fully train the network.
For this reason, a technique called \textit{data augmentation} is applied to provide the model
with enough variation. In particular, before being fed to the network, a random combination of transformations, such as scale, translation, rotation and brightness changes, is applied to the image and to the resulting edge feature map, as shown in Fig. \ref{fig:augmented_image}.

\begin{figure}[H]
	\includegraphics[width=\linewidth]{augmented_image.png}
	\caption{Example of Data Augmentation}
	\label{fig:augmented_image}
\end{figure}

During the training, finding the best compromise between good results and overfitting proved to be a challenging problem. In particular, the \textit{loss function}, in this case \textit{binary crossentropy},
was not representative of the model's quality. As it can be seen in Fig. \ref{fig:model_loss}, the loss value of the validation set settles around epoch 40. Despite this, further experimentation shows that the model still improves well beyond that epoch. For this reason, a new approach was needed to evaluate the performances of the model.

\begin{figure}[htb!]
	\centering
	\includegraphics[width=0.7\linewidth]{model_loss.png}
	\caption{Model loss over the first 80 epochs.}
	\label{fig:model_loss}
\end{figure}

Despite being simple, the proposed solution is to save the current model's results over a set of test images after each epoch, as well as saving the model itself. This approach allows a simple comparative analysis, and allowed us to select the best model according to its actual results. Epoch 70 produced the best performing model from a generalization standpoint, while further epochs shown interesting but less effective trends. In particular, as you can see from Fig. \ref{fig:edge_comparison_epoch}, the model at epoch 70 is capable of detecting both (A) and (B) edges fairly well, whereas the one at epoch 140 presented an improved result in (A), removing uninteresting objects from the top portion of the image, but was almost incapable of recognizing edges in (B). This phenomenon could be explained as the model starting to empathize straight lines and discarding curved ones. Unfortunately, the latter are a common case and should be considered, therefore the first model was chosen.

\begin{figure}[!htbp]
	\centering
	\includegraphics[width=\linewidth]{edge_comparison_epoch.png}
	\caption{Comparison of the edge detector over different epochs.}
	\label{fig:edge_comparison_epoch}
\end{figure}

\subsection{Comparison between the two approaches}

An overview of the end result can be seen in Fig. \ref{fig:deep_comparison}, which also shows a comparison with the previous Canny-based approach.

\begin{figure}[!htbp]
	\centering
	\makebox[\textwidth][c]{\includegraphics[width=1.2\textwidth]{deep_edge_comparison.png}}%

	\caption{Comparison of Canny and Deep Learning-based edge detection.}
	\label{fig:deep_comparison}
\end{figure}

\section{Corner detection}

The corner detection task goal is to find the four document corners starting from the one-channel edges image. These points should be as precise as possible in order to define the tightest convex polygon around the document. The intuitive method to solve this task is to use the Hough transformation, as suggested in a Dropbox article \footnote{https://blogs.dropbox.com/tech/2016/08/fast-and-accurate-document-detection-for-scanning/}, to find straight lines that best approximate the document edges; the corners will be the intersection between those lines.

The suggested method, relies on finding more than one line per edge resulting in a set of intersections points. Those points are used to compute polygons and the one which best approximate the document edges is chosen to define the four corners. However, from empirical experiments, this could lead to an high amount of intersections meaning an enormous amount of polygons to test. Instead, KODA implementation extracts the best four lines ahead, so their intersections are immediately the best corners.

All the operations described in the following section are computed with reference to the resized edges image which is by default 256x256.

\subsection{Hough Transform}

OpenCV provides an interface for computing Hough lines \footnote{https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough\_lines/hough\_lines.html} using polar coordinates (rho, theta); the problem is to calibrate the parameters and select the best lines retrieved. In order to remove unwanted noise in the edges image, an intensity threshold, which value was determined empirically (80), is applied before submitting it to the OpenCV interface. The threshold and the resolution interface parameters also were found empirically. The input images come from a natural environment with no control of lighting, so it can be expected that edges images intensity may heavily varies. For this reason, if no Hough lines are found, the task iteratively tries to reduce the threshold parameter by 10\% until enough lines are found or a maximum number of iterations is reached (default is 3).

Once a set of lines is found, a sub-task is responsible to select the four best ones, one per each document edge. This is accomplished exploiting the fact that the Hough lines returned by the OpenCV interface are ordered by confidence: the first four strongest ones are selected, as shown in Fig. \ref{fig:hough}.

\begin{figure}[H]
	\makebox[\textwidth][c]{\includegraphics[width=1.2\textwidth]{Hough.png}}
	\caption{Hough lines computation extracting the best one per each document edge.}
	\label{fig:hough}
\end{figure}

\subsection{Computing corners}

Once the four Hough lines corresponding to the document edges are found, computing the document corners means to correctly intersect the lines. In order to distinguish lines that will be intersected from the others, they are sorted based on theta, so parallel lines are sequentially near to each other. In particular, the intersection is calculated as follow:

\begin{equation*}
	\begin{bmatrix} \cos(\sigma_1) & \sin(\sigma_1) \\ \cos(\sigma_2) & \sin(\sigma_2) \end{bmatrix}
	\begin{bmatrix} x \\ y \end{bmatrix}
	=
	\begin{bmatrix} \gamma_1 \\ \gamma_2 \end{bmatrix}
\end{equation*}
\begin{equation*}
	Ax = b
\end{equation*}

The implementation to find the intersection relies on \texttt{numpy.linalg.solve}\footnote{ \texttt{numpy.linalg.solve}: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html} which may raises an exception if the A matrix is singular because the computation is not possible. Whenever this happens, a least square solution, if it converges, via \texttt{numpy.linalg.lstsq} \footnote{\texttt{numpy.linalg.lstsq}: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html} is provided.

To maintain uniformity for the client API, the corners are sorted following the Cartesian quadrants and they are scaled up based on the original image size provided as input.

\begin{figure}[H]
	\makebox[\textwidth][c]{\includegraphics[width=1.2\textwidth]{computingcorners.png}}
	\caption{Computing corners as intersections of extracted Hough lines.}
	\label{fig:computingcorners}
\end{figure}

\section{Document analysis}

\subsection{Warping}

The first step of document analysis task is to warp the found paper within the image through a perspective transform using the detected corners. The document is expected to be A4 format, so the warped image size is computed considering the minimum of the possible widths (top, bottom) and heights (left, right), then forcing the greater dimension to be equals to the lesser one multiplied by the square root of 2 (ISO 216).

\begin{figure}[H]
	\makebox[\textwidth][c]{\includegraphics[width=1\textwidth]{Warping.png}}
	\caption{Comparison between original and warped images.}
	\label{fig:warping}
\end{figure}

\subsection{OCR}

In order to implement the \textit{Optical Character Recognition} feature needed to highlight keywords, KODA uses \textit{Tesseract}\footnote{https://github.com/tesseract-ocr/tesseract}, an open-source OCR engine by Google. Moreover, it uses \textit{TesserOCR}\footnote{https://github.com/sirfz/tesserocr}, a Python wrapper which provides a fast binding to the C++ native library and exposes all the low-level APIs.

Due to the keyword highlighting feature, it was crucial to get the position of single words rather than blocks of text. This is easily achieved using Tesseract's \textit{Iterators}, by setting the \textit{Page Iterator Level} to \texttt{WORD}. Thereafter, KODA can cycle through each word, filtering out the ones with low confidence and extracting the bounding boxes, as well as the text, as shown in Fig. \ref{fig:ocr}.

\begin{figure}[H]
	\makebox[\textwidth][c]{\includegraphics[width=1\textwidth]{ocr.png}}
	\caption{Example of OCR word segmenting}
	\label{fig:ocr}
\end{figure}

\subsection{Keyword highlighting}

The highlighting task goal is to apply a colored overlay on all the user specified keyword occurrences within the document in the image. Assuming the OCR library provides the bounding box of a detected word, a colored rectangle is drawn correspondingly on a image copy. The task output is the weighted sum of the original image and the overlay, resulting in a transparent highlighting effect on the keywords searched by the user.

The formula applied \footnote{https://docs.opencv.org/2.4/modules/core/doc/operations\_on\_arrays.html} is formally as follow: %

\begin{equation}
	out = src_1 \cdot \alpha + src_2 \cdot \beta + \gamma
\end{equation}

Where:

\begin{center}
	$src_1$ = overlay image \\
	$src_2$ = original image \\
	$\alpha$ = 0.3  \\
	$\beta$ = 1 - $\alpha$ \\
	$\gamma$ = 0
\end{center}

However, the OCR library performs on the warped image, instead of the original one. For this reason, the overlay has to be warped back to the document shape as in the original image. This requirement is satisfied by applying a perspective transformation using the inverse of the perspective transform matrix used to find the warped image.

The KODA implementation (shown in Fig. \ref{fig:keywordhighliting}) relies on this concept, but actually transforms each bounding box instead of the entire final overlay. Transforming the warped image copy (final overlay) would lead to black pixels all over the areas where the document is not present: adding it to the orginal images results in darken areas outside the document. To avoid this problem, the highlights are drawn directly on the original image copy (not warped) after they are transformed correctly.

\begin{figure}[H]
	\makebox[\textwidth][c]{\includegraphics[width=1\textwidth]{kh.png}}
	\caption{Process to apply the highlight overlay.}
	\label{fig:keywordhighliting}
\end{figure}

\begin{figure}[H]
	\makebox[\textwidth][c]{\includegraphics[width=1.5\textwidth]{Highlighting.png}}
	\caption{ Examples of the keyword "on" highlighting. Note: the keyword is not searched as a perfect match, but as a substring of actual words. The highlight is applied to the entire word.}
	\label{fig:keywordhighliting2}
\end{figure}

\section{Conclusion}

The proposed pipeline is able to correctly recognize and analyze the majority of document pictures in real-world scenarios, searching for the given keywords and preserving the original perspective, as shown in Fig. \ref{fig:result}.

\begin{figure}[H]
	\makebox[\textwidth][c]{\includegraphics[width=1\textwidth]{result.png}}
	\caption{Example of KODA pipeline result for keyword "the". Overall output image on the left and close up on the right.}
	\label{fig:result}
\end{figure}

That said, KODA is still limited in certain situations. Firstly, the edge detection model was trained specifically to recognize white documents and, as a result, struggles with colored ones.
Moreover, although the deep learning-based edge detection offers superior results compared to traditional techniques, it is computationally expensive. In particular, the model itself weights about 360 megabytes and takes about 0.5/1 seconds to process the image on a modern desktop CPU, which makes it unsuitable for mobile applications.

Future improvements may focus on the creation of a less computationally and memory expensive edge detection, so that the pipeline can be ported on a mobile device.

\end{document}


\section{\textbf{SimpleSnapshotTest}}

\subsection{Particular Case (problem)}
The problem we want to solve is that of atomic snapshots: we want to
read atomically a set of registers. For this problem we assume the
registers are themselves atomic, and that each one supports
multiple-readers but just one writer. The problem is better defined in
terms of the Java interface we want to implement: \\

\begin{lstlisting}[style=nonumbers]
public interface Snapshot<T> {
  public void update(T v);
  public T[] scan();
}
\end{lstlisting}
\hfill

While the method \C{update} allows each thread to modify its own
register (single-writer), the \C{scan} method allows any thread to
read all the registers as a single atomic operation
(multiple-readers). Ideally, we would like to have a wait-free
implementation of these two methods.

\subsection{Solution}
The solution presented in the book is a natural evolution to the
sequential implementation (which uses \C{synchronized} methods for
both \C{update} and \C{scan} methods). The solution is called
\C{SimpleSnapshot} and it uses stamped values for the \C{update}
method; there is no need to seek for the maximum, just to increase the
current stamp per thread (each thread only writes to its own
register). This makes the \C{update} method wait-free, which is the
ideal (it was not very hard to achieve indeed, given the single-writer
restriction). \\

For the \C{scan} method we call an auxiliary function \C{collect},
which represents a non atomic read of all the registers (which are
copied into a new array and returned); if two
consecutive \C{collect} calls return same values, it means that between
those two calls there were no writes. When we reach that condition, we
return the array of values; otherwise we repeat the iteration. Please
note that \C{scan} method is not wait-free (we do not know how many
times we will need to iterate), but at least is
obstruction-free (we are not blocking other threads from trying their
own \C{scan} calls). \\

The main parts of the code are presented below for reference: \\

\begin{lstlisting}[style=numbers]
public class SimpleSnapshot<T> implements Snapshot<T> {
  // array of atomic MRSW registers
  private StampedValue<T>[] a_table;
  ...
  public void update(T value) {
    int me = ThreadID.get();
    StampedValue<T> oldValue = a_table[me];
    StampedValue<T> newValue =
        new StampedValue<T>((oldValue.stamp)+1, value);
    a_table[me] = newValue;
  }
  private StampedValue<T>[] collect() {
    StampedValue<T>[] copy = (StampedValue<T>[]) new StampedValue[a_table.length];
    for (int j = 0; j < a_table.length; j++)
      copy[j] = a_table[j];
    return copy;
  }
  public T[] scan() {
    StampedValue<T>[] oldCopy, newCopy;
    oldCopy = collect();
    collect: while (true) {
      newCopy = collect();
      if (! Arrays.equals(oldCopy, newCopy)) {
        oldCopy = newCopy;
        continue collect;
      }
      // clean collect
      T[] result = (T[]) new Object[a_table.length];
      for (int j = 0; j < a_table.length; j++)
        result[j] = newCopy[j].value;
      return result;
    }
  }
}
\end{lstlisting}
\hfill

\subsection{Experiment Description}

The test program \C{SimpleSnapshotTest} consists of two individual
test cases:

\begin{itemize}
  \item \C{testSequential}: with a single thread we update its value
    first (11), and then obtain an snapshot (\C{scan}); expectation is
    to have a single value in array (the one we wrote).
  \item \C{testParallel}: we create a couple of threads, and each one
    writes its own register twice (first putting 11, then 22). Then
    each thread proceeds to call \C{scan} method and saves its own
    returned values into a global results table. At the end of test,
    main thread check that both threads got the same results (which
    should be a two element array, both with value 22).
\end{itemize}

\subsection{Observations and Interpretations}
The test runs fine, both in 2 and 24 core machines; sample output
below: \\

\begin{verbatim}
$ junit register.SimpleSnapshotTest
.sequential
.parallel

Time: 0.002

OK (2 tests)
\end{verbatim}
\hfill


% Prepared by Calvin Kent
%
% Exam Practice Template v19.02
%
%%% 201901/MATHxxx/Midterm or Final Practice
%
\documentclass[11pt,oneside]{book} %
\usepackage{CKpreamble}
\usepackage{CKlecture}
%
\renewcommand*{\doctitle}{Exam Practice}
\begin{document}
	% Start of Class settings
	\renewcommand*{\headtitle}{MATH xxx Midterm yy Practice} % Head title
	\renewcommand*{\term}{Term 2000} % Term
	\renewcommand*{\coursecode}{MATH 000} % Course code
	\renewcommand*{\coursename}{Course Name} % Full course name
	\renewcommand*{\profname}{ProfFName ProfLName} % Prof Name
	\renewcommand*{\cnlink}{http://www.student.math.uwaterloo.ca/~c2kent/} % course notes link Course notes link
	% End of Class settings
	\setfigpath
	\pagenumbering{roman}
	\inserttoctitle
	\pagestyle{plain}
	\addctoc{\small Preface and Notation}
	\section*{Preface and Notation}
	\fppreface
	\crule{red}
	\hfill Calvin KENT
	\clearpage
	\pagenumbering{arabic}
	\pagestyle{examprac}
	\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
	\renewcommand{\sectionmark}[1]{ \markright{#1}{} }
	%%% Note to user: CTRL + F <CHANGE ME:> (without the angular brackets) in CKpreamble to specify graphics paths accordingly.
	% Exam practice sheet start
\begin{pracchap}[1]{First Chapter}
	\section{Source code for .sty and .tex files are available on Github}
	\subsection{Links}
	Templates: \url{http://www.student.math.uwaterloo.ca/~c2kent/LectureNotes/index.html}\\
	Github: \url{https://github.com/CalvinKent/My-LaTeX}
\end{pracchap}
\begin{pracchap}[-1]{Set Theory}
	\begin{rem}
		content...
	\end{rem}
	\section{Cardinality}
	\begin{thrm}
		content...
	\end{thrm}
	\begin{proof}
		content...
	\end{proof}
	\subsection{Continuum Hypothesis}
	\begin{qsm}
		content...
	\end{qsm}
\end{pracchap}
\end{document}
%	\begin{figure}[H]
%	\centering
%	\includegraphics[width=0.75\linewidth]{p}
%	\caption{caption.\label{fig:}}
%	\end{figure}


\documentclass{IEEEtran}
\usepackage{tikz}
\tikzstyle{box}=[rectangle,draw=black, ultra thick, minimum size=1cm]
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{listings}
\usepackage{caption}
\usepackage{lipsum}
\usepackage{amsmath}
\usepackage[hidelinks]{hyperref}

\author{Alp Gokcek \\gokcekal@mef.edu.tr \\ MEF University \\ \today}
\title{\href{https://github.com/alpgokcek/turkey-navigation-ai}{Navigation on Turkey Map}}

\renewcommand\IEEEkeywordsname{Keywords}

\begin{document}
	\maketitle
	\begin{abstract}
		Search problem is a well-known issue in the field of Artificial Intelligence. There are various algorithms designed for this problem, such as Dijkstra, A-star, Breadth-First Search, and much more. This paper has demonstrated and applied one of the most efficient search algorithms for the problem, which is the A-star algorithm for finding the shortest path on a given map.
	\end{abstract}
\begin{IEEEkeywords}
	Shortest-path, A-star search, search algorithms.
\end{IEEEkeywords}
\section{Introduction \& Problem Statement\label{introduction}}
Searching is a comprehensive technique of problem-solving in AI. Before evaluating the problem, there are several terminological words and phrases that have to be known.
\subsection{Terminology}
There are several terminological things to know during the paper. These are:
\begin{itemize}
	\item \textbf{Node(State):} All potential outcomes of the problem with a unique identification
	\item \textbf{Transition:} Traveling between states or nodes.
	\item \textbf{Starting Node:}  Where to start searching
	\item \textbf{Goal Node:} The target to stop searching.
	\item \textbf{Cost:} Cost for the path from a node to another node. This value can be the distance between nodes, time, and much more..
	\item \textbf{g(n):} This function returns the exact cost of the path from the starting node to any node $n$.
	\item \textbf{h(n):} This function returns heuristic estimated cost from node n to the goal node.
	\item \textbf{f(n):} Lowest cost in the neighboring node n
\end{itemize} \cite{astar-tds}
\subsection{Problem}
These algorithms aim to travel from a starting node to a goal node by transiting through transitional nodes. It is not just used in shortest-path problems but also in single-player games like puzzles, Sudoku, and many more. For instance, in N-puzzle or also known as Sliding Puzzle, there is a square-shaped board, and it has N-tiles with one empty space that is available for sliding a block. In this case, the approach is to find all possible solutions through moving each block one by one until reaching the goal state.\\

In the shortest-path problem, as the name refers, the main goal is to find the shortest path between 2 nodes, in our case these nodes are cities, if any possible path exists. \cite{shortest-path} This problem is also a search problem and a search operation is performed from starting node to the goal node. There are several algorithms designed for solving searching problem and they have different advantages over each other. The most known algorithms can be listed as:\\
\begin{itemize}
	\item Breadth-First Search
	\item Depth-First Search
	\item A-star(A*) Search
	\item Greedy Best First Search
	\item Uniform Cost Search (Dijkstra's Algorithm)
\end{itemize}
The most used ones are A* Search Algorithm, Breadth-First, and the Dijkstra Algorithm. \cite{astar-redblob} Search pattern differences of Breadth-First, Dijkstra and A-star search algorithms is illustrated in Figure \ref{search-patterns}. \\

\includegraphics[scale=1.35]{search.jpg}
\captionof{figure}{Search patterns of Breadth-First, Dijkstra and A* \cite{astar-redblob}\label{search-patterns}\\}

\subsection{A* (A-star) Search Algorithm}
\textit{A* algorithm} is a heuristics-based algorithm for the shortest path-finding. It is the optimized version of the Dijkstra Algorithm for a single target. A* algorithm aims to reach only the goal node in the shortest path possible, whereas, in the Dijkstra Algorithm, all possible paths from all nodes would be found. In other words, the Dijkstra algorithm is finding the path blind by calculating all the paths possible, whereas, in A-star, it senses the shortest path with the help of the heuristic function. So, if the problem is to reach from one node to another, the Dijkstra Algorithm is not optimal. Therefore, the need for the A* algorithm arose. Its working principle is based on heuristics, at each node that expanded, the heuristic function would be executed, and the cost of transition is calculated. \cite{astar-redblob} The pros and cons of the algorithm could be found below.\\
\subsubsection{Advantages}
The A-star algorithm is a better performing algorithm compared to other search algorithms. There are several pros that can be mentioned:
\begin{itemize}
	\item On the worst case, its complexity is $\mathcal{O}(n\log{}n)$. Other popular search algorithms such as Dijkstra Algorithm has the complexity of $\mathcal{O}(n^2)$.
	\item A-star is optimal since there is a significant difference between the number of nodes expanded during the execution.
	\item If the branching factor is finite and each transition has a fixed cost, then the algorithm becomes complete. \cite{astar-brainkart}\\
\end{itemize}
\subsubsection{Disadvantages}
As each algorithm has downsides, A-star has downsides too. Several cons that can be listed are:
\begin{itemize}
	\item A-star algorithm uses a lot of memory.
	\item The execution speed is highly dependent on the heuristic function $h(n)$. The heuristic function must be admissible
	\item A-star algorithm is not a uniformed algorithm; in other words, it can only target one node in one execution. For instance, if there are $n$ targets on the graph, A-star would be executed $n$ times, which may or may not be the optimal solution.
\end{itemize}

\includegraphics[scale=0.32]{solving-puzzle.jpeg}
\captionof{figure}{Solution for N-puzzle with A* algorithm \label{search-example}\\}

In Figure \ref{search-example}, a sample solution could be found which is solved with A-star algorithm.

\subsection{Breadth-First Search Algorithm}
Breadth-First search is the intuitive solution for everyone. It is simple compared to other algorithms and it does not consider the costs, thus it explores equally in all directions. This method is significantly useful algorithm but it is not frequently used in shortest-path problems since it is not optimal for this problem at all.

\subsection{Dijkstra Algorithm}
\textit{Dijkstra's Algorithm} calculates the shortest path from one desired node of the graph to every other node. It is widely used and adopted for many different applications, such as computer networks, road maps, social platforms and much more. There are various implementations of the algorithm; the main difference between them is how they store the vertices of the graph, denoted with $Q$. They can be stored in regular arrays, linked lists, adjacency lists, or tree structures. The algorithm's running time can be represented with the number of edges $|E|$ and the number of vertices $|V|$ using big-o notation. Running time mainly depends on these structures, storing $Q$ in arrays it is $\mathcal{O}(|V|^2)$ but with the help of \textit{min-priority queue} it drops down to $\mathcal{O}((|E| + |V|) \log{|V|})$ time in the worst case.

\section{Solution Design Process\label{solution-design}}
For this problem, the initial approach was to implement the Dijkstra Shortest Path Algorithm and prompt the outcome to the user after the execution of the algorithm. As mentioned in the previous section, Dijkstra Algorithm is a uniformed algorithm; in other words, algorithm blindly finds the shortest paths from each node to every node. This approach is time consuming and not optimal since our aim is to find just a path between start node to goal node. It is redundant to find paths that will not be used. Thus, there was a need to improve the performance of the program. The second approach was to implement the A-star Search Algorithm. As mentioned in the previous section, the A-star algorithm was a better choice in case of this problem if the implementation uses a admissible heuristic function. After the research, it was decided that the best possible heuristic function for this problem is Manhattan distance formula.

\subsection{Manhattan Distance}
Mathattan distance or also known as Taxicab geometry is a type of geometry in which the standardized Euclidean geometry distance function is replaced by a new metric in which the distance between the two points corresponds to the sum of the absolute differences between their Cartesian coordinates. \cite{manhattan-distance}\\

Formally, the Manhattan Distance $d_1$ between two vectors $\vec{p}$ and  $\vec{q}$ where $\vec{p} = (p_1, p_2, ...)$ and $\vec{q} = (q_1, q_2, ...) $ in $n$-dimensional vector space can be defined as:\\ \\
$d_1(\vec{p}, \vec{q}) = \lVert \vec{p} - \vec{q} \rVert_1 \sum_{i-1}^{n} | p_i - q_i |$.\\

For our problem, the map is a 2-D plane. Thus, the Manhattan distance between \textit{City A} $\vec{city_a} = ({city_a}_x, {city_a}_y)$  and \textit{City B} $\vec{city_b} = ({city_b}_x, {city_b}_y)$ is $|{city_a}_x - {city_b}_x| + |{city_a}_y - {city_b}_y|$.

\section{Implementation}
There were 3 main components for this problem and during the implementation phase, I have followed this order. These tasks were:
\begin{enumerate}
	\item Creating and collecting data
	\subitem Finding a map
	\subitem  Creating the data
	\item Deciding and implementing algorithm
	\subitem Heuristic Function
	\subitem Shortest-path Algorithm
	\item Creating the GUI
\end{enumerate}.
With the help of this ordered list, I did not face any difficulties and completed these tasks easily.
\subsection{Creating and collecting data}
In this part there were two tasks which are finding a map and creating the data. In first task, it was pretty easy to find a map that can be used. I have picked a Turkey map that has spotted the centers of provinces with circles, so that I can create same circle over it and use pixel-coordinates in the algorithm for distance calculation as well. In the second task, I have made an comprehensive research whether there is a ready-to-use data published in web. I could not find any useful resources. Thus, I have started to create my own data. I have created files for cities and the roads between the cities. In Turkey, there are 81 provinces. In \textit{cities} file, I have added the names of the provinces and their pixel-coordinates one-by-one. After that, I have researched the roads between neighbor cities. I have added them to the \textit{roads} file one-by-one as well.

\subsection{Deciding and implementing algorithm}
In this part, similar to the previous part, there were two tasks which are deciding and implementing heuristic function and shortest-path algorithm. I have made an comprehensive research while deciding the search algorithm. During the research, I have found out that A* search algorithm with the Manhattan distance function as the heuristics was the best and appropriate solution. The reasons behind the chosen heuristic function and shortest-path algorithm is already mentioned in Section \ref{introduction} and Section \ref{solution-design}. \\

\begin{algorithm}
	\caption{Manhattan Distance as Heuristic Function\\}\label{heuristic-pseudo}
	\begin{algorithmic}[1]
		\Function{heuristic}{$city_a, city_b$}
		\State $x_1 \leftarrow$ x position of $city_a$
		\State $y_1 \leftarrow$ y position of $city_a$
		\State $x_2 \leftarrow$ x position of $city_b$
		\State $y_2 \leftarrow$ y position of $city_b$\\
		\Return absolute of $(x_1 - x_2)$ + absolute of $(y_1 - y_2)$
		\EndFunction\\
	\end{algorithmic}
\end{algorithm}

In \textit{Algorithm \ref{heuristic-pseudo}},  pseudocode of the heuristic function could be found and in \textit{Algorithm \ref{astar-pseudo}},  pseudocode of the shortest-path could be found. I have made the implementation of this pseudocode in Python programming language. I have created classes for \textit{City}, \textit{Road}, and \textit{Map}.

\begin{algorithm}
	\caption{A* Search Path Finding}\label{astar-pseudo}
	\begin{algorithmic}[1]
	\Function {a\_star\_search} {$start\_city, end\_city$}
		\State$frontier \leftarrow$ empty Priority Queue
		\State$came\_from \leftarrow$ empty dictionary
		\State$cost\_so\_far \leftarrow$ empty dictionary
		\State add $(0, start\_city)$ tuple to the $frontier$
		\State$came\_from[start\_city] \leftarrow None $
		\State$cost\_so\_far[start\_city] \leftarrow 0$
		\While{$frontier$ \textbf{is not} empty}
			\State$current \leftarrow$ popped first element in $frontier$
			\If{$current$ \textbf{is the} $end\_city$}
				\State$current \leftarrow end\_city$
				\While{$current\_city$ \textbf{is not} None}
					\State add $current\_city$ to $path$
					\State$current\_city \leftarrow came\_from[current\_city]$
				\EndWhile
				\State $path \leftarrow$ reverse order the $path$ list
				\State \textbf{return} $path\, cost\_so\_far$
			\EndIf
			\For{$ next\_city$ \textbf{in the} current city's neighbors}
			\State$new\_cost \leftarrow cost\_so\_far[current] +$ cost of $next\_city$
				\If{$next\_city$ \textbf{not in} $cost\_so\_far$ \textbf{or}\\ $new\_cost < cost\_so\_far[next\_city]$}
					\State $cost\_so\_far \leftarrow new\_cost$
					\State $priority \leftarrow new\_cost$ \\$ + heuristic(next\_city, end\_city)$
					\State add $(priority, next\_city)$ tuple to the frontier
					\State$came\_from[next\_city] \leftarrow current$
				\EndIf
			\EndFor\\
		\EndWhile

		\Return $came\_from\, cost\_so\_far$
	\EndFunction
	\end{algorithmic}
\end{algorithm}
%\vspace{2cm}

\subsection{Creating the GUI}
Last part was to create a GUI for the user. As I mentioned previously, I have used Python programming language while implementing the shortest-path algorithm. Thus, I have chosen to use Tkinter library to create the GUI which I have already used in multiple projects.

\section{Testing and Bug-fixing}
Before implementing the GUI, I have passed 2 \textit{City} instances to the shortest-path method and check whether the result is true. After implementing the GUI, I have tested the program comprehensively. While checking the output is correct, I have checked and compared the path and final cost of the path which is an estimation of distance in terms of kilometers, with a real navigation app such as Google Maps and Yandex. Luckily, I have not faced with any issues during this process.

	\bibliographystyle{IEEEtran}
	\bibliography{ref.bib}
%%%%%%%%%%%%
% https://www.tutorialspoint.com/artificial_intelligence/artificial_intelligence_popular_search_algorithms.htm
% https://blog.goodaudience.com/solving-8-puzzle-using-a-algorithm-7b509c331288
%
%%%%%%%%%%%%
\end{document}


\chapter*{Conclusion}
\addcontentsline{toc}{chapter}{Conclusion}

In this work, the multi-instance paradigm was described using an algebraic formalism. Two methods for credit assignment in learning neural networks were presented - back-propagation and target propagation. The back-propagation algorithm is a well-explored and standard algorithm, whereas target propagation represents a more biologically plausible alternative. Target propagation isn't as well explored, but is backed by experimental results in computational neuroscience (see \cite{guerguiev_towards_2017}). The aim of this work was to apply the target propagation method to multi-instance problems.

A simple experiment was designed to compare the performance of back-propagation and target propagation. The results show that as it was designed and implemented, target propagation is not a viable alternative to back-propagation. While back-propagation based models produce reasonably useful models, the target propagation based models failed to solve the presented problem. \cite{bartunov_assessing_2018} show similar difficulties in using target propagation for harder problems, such as classification on the ImageNet dataset (see \cite{russakovsky_imagenet_2015}).

Due to the inferior performance of the target propagation models, their applicability to multi-instance learning was not further explored. Rather, a deeper inspection of the learning process of the models was concluded, with several hypotheses as to why the model behaves as it does. The poor performance of the model might be attributed to several factors, such as difficulties in learning auto-encoders on low-dimensional data, improper configuration of the model hyperparameters as there are many which could influence the results, or an intrinsic problem with the method itself. The data are not conclusive enough to single out a particular factor, although they point to problems in training the encoder network rather than the decoder network.

Further work in analysing biologically motivated learning algorithms is needed, particularly an evaluation of their mechanics and performance for complex problems such as the aforementioned ImageNet dataset. For target propagation in particular, an analysis of the influence of hyperparameter tuning may be a starting point. Also note that hyperparameter values needn't be constant. Annealing of hyperparameters such as the last layer target parameter \( \eta \) might produce some better results. Another direction may be the use of regularisation of layer parameters or layer outputs.