\chapter{Introduction}\label{ch01}
\section{Preface}
\subsection{Introduction to the Study of Digital Logic}

Digital logic is the study of how electronic devices make decisions. It functions at the lowest level of computer operations: bits that can either be ``on'' or ``off'' and groups of bits that form ``bytes'' and ``words'' that control physical devices. The language of digital logic is Boolean algebra, which is a mathematical model used to describe the logical function of a circuit; and that model can then be used to design the most efficient device possible. Finally, various simple devices, such as adders and registers, can be combined into increasingly complex circuits designed to accomplish advanced decision-making tasks.

\subsection{Introduction to the Author}

I have worked with computers and computer controlled systems for more than 30 years. I took my first programming class in 1976; and, several years later, was introduced to digital logic while taking classes to learn how to repair computer systems. For many years, my profession was to work on computer systems, both as a repair technician and a programmer, where I used the principles of digital logic daily. I then began teaching digital logic classes at Cochise College and was able to share my enthusiasm for the subject with Computer Information Systems students. Over the years, I have continued my studies of digital logic in order to improve my understanding of the topic; I also enjoy building logic circuits on a simulator to solve interesting challenges. It is my goal to make digital logic understandable and to also ignite a lifelong passion for the subject in students.

\subsection{Introduction to This Book}

This book has two goals:

1. \textsc{Audience}. Many, perhaps most, digital logic books are designed for third or fourth year electronics engineering or computer science students and presume a background that includes advanced mathematics and various engineering classes. For example, it is possible to find a digital logic book that discusses topics like physically building circuits from discrete components and then calculating the heat rise of those circuits while operating at maximum capacity. This book, though, was written for students in their second year of a Computer Information Systems program and makes no assumptions about prior mathematics and engineering classes.

2. \textsc{Cost}. Most digital logic books are priced at \$150 (and up) but this book is published under a Creative Commons license and, though only a tiny drop in the proverbial textbook ocean, is hoped to keep the cost of books for at least one class as low as possible.

Following are the features for the various editions of this book:

\begin{enumerate}
  \item 2012. Prior to 2012, handouts were given to students as they were needed during class; however, it was in this year that the numerous disparate documents were assembled into a cohesive book and printed by a professional printing company.
  \item 2013. A number of complex circuits were added to the book, including a Hamming Code generator/checker, which is used for error detection, and a \ac{CPU} using discrete logic gates.
  \item 2014. New material on Mealy and Moore State Machines was included, but the major change was in the laboratory exercises where five Verilog labs were added to the ten gate-level simulation labs.
  \item 2015. New information was added about adding/subtracting \ac{BCD} numbers and representing floating point numbers in binary form; and all of the laboratory exercises were re-written in Verilog. Also, the book was reorganized and converted to \LaTeX for printing.
  \item 2018. The labs were re-written using \textit{Logisim-evolution} because students find that system easier to understand than iVerilog.
\end{enumerate}

This book was written with \LaTeX \: using TeXstudio. The source for this book is available at GITHUB, \url{http://bit.ly/2w6qU2C}, and anyone is welcomed to fork the book and develop their own version.

% Add some vertical space
\medskip

% This is a "sidebar" box
\begin{tcolorbox}[colback=blue!5!white,colframe=blue!75!black]
  % Upper half of box: my "title" area
  \textcolor{blue}{DISCLAIMER}
  % Lower half of the box: the content
  \tcblower
  \textsf{I wrote, edited, illustrated, and published this book myself. While I did the best that I could, there are, no doubt, errors. I apologize in advance if anything presented here is factually erroneous; I'll correct those errors as soon as they are discovered. I'll also correct whatever typos I overlooked, despite TeXstudio's red squiggly underlines trying to tell me to check my work. --George Self}
\end{tcolorbox}

% Add some vertical space
\medskip

\subsection{About the Creative Commons License}

This book is being released under the Creative Commons 0 license, which is the same as public domain. That permits people to share, remix, or even rewrite the work so it can be used to help educate students wherever they are studying digital logic.

\section{About Digital Logic}

\subsection{Introduction}

Digital logic is the study of how logic is used in digital devices to complete tasks in fields as diverse as communication, business, space exploration, and medicine (not to mention everyday life). This definition has two main components: logic and digital. \emph{Logic} is the branch of philosophy that concerns making reasonable judgment based on sound principles of inference. It is a method of problem solving based on a linear, step-by-step procedure. \emph{Digital} is a system of mathematics where only two possible values exist: \emph{True} and \emph{False} (usually represented by 1 and 0). While this approach may seem limited, it actually works quite nicely in computer circuits where \emph{True} and \emph{False} can be easily represented by the presence or absence of voltage.

Digital logic is not the same as programming logic, though there is some relationship between them. A programmer would use the constructs of logic within a high-level language, like Java or C++, to get a computer to complete some task. On the other hand, an engineer would use digital logic with hardware devices to build a machine, like an alarm clock or calculator, which executes some task. In broad strokes, then, programming logic concerns writing software while digital logic concerns building hardware.

Digital logic may be divided into two broad classes:

\begin{enumerate}

  \item \textsc{Combinational Logic}, in which the outputs are determined solely by the input states at one particular moment with no memory of prior states. An example of a combinational circuit is a simple adder where the output is determined only by the values of two inputs.

  \item \textsc{Sequential Logic}, in which the outputs depend on both current and prior inputs, so some sort of memory is necessary. An example of a sequential circuit is a counter where a sensed new event is added to some total contained in memory.

\end{enumerate}

Both combinational and sequential logic are developed in this book, along with complex circuits that require both combinational and sequential logic.

\subsection{A Brief Electronics Primer}

Electricity is nothing more than the flow of electrons from point \emph{A} to point \emph{B}. Along the way, those electrons can be made to do work as they flow through various devices. Electronics is the science (and, occasionally, art) of using tiny quantities of electricity to do work. As an example, consider the circuit schematic illustrated in Figure \ref{IN:fig:simple_lamp_circuit}:

\begin{figure}[htb]
  \myfloatalign
  \begin{tikzpicture} [circuit logic US, scale=1.00]
  % make all path lines (the node shapes) a little thicker
  \tikzstyle{every path}=[line width=0.20mm]

  % Draw the figure
  \draw
    (0,0)
    to[battery,label=$B_1$] (0,2) % The voltage source
    to[short] (2,2)
    to[lamp,label=$Lmp_1$] (2,0) % The lamp
    to[ospst,label=$Sw_1$] (0.25,0) % The switch
    to[short] (0,0)
  ;
  \end{tikzpicture}
  \caption{Simple Lamp Circuit}
  \label{IN:fig:simple_lamp_circuit}
\end{figure}

In this diagram, battery \emph{B1} is connected to lamp \emph{Lmp1} through switch \emph{Sw1}. When the switch is closed, electrons will flow from the negative battery terminal, through the switch and lamp, and back to the positive battery terminal. As the electrons flow through the lamp's filament it will heat up and glow: light!

\begin{figure}[htb]
  \myfloatalign
  \begin{tikzpicture} [circuit logic US, scale=1.00]
  % make all path lines (the node shapes) a little thicker
  \tikzstyle{every path}=[line width=0.20mm]

  % Draw the figure
  \draw
    (4,1) node[npn,rotate=90,label=$Q_1$,anchor=E](npn) {}

    % From origin to switch to Q1 base
    (0,0)
    to[ospst,label=$Sw_1$] (2,0) % Switch
    to[short] (3.25,0)
    to (npn.base)

    (npn.emitter)
    to[short] (4.5,1)
    to[battery,l_=$B_1$] (4.5,3.5) % Battery
    to[short] (4.5,5)
    to[short] (0,5)
    to[resistor,l=$R_1$] (0,0) % Resistor
    to (0,0)

    % Internal loop from Q1 Collector to Lamp
    (npn.collector)
    to[short] (2,1)
    to[short] (2,3.5)
    to[lamp,label=$Lmp_1$] (4.25,3.5) % Lamp
    to(4.5,3.5)
  ;
  \end{tikzpicture}
  \caption{Transistor-Controlled Lamp Circuit}
  \label{IN:fig:xistor_lamp_circuit}
\end{figure}

A slightly more complex circuit is illustrated in Figure \ref{IN:fig:xistor_lamp_circuit}. In this case, a transistor, \emph{Q1}, has been added to the circuit. When switch \emph{Sw1} is closed, a tiny current can flow from the battery, through the transistor's emitter (the connection with the arrow) to its base, through the switch, through a resistor \emph{R1}, and back to the positive terminal of the battery. However, that small current turns on transistor \emph{Q1} so a much larger current can also flow from the battery, through the collector port, to lamp \emph{Lmp1}, and back to the battery. The final effect is the same for both circuits: close a switch and the lamp turns on. However, by using a transistor, the lamp can be controlled by applying any sort of positive voltage to the transistor's base; so the lamp could be controlled by a switch, as illustrated, or by the output of some other electronic process, like a photo-electric cell sensing that the room is dark.

Using various electronic components, like transistors and resistors, digital logic ``gates'' can be constructed, and these become the building blocks for complex logic circuits. Logic gates are more thoroughly covered in a later chapter, but one of the fundamental logic gates is an \textsf{OR} gate, and a simplified schematic diagram for an \textsf{OR} gate is in Figure \ref{IN:fig:xistor_or_gate}. In this circuit, any voltage present at \emph{Input A} or \emph{Input B} will activate the transistors and develop voltage at the output.

\begin{figure}[htb]
  \myfloatalign
  \begin{tikzpicture} [circuit logic US, scale=1.00]
  % make all path lines (the node shapes) a little thicker
  \tikzstyle{every path}=[line width=0.20mm]

  % Draw the figure
  \draw
    (2,3) node[npn,label={[label distance=0.4cm]120:$Q_1$},anchor=B](npn1) {}
    (2,1) node[npn,label={[label distance=0.4cm]120:$Q_2$},anchor=B](npn2) {}
    (3.75,0.225) node[cground](ground) {}
    (5.0,4.25) node[circ,label={0:out}] (out) {}
    (0.0,3) node[circ,label={180:A}] (in_a) {}
    (0.0,1) node[circ,label={180:B}] (in_b) {}

    % Ground to Q2 Emitter
    (ground)
    to (npn2.emitter)
    (0,1)
    to[R, l_=$R_2$] (2,1)
    (0,3)
    to[R, l_=$R_1$] (2,3)
    % Ground to Q1 Emitter
    (ground)
    to[short] (3.75,2.225)
    to (npn1.emitter)
    % Q2 collector to short
    (npn2.collector)
    to[short] (4.25,1.75)
    to[short] (4.25,3.75)
    % Q1 collector to R3
    (npn1.collector)
    to[short] (4.25,3.75)
    to[R, l_=$R_3$] (4.25,6.5)
    % short to out
    (4.25,4.25)
    to (out)
  ;
  \end{tikzpicture}
  \caption{Simple OR Gate Using Transistors}
  \label{IN:fig:xistor_or_gate}
\end{figure}

Figure \ref{IN:fig:nmos_or_gate}\footnote{This schematic diagram was created by Ram\'{o}n Jaramillo and found at \url{http://www.texample.net/tikz/examples/or-gate/}} shows a more complete \textsf{OR} gate created with what are called ``N-Channel metal-oxide semiconductor'' (or \emph{nMOS}) transistors. The electronic functioning of this circuit is beyond the scope of this book (and is in the domain of electronics engineers), but the important point to keep in mind is that this book concerns building electronic circuits designed to accomplish a physical task rather than write a program to control a computer's processes.

\begin{figure}[bth]
  \myfloatalign
  % Title: OR gate
  % Author: Ramón Jaramillo.
  \begin{tikzpicture}[scale=2]
  \draw[color=black, thick]
  % Name of MOSFET transistors left to right
  % Left to right in baseline: nmos1, nmos2, nmos3, nmo4 & nmos5.
  % Drawing the transistor and nodes using relative coordinates
  (0,0) node[nigfete] (nmos1) {}
  (1,0) node[nigfete] (nmos2) {}
  (nmos1.S) to (nmos2.S)
  % Connections to V_DD
  to [short,*-o] ++(0,-0.5) {} node[below=2mm] {$V_{DD}$}
  (1,1) node[pfet,yscale=-1] (pmos1) {} % yscale=-1 is mandatory to drawing
  % MOSFETs with the right sense.
  (1,2) node[pigfete,yscale=-1] (pmos2) {}
  (nmos2.G) to [short,-*] (pmos1.G)
  (pmos1.D) to (nmos2.D)
  (nmos1.G) -- ++(0,1.5) -- ++(0,0.75)
  (pmos2.G) to [short,-*] ++(-1,0){}
  to [short,-o] ++(-0.5,0) {} node[above=3mm] {$A$}
  (pmos1.G) to [short,-o] ++(-1.5,0) {} node[above=3mm] {$B$}
  (pmos1.B) -- ++(0.2,0) -| ++(0,1.15) to [short,-*]++(-0.21,0)
  (pmos1.S) -- (pmos2.D) {}
  % Inverter
  (2,0) node[nigfete](nmos3) {}
  (2,1) node[pigfete, yscale=-1] (pmos3) {}
  (nmos3.G) to (pmos3.G)
  (nmos3.D) to (pmos3.D)
  % Gate OR Output
  (3,0) node[nigfete](nmos4){}
  (3,1) node[pigfete, yscale=-1] (pmos4) {}
  (nmos4.G) to (pmos4.G)
  (nmos4.D) to (pmos4.D)
  %%%
  (4,0) node[nigfete] (nmos5) {}
  (4,1) node[pigfete, yscale=-1] (pmos5) {}
  (nmos5.G) to (pmos5.G)
  (nmos5.D) to (pmos5.D)
  % Connection to V_DD
  (nmos3.S) to [short,-*] (nmos4.S) -- (nmos5.S)
  to [short,*-o] ++(0,-0.5) {} node[below=2mm] {$V_{DD}$}
  % Connection to V_DD
  (pmos3.S) -- (pmos4.S) -- (pmos5.S)
  to [short,*-o] ++(0,1.5) {} node[above=2mm] {$V_{SS}$}
  (pmos2.S) to [short,-o] ++(0,0.5) {} node[above=2mm] {$V_{SS}$}
  %(3,-0.4) node[circ] {}
  % Connections between nodes
  (nmos1.D) -| (0,0.5) to [short,-o] (1,0.5) {}
  (1,0.5) to  [short,*-*] ++(0.5,0) {}
  (2,0.5) to  [short,*-*] ++(0.5,0) {}
  (3,0.5) to  [short,*-*] ++(0.5,0) {}
  (4,0.5) to  [short,*-o] ++(0.5,0) {} node[right=3mm] {$C=A+B$}
  ;
  \end{tikzpicture}
  \caption{OR Gate}
  \label{IN:fig:nmos_or_gate}
\end{figure}

Early electronic switches took the form of vacuum tubes, but those were replaced by transistors which were much more energy efficient and physically smaller. Eventually, entire transistorized circuits, like the \textsf{OR} gate illustrated in Figure \ref{IN:fig:nmos_or_gate}, were miniaturized and placed in a single \ac{IC}, sometimes called a ``chip,'' smaller than a postage stamp.

\acp{IC} make it possible to produce smaller, faster, and more powerful electronics devices. For example, the original ENIAC computer, built in 1946, occupied more than 1800 square feet of floor space and required 150KW of electricity, but by the 1980s integrated circuits in hand-held calculators were more powerful than that early computer. Integrated circuits are often divided into four classes:

\begin{enumerate}
  \item Small-scale integration with fewer than 10 transistors
  \item Medium-scale integration with 10-500 transistors
  \item Large-scale integration with 500-20,000 transistors
  \item Very large-scale integration with 20,000-1,000,000 transistors
\end{enumerate}

Integrated circuits are designed by engineers who use software written specifically for that purpose. While the intent of this book is to afford students a foundation in digital logic, those who pursue a degree in electronics engineering, software engineering, or some related field, will need to study a digital logic language, like iVerilog.

\section{Boolean Algebra}

\subsection{History}

The Greek philosopher Aristotle founded a system of logic based on only two types of propositions: \emph{True} and \emph{False}. His bivalent (two-mode) definition of truth led to four foundational laws of logic: the Law of Identity (\emph{A is A}); the Law of Non-contradiction (\emph{A is not non-A}); the Law of the Excluded Middle (\emph{either A or non-A}); and the Law of Rational Inference. These laws function within the scope of logic where a proposition is limited to one of two possible values, like \emph{True} and \emph{False}; but they do not apply in cases where propositions can hold other values.

The English mathematician George Boole (1815-1864) sought to give symbolic form to Aristotle's system of logic. Boole wrote a treatise on the subject in 1854, titled \emph{An Investigation of the Laws of Thought, on Which Are Founded the Mathematical Theories of Logic and Probabilities}, which codified several rules of relationship between mathematical quantities limited to one of two possible values: \emph{True} or \emph{False}, $ 1 $ or $ 0 $. His mathematical system became known as \emph{Boolean Algebra}.

All arithmetic operations performed with Boolean quantities have but one of two possible outcomes: $ 1 $ or $ 0 $. There is no such thing as $ 2 $ or $ -1 $ or $ \frac{1}{3} $ in the Boolean world and numbers other than $ 1 $ and $ 0 $ are invalid by definition. Claude Shannon of MIT recognized how Boolean algebra could be applied to on-and-off circuits, where all signals are characterized as either \emph{high} ($ 1 $) or \emph{low} ($ 0 $), and his 1938 thesis, titled \emph{A Symbolic Analysis of Relay and Switching Circuits}, put Boole's theoretical work to use in land line telephone switching, a system Boole never could have imagined.

While there are a number of similarities between Boolean algebra and real-number algebra, it is important to bear in mind that the system of numbers defining Boolean algebra is severely limited in scope: $ 1 $ and $ 0 $. Consequently, the ``Laws'' of Boolean algebra often differ from the ``Laws'' of real-number algebra, making possible such Boolean statements as $ 1 + 1 = 1 $, which would be considered absurd for real-number algebra.

\subsection{Boolean Equations}

Boolean algebra is a mathematical system that defines a series of logical operations performed on a set of variables. The expression of a single logical function is a \emph{Boolean Equation} \marginpar{Sometimes Boolean Equations are called \textsc{Switching Equations}} that uses standardized symbols and rules. Boolean expressions are the foundation of digital circuits.

Binary variables used in Boolean algebra are like variables in regular algebra except that they can only have two values: one or zero. Boolean algebra includes three primary logical functions: \textsf{AND}, \textsf{OR}, and \textsf{NOT}; and five secondary logical functions: \marginpar{XNOR is sometimes called \textsc{equivalence} and Buffer is sometimes called \textsc{transfer}.} \textsf{NAND}, \textsf{NOR}, \textsf{XOR}, and \textsf{XNOR}, and Buffer. A Boolean equation defines an electronic circuit that provides a relationship between input and output variables and takes the form of:

\begin{align}
\label{01:eq:simple_boolean_equation}
C &= A * B
\end{align}

where \emph{A} and \emph{B} are binary input variables that are related to the output variable \emph{C} by the function \textsf{AND} (denoted by an asterisk).

In Boolean algebra, it is common to speak of \emph{truth}. This term does not mean the same as it would to a philosopher, though its use is based on Aristotelian philosophy where a statement was either \emph{True} or \emph{False}. In Boolean algebra as used in electronics, \emph{True} commonly means ``voltage present'' (or ``$ 1 $'') while \emph{False} commonly means ``voltage absent'' (or ``$ 0 $''), and this can be applied to either input or output variables. It is common to create a \emph{Truth Table} for a Boolean equation to indicate which combination of inputs should evaluate to a \emph{True} output and Truth Tables are used very frequently throughout this book.

\section{About This Book}

This book is organized into two main parts:

\begin{enumerate}
  \item \textsc{Theory}. Chapters two through six cover the foundational theory of digital logic. Included are chapters on binary mathematics, Boolean algebra, and simplifying Boolean expressions using tools like Karnaugh maps and the Quine-McCluskey method.

  \item \textsc{Practice}. Chapters seven through nine expand the theory of digital logic into practical applications. Covered are combinational and sequential logic circuits, and then simulation of various physical devices, like elevators.
\end{enumerate}

There is also an accompanying lab manual where \textit{Logisim-evolution} is used to build digital logic circuits. By combining the theory presented in this book along with the practical application presented in the lab manual it is hoped that students gain a thorough understanding of digital logic.

By combining the theoretical background of binary mathematics and Boolean algebra with the practical application of building logic devices, digital logic becomes understandable and useful.


\documentclass[twocolumn,letterpaper]{article}

\usepackage{url}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[margin=1in]{geometry}
\usepackage{gensymb}
\usepackage{lipsum}

%opening
\title{NeuroEvolution of Augmenting Topologies}
\author{Nathan Zabriskie}

\begin{document}

\maketitle

\section{Introduction} \label{sec:intro}
Genetic algorithms learn by heuristically sampling possible solutions to some problem, assigning a fitness
score to each solution based on its performance, and then combining and tweaking the most
``fit'' solutions to create more samples for the next step of the simulation. Traditionally,
genetic algorithms would operate on a fixed network topology specified by the user, with each
genome representing some possible weights for connections within the network. Although
researchers made some early attempts to develop genetic algorithms that evolved topologies along with
weights (called Topology and Weight Evolving Artificial Neural Networks or TWEANNs),
these algorithms often suffered from a number of problems including information loss due to competing
conventions and overly penalizing new, larger topologies.

In \textit{Evolving Neural Networks through Augmenting Topologies} \cite{neat_paper} Stanley and
Miikkulainen present their algorithm NeuroEvolution of Augmenting Topologies (NEAT) which addresses some of
these problems. This paper examines the contributions of NEAT to genetic learning and evaluates its
performance on a number of tasks.

\section{NEAT Overview} \label{sec:overview}
As mentioned above, early TWEANN models often suffered from information loss during the crossover phase
at the end of each generation due to what Stanley and Miikulainen term the competing conventions problem.
When two genomes contain hidden nodes that encode the same information, if their hidden nodes are stored
in different orders their offspring will invariably lose the capabilities of at least one of the
hidden nodes as shown in figure \ref{fig:crossover_loss}.

NEAT addresses this problem by assigning each connection within the network an innovation number or ID based on
when the connection first appears. Each time a connection is added during the offspring creation phase at the end of a generation it is compared with other connections that have appeared in the same generation.
If a matching connection is found then the connection is assigned the same innovation number as the earlier
connection, otherwise it is given a new, unique number. In future generations when performing crossover,
connections are aligned according to their innovation numbers which helps prevent loss due to competing
conventions.

\begin{figure}[h]
	\centering
	\includegraphics[width=0.3\textwidth]{images/crossover.png}
	\caption{Information loss due to crossover.}
	\label{fig:crossover_loss}
\end{figure}

Tracking connections in this way also helps solve another problem in TWEANN's, the penalizing of new
developments. In order to encourage the development of simple networks many TWEANN algorithms add a penalty
term to fitness calculations based on the number of nodes in a network. Although this will in theory keep
networks small, it can have some unintended side effects. Often when a new node is added to a network
it takes some number of generations before the node develops any useful function and contributes to the
genome's fitness. If a genome with a new node is overly penalized just for having an extra node it might be
eliminated from the population before it has a chance to develop thus preventing what could potentially be a
useful new topology.
\begin{figure}[h]
	\centering
	\includegraphics[width=0.5\textwidth]{images/speciation.png}
	\caption{Aligning genomes by innovation number simplifies the counting of differing genes.}
	\label{fig:speciation}
\end{figure}
NEAT solves this problem by organizing genomes into species based on how similar each genome is to each other.
Although NEAT is not the first algorithm to speciate genomes, the tracking of innovation numbers makes
calculating the distance between genomes trivial. Again connection genes within each genome are simply
aligned according to their innovation numbers and the number of differing genes are counted as shown in figure \ref{fig:speciation}. Then the distance
$\delta$ between the two genomes is calculated by
\begin{equation}
\delta = \frac{c_1E}{N}\ + \frac{c_2D}{N}\ + c_3\overline{W}
\end{equation}
where $E$ is the number of excess genes (genes in one genome that have higher IDs than the maximum
ID in the other genome), $D$ is the number of disjoint genes, $\overline{W}$ is the average weight
difference between matching genes, and $c_1$, $c_2$, and $c_3$ are user-defined constants. Genomes with
$\delta$ smaller than some value are put into the same species and compete only within that species. If a
species does not improve its fitness for a number of generations then it is eliminated. By protecting new
innovations in this way, new topologies are given time to develop but are eventually trimmed out if they
fail to produce positive results.

\section{Experiments} \label{sec:experiments}
Although genetic algorithms can be used to do supervised learning, their performance often lags behind
more mathematically rigorous methods such as simple back-propagation. After ensuring that my implementation of NEAT worked correctly, I tested my model on several classic datasets such as the iris and diabetes datasets but I was very unimpressed by the
results. Although the final accuracies on these problems was comparable to standard neural networks, NEAT was
much slower to train and very sensitive to overfit. I eventually moved on to reinforcement learning tasks
because as detailed in \cite{whitley}, genetic algorithms excel in situations where gradients are difficult to
calculate and standard gradient-descent algorithms break down. As the reinforcement learning tasks better
showcase the strengths of this model this section will primarily deal with those experiments. NOTE: In all
network diagrams green nodes are inputs, blue nodes are outputs, black edges represent forward connections,
red edges represent recurrent connections, and dashed lines represent disabled connections that have no
effect.

\subsection{XOR Verification} \label{sec:xor}
I used the XOR task to ensure that my NEAT implementation worked properly as Stanley and
Miikkulainen explicitly mention it in their paper as a good test problem. For this experiment I
initialized a population of 150 genomes with 2 input nodes and 1 output node. Each genome received
a fitness calculated by the formula
\begin{equation}
fitness = (4-\sum_{n} \left|t_n-z_n\right|)^2
\end{equation}
where $t_n$ is the desired output for input \textit{n} and $z_n$ is the actual output for each of the 4
possible inputs. After I discovered and fixed several bugs the system performed quite well on this task.
In no run did it fail to find a solution and over 10 runs the final network had an average of 2.6 hidden
nodes where 1 is optimal. Although it did not find the optimal solution in every run it did develop a
network with 1 hidden node in $\approx20\%$ of runs.

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.2\textwidth]{images/xor_end.png}
	\caption{An example XOR solution}
	\label{fig:xor}
\end{figure}

\subsection{Flappy Bird} \label{sec:flappy}
After I was satisfied that my system was working correctly, I created a reinforcement learning environment by hooking my NEAT implementation up to the Bizhawk
Nintendo Entertainment System emulator and had it learn to play the game Flappy Bird. In Flappy Bird the player is tasked with guiding a bird through a series of vertical gaps by having the bird
flap its wings to gain height. This sounds simple enough but it turns out to be a very difficult game for a
human player due to the unforgiving collision detection and narrow gaps.

\begin{figure}[t]
	\centering
	\begin{subfigure}[b]{0.45\textwidth}
		\includegraphics[width=\textwidth]{images/flappy.png}
		\caption{A frame from the Flappy Bird simulation}
	\end{subfigure}
	~
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{images/flappy_network.png}
		\caption{An example solution network}
		\label{fig:flappy_network}
	\end{subfigure}
	\caption{Flappy Bird reinforcement learning task}
	\label{fig:flappy}
\end{figure}

Unfortunately, Bizhawk
can only interact with Lua scripts whereas I implemented NEAT in Python. To communicate between these
platforms I created a server that runs NEAT in a Python environment and receives commands over a socket
connection. The Lua client simply sends commands to the server to perform tasks like ending a generation, getting a genome's output,
or assigning a fitness score to a genome. Although this adds some undesired overhead, this setup was more than fast enough to handle all the commands necessary to run the emulator at full speed and quickly iterate
through generations.

 In order to get any meaningful
information out of the emulator it is necessary to read values out of the game's RAM which I had not had
previous experience with. Luckily I was able to determine how to get the current height of the bird and
the height of the next upcoming gap so these two values served as the inputs to the system. Each genome
had to take these two inputs and then return whether the bird should flap its wings or stay idle. Genomes
received a fitness based on how many frames they survived plus a bonus for each gap they successfully
navigated.

Using only these two inputs NEAT created an agent that could play the game indefinitely in an average of
11.3 generations. As shown in figure \ref{fig:flappy_network} the networks that evolved were not complicated
but they performed very well. While watching the agents play the game I noticed that a large number of the
agents in early generations would press the flap button at every possible opportunity, quickly flying off the top of the
screen. This prevents them from crashing into the ground immediately but also prevents them from ever clearing
one of the vertical gaps. To discourage this behavior I eventually added a penalty term to the fitness
calculation for every flap taken. This penalty helped focus the efforts of the genomes and decreased the
average number of generations required to 4.6.

\subsection{Cart Pole (Pole Balacning)}  \label{sec:pole}
Since Flappy Bird ended up being a little on the easy side, I moved on to what I hoped would be a more
difficult reinforcement learning task. As described in \cite{cartpole}, the pole balancing problem is a classic control task
in which systems must balance an inverted pendulum on a cart by pushing the cart left or right. The original
formulation of the problem provides the agent with 4 observations at each time step: the position of the
cart, the velocity of the cart, the angle of the pole, and the velocity of the tip of the pole.

\begin{figure}[h]
	\centering
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{images/pole_balance.png}
		\caption{Example frame of the cart pole simulation.}
	\end{subfigure}
	~
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{images/cart_pole_final.png}
		\caption{An example solution.}
		\label{fig:cart_solution}
	\end{subfigure}
	\caption{The pole balancing problem with withheld velocity}
	\label{fig:cartpole}
\end{figure}

At each step
of the simulation the agent is provided with the 4 observations and must return its chosen action, to push
the cart left or to push the cart right. In the Python OpenAI Gym \cite{gym} the simulation ends when the
pole angle is $> 20.2^{\circ}$ from vertical or the cart is $> 2.4$ units from the center of the track. For each
frame the agent survives, its fitness increases by one point. The problem is considered solved when an agent
receives $> 475$ fitness averaged over 100 runs.

Using all 4 observations, NEAT solved the problem using the initial population in $\approx80\%$ of runs, meaning
that the system never had to change a single genome in order to achieve the required fitness. This is not
particularly interesting so I decided to increase the challenge of the problem by only providing each network
with the positions of the cart and pole, withholding the velocity. Under these conditions the system must
evolve recurrent connections in addition to the normal forward connections in order to recover the missing
information. Even with the missing information NEAT handled the problem very nicely. Over 10 runs it solved
the problem in an average of 109 generations. Figure \ref{fig:cart_solution} shows a particularly
appealing solution with only one hidden node. Although it's difficult to determine exactly what each
connection represents, the two active recurrent connections indicate that the system retains some history
of previous positions in order to reconstruct the velocity.

\section{Modifications} \label{sec:mods}
In order to better understand the reasoning behind some design decisions in the original NEAT algorithm I made
several modifications to the base system.

\subsection{Dynamic Speciation Threshold} \label{sec:speciation}
While experimenting with the NEAT algorithm I was often frustrated by how sensitive it was to the changes
in its hyper parameters. Often it would take many runs to find parameters that allowed the system to find a
solution and any small perturbations to these parameters would drastically influence run times. One
particularly sensitive parameter was the distance threshold for $\delta$ that determines whether two genomes
are in the same species or not. If this threshold was too low then the number of species would explode. If
it was too high then there would only be one species and the system would not have enough diversity.

\begin{figure}[h]
	\centering
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{images/no_adjust.png}
		\caption{Species over time with no dynamic threshold}
		\label{fig:no_adjust}
	\end{subfigure}
	~
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{images/with_adjust.png}
		\caption{Species over time with dynamic threshold. The target number of species is marked with a red line}
		\label{fig:with_adjust}
	\end{subfigure}
	\caption{Effects of dynamic $\delta$ threshold}
	\label{fig:speciation_adjustment}
\end{figure}

While searching for how other people dealt with this problem I came across \cite{neat_web}, a website created
by one of the original paper authors, that has some helpful information about how to improve stability in
NEAT. One item suggested making the $\delta$ threshold dynamic in order to maintain a target number of
species. Making this addition proved very useful. With this change in place I could simply set the threshold to
a reasonable value and let the system run without needing to repeatedly tweak the threshold to find one that led to a solution. It is worth mentioning that although figure \ref{fig:no_adjust} and
\ref{fig:with_adjust} look similar, this is mostly due to the fact that figure \ref{fig:no_adjust} was
generated only after testing many threshold values to find one that allows the system to find a solution
whereas \ref{fig:with_adjust} was generated in one shot.

\subsection{Innovation Tracking} \label{sec:innovations}
During my initial reading of \cite{neat_paper} I found it odd that innovation numbers were only shared among
connections created during the current generation instead of among all connections in every network. It seemed to me that by always giving connections that connected the same nodes the same ID you could decrease the
number of connections present in the final solution. To test this hypothesis I ran the pole balancing problem
5 times with the default ID sharing strategy and 5 times with the IDs shared across all connections
regardless of generation. The results are summarized in table \ref{tab:tracking}.

% Please add the following required packages to your document preamble:
% \usepackage{graphicx}
\begin{table}[]
	\centering
	\resizebox{0.45\textwidth}{!}{%
		\begin{tabular}{|r|c|c|c|}
			\hline
			&  & Per Generation & Per Run \\ \hline
			Generations & Mean & 94.8 & 124.0 \\ \cline{2-4}
			& SD & 40.16 & 37.89 \\ \hline
			Average No. of Hidden Nodes & Mean & 4.6 & 5.2 \\ \cline{2-4}
			& SD & 1.14 & 3.19 \\ \hline
			No. of Connections & Mean & 15.8 & 13.8 \\ \cline{2-4}
			& SD & 5.11 & 6.41 \\ \hline
		\end{tabular}%
	}
	\caption{Effects of sharing IDs within each generation compared to each run.}
	\label{tab:tracking}
\end{table}

Overall the change in ID tracking seemed to make the algorithm perform worse overall. On average it took significantly more generations to find a solution. Although on average it did decrease the number of
connections present in the final solution, it also greatly increased the variability in how many hidden
nodes were present in the solution, meaning that solutions were less consistent. I assume these decreases in performance are the result of unrelated connections sharing the same ID. Even though two connections might
connect the same two nodes, the time at which they are introduced into the system is an important part of
their identity. A connection between nodes A and B in generation 1 probably has a different meaning than a
connection between the same nodes added in generation 100 as the rest of the network has changed. Forcing
these connections to share the same ID essentially reintroduces the competing conventions problem which NEAT
tries to avoid in the first place.

\section{Conclusion} \label{sec:conclusion}
NEAT provides several useful extensions to both the basic genetic algorithm and TWEANN algorithms. By
evolving networks starting from a minimal solution, NEAT frees the user from having to make any decisions
about the topology of the network before execution begins. In addition, because it starts from minimal networks, NEAT tends to create minimal solutions which can be desirable in many contexts. Although, like many genetic algorithms, NEAT is not as ideal as standard gradient-descent algorithms for
normal supervised learning tasks, it excels in reinforcement learning environments.

This model does suffer from a high sensitivity to choice of hyper parameters but this sensitivity can be
somewhat lessened by making some parameters dynamic. These changes make the algorithm even easier to use as
the user can more fully trust the algorithm not to explode.

If I were to continue working with this model I would like to explore more ways to decrease the
importance of hyper parameters and increase stability of the algorithm. I would also like to further optimize NEAT for use in
supervised learning by experimenting with ways to reduce its tendency to overfit, possibly using dropout or
other regularization techniques. Using the Bizhawk framework I built up I would also like to have NEAT learn to play more complicated games to push it to its limit. It would also be interesting to run NEAT
using the output of a CNN as input so that it could potentially utilize information from raw pixel values
rather than pulling data straight out of RAM.

\medskip
\bibliographystyle{unsrt}
\bibliography{ref}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{indentfirst}

\title{SICP Exercise 1-6}
\author{Samuel Lair}
\date{January 19, 2020}

\begin{document}

\maketitle

\section{Solution}

This exercise is equivalent to asking what will happen when Scheme interprets the following program:

\begin{verbatim}
        (define (average x y)
            (/ (+ x y) 2))
        (define (improve guess x)
            (average guess (/ x guess))
        (define (square x) (* x x))
        (define (abs x)
            (cond ((< x 0) (- x))
                  (else x)))
        (define (good-enough? guess x)
            (< (abs (- (square guess) x)) 0.001)
        (define (new-if predicate then-clause else-clause)
            (cond (predicate then-clause)
                (else else-clause)))
        (define (sqrt-iter guess x)
            (new-if (good-enough? guess x)
                guess
                (sqrt-iter (improve guess x)
                           x)))
        (sqrt-iter 1 2)
\end{verbatim}

Scheme uses applicative-order evaluation.  The built-in if is a special form where only the branch chosen by the value of the predicate is evaluated.  However, new-if is an operator.  Therefore, both of the operands will evaluated regardless of the value of the predicate.

This poses a problem because sqrt-iter is a recursive operator.  Even when the base case is satisfied (i.e. the guess is good-enough), the Scheme interpeter will still evaluate yet another sqrt-iter combination.  Therefore, the Scheme interpreter is stuck in an infinite loop when it evaluates (sqrt-iter 1 2).

However, this problem is solved when if is used instead of new-if.  In this version of the program, when the guess is good-enough, guess is evaluated without evaluating the recursive combination.
\end{document}


\documentclass[11pt,a4paper]{article}

% These are extra packages that you might need for writing the equations:
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{xcolor}
\lstset {language=C++,
		 basicstyle=\ttfamily,
         keywordstyle=\color{blue}\ttfamily,
         stringstyle=\color{red}\ttfamily,
         commentstyle=\color{purple}\ttfamily,
         morecomment=[l][\color{magenta}]{\#},
       	 basicstyle=\tiny}

% You need the following package in order to include figures in your report:
\usepackage{graphicx}

% With this package you can set the size of the margins manually:
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}

% Enter the exercise number, your name and date here:
\noindent\parbox{\linewidth}{
 \parbox{.25\linewidth}{ \large ICP, Exercise 02 }\hfill
 \parbox{.5\linewidth}{\begin{center} \large Beat Hubmann \end{center}}\hfill
 \parbox{.2\linewidth}{\begin{flushright} \large Oct 8, 2018 \end{flushright}}
}
\noindent\rule{\linewidth}{2pt}

\section{Introduction}

The burning method algorithm to study percolation was implemented. A series experiments on square site lattices of different sizes was conducted using the algorithm.

\section{Algorithm Description}
The algorithm was implemented as described in the course lecture notes~\cite{herrmann}~(page 25).

\section{Results}

\subsection{Task 1}

The program was implemented as instructed. A sample square site lattice with size $100 \times 100$ and occupation probability $p = 0.5$ is shown in figure~\ref{fig1}.

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.2]{task1.eps}
\end{center}
\caption{Sample square site lattice with side length $N = 100$ and occupation probability $p=0.5$.}
\label{fig1}
\end{figure}

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.2]{task2_002.eps}
\end{center}
\caption{Sample square site lattice with side length $N = 100$ and occupation probability $p=0.6$: Forest fire simulation at time step $t=2$.}
\label{fig2_1}
\end{figure}

\subsection{Task 2}
Based on the code from task 1, the program was implemented without issues. Snapshots from different time steps are shown in figure~\ref{fig2_1} and in figure~\ref{fig2_2}.

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.2]{task2_100.eps}
\end{center}
\caption{Sample square site lattice with side length $N = 100$ and occupation probability $p=0.6$: Forest fire simulation at time step $t=100$.}
\label{fig2_2}
\end{figure}

\subsection{Task 3}
The code was implemented and 1000 measurements were taken for each occupation probability $p \in \{0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1\}$ for different site lattice side lengths $N \in \{10, 25, 50, 100, 250, 500, 1000\}$. To populate the lattices, the Mersenne Twister pseudorandom number generator from the \texttt{C++} standard library was used. The seeds were different for each of the 1000 measurements in each series. The figures show the obtained results for each series' order~parameter~\ref{fig3_1}, average fire duration~\ref{fig3_2} and average shortest path length~\ref{fig3_3}.

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.9]{figure3_1.eps}
\end{center}
\caption{Measured order parameters for different site grid side lengths $N$ and initial site occupation probability $p$.}
\label{fig3_1}
\end{figure}

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.9]{figure3_2.eps}
\end{center}
\caption{Measured average fire duration for different site grid side lengths $N$ and initial site occupation probability $p$.}
\label{fig3_2}
\end{figure}

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.9]{figure3_3.eps}
\end{center}
\caption{Measured shortest path length for different site grid side lengths $N$ and initial site occupation probability $p$.}
\label{fig3_3}
\end{figure}

\section{Discussion}
The results were in line with the theoretical expectations from class. The algorithm as such already scales badly in $N$ by design; writing (sequential) \texttt{.ppm} output files makes things a lot worse. Drawing the lattices for output was therefore skipped in task 3. Additionally, I designed my main function to use a single loop for which parallelisation with \texttt{OpenMP} helped increasingly with growing $N$.

\begin{thebibliography}{99}

\bibitem{herrmann}
	Herrmann, H. J.,\\
	Singer, H. M.,\\
	Mueller L.,\\
	Buchmann, M.-A.,\\
	\emph{Introduction to Computational Physics - Lecture Notes},\\
	ETH Zurich,\\
	2017.
\end{thebibliography}

\end{document}


\section{Assembling Long (454) Read}

\begin{note}
The data you will examine in this exercise is again from Staphylococcus aureus
which has a genome of around 3MB. The reads are 454 single end.

The required data can be downloaded from the SRA. Specifically, the run data
(SRR000892, SRR000893) from the SRA Experiment SRX000181.

\center{\url{http://www.ebi.ac.uk/ena/data/view/SRX000181}}
\end{note}

\begin{information}
The following exercise focuses on processing 454 long reads with velvet and how
this differs compared to short reads.
\end{information}

\begin{steps}
First move to the directory you made for this exercise and make a suitable named
directory for the exercise before downloading the read files:
\begin{lstlisting}
cd ~/NGS/velvet/part3
mkdir SRX000181
cd SRX000181
\end{lstlisting}

The downloaded files can be used directly in velvet. To let velvet know that
these FASTQ files are long reads, you pass in the parameter \texttt{-long} by
using the commands:
\begin{lstlisting}
ln -s ~/NGS/Data/SRR000892.fastq.gz
ln -s ~/NGS/Data/SRR000893.fastq.gz
velveth run_25 25 -create_binary -fastq.gz -long *.fastq.gz
time velvetg run_25
\end{lstlisting}
\end{steps}

\begin{questions}
Take a look at the \texttt{stats.txt} file. Which columns are used compared to
short reads and why?
\begin{answer}
long\_cov instead of e.g. short1\_cov
\end{answer}

Which N50 do you get?
\begin{answer}
28 (k-mer N50)
\end{answer}

How long did the \texttt{velvetg} run take?
\begin{answer}
\texttt{real    0m36.333s; user    2m33.238s; sys     0m3.893s}
\end{answer}
\end{questions}

\begin{steps}
The right thing to do is to run \texttt{velvetg} setting the cut-offs. But for long reads
there is an option called \texttt{-long\_cov\_cutoff} to filter them
independently because of the difference in usage in velvet. To investigate with
R, as you did in the previous exercises, start up R and produce the weighted
histogram using the column \texttt{long\_cov} by typing:
\begin{lstlisting}[style=R]
R --no-save
library(plotrix)
data <- read.table("run_25/stats.txt", header=TRUE)
weighted.hist(data$long_cov, data$lgth, breaks=0:50)
\end{lstlisting}

\begin{figure}[H]
\centering
\includegraphics[width=0.8\textwidth]{de_novo/velvet/velvet_Rplot003.png}
\caption{\label{fig:velvetRplot003}}
\end{figure}

For me the histogram suggests to me to choose a coverage cut-off of around 9
with an expected coverage of about 19.

If you disagree, feel free to try different settings, but first leave R before
running \texttt{velvetg}:
\begin{lstlisting}[style=R]
q()
\end{lstlisting}

\begin{lstlisting}
cp run_25/contigs.fa run_25/contigs.fa.0

time velvetg run_25 -long_cov_cutoff 9
cp run_25/contigs.fa run_25/contigs.fa.1

time velvetg run_25 -long_cov_cutoff 9 -exp_cov 19
cp run_25/contigs.fa run_25/contigs.fa.2

gnx -min 100 -nx 25,50,75 run_25/contigs.fa*
\end{lstlisting}

\end{steps}

\begin{questions}
What is the N50 and runtime using:\\
\texttt{-long\_cov\_cutoff 9}
\begin{answer}
\texttt{5,747; real  0m11.457s; user  0m10.855s; sys  0m0.329s}
\end{answer}

\texttt{-long\_cov\_cutoff 9 -exp\_cov 19}
\begin{answer}
\texttt{16,781; real 0m29.830s; user 2m32.530s; sys 0m3.523s}
\end{answer}

other runs?

Which other parameters could improve the assembly quality for long reads?
\begin{answer}
\texttt{-conserveLong}
\end{answer}

What do you think about assembling 454 reads with Velvet?
\begin{answer}
It's working!
\end{answer}
\end{questions}


\section{Spur \& Helical Gear Design}

\newthought{Now we have selected our gear ratio} and level of damping within our system, we can start to look at the design of the gearbox that will be used to generate the ratio. In this exercise we will look at creating a multi-stage spur or helical gearbox and it will good for you to discuss the affordances of each in your report before selecting one type to carry forward.

This section describes some common types of gear as well as providing some gear design guidelines that you will be using to create your multi-stage gearbox. The result will be a spreadsheet of all the stages of your gearbox with a breakdown of the forces as well as an engineering schematic of your gearbox layout.

\subsection{Gear Types}

% https://s.hswstatic.com/gif/gear-spur.jpg
\begin{marginfigure}[10em]
  \centering
  \includegraphics[width=0.4\textwidth]{figs/spur.png}
  \caption{Spur Gear}
\end{marginfigure}
Spur gears are one of the most common types of gear in mechanical systems.
The teeth often have a involute profile to achieve a constant drive ratio between the gears. Gears have to be fitted on parallel shafts. An affordance of the design is that no axial load is produced by the mating of the teeth. They are typically used in low/medium speed environments where the pitch line velocity is $<\SI{25}{\metre\per\second}$~\footnote{rule of thumb}. Applications include engines, power plants, fuel pumps, washing machines and rack \& pinion mechanisms.

They have the ability to transmit large amounts of power with designs known to cope with \SI{50,000}{\kilo\watt}. They are very reliable, provide a constant velocity ratio and are simple to manufacture. However, initial contact between teeth occurs across the entire tooth face, which leads to higher stresses on the gear teeth. They can also become very noisy at high speeds.

% https://s.hswstatic.com/gif/gear-helical1.jpg
\begin{marginfigure}[12em]
  \centering
  \includegraphics[width=0.4\textwidth]{figs/helical.png}
  \caption{Helical Gear}
\end{marginfigure}
Helical gears differ from spur gears by introducing an angle to the leading edge of the gear teeth and the axis of rotation. This leads to a curved gear that is a segment of a helix. The angling of the gear enables smoother running due to a less aggressive engagement of the gear faces. This also leads to a reduction in noise when running at high speeds and the ability to carry more load per width of gear.

Thus, helical gears are often used in higher speed environments $>\SI{25}{\metre\per\second}$ and environments where noise abatement is an issue. Applications include elevators and conveyors.

However, the angling of the gears leads to the introduction of an axial force component, which requires reaction by thrust bearings along the shafts. In addition, there is greater heat generation through the mating of gears and this in turn, leads to a reduction in efficiency when compared to spur.

\begin{marginfigure}
  \centering
  \includegraphics[width=0.6\textwidth]{figs/herringbone-gear.jpg}
  \caption{Double Helical (Herringbone)}
\end{marginfigure}
Herringbone gears are effectively two helical gears positioned back to back with one another and are used in a range of application including 3D printers and heavy machinery. They have the same affordance of helical gears of providing a smoother motion and have a greater resistance to operation disruption if teeth start to become damaged and/or missing. The main challenge with herringbone gears is that they are difficult to manufacture, which leads to the gear sets costing more than their helical/spur counterparts.

% http://www.aero-news.net/images/content/commav/2008/PW-Geared-Turbofan-0708b.jpg
\begin{marginfigure}
  \centering
  \includegraphics[width=0.8\textwidth]{figs/planetary-gear.jpg}
  \caption{Epicyclic}
\end{marginfigure}
Epicyclic gears provide higher efficiency and power dense solutions to transferring power. They are also highly accurate and enable high gear ratios to be attained per area than that of spur and helical. They also allow in-line input-output shafts.

Applications include lathes, hoists, pulley blocks, watches, automatic transmissions, turbofans and hybrid vehicles. The main challenges in designing epicyclic gears are there loud operation and the high-level of accuracy that needs to be attained between the various gears to ensure the forces are distributed evenly.

% http://www.linngear.com/wp-content/uploads/2012/10/34.png
\begin{marginfigure}
  \centering
  \includegraphics[width=0.8\textwidth]{figs/bevel.png}
  \caption{Bevel}
\end{marginfigure}
Bevel gears are very useful when one wants to change the direction of the power transmission and are applied in differential drives in vehicles, hand drills and assembly machinery. Challenges are in the manufacture of the these components to gain the accuracy required and in mounting the gears in order to provide even contact across the cycle. Bevel gears can have different teeth profiles including involute, spiral and hypoid.

%http://zentexind.com/wp/wp-content/uploads/2016/09/worm-worm-wheel-500x500.jpg
\begin{marginfigure}
  \centering
  \includegraphics[width=0.8\textwidth]{figs/worm.jpg}
  \caption{Worm \& Wheel}
\end{marginfigure}
The worm \& wheel also enables a \ang{90} torque transfer and provide a near silent and smooth operation. A particular advantage of the worm \& wheel is that they are self-locking and are often used to introduce safety within a system. Application include elevators, hoists, packaging equipment, rock crushers and tuning instruments. They are also able to achieve high-velocity ratios within a single-stage and can absorb shock loading.

Challenges in designing worm \& wheel systems are that they are typically more expensive to manufacture and have much greater power lower losses. This is due to the greater level of heat generation from the multiple teeth in contact between worm \& wheel.

\subsection{Spur \& Helical Gear Guidelines}

Adapted From the Mechanical Design Data Manual.~\cite{MechanicalDesignDataManual2000}

\subsubsection{Introduction}

There are many types of gears used in engineering as set out in Australian Standard AS2075 --- Glossary of Terms and Notations for Gears. Some of the types encountered include:

\begin{itemize}
  \item spur
  \item helical
  \item double helical (herringbone)
  \item bevel
  \item hypoid
  \item worm
\end{itemize}

Two gears in mesh are called a gear pair. Normally gears mesh externally but sometimes the mesh may be internal, that is, one of the gears has the teeth cut internally. The smaller gear of a pair is called the pinion whereas the larger is called the wheel. In a pair of gears, the gear transmitting input torque and power is called the driver whereas the gear transmitting output torque and power is known as the driven. Normally in mechanical power transmission, the driver is the pinion so the wheel rotates at a slower speed than the pinion, that is, there is a reduction in speed through the gear pair. (You might like to discuss the reason why this is so).

Usually gears are cylindrical but other forms are possible of which the most common is the rack and pinion. Here the wheel is flat (of infinite diameter) and is known as a rack.

When more than two gears are in continuous mesh, this is known as a gear train. There are several types of gear train used in mechanical power transmission, including simple, compound and planetary. These are illustrated in Figure~\ref{fig-1}.

\begin{figure}[h!]
  \center
  \includegraphics[width=\textwidth]{figs/gear-trains.png}
  \caption{Gear Trains}
  \label{fig-1}
\end{figure}

In the case of the simple gear train, the intermediate gear (or gears) are known as \emph{idler} gears because they do not change the ratio between the driven and driver gears. (You might like to discuss the purpose of idler gears).

Of the various types of gears used in engineering, the most common are spur and helical gears. These are illustrated in \cref{fig-2}.

Spur\marginnote{Spur} gears have the teeth cur parallel to the axis of the shaft.

Helical\marginnote{Helical} gears have the teeth cut at an angle to the axis of the shaft. The angle is called the helix angle, $\alpha$ and is usually about \ang{20} for single helical gears or about 30-\ang{35} for double helical (herringbone) gears.\marginnote[-0.5cm]{\it Note: With a helical gear pair, one helix must be right-hand and the other left-hand.}

\begin{figure}[h!]
  \center
  \includegraphics[width=0.9\textwidth]{figs/spur-helical.png}
  \caption{Spur and helical gears}
  \label{fig-2}
\end{figure}

For\marginnote{Velocity Ratio (VR)} all gears other than the worm and wheel, the velocity ratio of a gear pair is the ratio of the number of teeth in each gear. For example, if a 20 tooth pinion meshes with a 40 tooth wheel, the velocity ratio is:

\begin{equation}
  \text{VR} = \frac{40}{20} = 2
\end{equation}

In the case of a compound gear train, the overall velocity ratio is the multiplication of the velocity ratio of each gear pair. If in the above example, the wheel had a 20 tooth pinion attached to it, that meshed with a 60 tooth wheel, the overall velocity ratio is:

\begin{equation}
  \text{VR} = \frac{40}{20} \times \frac{60}{20} = 2 \times 3 = 6
\end{equation}

In the case of a worm and wheel, the velocity ratio is the number of teeth in the wheel divided by the number of starts in the worm. So if a 12 start worm meshed with a 61 tooth wheel, the velocity ratio is:

\begin{equation}
  \text{VR} = \frac{61}{12} = 5.083
\end{equation}

It\marginnote{Number of Teeth} is impractical to have gears with too few teeth (just try and draw a gear with say 3 teeth). To get a reasonable tooth profile without undercutting and with a smooth transfer of power from pinion to wheel, a rule-of-thumb is that spur gears should have at least 17 teeth on the pinion and helical gears (\ang{20} helix angle) should have at least 14 teeth on the pinion.

For maximum life with meshing gears it is desirable to distribute the wear uniformly amongst all teeth, that is not to have the same teeth in mesh for every revolution of the wheel. The best situation possible is that the same teeth mesh together again only after the pinion revolutions equals the number of teeth in the wheel. This is also known as having all the teeth hunting. For example if there are 40 teeth in the wheel, the best possible situation is that re-meshing of the same teeth occurs only after the pinion has made 40 revolutions. The condition for this is that the velocity ratio cannot be reduced to a simpler ratio, that is, there is no common factor between the number of teeth in the pinion and the number of teeth in the gear.

Some possible combinations are given in Table \ref{tbl-1}.

\begin{table}
  \caption{Pinion and wheel combinations}
  \label{tbl-1}
  \center
  \begin{tabular}{p{0.3\textwidth} p{0.3\textwidth} p{0.3\textwidth}}
    \toprule
    Teeth in Pinion & Teeth in Wheel & Revolutions of the pinion when cycle repeats \\
    \midrule
    18 & 38 & 19 \\
    19 & 38 & 2 \\
    20 & 38 & 19 \\
    21 & 38 & 38 \\
    18 & 40 & 20 \\
    19 & 40 & 20 \\
    20 & 40 & 2 \\
    21 & 40 & 40 \\
    \bottomrule
  \end{tabular}
\end{table}

It can be seen therefore, that there can be a huge difference in the way wear will be distributed for approximately the same velocity ratio. However, in practice, exact velocity ratios are often needed (for example, for a camshaft) and in such cases, hunting teeth cannot be provided.

For\marginnote{Limiting Velocity Ratio} a worm-wheel gear pair, the smallest velocity ratio obtainable in practice is about 5. For all other gear pairs, the smallest ratio is 1. There is no theoretical upper limit to the velocity ratio, however high velocity ratios are undesirable because of the large number of teeth that need to be cut in the wheel. This makes accurate machining difficult (if not impossible) and requires large centre distances which are also not usually desirable.

For example, consider a velocity ratio of 125 obtained with single pair of spur gears. With a pinion of 17 teeth and PCD \SI{17}{\milli\metre}, the wheel would require 2125 teeth and a PCD of \SI{2125}{\milli\metre}! The total number of teeth to be cut is 2142!

The same velocity ratio can be obtained with a compound gear train with 3 pairs of gears in mesh, each with a velocity ratio of 5. Then the total number of teeth that need to be cut is 306 (7 times less) and the maximum wheel PCD is only \SI{85}{\milli\metre}.

Rule-of-thumb limits for a gear pair are given in \cref{tbl-2}.

\begin{table}[h]
  \caption{Rule-of-thumb VR limits for a gear pair}
  \label{tbl-2}
  \center
  \begin{tabular}{p{0.3\textwidth} p{0.3\textwidth} p{0.3\textwidth}}
    \toprule
    Type of gear pair & VR lower limit & VR upper limit \\
    \midrule
    Worm and wheel & 5 & 60 \\
    All others & 1 & 5 \\
    \bottomrule
  \end{tabular}
\end{table}

The\marginnote{Efficiency} efficiency of a gear pair depends upon a number of factors:

\begin{itemize}
  \item Accuracy and finish. The better this is the higher the efficiency. In most cases, gears are accurately cut and machined to a fine finish. However in some cases gears may be cast and not machined.
  \item Bearings used to support the gear shafts. The lower the bearing friction, the higher the efficiency. In most cases, rolling-element bearings are used.
  \item Lubrication of the gears and bearings. The better the lubrication, the higher the efficiency. In most cases the gears are oil-lubricated but pre-sealed rolling-element bearings are also used.
\end{itemize}

With well machined and lubricated gears supported by rolling element bearings, the efficiency is usually about 95-96\% for each gear pair. With a gear train, the overall efficiency is approximately the product of the efficiency of each gear pair. For example with a three pair compound gear train, if each pair has an efficiency of 96\%, the overall efficiency will be about $0.96\times0.96\times0.96=0.855$.

\subsubsection{Gear Parameters}

Consider two gears in mesh as shown in \cref{fig-3}:

\begin{figure}[h!]
  \center
  \includegraphics[width=0.9\textwidth]{figs/meshing-gears.png}
  \caption{Meshing gears}
  \label{fig-3}
\end{figure}

If the pinion has $n$ teeth and $\text{PCD}=d$, and the wheel has $N$ teeth and $\text{PCD}=D$ then:

\begin{equation}
  \text{VR} = \frac{N}{n} = \frac{D}{d}
\end{equation}

The nominal centre distance $C$ is:

\begin{equation}
  C = 0.5(d+D)
\end{equation}

This is known as the nominal centre distance because the actual centre distance is usually somewhat greater than this.

The height of a tooth above the PCD line is known as the addendum (A). The height of a tooth below the PCD line is known as the dedendum (B).

The angle made by the tangent to the gears at the point of contact is called the pressure angle ($\theta$). This angle is usually \ang{20} and should be assumed so unless otherwise stated.

The point of contact is called the pitch point (P). In order to obtain a constant velocity ratio the pitch point should not move as the gears move into and out of mesh. If the pitch point did move in and out with each mesh, then this would cause acceleration/deceleration with each mesh. If the gears are rotating at relatively high speeds this would cause high acceleration/deceleration with high inertia forces and accelerated wear.

The pitch point remains fixed when the gears are cut with an involute profile. The involute profile can best be demonstrated by unwinding a thin cord from around a cylinder as shown in Figure \ref{fig-4}.

\begin{figure}[h!]
  \center
  \includegraphics[width=0.9\textwidth]{figs/fig4.png}
  \caption{Involute profile}
  \label{fig-4}
\end{figure}

In\marginnote{Rack and Pinion} the case of a rack and pinion, the mating profile to the involute on the pinion is a straight sided rack as illustrated in \cref{fig-5}. The angle of the rack is the pressure angle as previously defined

\begin{figure}[h!]
  \center
  \includegraphics[width=0.8\textwidth]{figs/fig5.png}
  \caption{Rack profile}
  \label{fig-5}
\end{figure}

To\marginnote{Clearence} minimise friction, the teeth should contact only along the front face of the driver and the back face of the driven. There should be both circumferential and radial clearance as shown in \cref{fig-6}.

\begin{figure}[h!]
  \center
  \includegraphics[width=0.9\textwidth]{figs/fig6.png}
  \caption{Clearence}
  \label{fig-6}
\end{figure}

Radial clearance (or bottom clearance) is obtained by making the dedendum greater than the addendum. Usually $\text{B}=1.25\text{A}$. Circumferential clearance is usually very small when the gears are new because wear automatically increases it. It can be obtained by making the centre distance slightly larger than the nominal centre distance. Circumferential clearance causes backlash and can be measured by holding one gear fixed and moving the other gear back and forth relative to it.

One\marginnote{Module (M)} of the most important parameters in gear design is the module which may be defined as the pitch circle diameter divided by the number of teeth. For a gear pair, the module must be the same for pinion and wheel. Hence:

\begin{equation}
  \text{M} = \frac{d}{n} = \frac{D}{N}
\end{equation}

Standard modules (first choice) in \si{\milli\metre} are:

1 1.25 1.5 2 2.5 3 4 5 6 8 10 12 16 20 25 32 40 50

As the module increases, the size of the gear teeth increase. That is, the gears become stronger and can transmit more torque and power. For example with a PCD of \SI{200}{\milli\metre}, a module of \SI{1}{\milli\metre} would mean 200 small teeth whereas a module of \SI{10}{\milli\metre} would mean 20 large teeth. This is illustrated in \cref{fig-7}.

\begin{figure}[h!]
  \center
  \includegraphics[width=0.9\textwidth]{figs/fig7.png}
  \caption{Module - tooth size comparison (PCD \SI{200}{\milli\metre})}
  \label{fig-7}
\end{figure}

The addendum, dedendum and depth of gear teeth are directly related to the module. For gear teeth of standard proportions the following relationships apply:

\begin{itemize}
  \item Addendum $\text{A} = \text{M}$
  \item Dedendum $\text{B} = 1.25\text{M}$
  \item Tooth depth $=\text{A}+\text{B}=2.25\text{M}$
\end{itemize}

The face width of gears is also related to the module but is also dependent upon the gear load (torque). The following rule-of-thumb face widths $\text{W}$ of the wheel are usual in mechanical power transmission:

\begin{itemize}
  \item Relatively light loads $\text{W}=8\text{M}$
  \item Moderate load $\text{W}=10\text{M}$
  \item Heavy loads $\text{W}=12\text{M}$
\end{itemize}

\textbf{Note:} The face width of the pinion is usually 5--10\% larger than the wheel (depending upon assembly tolerances)

A\marginnote{Example One} spur gear pair are to have a velocity reduction ratio in the range 2.5 to 2.7. The proposed number of teeth in the pinion is 18 with a module of 5mm. Determine:

\begin{enumerate}
  \item[a)] A suitable number of teeth in the wheel and the reduction ratio in order to have all hunting teeth,
  \item[b)] PCD of pinion and wheel,
  \item[c)] Nominal centre distance,
  \item[d)] Addendum, dedendum and tooth depth,
  \item[e)] Face width of pinion and wheel if loads can be described as moderate.
\end{enumerate}

a)\marginnote{Solution} Various combination of teeth in the wheel, velocity ratio and revolutions of the pinion when the cycle repeats are set out in the \cref{tbl-3}

\begin{table}[h]
  \caption{Pinion and wheel combinations}
  \label{tbl-3}
  \center
  \begin{tabular}{p{0.2\textwidth} p{0.2\textwidth} p{0.2\textwidth} p{0.24\textwidth}}
    \toprule
    Teeth in pinion & Teeth in wheel & Velocity Ratio & Pinion revolution when cycle repeats \\
    \midrule
    18 & 45 & 2.5 & 5 \\
    18 & 46 & 2.556 & 23 \\
    18 & 47 & 2.611 & 47 \\
    18 & 48 & 2.667 & 8 \\
    \bottomrule
  \end{tabular}
\end{table}

Hence it is seen that the only combination that give a velocity ratio in the required range with all teeth hunting is the 18:47 ratio. Hence choose \textbf{47} teeth in the wheel with velocity ratio \textbf{2.556}.

\noindent{} b)

\begin{itemize}
  \item PCD of the pinion $= 5 \times 18 = 90\si{\milli\metre}$
  \item PCD of the wheel $= 5 \times 47 = 235\si{\milli\metre}$
\end{itemize}

\noindent{} c) Nominal centre distance $C = 0.5(d+D) = 0.5(90+235)=162.5\si{\milli\metre}$

\noindent{} d)
\begin{itemize}
  \item Addendum $\text{A} = \text{M} = 5\si{\milli\metre}$
  \item Dedendum $\text{B} = 1.25\text{M} = 6.35\si{\milli\metre}$
  \item Tooth Depth $\text{A} + \text{B} = 11.25\si{\milli\metre}$
\end{itemize}

\noindent{} e)
\begin{itemize}
  \item For moderate loading assume face width of the wheel $\text{W}=10\text{M}=50\si{\milli\metre}$
  \item Face width of the pinion, say 7\% larger $= 1.07 \times 50 = 53.5\si{\milli\metre}$
\end{itemize}

\subsubsection{Gear design}

A comprehensive design procedure for gears is very complex and outside the scope of this manual. For example, AS2938 - Gears - Spur and Helical - Guide to Specification and Rating lists 88 different variables and constants and contains 32 different charts!

The most important factor to determine when designing gears for a certain application is the module. As has been stated, the greater the loading, the larger the size of the gear teeth and the larger the module.

A simplified factor to module selection is the use of a chart as the one given as \cref{fig-8}. This chart has been drawn for spur gears with face widths = 10M and with 18 teeth in the pinion. It may be used for face widths in the range 8-12M and for pinions with 17-19 teeth. It may also be used for helical angles up to \ang{20}. However, because helical gears are inherently stronger than spur gears, a somewhat smaller module will be satisfactory (say one standard size down).

\begin{figure}[th!]
  \center
  \includegraphics[width=0.9\textwidth]{figs/fig8.png}
  \caption{Module selection chart}
  \label{fig-8}
\end{figure}

A\marginnote{Example Two} helical pinion transmits \SI{20}{\kilo\watt} at \SI{1500}{rpm} to a mating gear. Loading is moderate. Determine suitable values for:

\begin{enumerate}
  \item[a)] The module for pinion and gear;
  \item[b)] The face width of the pinion and the gear;
  \item[c)] The PCD of the pinion.
\end{enumerate}

a)\marginnote{Solution} From \cref{fig-8}, the module lies between 6 and 8. A module of 7 is not standard (first choice). Because the gear is a helical one, the choice of the smaller module is justified. Hence choose $\text{M} = 6$.

b) With $\text{M}=6$ and face width of the gear $= 10 \text{M}$ (moderate loading), therefore the face width of the gear $= 60\si{\milli\metre}$.

For the pinion, make the face width about 7\% larger, say 64\si{\milli\metre}.

c) Make the number of teeth on the pinion = 19.

Then the pinion $\text{PCD} = 6 \times 19 =144\si{\milli\metre}$.

\subsubsection{Gear tooth forces}

The forces acting on gears in mesh occurs at the point of contact of the gears (the pitch point). The force $F$ as shown in \cref{fig-9}.

\begin{figure}[h!]
    \center
    \includegraphics[width=0.9\textwidth]{figs/fig9.png}
    \caption{Module selection chart}
    \label{fig-9}
\end{figure}

In this diagram:

\begin{itemize}
  \item[$F=$] resultant transverse force on the tooth at the pitch point. This is also the resultant transverse load on the shaft at the location of the gear. Note that this force acts perpendicular to the tooth at the pitch point.
  \item[$F_s=$] separating (or radial) force on the tooth at the pitch point. Its line of action passes through the centrelines of the two gears. This force is needed to keep the gears together and without it, the gears would simply slip out of mesh and not transmit any torque.
  \item[$F_t=$] tangential force acting on the tooth at the pitch point. This force multiplied by the pitch circle radius gives the torque transmitted by the gear.
  \item[$\theta=$] the pressure angle, that is the angle between forces $F$ and $F_t$. For most gears, this angle is \ang{20} and should be assumed so unless otherwise stated.
\end{itemize}

Because\marginnote{Spur Gear Forces} torque is the tangential force multiplied by the pitch circle radius it is clear that the tangential force on the gear is calculated by:

\begin{equation}
  f_t = \frac{2T}{d}
\end{equation}

Where $T=$ Torque (\si{\newton\metre}) and $d=\text{PCD}$ of the gear in metres.

From \cref{fig-9}, the forces on the spur gear are as follows:

\marginnote{Separating Force}

\begin{equation}
  F_s = F_t\tan\theta
\end{equation}

\marginnote{Resultant Transverse Force}

\begin{equation}
  F=\sqrt{F_t^2+F_s^2}
\end{equation}

Because\marginnote{Helical Gear Forces} the teeth are cut at an angle (the helix angle), there is also an axial force on the gears. The tangential force is the same as for the spur gears but the formula for the separating force becomes:

\begin{equation}
  F_s = \frac{F_t\tan\theta}{\cos\alpha}
\end{equation}

Where $\alpha$ is the helix angle.

\marginnote{Axial Force}

\begin{equation}
  F_a = F_t\tan\alpha
\end{equation}

A\marginnote{Example Three} spur gear PCD \SI{100}{\milli\metre} transmits \SI{800}{\newton\metre} of torque. Determine the tangential, separating and resultant force on each gear.

The\marginnote{Solution} tangential force is:

\begin{equation}
  F_t = \frac{2T}{d} = \frac{2\times 800}{0.1} = 16\si{\kilo\newton}
\end{equation}

Assuming a pressure angle of \ang{20}, the separating force is:

\begin{equation}
  F_s = F_t\tan\theta = 16\tan20 = 5.82\si{\kilo\newton}
\end{equation}

The resultant force is:

\begin{equation}
  F = \sqrt{F_t^2+F_s^2} = \sqrt{16^2+5.82^2} = 17\si{\kilo\newton}
\end{equation}

Repeat\marginnote{Example Four} Example 3 for helical gears with helix angle \ang{20} (all other data being the same). Also calculate the axial force.

\begin{equation}
  F_t = \frac{2T}{d} = \frac{2\times 800}{0.1} = 16\si{\kilo\newton}
\end{equation}

\begin{equation}
  F_s = \frac{F_t\tan\theta}{\cos\alpha} = \frac{16\tan20}{\cos20} = 6.2\si{\kilo\newton}
\end{equation}

\begin{equation}
  F = \sqrt{F_t^2+F_s^2}=\sqrt{16^2+6.2^2}=17.2\si{\kilo\newton}
\end{equation}

\begin{equation}
  F_a = F_t\tan\alpha=16\tan20 = 5.82\si{\kilo\newton}
\end{equation}


\subsection{Impact of the Training Horizon}
\label{training}

Whereas it is reasonable to assume that forecasts become more accurate as the
    training horizon expands, our study reveals some interesting findings.
First, without demand, \textit{trivial} indeed performs better with more
    training material, but improved pattern recognition cannot be the cause
    here.
Instead, we argue that the reason for this is that the longer there has been
    no steady demand, the higher the chance that this will not change soon.
Further, if we focus on shorter training horizons, the sample will necessarily
    contain cases where a pixel is initiated after a popular-to-be restaurant
    joined the platform:
Demand grows fast making \textit{trivial} less accurate, and the pixel moves
    to another cluster soon.

Second, with low demand, the best-performing \textit{hsma} becomes less
    accurate with more training material.
While one could argue that this is due to \textit{hsma} not fitting a trend,
    the less accurate \textit{hses} and \textit{hets} do fit a trend.
Instead, we argue that any low-demand time series naturally exhibits a high
    noise-to-signal ratio, and \textit{hsma} is the least susceptible to
    noise.
Then, to counter the missing trend term, the training horizon must be shorter.

With medium demand, a similar argument can be made; however, the
    signal already becomes more apparent favoring \textit{hets} with more
    training data.

Lastly, with high demand, the signal becomes so clear that more sophisticated
    models can exploit longer training horizons.