\documentclass[a4paper]{article}

\usepackage[margin=1in]{geometry} % full-width

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{graphicx, color}
\usepackage[caption=false]{subfig}

% Author info
\title{Short explanation of the Intermediate Axis Theorem}
\author{Victor I.  Danchev \\ \href{vidanchev@uni-sofia.bg}{vidanchev@uni-sofia.bg}}

\date{
	Sofia University St.  Kliment Ohridski \\
	\today
}

\begin{document}
	\maketitle

	\begin{abstract}

	Have you ever seen \href{https://www.youtube.com/watch?v=1n-HMSCDYtM}{this video}?
	It first came to my attention in 2014 (when I was a first year student in Theoretical Physics).
	It took me about 1 year to really understand what's going and another two to feel comfortable simulating it on a computer.
	In fact,  this is not such a difficult task.
	I've met a lot of people since then who were very interested in the phenomenology and the effect - it is usually referred to as the "Intermediate Axis Theorem".
	The purpose of this handout is to provide a concise and straightforward (hopefully intuitive) description of the intermediate axis theorem and to derive it in the case of small angles of deflection.
	This derivation will then be used to test a simple numerical integrator (using Runge-Kutta method) and verify its accuracy in this short deflection regime.
	Be warned though, it may contain traces of math!
	\end{abstract}

	\tableofcontents

	\newpage

	\section{Introduction} \label{Intro}

	The intermediate axis theorem states that a body's rotational motion in 3 dimensions will only be stable around an axis which has the highest/lowest moment of inertia.
	In other words,  a body rotating around the intermediate axis is quasi-stable and the smallest deviation from a perfect alignment between the angular velocity vector and that intermediate axis will result in exponential departure of that vector.
	Since a real body cannot be perfectly rotated around that intermediate axis, the result is an oscillatory motion between the two opposing states (i.e. periodic flipping between one angular velocity vector and its exact opposite) caused by any small misalignment or external effect.

	\section{Preliminaries - motion of rigid body in 3 dimensions}\label{rigid_body_dynamics}

	Although they are all around us, rigid bodies have a very non-linear motion which leads to the intermediate axis theorem.
	I will start from the motion of a free rigid body (i.e. no external torques) to model the motion.
	Such a body can be described by 6 degrees of freedom, 3 of which form its angular velocity vector $\vec{\omega}$.
	The other parameters must be \textbf{at least} three but may be more (connected by constraints).
	Some popular representations are directive cosine matrix (DCM), quaternions, Euler angles, Rodriguez parameters, etc.
	Euler's equation looks something like this
	\begin{eqnarray}\label{Euler_eq}
		\frac{{}^{\mathcal{B}}d \vec{L} }{dt} + \vec{ \omega }_{\mathcal{B/N}}\times\vec{L} = \vec{\tau},
	\end{eqnarray}
	where $\vec{L}$ is the angular momentum vector, $\omega_{\mathcal{B/N}}$ is the angular rate of the body frame $\mathcal{B}$ with respect to some other reference inertial frame $\mathcal{N}$ (to be denoted just $\omega$ further down) and $\tau$ is the sum of any external torques on the body.

	I will not go into the details of how this equation comes to be, but it is in fact a consequence of one of the most fundamental conservation laws - the conservation of angular momentum.
	In fact, this law is fully derived from the fact that the total change of angular momentum in some reference inertial frame (such as $\mathcal{N}$) is equal to the external torques, or
	\begin{eqnarray}\label{L_conservation}
		\frac{{}^{\mathcal{N}}d \vec{L} }{dt} = \vec{\tau}.
	\end{eqnarray}
	Purely geometrically (looking at how basis vectors transform and all that), one can show that a time derivative of a vector in one frame (say $\mathcal{A}$) is related to that in another (say $\mathcal{B}$) as
	\begin{eqnarray}\label{omega_transform}
		\frac{{}^{\mathcal{B}}d \vec{v} }{dt} = \frac{{}^{\mathcal{A}}d \vec{v} }{dt} + \omega_{\mathcal{A/B}}\times\vec{v}.
	\end{eqnarray}

	Euler's equations \eqref{Euler_eq} are simply a direct consequence of \eqref{L_conservation} and \eqref{omega_transform} when applied with some external torque.
	We won't go into anything more detailed, but two excellent sources where you can read more are \cite{David_Tong,Goldstein}.

	Now it is not so obvious that these equations are non-linear but once they are written out in full (element-wise by $\omega$) this is easy to see.
	To make things simpler, we will pass into a special body reference frame aligned with the so-called body axes.
	Long story short - the angular momentum is obtained from the angular velocity through a linear transformation which is a 3x3 tensor (matrix in any given basis) as $\vec{L} = \mathbb{I}\vec{\omega}$.
	Now this tensor has to have certain properties to be physically meaningful - namely it has to be symmetric ($\mathbb{I}^T = \mathbb{I}$).
	This means that it always has three real eigenvalues and three real eigenvectors which form what is called a principal body frame.
	When written in terms of these principal axes, the tensor's components are just diagonal terms.

	To summarize - if we pick any body frame (X, Y, Z of the body how we like them), this will lead to 6 independent components for $\mathbb{I}$ and make our life really complicated.
	To avoid this, from this point on we'll be working in the principal body axes where $\mathbb{I} = \mathrm{diag}( I_1 , I_2 , I_3 )$.
	The good news is that for a solid body, this tensor is a constant (i.e. $I_1, I_2$ and $I_3$ are constants).
	This is not the case if you take into account flexible and deformable bodies - they are \textbf{a lot} more complicated to describe.

	Now, considering that we're in the principal body frame, the components of the angular momentum in this frame are just $L_1 = I_1 \omega_1$, $L_2 = I_2 \omega_2$ and $L_3 = I_3 \omega_3$.
	Using this directly in \eqref{Euler_eq} and computing the right-hand side with $\vec{\tau} = 0$, we get the 3 equations for the angular rate
	\begin{eqnarray}
		I_1\dot{\omega_1} & = & ( I_2 - I_3 )\omega_2 \omega_3 \nonumber \\
		I_2\dot{\omega_2} & = & ( I_3 - I_1 )\omega_3 \omega_1 \\
		I_3\dot{\omega_3} & = & ( I_1 - I_2 )\omega_1 \omega_2. \nonumber
	\end{eqnarray}
	Now we can see that the system is indeed non-linear - on the righ-hand side the $\omega$ components are mixing and are always of second order.
	We can also see that there is some interplay between the moments of inertia (their differences and division appearing into the right-hand side).
	It is best to divide to the constant terms on the left, leaving us with the standard system form
	\begin{eqnarray}
		\dot{\omega_1} & = & \frac{( I_2 - I_3 )}{I_1}\omega_2 \omega_3 \nonumber \\
		\dot{\omega_2} & = & \frac{( I_3 - I_1 )}{I_2}\omega_3 \omega_1 \\ \label{Euler_full}
		\dot{\omega_3} & = & \frac{( I_1 - I_2 )}{I_3}\omega_1 \omega_2. \nonumber
	\end{eqnarray}

	As we'll see later - the whole "magic" of the intermediate axis comes from these right-hand coefficients formed by the difference of two moments of inertia divided by the third!

	These equations describe the \textbf{angular velocity} of a body in 3 dimensions, now if we want to describe the full body motion, we must also add equations for its orientation.
	The different parametrizations already mentioned will have a different number of parameters but these parameters (with constraints so that physically there are only 3 degrees of freedom).
	Something which all of these have in common, however, is that these parameters' derivatives are somehow related to the angular rate vector $\vec{\omega}$.
	In the code, I have used the relation between unit quaternions and attitude to integrate the 7 equations of motion (with 1 constraint) numerically.
	For the purpose of this small proof, however, we will only need to use the Euler equation.
	More on attitude (orientation) parametrizations and their applications can be found in the excellent book \cite{ADCS_bible}.

	\section{Perturbative solution and stability}

	Finding exact solutions to \eqref{Euler_full} is not an easy task as is usually the case with non-linear systems of equations.
	Nevertheless, one very simple exact solution can be found -- the case where a body is rotating exactly around one of its principal axes.
	For the sake of concreteness, say that the angular velocity $\omega$ as measured in the body frame is only along the Z-axis or in other words $\vec{\omega} = [ 0 , 0 , \Omega ]^T$.
	This trivially satisfies \eqref{Euler_full}, where $\Omega = \mathrm{const}$ is some fixed value.
	The left-hand side is 0 since the angular velocity is constant for all 3 axes and since at least one of $\omega_1$ or $\omega_2$ multiplies the right-hand side (but they are both 0), all equations are satisfied.

	This shows us that a body can spin about any of its principal axes but doesn't tell us anything about stability.
	Stability is a statement about how a system acts when "slightly" perturbed or when a "slightly" different intiali condition is given.
	When a system is stable, nearby trajectories (with very close initial conditions) tend to stay close indefinitely.
	The word "slightly" has its commas since it is not very precise in this way.
	What do we mean by perturbing "slightly" -- this usually means that we change the initial condition by a value which is of a much lower magnitude than the one we started with (i.e. we can treat it as a very small correction).
	Consider perturbing our system transversely to the original direction with the initial "kick" $\delta \vec{\omega} = [ \delta\omega_1 , \delta\omega_2 , 0 ]$, where $\delta\omega_i << \Omega$ are of the same small order.
	The statement "slightly perturbed" for us means that we can safely work to 1st order in $\delta\omega$.
	This is true since if $\delta\omega << \Omega$, then $\delta\omega^n$ will be of progressively smaller magnitude.
	Say that $\delta\omega / \Omega \cong 0.1$, then each follow-up term will be an order of magnitude smaller and if we are looking at 1\% accuracy for example, we can safely disregard anything but the first order.
	In this case we are looking for a quantitative effect, so this will be a perfectly adequate.
	Adding the small perturbation $\delta\vec{\omega}$, the equations of motion become
	\begin{eqnarray}\label{Perturbed_Euler_1}
		\delta\dot{\omega}_1 & = & \frac{( I_2 - I_3 )}{I_1}\Omega \delta\omega_2 + O( \delta\omega^2 ) \\ \label{Perturbed_Euler_2}
		\delta\dot{\omega}_2 & = & \frac{( I_3 - I_1 )}{I_2}\Omega \delta\omega_1 + O( \delta\omega^2 ) \\ \label{Perturbed_Euler_3}
		\delta\dot{\omega}_3 & = & O( \delta\omega^2 ).
	\end{eqnarray}

	As mentioned above, $\delta\omega_i << \Omega$ means that we can assume for the quadratic term $O(\delta\omega^2) \rightarrow 0 $.
	The third equation is automatically satisfied because of this while the other two give us a linear system for the small perturbations $\delta \omega_1$ and $\delta \omega_2$
	What we have performed is called a linearization near an equilibrium point (where the original system has a solution with zero-derivative).

	Differentiating \eqref{Perturbed_Euler_1} and substituting \eqref{Perturbed_Euler_2} into the right-hand-side (this can be done the other way around), we get to first order in $\Omega$
	\begin{eqnarray}\label{Perturbed_oscillator}
		\delta\ddot{\omega}_1 = \frac{( I_2 - I_3 )}{I_1}\frac{( I_3 - I_1 )}{I_2}\Omega^2 \delta\omega_1.
	\end{eqnarray}

	This is a linear 2nd order equation which looks just like a harmonic oscillator $\ddot{x} = \kappa^2 x$.
	We can define
	\begin{eqnarray}\label{kappa_eq}
		\kappa^2 = \frac{( I_2 - I_3 )}{I_1}\frac{( I_3 - I_1 )}{I_2}\Omega^2,
	\end{eqnarray}
	which means that we can solve this right away.
	The solutions to this equation are quantitatively different depending on the sign, however.
	For $\kappa^2 < 0$ the general solution is a superposition of sine and cosine functions (complex exponentials) while for $\kappa^2 > 0$ it is a superposition of real exponentials.
	In the former case, solutions are oscillatory and we have stability since $\delta \omega_1$ and $\delta \omega_2$ will remain bounded within the same range (they will be proportional to $\sin( \kappa t )$ and $\cos( \kappa t )$).
	In the latter case, however, solutions are exponential and even the smallest perturbation will grow, leading us away from stability (proportional to $\exp( \pm \kappa t )$ ).

	We will write the exact solution to the system in the following section but the important point is that only the sign of $\kappa$ determines the stability of the equilibrium.
	For it to be stable, we must have $\kappa^2 < 0$.
	Now $\Omega^2 > 0$ since $\Omega$ is some real number which is the unperturbed angular velocity.
	The individual moments of inertia are each a positive number as well $ I_1 > 0, I_2 > 0, I_3 > 0 $ on purely physical grounds (mass times distance squarred).
	Then only the relative difference between the moments of inertia determines the sign.
	Motion is stable when $$ ( I_2 - I_3 )( I_3 - I_1 ) < 0. $$
	There are two ways for this to happen:
	\begin{itemize}
		\item $ I_2 > I_3 $ \textit{and} $ I_3 < I_1 \rightarrow I_3 $ is the smallest moment of inertia
		\item $ I_2 < I_3 $ \textit{and} $ I_3 > I_1 \rightarrow I_3 $ is the largest moment of inertia.
	\end{itemize}
	It is easy to convince oneself that in any situation when $I_3$ is the intermediate moment of inertia - the sign will be positive and so the motion will be unstable (following exponential instead of periodic dependence).

	So, we have proven this for $I_3$ but keep in mind that nothing is special about the Z-axis -- we just selected our original angular velocity to be along it.
	We can perform the same analysis for $[ 0 , \Omega , 0 ]^T$ and $[ \Omega , 0 , 0 ]$ with the perturbations along $\delta\vec{\omega} = [ \delta\omega_1 , 0 , \delta\omega_3 ]^T $ or $\delta\vec{\omega} = [ 0 , \delta\omega_2 , \delta\omega_3 ]^T $ respectively and we would get the same result for $I_1$ and $I_2$ (of course it's much easier just to transform our coordinate frame).

	In conclussion, whichever axis we are rotating about -- there is an equilibrium but this equilibrium is stable only if the axis is with the largest or smallest moment of inertia - otherwise the smallest external disturbance or misalignment will drive the rotation away from that axis exponentially quickly!
	This is the intermediate axis theorem in mathematical language -- it is a statement about the stability of a nonlinear system.

	\section{Comparison with the video and code verification}

	\subsection{Estimating the T-Handle moments of inertia}

	The general definition of the moment of inertia tensor is
	\begin{eqnarray}\label{moi_definition}
		\mathbf{I} = \int_{V} \rho{\vec{r}}( \hat{ \delta } |\vec{r}|^2 - \vec{r} \otimes \vec{r} ) d^3 \vec{r},
	\end{eqnarray}
	where $\hat{\delta} = \mathrm{diag( 1 , 1 , 1 )}$ is the unit 3x3 matrix, $\vec{r}$ is the radius vector from the point where we measure the moment of inertia and $\rho(\vec{r})$ is the mass density at that point.
	The integration is over the full body and due to the cumulative property of volume integrals (we can split one integral over as many sub-volumes as we wish), we can add the moments of inertia of the different parts \textbf{as long as these are taken about the same point.}
	Once again, I will not go into the details of how this is derived (it is not very difficult from the point-mass definition of angular momentum) -- look into either of \cite{David_Tong,Goldstein,ADCS_bible} for the details.

	Applying this definition to a homogeneous cylinder of length $l$, radius $r$ and mass $m$, one can easily get the following moments of inertia for it
	\begin{eqnarray}
		I_{\mathrm{axis}} &=& \frac{1}{2} m r^2 \\
		I_{\mathrm{ortho}} &=& \frac{1}{12}m( 3r^2 + l^2 ),
	\end{eqnarray}
	where $I_{\mathrm{axis}}$ denotes the axis of rotation corresponding to the cylinder axis of symmetry, while $I_{\mathrm{ortho}}$ is the moment of inertia about any axis orthogonal to it.
	By "homogeneous", I mean that $m = V\rho = 2\pi r l \rho$ where $\rho = \mathrm{const}$ throughout the body.

	The definition \eqref{moi_definition} also makes it quite straightforward to prove what is known as the parallel axis theorem.
	Displacing the origin of the coordinate system from the center of mass only shifts it based on this displacement vector.
	Given a moment of inertia $\mathbf{I}$ about the center of mass of a body with mass $m$, a shift of coordinates with $\vec{r}' = \vec{r} + \vec{R}$ will produce a new moment of inertia about the new point $\vec{R}$ from the center of mass -- $\mathbf{I}'$, which is given by
	\begin{eqnarray}
		\mathbf{I}' = \mathbf{I} + mR^2
	\end{eqnarray}

	I will not go in so much detail as to how I have estimated the T-handle moments of inertia.
	In summary -- I've considered the body as composed of two cylinders with different radii and lengths and summed the moment of inertia for them as a break-up of the larger volume integral with several applications of the parallel axis theorem.
	I've obtained the center of mass considering that the material is homogeneous (i.e. the mass ratio of the two cylinders is only proportional to their volumes ratio).

	Referring to FIG.\ref{T_handle_axes} for the names of the principal axes, my estimates are:
	\begin{itemize}
		\item $I_1 \cong 7.27 \times 10^{-5} \ [\mathrm{kg.m^2}]$
		\item $I_2 \cong 1.46 \times 10^{-4} \ [\mathrm{kg.m^2}]$
		\item $I_3 \cong 2.10 \times 10^{-4} \ [\mathrm{kg.m^2}]$
	\end{itemize}

	\begin{figure}[ht]
		\centering
		\includegraphics[width=0.5\textwidth]{T_handle_axes.png}
		\caption{The notation adopted for the three moments of inertia shown on top of the T-handle. $I_1$ is the smallest moment of inertia while $I_3$ is the largest.}\label{T_handle_axes}
	\end{figure}

	\noindent \textbf{Note:} I don't claim that the results above are very accurate, they are order of magnitude estimates based on sizes I can judge from the screen.
	I am confident that a much better estimation can be extracted with some more effort and by computing the moment of inertia tensor directly through a CAD model.

	The \textbf{Results} folder has several .gifs generated using these numbers and initial values in the range 0.1 -- 1 Hz along the intermediate axis with a perturbation which is 100 times smaller about the smallest moment of inertia.
	These indeed appear quite close to what is seen on the video, but just for each case, I will compare the numerical results to the perturbative solution based on \eqref{Perturbed_oscillator} to confirm.

	\subsection{Numerical vs. perturbative results}
	For verification purposes, I want to compare the numerical solution to what is predicted by the linearized equations (for small perturbations).
	Taking the definition \eqref{kappa_eq}, the general solution to the 2nd order linear differential equation \eqref{Perturbed_oscillator} is
	\begin{eqnarray}\label{dom1_sol_stable}
		\delta \omega_1 = c_1 \cos( \kappa t ) + c_2 \sin( \kappa t ),
	\end{eqnarray}
	when $I_3$ is the largest or smallest moment of inertia, and
	\begin{eqnarray}\label{dom1_sol_unstable}
		\delta \omega_1 = c_1 \exp( \kappa t ) + c_2 \exp( \kappa t )
	\end{eqnarray}
	when $I_3$ is the intermediate moment of inertia.
	In either of these equations, the constants $c_1$ and $c_2$ are determined fully by the initial conditions (what $\delta \vec{\omega}(0)$ is).
	One can also integrate \eqref{Perturbed_Euler_2} given the $\delta \omega_1$ solution and obtain the function $\delta \omega_2(t)$ accordingly.
	Focusing on the case where $I_3$ is the largest or smallest moment of inertia, this gives
	\begin{eqnarray}\label{dom2_sol_stable}
		\delta \omega_2 = \Theta \left[ c_1 \sin( \kappa t ) - c_2 \cos( \kappa t ) \right],
	\end{eqnarray}
	where I have defined an axiluary dimensionless quantity which comes out of the integration as
	\begin{eqnarray}
		\Theta = \sqrt{ \frac{ (I_3 - I_1) I_1 }{ ( I_2 - I_3 ) I_2 } }.
	\end{eqnarray}
	It is simple to see that \eqref{dom1_sol_stable} and \eqref{dom2_sol_stable} along with $\delta \omega_3 = 0$ indeed solve the system of equations \eqref{Perturbed_Euler_1}-\eqref{Perturbed_Euler_3}.
	As expected, the $\delta \vec{\omega}$ vector describes a periodic closed trajectory (ellipse as we'll see bellow) which depends on the initial data when perturbed lightly about the largest or smallest moment of inertia -- it is precession.

	Now we can choose a particular starting state to use for verification purposes.
	A simple option is to perturb the angular velocity only in one direction.
	Suppose that the initial perturbation vector is $\delta \vec{\omega}( 0 ) = [ \delta \omega , 0 , 0 ]^T$.
	Then applying the boundary conditions $c_1 = \delta \omega$ and $c_2 = 0$ which leads us to the full angular rate solution (up to $O(\delta \omega^2)$ terms) as
	\begin{eqnarray}\label{Perturbative_result}
		\vec{\omega} =
		\begin{bmatrix}
				\delta \omega \cos( \kappa t ) \\
				\Theta \delta \omega \sin( \kappa t ) \\
				\Omega
		\end{bmatrix}.
	\end{eqnarray}

	It is now straightforward to check whether this solution matches with our numerical solution.
	The script $\mathrm{Perturbative\_Test.py}$ executes and plots exactly a comparison between the result \eqref{Perturbative_result} and the numerical integration for the same initial conditions $\vec{\omega}( 0 ) = [ \delta \omega , 0 , \Omega ]^T$.
	Figure \ref{Numerical_vs_Perturbative_plot} shows the results for two different ratios of the perturbation to the "primary" angular velocity.

	\begin{figure}[ht]
		\subfloat[$\delta\omega/\Omega = 0.1$]{
			\begin{minipage}[c][1\width]{
				0.5\textwidth}
			 	\centering
			 	\includegraphics[width=1\textwidth]{Num_vs_Perturbed_Comparison_0.100scale.eps}
		  	\end{minipage}}
		\subfloat[$\delta\omega/\Omega = 0.5$]{
		  	\begin{minipage}[c][1\width]{
				0.5\textwidth}
				\centering
				\includegraphics[width=1\textwidth]{Num_vs_Perturbed_Comparison_0.500scale.eps}
		  	\end{minipage}}
	  	\caption{Comparison of the perturbative and numerical solutions for the same body with initial conditions ratio $\delta \omega/\Omega$ equal to 0.1  and 0.5 respectively.}\label{Numerical_vs_Perturbative_plot}
	\end{figure}

	It is quite evident that the approximation holds much better in the first case where $O(\delta\omega^2)$ is only a 1\% correction to the end result.
	One can hardly see a deviation between the two results in this case.
	In the latter case, the perturbative solution starts exactly on the numerical one but it diverges much faster since the error $O(\delta \omega^2)$ is about 25\% ($0.5^2$) and higher terms would be needed to retain the accuracy.
	This is well expected and of course the larger the ratio of $\delta \omega/\Omega$ becomes, the faster our approximation will break down.

	Nevertheless, it shows that the numerical solver acts just as it should in the "weak regime" of the equations which is often a good test when no exact solution in the full non-linear regime can be obtained.
	One can easily check that similar small deviations with primary $\Omega$ around the smallest moment of inertia (X-axis) will also lead to very good agreement between the perturbative solution and the numerical solver.

	On the other hand, some of you might try to modify the code in order to see how well the approximation works in the case of intermediate axis rotation.
	More generally, you might want to see if you can see the "flip" of the T-handle from the perturbation-only?
	A quick glimpse at \eqref{dom1_sol_unstable} should persuade you that the answer is a "no".
	The two exponentials are completely unconstrained - arbitrarily small perturbation $\delta \omega$ will grow indefinitely which is definitely not a physical behaviour.
	The reason for that is very simple -- the linear equations \eqref{Perturbed_Euler_1}-\eqref{Perturbed_Euler_3} are only an approximation around an equilibrium point.
	If you think about the differential equation as a trajectory in some 3D space (in this case the space would be a hypersphere), linearizing the equations is equivalent to solving them on a hyperplane which is tangent to that space instead of doing so on the real space.
	The linearization "blurs" the curvature of the solution around the equilibrium point.
	Of course, if you stay very close to the point - this is a good approximation (the Earth looks flat up close).
	When the solution is inherently unstable, however (like the intermediate axis case), it moves away from that equilibrium and towards a region where the approximation fails completely.
	This is quite apparent in this case - the real angular velocity of an isolated body cannot change indefinitely (it is bound by the conservation of angular momentum), while the perturbative solution \eqref{dom1_sol_unstable} predicts unbounded growth.
	Geometrically speaking - one is a trajectory on a sphere while the other is its projection on a plane tangent to that sphere (with the pole at infinity)!
	If you are interested in the geometrical description and interpretation of such phenomena, \cite{Geometry_Frankel} is an excellent book with many applications (including rigid body dynamics) to get you started!

	\section{Conclussions}
	I started this small project to explain a phenomenon which I found quite interesting to model as an undergraduate.
	Probably the most interesting thing about the intermediate axis theorem to me was that it is something we can see all around us (even by throwing a textbook) but very few people have an intuition about it even though it was first described some 200 years ago!
	Beyond the phenomenon itself, I hope that you found its description and my rumbling about stability, non-linear systems and geometry useful (or at least interesting)!
	This document and the accompanying code have no purpose beyond education!

	\newpage
	\begin{thebibliography}{9}

	\bibitem{David_Tong}
	D.Tong, \textit{Lecture Notes on Classical Dynamics}, available \href{http://www.damtp.cam.ac.uk/user/tong/dynamics/clas.pdf}{here}.

	\bibitem{Goldstein}
	H.~Goldstein, C.~Poole and J.~Safko, \textit{Classical Mechanics}.

	\bibitem{ADCS_bible}
	F.~Landis~Markley and John~L.~Crassidis, \textit{Fundamentals of Spacecraft Attitude Determination and Control}.

	\bibitem{Geometry_Frankel}
	T.~Frankel, \textit{The Geometry of Physics: An Introduction}.

	\end{thebibliography}

\end{document}


\section{Task C}
\label{sec:task-c}

The second boundary value problem has been solved using Matlab script
\texttt{forming\_process.m} (see section \ref{app:matlab-code}).
Figure \ref{fig:forming-bvp} shows total reaction force plotted against
displacement of the top nodes and the shape of the plate before
and after the deformation.
\begin{figure}[th]
  \centering
  % Reaction force
  \begin{subfigure}[t]{\textwidth}
    \begin{tikzpicture}
      \begin{axis}[
        width = 0.95\textwidth,
        height=\axisdefaultheight,
        tick label style={/pgf/number format/fixed},
        try min ticks=6,
        minor tick num=1,
        grid=both,
        xlabel = {\(u_{x}\), [mm]},
        ylabel = {Reaction force, [N]},
        xmin=0, xmax=5, ymax=1.6e4
        ]
        \addplot table[skip first n=1] {data/force_displacement_forming.dat};
      \end{axis}
    \end{tikzpicture}
    \caption{}
  \end{subfigure}

  % Geometry
  \begin{subfigure}[t]{\textwidth}
    \begin{tikzpicture}
      \begin{axis}[
        width = 0.95\textwidth,
        try min ticks=6,
        enlargelimits=false,
        axis equal image,
        axis on top,
        xlabel={\(x\), [mm]},
        ylabel={\(y\), [mm]}
        ]
        \addplot graphics[xmin=0,xmax=24.66,
                          ymin=0,ymax=20] {plate_with_hole_mesh};
      \end{axis}
    \end{tikzpicture}
    \caption{}
  \end{subfigure}
  \caption{(a) Reaction force versus displacement and (b) deformed mesh.}
  \label{fig:forming-bvp}
\end{figure}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../main"
%%% End:


\subsubsection{Total number of messages sent}\label{subsubsec:hd2krmessages}

This is an indication of the energy efficiency of the entire network.

The maximum number of copies is the dominant factor (\(85.98\%\)), as expected.
Also the broadcast radius \(R\) and its combination with the maximum
number of copies have a valuable impact on this index. The unexplained variation
is very low (\(0.53\%\)).

In \figref{subfig:hdperfmessagesm} we see that the total number of messages sent
decreases with an lower value of the \code{maxCopies} parameter, as expected.
We note that with \(m\!=\!6\) we get that nearly all the users of the network
relay the message. In \figref{subfig:hdperfmessagesR} we can see that less
messages are sent if an higher broadcast radius is used when \(m\!=\!2\). So,
with a low \(m\), we can further decrease the total number of messages sent by
increasing \(R\). Of course increasing the broadcast radius is not good for our
purpose to optimize the energy efficiency of the network, but as we can see from
\figref{subfig:hdperfmessagesT}, the same consideration made for \(R\) are also
valid for the hear window size \(T\). We perhaps expect \(T\) to hugely affect
the total broadcast time, so the selection of the factor \(T\) is a trade-off
between the energy efficiency and the total broadcast time.

\begin{figure}[hbt]
	\centering
	\begin{subfigure}[b]{0.33\textwidth}
		\centering
		\includegraphics[width=\textwidth]{img/hd/messages-m-perfplot}
		\caption{When the maximum number of copies is reduced the total
		number of messages sent decreases}\label{subfig:hdperfmessagesm}
	\end{subfigure}
	\begin{subfigure}[b]{0.33\textwidth}
		\centering
		\includegraphics[width=\textwidth]{img/hd/messages-R-perfplot}
		\caption{Increase the broadcast radius when \(m\) is low to
		reduce the total number of messages
		sent}\label{subfig:hdperfmessagesR}
	\end{subfigure}
	\begin{subfigure}[b]{0.32\textwidth}
		\centering
		\includegraphics[width=\textwidth]{img/hd/messages-T-perfplot}
		\caption{Increase the size of the hear window when \(m\) is low
		to reduce the total number of messages
		sent}\label{subfig:hdperfmessagesT}
	\end{subfigure}
	\caption{Performance plots for the total number of messages
	sent}\label{fig:hdperfmessages}
\end{figure}


    \filetitle{\#MxN}{Specify the subplot division of the figure windows}{qreportlang/subplot}

	\paragraph{Syntax with user-suplied
subdivision}\label{syntax-with-user-suplied-subdivision}

\begin{verbatim}
#MxN
\end{verbatim}

\paragraph{Syntax for automatic
subdivision}\label{syntax-for-automatic-subdivision}

\begin{verbatim}
#auto
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{M} {[} numeric {]} - Number of rows of graphs in each figure
  window from this point on.
\item
  \texttt{N} {[} numeric {]} - Number of columns of graphs in each
  figure window from this point on.
\end{itemize}

\paragraph{Description}\label{description}

\paragraph{Example}\label{example}

This command produces figure windows with 3x2 graphs in each.

\begin{verbatim}
#3x2
\end{verbatim}


\hypertarget{functionals}{%
\section{Functionals}\label{functionals}}

A number of \texttt{functionals} are available in Morpho. Each of these
represents an integral over some \texttt{Mesh} and \texttt{Field}
objects (on a particular \texttt{Selection}) and are used to define
energies and constraints in an \texttt{OptimizationProblem} provided by
the \texttt{optimize} module.

Many functionals are built in. Additional functionals are available by
importing the \texttt{functionals} module:

\begin{lstlisting}
import functionals
\end{lstlisting}

Functionals provide a number of standard methods:

\begin{itemize}

\item
  \texttt{total}(mesh) - returns the value of the integral with a
  provided mesh, selection and fields
\item
  \texttt{integrand}(mesh) - returns the contribution to the integral
  from each element
\item
  \texttt{gradient}(mesh) - returns the gradient of the functional with
  respect to vertex motions.
\item
  \texttt{fieldgradient}(mesh, field) - returns the gradient of the
  functional with respect to components of the field
\end{itemize}

Each of these may be called with a mesh, a field and a selection.

\hypertarget{length}{%
\section{Length}\label{length}}

A \texttt{Length} functional calculates the length of a line element in
a mesh.

Evaluate the length of a circular loop:

\begin{lstlisting}
import constants
import meshtools
var m = LineMesh(fn (t) [cos(t), sin(t), 0], 0...2*Pi:Pi/20, closed=true)
var le = Length()
print le.total(m)
\end{lstlisting}

\hypertarget{areaenclosed}{%
\section{AreaEnclosed}\label{areaenclosed}}

An \texttt{AreaEnclosed} functional calculates the area enclosed by a
loop of line elements.

\begin{lstlisting}
var la = AreaEnclosed()
\end{lstlisting}

\hypertarget{area}{%
\section{Area}\label{area}}

An \texttt{Area} functional calculates the area of the area elements in
a mesh:

\begin{lstlisting}
var la = Area()
print la.total(mesh)
\end{lstlisting}

\hypertarget{volumeenclosed}{%
\section{VolumeEnclosed}\label{volumeenclosed}}

A \texttt{VolumeEnclosed} functional is used to calculate the volume
enclosed by a surface. Note that this estimate may become inaccurate for
highly deformed surfaces.

\begin{lstlisting}
var lv = VolumeEnclosed()
\end{lstlisting}

\hypertarget{volume}{%
\section{Volume}\label{volume}}

A \texttt{Volume} functional calculates the volume of volume elements.

\begin{lstlisting}
var lv = Volume()
\end{lstlisting}

\hypertarget{scalarpotential}{%
\section{ScalarPotential}\label{scalarpotential}}

The \texttt{ScalarPotential} functional is applied to point elements.

\begin{lstlisting}
var ls = ScalarPotential(potential, gradient)
\end{lstlisting}

You must supply two functions (which may be anonymous) that return the
potential and gradient respectively.

This functional is often used to constrain the mesh to the level set of
a function. For example, to confine a set of points to a sphere:

\begin{lstlisting}
import optimize
fn sphere(x,y,z) { return x^2+y^2+z^2-1 }
fn grad(x,y,z) { return Matrix([2*x, 2*y, 2*z]) }
var lsph = ScalarPotential(sphere, grad)
problem.addlocalconstraint(lsph)
\end{lstlisting}

See the thomson example for use of this technique.

\hypertarget{linearelasticity}{%
\section{LinearElasticity}\label{linearelasticity}}

The \texttt{LinearElasticity} functional measures the linear elastic
energy away from a reference state.

You must initialize with a reference mesh:

\begin{lstlisting}
var le = LinearElasticity(mref)
\end{lstlisting}

Manually set the poisson's ratio and grade to operate on:

\begin{lstlisting}
le.poissonratio = 0.2
le.grade = 2
\end{lstlisting}

\hypertarget{equielement}{%
\section{EquiElement}\label{equielement}}

The \texttt{EquiElement} functional measures the discrepency between the
size of elements adjacent to each vertex. It can be used to equalize
elements for regularization purposes.

\hypertarget{linecurvaturesq}{%
\section{LineCurvatureSq}\label{linecurvaturesq}}

The \texttt{LineCurvatureSq} functional measures the integrated
curvature squared of a sequence of line elements.

\hypertarget{linetorsionsq}{%
\section{LineTorsionSq}\label{linetorsionsq}}

The \texttt{LineTorsionSq} functional measures the integrated torsion
squared of a sequence of line elements.

\hypertarget{meancurvaturesq}{%
\section{MeanCurvatureSq}\label{meancurvaturesq}}

The \texttt{MeanCurvatureSq} functional computes the integrated mean
curvature over a surface.

\hypertarget{gausscurvature}{%
\section{GaussCurvature}\label{gausscurvature}}

The \texttt{GaussCurvature} computes the integrated gaussian curvature
over a surface.

\hypertarget{gradsq}{%
\section{GradSq}\label{gradsq}}

The \texttt{GradSq} functional measures the integral of the gradient
squared of a field. The field can be a scalar, vector or matrix
function.

Initialize with the required field:

\begin{lstlisting}
var le=GradSq(phi)
\end{lstlisting}

\hypertarget{nematic}{%
\section{Nematic}\label{nematic}}

The \texttt{Nematic} functional measures the elastic energy of a nematic
liquid crystal.

\begin{lstlisting}
var lf=Nematic(nn)
\end{lstlisting}

There are a number of optional parameters that can be used to set the
splay, twist and bend constants:

\begin{lstlisting}
var lf=Nematic(nn, ksplay=1, ktwist=0.5, kbend=1.5, pitch=0.1)
\end{lstlisting}

These are stored as properties of the object and can be retrieved as
follows:

\begin{lstlisting}
print lf.ksplay
\end{lstlisting}

\hypertarget{nematicelectric}{%
\section{NematicElectric}\label{nematicelectric}}

The \texttt{NematicElectric} functional measures the integral of a
nematic and electric coupling term integral((n.E)\^{}2) where the
electric field E may be computed from a scalar potential or supplied as
a vector.

Initialize with a director field \texttt{nn} and a scalar potential
\texttt{phi}: var lne = NematicElectric(nn, phi)

\hypertarget{normsq}{%
\section{NormSq}\label{normsq}}

The \texttt{NormSq} functional measures the elementwise L2 norm squared
of a field.

\hypertarget{lineintegral}{%
\section{LineIntegral}\label{lineintegral}}

The \texttt{LineIntegral} functional computes the line integral of a
function. You supply an integrand function that takes a position matrix
as an argument.

To compute \texttt{integral(x\^{}2+y\^{}2)} over a line element:

\begin{lstlisting}
var la=LineIntegral(fn (x) x[0]^2+x[1]^2)
\end{lstlisting}

The function \texttt{tangent()} returns a unit vector tangent to the
current element:

\begin{lstlisting}
var la=LineIntegral(fn (x) x.inner(tangent()))
\end{lstlisting}

You can also integrate functions that involve fields:

\begin{lstlisting}
var la=LineIntegral(fn (x, n) n.inner(tangent()), n)
\end{lstlisting}

where \texttt{n} is a vector field. The local interpolated value of this
field is passed to your integrand function. More than one field can be
used; they are passed as arguments to the integrand function in the
order you supply them to \texttt{LineIntegrand}.

\hypertarget{areaintegral}{%
\section{AreaIntegral}\label{areaintegral}}

The \texttt{AreaIntegral} functional computes the area integral of a
function. You supply an integrand function that takes a position matrix
as an argument.

To compute integral(x*y) over an area element:

\begin{lstlisting}
var la=AreaIntegral(fn (x) x[0]*x[1])
\end{lstlisting}

You can also integrate functions that involve fields:

\begin{lstlisting}
var la=AreaIntegral(fn (x, phi) phi^2, phi)
\end{lstlisting}

More than one field can be used; they are passed as arguments to the
integrand function in the order you supply them to
\texttt{AreaIntegrand}.

\hypertarget{floryhuggins}{%
\section{FloryHuggins}\label{floryhuggins}}

The \texttt{FloryHuggins} functional computes the Flory-Huggins mixing
energy over an element:

\begin{lstlisting}
a*phi*log(phi) + b*(1-phi)+log(1-phi) + c*phi*(1-phi)
\end{lstlisting}

where a, b and c are parameters you can supply. The value of phi is
calculated from a reference mesh that you provide on initializing the
Functional:

\begin{lstlisting}
var lfh = FloryHuggins(mref)
\end{lstlisting}

Manually set the coefficients and grade to operate on:

\begin{lstlisting}
lfh.a = 1; lfh.b = 1; lfh.c = 1;
lfh.grade = 2
\end{lstlisting}


\documentclass[10pt,letter]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[makeroom]{cancel}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{stmaryrd}
\def\fatbar{\talloblong}
\onehalfspacing
\usepackage{fullpage}
\newcommand{\R}{\mathbb{R}}
\newcommand{\inner}{\langle\cdot,\cdot\rangle}
\newcommand{\inr}[2]{\langle #1, #2\rangle}
\newcommand\norm[1]{\left\lVert#1\right\rVert}

\graphicspath{ {images/} }

\begin{document}

\title{CS142 Homework Set \#7 Solutions}

\author{Timur Kuzhagaliyev}

%\date{21st November, 2017}

\maketitle

\section*{Problem 1}

Intuitively, the algorithm should work because the logic described in the question is equivalent to running two instances of the 1-bit algorithm simultaneously and in perfect sync, so the messages could be superimposed.

The variables for officers remain the same as in proof used in class, except now $commit[u, r]$, $confirm[u, x, r]$ and $confirmationCount[u,r]$ operate on vectors of size 2, updating relevant vectors component-wise. Initially, the values for each commander are $(false, false)$, $(false, false)$ and $(0, 0)$ respectively, and general makes the decision to commit following the logic described in class, but now applied to each bit separately.

The officers now can also send out multiple commit messages, one for committing each bit while the other bit is uncommitted. Alternatively, if the officer commits both bits at the same time, a single commit message is sent. If an officer receives multiple commit messages from the some other officer, it takes the a logical OR of all messages.

I will now show that all of the lemmas and theorems defined for the 1-bit version of the algorithm hold for the 2 bit version.

\begin{enumerate}
\item{Lemma 1: $confirm[u,v,r] = commit[v,r - 1]$.}

\textbf{Proof:} If a non-faulty general $v$ commits one (or both) of its bits in round $r - 1$, it will send out a $commitMessage[v]$ to all other officers, containing the values for each bit. This means that all non-faulty generals $u$ would have received $commitMessage[v]$ by round $r$, and hence $confirm[u,v,r]$ holds (for the relevant component).
\item{Lemma 2: $confirm[u,x,r-1] \Rightarrow confirm[v, x, r]$ where $u, v$ are non-faulty officers.}

\textbf{Proof:} When $u$ received a 2-component $commitMessage[x]$ in $r - 1$, it has relayed it to all officers including $v$. In round $r$, $v$ has received $commitMessage[x]$ so $confirm[v, x, r]$ holds (for the relevant components).
\item{Theorem 1: If a non-faulty officer commits one (or both) of the bits in round r, then all non-faulty officers commit the same bits by round r + 1, where the comparison is defined component-wise}

\textbf{Proof:} We know that $commit[u, r] = commit[u, r - 1] \lor ((confirmationCount[u, r] \geq r) \land confirm[u,g,r])$. Suppose $u$ commits one (or both) bits for the first time in $r$, so the first part of the condition must be false. In this case, for $u$ to commit some bits it must have received at least $r$ commit messages from other officers and a commit message from the general for the same bits. Now, $u$ will relay all of the commit messages it has (including the general's and its own commit) to other generals.

This means that by round $r+1$, all non-faulty officers $v$ would have received $r+1$ commit messages and a commit message from the general for some bits, so $confirmationCount[v, r] \geq r) \land confirm[v,g,r]$ would hold and they would commit the same bits.

\item{Theorem 2: If no non-faulty officers commit one (or both) of their bits in the first t rounds, then no non-faulty officer will commit the same bits in later rounds, where $t$ is the maximum number of faulty generals.}

\textbf{Proof:} We know that $commit[u, r] = commit[u, r - 1] \lor ((confirmationCount[u, r] \geq r) \land confirm[u,g,r])$, similarly: $commit[u, t+1] = commit[u, t] \lor ((confirmationCount[u, t+1] \geq t + 1) \land confirm[u,g,t+1])$, for each component of the vector separately. By assumption, $commit[u, t]$ does not hold for the relevant bits, for all non-faulty officers $u$. This means that each non-faulty agent can have a maximum of $t$ commit messages for the relevant bit (from the faulty officers), and at round $t+1$ the condition $confirmationCount[u, t+1] \geq t + 1$ will not hold. Hence $commit[u, t+1]$ will be $false$, for round $t+1$ and all subsequent rounds (if any).
\end{enumerate}

The proofs above demonstrate that the algorithm will indeed work, much like the algorithm discussed in class.

\section*{Problem 2}

The algorithm would not work if forgery is allowed. The original version, which relied on the fact that forgery was disallowed, made heavy use of the fact that the message from the general could not be forged (i.e. $confirm[u,g,r]$ would always be what the general originally sent, whether the general is faulty or non-faulty).

We will exploit this fact to show how the safety property can be violated. Consider the case where the general is non-faulty and it sends out a command to retreat. Our safety property dictates that all non-faulty officers must obey this command.

Now, the 2 faulty commanders can work together - they can both modify the general's message to make it look like the original command was attack, and then send out their commit messages and the forged general message to some non-faulty commanders during round 1.

In round 2, these non-faulty commanders would have received at least 2 commit messages (from the faulty commanders) and the (forged) attack message from the general, hence they will commit. This violates the safety property as the non-faulty commanders disobeyed the order of the non-faulty general, and they cannot change their decision.

Therefore the algorithm is not correct in the case where forger is allowed.

\pagebreak

\section*{Problem 3}

\paragraph{a)} Types of messages used in the Paxos algorithm:

\begin{enumerate}
\item{\textbf{prepare(n)}.} This message type has a single argument $n$ which is a value in a total order. In this code $n$ is a pair $(t, id)$, where $t$ is a non-negative integer that can only increase and $id$ is the static ID for a particular agent, and the comparisons in total order are made lexicographically. To simplify the descriptions in the future I'll refer to pairs $(t, id)$ in the total order as "timestamps".

Message type \textbf{prepare(n)} is sent by a proposer and is received by an acceptor.

\item{\textbf{granted(n, latest\_accepted, latest\_accepted\_n)}.} This message type has three arguments: $n$ and $latest\_accepted$ are timestamps, $latest\_accepted$ is some value representing the proposal that was last accepted by the acceptor. If $latest\_accepted$ is not $None$, then $latest\_accepted\_n$ contains the timestamp for which the proposal was accepted. $n$ contains the value of the corresponding (successful) \textbf{prepare(n)} message.

Message type \textbf{granted(n, latest\_accepted, latest\_accepted\_n)} is sent by an acceptor and is received by a proposer.

\item{\textbf{nack\_prepare(n)}.} This message contains a single argument $n$. It is used to notify proposers that their message \textbf{prepare(n)} was not successful.

Message type \textbf{nack\_prepare(n)} is sent by an acceptor and is received by a proposer.

\item{\textbf{accept(n, proposal)}.} This message contains two arguments: $n$ is the timestamp that corresponds to the last \textbf{prepare(n)} message the proposer sent (and subsequently the returned \textbf{granted(n, ...)} message), $proposal$ is some value that the proposer wishes to propose (subject to constraints described below).

Message type \textbf{accept(n, proposal)} is sent by an proposer and is received by a acceptor.

\item{\textbf{accepted(proposal)}.} The message contains a single argument, $proposal$, which corresponds to the value that the acceptor has accepted.

Message type \textbf{accepted(proposal)} is sent by an acceptor and is received by a learner.
\end{enumerate}

\pagebreak

\paragraph{b)} Local variables for each type of agent in the Paxos algorithm:

\begin{enumerate}
\item{\textbf{Proposer}.} A local variable of proposer is $last\_n$, which is a timestamp. $last\_n$ is the last value of $n$ sent by this proposer in a \textbf{prepare(n)} message. The other local variable is $grantors$, which is a set of \textbf{granted(n, ...)} messages received by this proposer, used to track when the majority of acceptors are ready to accept a proposal. $grantors$ is reset to an empty set every time the proposer sends out a new \textbf{prepare(n)} message and updates $last\_n$. Initially $last\_n$ is some $None$ and $grantors$ is an empty set.

\item{\textbf{Acceptor}.} Acceptors maintain three local variables. $highest\_prepare\_n$ is a timestamp corresponding to the highest value of $n$ acceptor has received through a \textbf{prepare(n)} message. $latest\_accepted$ and $latest\_accepted\_n$ correspond to the latest accepted proposal and the timestamp of that proposal respectively. Said proposal must have been received through a (successful) \textbf{accept(n, proposal)} message. Initially $highest\_prepare\_n$ is some arbitrary negative value, while $latest\_accepted$ and $latest\_accepted\_n$ are both $None$.

\item{\textbf{Learner}.} Learner maintains two local variables. First variable is $learned$, which corresponds to the value of the proposal the learner has learned by receiving an \textbf{accepted(proposal)} message. The second variable is the hashmap $proposals$. The keys in this hashmap are the IDs of acceptors and the values are the proposals received from relevant agents through \textbf{accepted(proposal)} messages. Initially $learned$ is $None$ and $proposals$ is a hashmap with a value for each acceptor ID initialised to $None$.
\end{enumerate}

\pagebreak

\paragraph{c)} Programs for each agent type in the Paxos algorithm written in (loose) UNITY notation can be found below. Note that in code any non-$None$ timestamp is greater than $None$. Additionally, if the message has been received by there is no action corresponding to it (e.g. the second part of guard was not satisfied), the message is ignored.
\begin{align*}
\textrm{\textbf{Program}} \qquad & \textrm{Proposer}
\\
\textrm{\textbf{var}} \qquad & id\; \textrm{:\; Unique ID of this agent}
\\ & last\_n\; \textrm{:\; $(t, id)$}
\\ & grantors\; \textrm{:\; set of received \textbf{granted(...)} messages}
\\
\textrm{\textbf{initially}} \qquad & last\_n = None
\\
\land\; & grantors = \varnothing
\\
\textrm{\textbf{actions}} \qquad &
\end{align*}
\begin{align*}
\textbf{if}\quad &triggered
\\
\textbf{then}\quad &\textrm{Pick some $n = (t, id)$ where $t$ is higher than that in $last\_n$,}
\\ &\textrm{send prepare(n) message to a majority set of acceptors,}
\\ &\textrm{assign } last\_n, grantors := n, \varnothing.
\\\\
\textbf{if}\quad &\textrm{receive } granted(n, latest\_accepted, latest\_accepted\_n) \land n = last\_n
\\ \textbf{then}\quad &\textrm{add the received \textbf{granted(...)} message to the $grantors$ set.}
\\\\
\textbf{if}\quad &\textrm{$grantors$ set contains messages from the majority of acceptors} \land \textrm{$latest\_accepted = None$ in all messages}
\\ \textbf{then}\quad &\textrm{pick some value $proposal$ representing a desired proposal,}
\\ &\textrm{send out \textbf{accept(last\_n, proposal)} message to acceptors appearing in $grantors$ set,}
\\ &\textrm{assign } grantors := \varnothing.
\\\\
\textbf{if}\quad &\textrm{$grantors$ set contains messages from the majority of acceptors} \land \textrm{$latest\_accepted \neq None$ in some messages}
\\ \textbf{then}\quad &\textrm{search messages in $grantors$ to find the $proposal$ corresponding to the highest $latest\_accepted\_n$,}
\\ &\textrm{send out \textbf{accept(last\_n, proposal)} message to acceptors appearing in $grantors$ set,}
\\ &\textrm{assign } grantors := \varnothing.
\end{align*}

\pagebreak

\begin{align*}
\textrm{\textbf{Program}} \qquad & \textrm{Acceptor}
\\
\textrm{\textbf{var}} \qquad & latest\_accepted\; \textrm{:\; proposal}
\\ & highest\_prepare\_n, latest\_accepted\_n\; \textrm{:\; $(t, id)$}
\\
\textrm{\textbf{initially}} \qquad & highest\_prepare\_n = latest\_accepted = latest\_accepted\_n = None
\\
\textrm{\textbf{actions}} \qquad &
\end{align*}
\begin{align*}
\textbf{if}\quad &\textrm{receive } prepare(n) \land n > highest\_prepare\_n
\\ \textbf{then}\quad &\textrm{respond with \textbf{granted(n, latest\_accepted, latest\_accepted\_n)},}
\\ &\textrm{assign } highest\_prepare\_n := n.
\\\\
\textbf{if}\quad &\textrm{receive } prepare(n) \land n \leq highest\_prepare\_n
\\ \textbf{then}\quad &\textrm{respond with \textbf{nack\_prepare(n)}.}
\\\\
\textbf{if}\quad &\textrm{receive } accept(n, proposal) \land n \geq highest\_prepare\_n
\\ \textbf{then}\quad &\textrm{send out \textbf{accepted(proposal)} to all learners,}
\\ &\textrm{assign } latest\_accepted, latest\_accepted\_n := proposal, n.
\end{align*}

\pagebreak

\begin{align*}
\textrm{\textbf{Program}} \qquad & \textrm{Learner}
\\
\textrm{\textbf{var}} \qquad & learned\; \textrm{:\; proposal}
\\ & proposals\; \textrm{:\; hashmap with acceptor IDs as keys and proposals as values}
\\
\textrm{\textbf{initially}} \qquad & learned = None
\\
\land\; & (\forall\, id : id \in \{\textrm{acceptor IDs}\} : proposals[id] = None)
\\
\textrm{\textbf{actions}} \qquad &
\end{align*}
\begin{align*}
\textbf{if}\quad &\textrm{receive } accepted(proposal)
\\ \textbf{then}\quad &\textrm{assign } proposals[id] := proposal, \textrm{ where $id$ is ID of the sender.}
\\\\
\textbf{if}\quad &\textrm{majority of values in $proposals$ are equal to some value $proposal$} \land proposal \neq None
\\ \textbf{then}\quad &\textrm{assign } learned := proposal.
\end{align*}

\end{document}


\documentclass{article} % For LaTeX2e
\usepackage{nips13submit_e,times}
\usepackage{hyperref}
\usepackage{url}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{breakurl}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage{fancyvrb}
\usepackage{booktabs}
\usepackage[font=footnotesize,labelfont=bf]{caption}
% \documentstyle[nips13submit_09,times,art10]{article} % For LaTeX 2.09
\bibliographystyle{ieeetr}

\title{Predicting water pump failure in Tanzania and optimising maintenance routes}

\author{
Sam Kim \\
Harvard College\\
Cambridge, MA 02138 \\
\texttt{samuelkim@college.harvard.edu} \\
\And
Gareth Haslam \\
Harvard Extension School\\
Cambridge, MA 02138 \\
\texttt{haslam.gareth@gmail.com} \\
}

% The \author macro works with any number of authors. There are two commands
% used to separate the names and addresses of multiple authors: \And and \AND.
%
% Using \And between authors leaves it to \LaTeX{} to determine where to break
% the lines. Using \AND forces a linebreak at that point. So, if \LaTeX{}
% puts 3 of 4 authors names on the first line, and the last on the second
% line, try using \AND instead of \And before the third author name.

\newcommand{\fix}{\marginpar{FIX}}
\newcommand{\new}{\marginpar{NEW}}

\nipsfinalcopy % Uncomment for camera-ready version

\begin{document}

\maketitle

\begin{abstract}
We describe MCMC techniques to model the functionality of water pumps in Tanzania and suggest an optimised route for their maintenance. Using field data describing various attributes of each pump, such as construction year, installer, and location, we describe a method to predict the current status of the pump from a set of three possibilities (functioning, functioning needs repair, and not functioning). We also optimize the route that a maintenance crew could take to repair damaged pumps in a version of the well--known, NP--hard, traveling salesman problem. Code and more information are available at \url{http://ghaslam.github.io/AM207/}.
\end{abstract}

\section{Introduction}

Access to clean water is a fundamental need for people all over the world. Around 11\% of the world's population currently lack such access due to either lack of natural resources or lack of infrastructure \cite{UN2013}. In Tanzania, despite the availability of water, only 33.5\% have access to a piped source \cite{Morisset2012}. Many others depend on pumps for their supply. When these pumps fail, that can create serious problems for the people who depend on them. Previous work on predicting the failure of mechanical devices such as pumps has benefitted from the use of both statistical analysis as well as data obtained from direct observation of the machines, for example diagnostic vibration sensors \cite{Nakamura2007}. As the popularity of machine learning and 'big data' continues to grow, researchers are also studying how failure can be predicted based on training data from large historical datasets, for example for rod pumps in the oil industry \cite{Liu2013}. Liu's research used subject matter experts to first identify whether a pump was operating normally or about to fail and then trained their model to recognise the features in the data such as daily usage, that predicted the label.

We aim to use Bayesian Methods to assign one of three categorical labels indicating the functional status each pump (functioning, functioning needs repair, and not functioning). The Bayesion models will be built on training data, then used to predict the functional status for wells with unknown status. Then, we treat the repair of the damaged pumps as a constrained optimisation problem over the space of all possible routes that the maintenance crew could take to reach the damaged wells. We ignore the existing road infrastructure, terrain, height etc. and assume that all locations are equally accessible.

\section{The Data}

The data for this project was provided as part of a data science challenge run by DrivenData.org \cite{DrivenData2015}. DrivenData aims to encourage data scientists from around the world to take part in online competitions to develop predictive statistical models that can address important problems in the fields of health, international development, and the environment. The data for the Tanzanian water pump competition was aggregated from the Tanzanian Ministry of Water by Taarifa, an open source platform for tracking infrastructure related issues \cite{Taarifa2015}.  The dataset contains records of 59,400 water pumps with information on a range of 39 parameters, such as pump construction year, installer, type of pump, water quality, population around the well, cost of water, quantity and others. These include a mix of categorical and numerical data, and there are also many missing values. Each pump is assigned a unique ID as well as precise coordinates for its locations. A subset of the parameters are shown in Table 1. The final column in Table 1 indicates the current status of the pump which the model will attempt to predict. The optimisation algorithm will then attempt to find the route that reaches the damaged pumps in the shortest distance.

\begin{table}[H]
\caption{Sample of the DrivenData Tanzania water wells dataset}
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{lrlrlrrrr}
\toprule
ID &         Installer        &  GPS Height & Longitude & Latitude &   Construction\_Year &  Source\_Type &  Quantity\_Type & Status\_Group  \\
\midrule
69572 &   Roman         & 1390             &       34.93 &  -9.86 &   1983 &                         Spring &          Enough  &           Functioning \\
8776 &   Grumeti          & 1399             &      34.69 &  -2.15 &   2002 &                          Rainwater &           Insufficient &          Functioning Needs Repair  \\
34310 &   World Vision & 686               &    37.46 &  -3.82 &   1967 &                           Dam &           Enough &           Functioning Needs Repair \\
67743 &   UNICEF       & 263                &   38.49 &  -11.15  &   1997 &                         Machine &           Dry &         Non-functioning \\
19728 &   Artisan          &  0                  &   31.13 &     -1.83 &   2007 &                         Rainwater &           Seasonal &           Functioning  \\
\bottomrule
\end{tabular}
}
\end{table}

In addition to the parameters given in the original dataset, we calculate some additional parameters such as age (based on the construction year), and relative remoteness (based on the average distance of the 5 nearest neighbours). Figure \ref{fig:map-phys} shows a map of the physical geography of Tanzania and Figure \ref{fig:map-points} showing the locations of the pumps. The high density of pumps appears as a solid blue colour but actually represents many individual points.

\begin{figure}
  \centering
  \begin{subfigure}[b]{0.5\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/Tanzania}
    \caption{Physical geography of Tanzania (courtesy Wikimedia).}
    \label{fig:map-phys}
  \end{subfigure}~\begin{subfigure}[b]{0.5\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/PumpMap}
    \caption{Distribution of water pumps.}
    \label{fig:map-points}
  \end{subfigure}
  \caption{Geography of Tanzania and distribution of water pumps.}
  \label{fig:map}
\end{figure}

\section{Modeling water well functionality}

\subsection{Model Classification}

Bayesian methods are only compatible with numerical data and do not work directly with classification problems, so we need to convert the categorical data and classes into numerical data and labels.

For the classification, we can take two approaches:
\begin{enumerate}
\item Assume that the 3 labels, "non functional", "functional needs repair", and "functional" lie on a spectrum where the distance between "non functional" and "functional" is greater than the difference between "non functional" and "functional needs repair" or "functional needs repair" and "functional. We can then have the Bayesian model predict a number $y$, and then convert this to a label using a range, where the exact limits can be adjusted:
\begin{align*}
\text{functional: } & 0.67\leq y_i \\
\text{functional needs repair: } & 0.33<y_i<0.67 \\
\text{non functional: } &  y_i \leq 0.33
\end{align*}
\item The assumption that the 3 labels lie on a linear spectrum is not necessarily a safe assumption, and the limits are set rather arbitrarily. A more natural and common method in machine learning is to build a classifier that decides between 2 classes. In this case, we can either build 3 one-versus-one classifiers or 3 one-versus-rest classifiers and use majority vote (or probabilities in the case of a tie) to make the final classification.
\end{enumerate}

We use the first approach for its simplicity, although further work investigate the performance of the second approach, which is much more versatile at the cost of computational complexity.

During the training stage of our model, we also need to convert the given labels into numbers. Assuming that we use the first approach above for prediction, we use a similar scheme:
\begin{equation*}
y_i =
\begin{cases}
0 & \text{ non functional}\\
0.5 & \text{ functional needs repair}\\
1 & \text{ functional}
\end{cases}
\end{equation*}

\subsection{Categorical data}

Many of the data features are categorical data that do not have any numerical interpretation, so we cannot convert them the same way that we converted our label. For example, the "installer" feature has labels such as "UNICEF," "Roman," and "Artisan." To deal with these, we use one-of-k representation in which we add a new feature column for every unique value. For example, we would add the columns "installer=UNICEF," "installer=Roman," and "installer=Artisan." The "installer=UNICEF" would be 1 if that data's "installer" was "UNICEF," and 0 otherwise. So if there are $k$ unique values for a particular categorical feature, then for each data point, we add on a vector of length $k$ which has a single 1 and $k-1$ 0s.

\subsection{The model}

We model $y_i$ using a logistic function that constrains the range to $0 < y_i < 1$, and the parameter for the logistic function is controlled by the features and weights, which are also parameters that we need to calculate. Our model comes from the assumption that there are certain factors impacting decay rate, and so the functionality is dependent on this decay rate.
We then select features from the data which we have a prior belief to have a strong influence on functionality, choose priors on their distributions and construct a Metropolis-Hastings Sampler to select the optimum value for each parameter. New predictions of functionality can then be made from this model.

We also add on a term for noise, $\epsilon_i$, which we assume is Gaussian noise controlled by the standard deviation $\sigma$.

\begin{gather*}
y_i = \frac{1}{1+e^{\alpha_i}}+\sigma\epsilon_i \\
\alpha_i = \beta_0 + \beta_1 x_{i,1} + \beta_2 x_{i,2} + ... + \beta_n x_{i,n}
\end{gather*}

There are $n$ features and $n+2$ parameters ($n+1$ $\beta$s and $\sigma$). The parameters are found by sampling from the posterior:

\begin{equation*}
p(Y,\Theta)=p(Y|\Theta)p(\Theta)
\end{equation*}

\section{Prediction Results}

\subsection{Something}
\subsection{Feature selection?}
\subsection{Comparison to machine learning approaches}

The k-Nearestt Neighbors algorithm (also known as the k-NN algorithm) is a machine learning algorithm that can be applied to clssification problems. There is no training aspect to the algorithm. Predictions are made by taking the $k$ nearest data points to the desired data point in feature space, and taking the majority of their labels. The labels can also be weighted by $1/d$, where $d$ is the distance from the desired data point to the labeled data point. We apply the k-NN algorithm using only the longitude and latitude data, so we are picking the geographically nearest neighbors. Accuracy in cross-validation is shown in the below figure as a function of $k$. We see that it reaches 63\% prediction accuracy.

\includegraphics[width=8cm]{figures/kNN_accuracy_vs_K.png}

\section{Modeling water pump functionality}

\subsection{Determining a baseline}

To give a measure of the effectiveness of our model, we first determine a naive baseline based on simply predicting that any given pump follows the most popular outcome. The distribution of the pumps functional status is shown in Figure \ref{fig:stat-hist}. Excluding records which have no data for latitude and longitude, most pumps in the dataset are in the functional category (31389). The number of pumps which are either non-functional or need repair is  22268 + 3931 = 26199. The accuracy of the baseline is thus:

\begin{align*}
ACC &= \Big(\frac{TP + TN}{P + N} \Big)\\
ACC &= \Big(\frac{31389}{31389 + 26199} \Big) = 0.545\\
\end{align*}

<<<<<<< HEAD
\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{figures/stat-hist.png}
\caption{The distribution of pump functional status.}
\label{fig:stat-hist}
\end{figure}

\subsection{Machine learning methods for prediction}

There exist a wide variety of methods for classifying categorical data such as linear and logistic regression, random forests, and K-nearest neighbours (k-NN), and an overview of their details is given by Murphy \cite{Murphy}. Python contains many libraries which can quickly and easily implement these methods and we use them for initial exploration of the data, for example identifying which parameters are likely to have most predictive power. An example of using k-NN to label unknown pumps based on the status of an increasing number of nearest neighbours is shown in Figure \ref{fig:kNN}. Above 21 nearest neighbors, the model is able to outperform the naive baseline by at least 8\%.

\begin{figure}
  \centering
    \includegraphics[width=0.5\textwidth]{figures/k-NN}
    \label{fig:kNN}
  \caption{The effect of increasing number of nearest neighbors on pump functional status prediction accuracy.}
\end{figure}
=======
When slice sampling, there are a number of parameter choices that are imporant. We want to choose our rectangle widths, burn-in, and thinning parameters appropriately. In testing, a thinning value of 10 reduced autocorrelation to less than 0.1 at a time lag of 1. We can see this result in Figure . We used the Gelman-Rubin potential scale reduction factor\cite{Gelman-Rubin} to determine if we were observing favorable mixing. Generally, a value less than 1.1 indicates good mixing. In both of our dimensions, the Gelman-Rubin statistic was less than this threshold. We also calculated the Geweke statistic, which is used to indicate convergence. A value less than 2 indicates convergence and for our draws, this statistic was $\ll$ 1. We can also examine convergence by looking at the trace plots for the samples. As we see in Figure \ref{fig:trace} these appear stationary.
>>>>>>> 03649a646cfb125064021fcf8f514829f49c338a

\subsection{Constructing our model}
To try to improve our predictions, we propose to model the data as a logistic regression (logit), but with the values of the parameters chosen probabilistically by Monte Carlo Methods.  The logit is often used to predict binary variables such as pass or fail. In our dataset, we have three possible outcomes, so we choose a modified version of the function, known as the ordered logit, which is able to assign to two or more categories. The general form of our model is thus:

\begin{align*}
y_i &= \frac{1}{1 + e^{\theta_i}} + \sigma \epsilon_i \\
\theta_i &= \beta_0 + \beta_1x_{i,1} + \beta_2x_{i,2} +  ... + \beta_{n+1}x_{i,n}\\
\end{align*}

where $y_i$ is our predicted pump status and values for $\beta$ are chosen by sampling from the posterior distribution:

\begin{align*}
p(Y, \Theta) = p(Y|\Theta)p((\Theta)
\end{align*}

The sampling is conducted using a Metropolis-Hastings sampler. Given that many of our parameters are categorical variables with many levels, then to avoid overfitting and to reduce computation time, we choose an initial model that includes parameters identified in the data exploration stage to have significant impact on the outcome.

We calculated the Geweke statistic, which is used to indicate convergence. A value between -2 and 2 indicates convergence and for our draws the parameters fell within this range. We can also examine convergence by looking at the trace plots for the samples. As we see in Figure \ref{fig:trace} these appear stationary.

<<<<<<< HEAD
\begin{figure}
  \centering
  \begin{subfigure}[b]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/MH-trace}
    \caption{Diagnostic plot for beta sampling}
    \label{fig:MH-trace}
  \end{subfigure}
  \begin{subfigure}[b]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/MH-trace1}
    \caption{Diagnostic plot for sigma sampling.}
    \label{fig:MH-trace1}
  \end{subfigure}
  \caption{Trace plots for parameter sampling.}
  \label{fig:trace}
\end{figure}
=======
>>>>>>> 03649a646cfb125064021fcf8f514829f49c338a

We could also show the Geweke plots and any other diagnostics.

\subsection{Other samplers? Or Other comments?}

\section{Optimizing maintenance crew route}

In the second part of this project, we aim to suggest an optimised route by which a maintenance crew could visit the damaged pumps. The choice of locations can either be taken from the originally labeled pumps or based on the outputs of our model's labels.

\subsection{The traveling salesman problem}

Having hopefully identified a method for accurately predicting which pumps will fail, we can imagine that the Tanzanian Ministry of Water will want to task a maintenance crew with visiting each of these locations. We may also assume that the maintenance crew are based in the commercial capital of Dar Es Salaam. The crew wishes to visit all $N$ locations in as efficient manner as possible. This problem is equivalent to the traveling salesman problem (TSP), first described in 1932 by Karl Menger \cite{Menger}. A compact mathematical formulation of the problem is given by Miller et. al. \cite{Miller1960} as follows:

\begin{equation*}
\textrm{Find variables } X_{ij} \textrm{ and } U_i i,j = 1, 2, \cdots , n \textrm{ that minimize}:\\
Z = \min \sum_{i=0}^n \sum_{j\ne i,j=0}^n C_{ij} X_{ij}
\end{equation*}

\begin{align*}
\textrm{subject to} & \\
	& \sum_{i=1}^n X_{ij} = 1 && j = 1, 2 , \cdots, n \\
	& \sum_{j=1}^n X_{ij} = 1 && i = 1, 2, \cdots, n \\
	&U_i - U_j + nX_{ij} \le n-1 && i, j = 2, \cdots, n i\ne j \\
	& x_{ij} \in \{0, 1\} && i,j=0, \cdots, n \\
\end{align*}

As is clear from the constraints, this is an integer linear program (ILP) where:

\begin{itemize}
  \item $x_{ij}$ is a binary decision variable indicating whether we go from location $i$ to location $j$.
  \item $c_{ij}$ is the distance\footnote{In our application, we deal with geospatial data on a large enough scale that the Euclidean distance is actually very imprecise. In order to model distances over the planet's surface, we use the Haversine formula.} between location $i$ and location $j$.
  \item The objective function is the sum of the distances for routes that we decide to take.
  \item The final constraint ensures that all locations are visited once and only once.
\end{itemize}

The problem, of course, is that brute force solution of the TSP is $\mathcal{O}$$(n!)$. Traditional, deterministic
algorithm approaches such as branch-and-bound or branch-and-cut are still impractical for larger numbers of nodes.
In many cases, exhaustive search for global optimality is not even particularly helpful as long as the solution
found is good enough. We will use simulated annealing (SA) to get acceptable solutions to the TSP.

Figure \ref{fig:routing-vanilla-tsp} shows a sample draw of conflict data (the blue points), and a near-optimal TSP route found through 50,000 iterations of simulated annealing.

\subsection{Packing the aid truck --- the Knapsack Problem}

We extend the TSP into a multi-objective optimization problem
where \emph{the contents of the aid trucks} also have an optimization component. Therein lies
the knapsack problem: subject to a volume or weight constraint, and given that different locations
might have very different needs such as food, vaccinations, or emergent medical supplies, \emph{which
supplies do we pack on the trucks}?

Here's the unbounded\footnote{Often, this problem is formulated such that you can only bring one of each item, but that does not make sense in our application. Rather, we want to be able to bring as many types of each type of aid as we think necessary, and we'll assume that as many as desired are available to load on the trucks before starting out from HQ.} version of the knapsack problem:

\begin{align*}
\max &\sum_{i=1}^n v_i x_i &&  \\
\mathrm{s.t.} & \\
    & x_i \in \mathbb{Z} \\
    & x_i \geq 0 \\
	& \sum_{i=1}^n w_ix_i \leq W
\end{align*}

In this formulation:

\begin{itemize}
  \item $x_{i}$ is a zero or positive integer decision variable indicating how many units of item $i$
        we load on the truck.
  \item $v_i$ is the utility we get from bringing along item $i$.
  \item $w_i$ is the weight of item $i$.
  \item $W$ is the maximum weight the truck can carry.
\end{itemize}

\subsection{A brief detour for modeling assumptions}

Before we can optimize this aid delivery mechanism, we will need to decide a way to model humanitarian aid needs at a given conflict.

Let us assume that there are $K$ distinct types of humanitarian aid to be delivered. (Without loss of generality, we will use three categories for all of our examples --- perhaps we can think of them food aid, first aid supplies, and medicines for concreteness.) We can model each conflict's aid needs as

$$\boldsymbol x \sim \mathrm{Dir}(\boldsymbol \alpha)$$

where $\boldsymbol \alpha$ parameterizes the distribution to generate vectors of length $K$ representing the relative proportions of needs.\cite{Murphy} For example, in our three category example we might draw the vector $(0.11,\,0.66,\,0.23)$ for a certain conflict, meaning that 11\% of the aid needed at this conflict is food aid, 66\% is first aid supplies, and 23\% is medicines. Now that we know the proportions for the given conflict, how might we turn this unitless vector into absolute amounts?

For that reason, let's assign each conflict a scaled size $s \in [1, 10]$ based on the number of casualties (a proxy for the severity of the conflict). We can use this size scalar to turn our proportion vector into a vector of absolute needs.

It should be noted that \textbf{both of these modeling methods for proportions and size are ``plug-and-play''} --- because of purposely designed loose coupling in our model, these methods could trivially be replaced by a different method of calculating or predicting the needs of each conflict. For example, if an independent model was used to calculate each of $K$ needs based on the features of each conflict, those quantities could easily be plugged in to this model. Ultimately, the only quantities that our TSP/Knapsack model needs is an $n \times K$ matrix of aid needs for $n$ cities and $K$ categories of aid.

\subsection{A new objective function to integrate TSP and Knapsack}

For the vanilla TSP, we simply try to minimize the total distance. Now that we are adding a new objective, we will need to integrate the two into a coherent \textbf{loss function}.  Here is the function we will actually try to minimize in the combined TSP/Knapsack:

$$L(\boldsymbol x) = \text{total distance} + \text{sum of squared aid shortfalls}$$

The effect of squaring aid shortfalls acts as a weight, causing greater importance to be placed on minimizing this aspect of the problem first. Proposals wherein aid shortfalls occur are heavily penalized. As we will see in later graphs, once the SA algorithm is able to avoid all shortfalls and the concurrent massive loss function penalties, a much slower descent begins to take place wherein the distance is slowly optimized. See figure \ref{fig:sorties-loss} for a depiction of this phenomenon.

\subsection{Implementing the Knapsack aspect}

Figure \ref{fig:routing-reloading} shows the same draw of cities as in figure \ref{fig:routing-vanilla-tsp}, this time factoring in limited carrying capacity for aid supplies on the aid delivery mechanism and using our new loss function. As we can see, the huge penalty incurred when supplies run out quickly induces the simulated annealing algorithm to converge on a solution with multiple stops at HQ to reload.

Figure \ref{fig:sorties} uses some uniformly distributed points on the $[0,50]$ plane to demonstrate how the proposed TSP/Knapsack routes converge as the number of iterations increases.

\subsection{Finding the optimal site for the resupply location}

Our initial assumption was that the HQ was located in the capital city of Kampala. However, we should ask whether our HQ could be more conveniently located. We can answer this question by treating the reload location as another parameter and continuing to sample HQ locations using SA. Figure \ref{fig:routing-reloading-hq-optimal} shows the TSP/Knapsack optimized once again, this time using a the optimal HQ location, while \ref{fig:comparative-loss-plot} compares the loss function as each method converges to its best possible configuration.

\section*{Conclusions}

In each part of this problem, analytical solutions either do not exist (e.g. in the distribution of events) or are computationally infeasible (e.g. in the TSP/Knapsack optimizations).  We found that using a metaheuristic such as SA converged on robust solutions in relatively short order. In the future, we would like to formulate our loss function based on real world data based on refugee locations and aid distribution requirements; our methodology would not change, but the solutions would be more useful for predictive tasks. Additionally, the separate models could be fit and incorporated which realistically model how much of each type of aid is needed at each conflict location. Future research might also include adding many more constraints or twists to the problem, and trying different stochastic optimization techniques such as genetic algorithms, Tabu search, or ant colony optimization.

\bibliography{Write-up_Ref}

\end{document}


\documentclass[]{article}
\usepackage[margin=1.0in]{geometry}
\usepackage{amssymb}

%title material
\title{Astronomy 400B Homework \#4}
\author{Please Show Your Work for Full Credit}
\date{Due April 9, 2015 by 9:35am}

%include latex definitions
\input{astro400B_definitions.tex}

%begin the document
\begin{document}

%make the title, goes after document begins
\maketitle

\section{Sparke \& Gallagher Problem 7.1}

Suppose that gas atoms and galaxies in a group move at the same average random speed $\sigma$ along each direction. At temperature $T$, the average energy of gas particle is $3k_B T/2$, where $k_B$ is Boltzmann's constant.  If the gas is mainly ionized hydrogen, these particles are protons and electrons; show that, if the atom's kinetic energy $(3 m_p/2)\sigma^2$ is shared equally
between them, then
\begin{equation}
T \approx \frac{(m_p/2)\sigma^2}{k_B} \approx 5 \times 10^6 \left( \frac{\sigma}{300~\km~\s^{-1}}\right)^2~\K.
\end{equation}
\noindent
Hot gas in a group or cluster is usually close to this {\it virial temperature}.

\section{Sparke \& Gallagher Problem 7.13}

If the lens $L$ is an object of mass $M_{\odot}$ at a distance $d_{\mathrm{Lens}}$ from
us, show that the Einstein radius for a star at distance $d_S = 2 d_{\mathrm{Lens}}$ is
\begin{equation}
\theta_{E} = \sqrt{\frac{R_s}{d_{\mathrm{Lens}}}} \approx 2 \times 10^{-3} \sqrt{\frac{1~\kpc}{d_{\mathrm{Lens}}}}~\mathrm{arcsec}.
\end{equation}

\section{Sparke \& Gallagher Problem 7.17}

If a lens at distance $d_{\mathrm{Lens}}$ bends the light of a much more
distant galaxy, so that $d_S$ and $d_{LS}\gg d_{\mathrm{Lens}}$, show that the
critical density is
\begin{equation}
\Sigma_{\mathrm{crit}} \approx 2 \times 10^4 \left( \frac{100~\Mpc}{d_{\mathrm{Lens}}}\right)~M_{\odot}~\pc^{-2},
\end{equation}
\noindent
and that the mass projected within angle $\theta_E$ of the center is
\begin{equation}
M(<\theta_E) \approx \left(\frac{d_{\mathrm{Lens}}}{100~\Mpc}\right)\left(\frac{\theta_E}{1~\mathrm{arcsec}}\right)^2 ~ 10^{10}~M_{\odot}.
\end{equation}

\end{document}


\section{Frisch-Waugh-Lovell theorem}


    \filetitle{!links}{Define dynamic links}{modellang/links}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
!links
   ParameterName := Expression;
   VariableName := Expression;
\end{verbatim}

\paragraph{Syntax with equation
labels}\label{syntax-with-equation-labels}

\begin{verbatim}
!links
   'Equation label' ParameterName := Expression;
   'Equation label' VariableName := Expression;
\end{verbatim}

\paragraph{Description}\label{description}

The dynamic links relate a particular parameter (or steady-state value)
on the LHS to a function of other parameters or steady-state values on
the RHS. \texttt{Expression} can be any expression involving parameter
names, variables names, Matlab functions and constants, or your own
m-file functions on the path; it must not refer to any lags or leads.
\texttt{Expression} must evaluate to a single number. It is the user's
responsibility to properly handle the imaginary (i.e.~growth) part of
the steady-state values.

The links are automatically refreshed in
\href{model/solve}{\texttt{solve}},
\href{model/sstate}{\texttt{sstate}}, and
\href{model/chksstate}{\texttt{chksstate}} functions, and also in each
iteration within the \href{model/estimate}{\texttt{estimate}} function.
They can also be refreshed manually by calling
\href{model/refresh}{\texttt{refresh}}.

The links must not involve parameters occuring in
\href{modellang/dtrends}{\texttt{!dtrends}} equations that will be
estimated using the \texttt{'outoflik='} option of the
\href{model/estimate}{\texttt{estimate}} function.

\paragraph{Example}\label{example}

\begin{verbatim}
!links
   R := 1/beta;
   alphak := 1 - alphan - alpham;
\end{verbatim}


% mainfile: ../../Refinement.tex
\begin{definition}[Process syntax]
\label{def_process_syntax}
The syntax of a \picalc{} process \textit{P} is defined by:
\begin{align*}
 P & \syntdef \procsum \ebnf \procpar{P_1}{P_2} \ebnf \procres{\vec{y}}{P} \ebnf \proccall{A}{\vec{v}}
\end{align*}
where:
\begin{itemize}
\item $\procsum$ is the guarded sum.
\item $\procpar{P_1}{P_2}$ is the parallel composition of processes.
\item $\procres{\vec{y}}{P}$ is the restriction of the scope of the names $\vec{y}$ to the process $P$
\item $\proccall{A}{\vec{v}}$ is a process call.
\end{itemize}
\end{definition}

\subsubsection{\findex[guarded sum]{Guarded sum}:} The guarded sum is the choice between multiple guarded processes. If the guard of one process took place, other guarded processes will be discarded. For example, the processes: $\procchoice{\inp{x}{}.P_1}{\inp{y}{}.P_2}$ will evolve to the process $P_1$ if the guard $\inp{x}{}$ occurred.

Furthermore, The process $\proczero$ is called the \findex[process!stop]{stop process} stands for the process that can do nothing. It can be omitted.
\subsubsection{\findex[guard]{Guard}:} The guard is also called \findex[process!action prefix]{action prefix} and denoted by $\pi$. Its syntax is defined by:
\begin{definition}[Action prefix syntax]
\label{def_prefix_syntax}
\begin{align*}
 \pi \syntdef \out{x}{\vec{y}} \ebnf \inp{x}{\vec{y}} \ebnf \tau
\end{align*}
where:
\begin{itemize}
\item $\out{x}{\vec{y}}$\footnote{$\out{x}{{}}$ means: send a signal via $x$. $\out{x}{{y}}$ means: send the name $y$ via $x$.  $\out{x}{{\vec{y}}}$ means: send the sequence $\vec{y}$ via $x$.} represents the action: send $\vec{y}$ via the channel $x$.
\item $\inp{x}{\vec{y}}$\footnote{$\inp{x}{{}}$ means: receive a signal via $x$. $\inp{x}{{y}}$ means: receive any name $y$ via $x$.  $\inp{x}{{\vec{y}}}$ means: receive any sequence $\vec{y}$ via $x$. ``$y$ here plays the role of parameter''} represents the action: receive $\vec{y}$ via the channel $x$.
\item $\tau$ represents an internal non-observable action.
\end{itemize}
\end{definition}

We use the symbols $\alpha$ or $\beta$ to denote an action. Furthermore, we call $x$ the subject and $\vec{y}$ the object of a an action $\out{x}{\vec{y}}$ or $\inp{x}{\vec{y}}$. The functions  $\sub{\alpha}$ and $\sub{\alpha}$ returns the subject and object of an action $\alpha$. More formally:
\[\sub{\alpha} \text{: returns the channel name through which the exchange occurs.}\]
\[\obj{\alpha} \text{: return the exchanged names across the channel.}\]

The set of all actions is defined as $\actions\define\outA\cup\inA\cup\set{\tau}$, Where:
\begin{itemize}
\item $\outA$ is the set of output actions, defined as $\outA\define\set[x,y_1,\dots,y_n\in\names]{\out{x}{\vec{y}}}$.
\item $\inA$ is the set of input actions, defined as $\inA\define\set[x,y_1,\dots,y_n\in\names]{\inp{x}{\vec{y}}}$.
\end{itemize}
\subsubsection{\findex[parallel composition]{Parallel composition}:}
The parallel composition operator $\procpar{}{}$ represents the concept of concurrency in the \picalc{}, where two processes can evolve concurrently. It represents an interleaving behavior of the concurrency.
For example let:  $P\define\procpar{P_1}{(\procpar{P_2}{P_3})}$ where: $P_1\define\inp{x}{y}.Q_1$, $P_2\define\out{x}{y}.Q_2$ and $P_3\define\inp{x}{y}.Q_3$. So $P\define\procpar{\inp{x}{y}.Q_1}{(\procpar{\out{x}{y}.Q_2}{\inp{x}{y}.Q_3})}$.
Possible evolution cases of $P$ are:
\begin{itemize}
\item $\procpar{P_1}{(\procpar{Q_2}{Q_3})}$. $P_2$ sends $y$ via $x$ to $P_3$.
\item $\procpar{Q_1}{(\procpar{Q_2}{P_3})}$. $P_2$ sends $y$ via $x$ to $P_1$.
\end{itemize}

The example above illustrates the privacy nature of the parallel operator in the \picalc{}. A process can communicate via a channel with only one process per time, i.e., a binary synchronization occurs via the channel. $P_2$ cannot communicate with both $P_1$, $P_3$ at the same time, while in \gls{CSP} a process can communicate with multiple processes at the same time via the same channel by sending multiple copies of the same message, i.e., in CSP a multiple synchronization occurs via the channel.

\subsubsection{\findex[restriction]{Restriction}:}

The expression $\procres{\vec{y}}{P}$ binds the names $\vec{y}$ to the process $P$. In other words: the visibility scope of the  names $\vec{y}$ is restricted to the process $P$. It is similar to declaring a private variable in programming languages. Thus, the names $\vec{y}$ are not visible outside $P$ and $P$ cannot use them to communicate with the outside. For example, let $P\define\procpar{P_1}{P_2}$ where: $P_1\define\procres{y}{\out{y}{z}.Q_1}$ and $P_2\define\inp{y}{z}.Q_2$. The process $P$ cannot evolve to $\procpar{Q_1}{Q_2}$, since the name $y$ in $P_1$ is only visible inside $P_1$, i.e., from the $P_2$'s point of view $P_1$ does not have a channel called $y$. This takes us to the definition of the
\findex[name!bound]{bound names} and \findex[free!bound]{free names}.

\begin{definition}[Bound names]
\label{def_bound_names}
 the bound names are all the restricted names in a process.
\end{definition}
\begin{definition}[Free names]
\label{def_free_names}
 the free names are all the names that occur in a process except the bound names.
\end{definition}

For example, let $P_1\define\procres{x}{(\out{x}{y}.P_2)}$ where $P_2\define\procres{z}{(\out{x}{z}.P_3)}$. The name $x$ is bound in $P_1$ but free in $P_2$.

\subsubsection{\findex[process!call]{Process call}:}
\label{subsubsection_process_call}

Let $P$ be a process and let $A$ be a process identifier. To be able to use the process $P$ recursively we use the process identifier $A$ as follows: $\procdef{A}{\vec{w}}\define{}P$. Thus, when we write $\proccall{A}{\vec{v}}$ we are using the identifier $A$ to call the process $P$ with replacing the names $\vec{w}$ in $P$ with the names $\vec{v}$. This replacement is called the $\alpha$-conversion.

For example, let $\procdef{A}{w}\define\out{w}{y}.\procdef{A}{w}$, then the behavior of $\proccall{A}{v}$ is equivalent to $\out{v}{y}.\proccall{A}{v}$.


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

\marginpar{Wednesday\\ 2020-11-11, \\ compiled \\ \today}

The viscous timescale is of the order \(t _{\text{visc}} \approx R^2 / \nu \).
It is the longest of the timescales in the accretion disk.

Let us suppose that \(t _{\text{visc}} \ll t _{\text{esc}}\), where \(t _{\text{esc}}\) is the timescale for changes in the mass transfer rate.
As long as this is the case, we can assume that the equations mentioned before are stationary: \(\partial_{t} = 0\).
The mass and angular momentum equations read
%
\begin{align}
R \pdv{\Sigma }{t} &= - \pdv{}{R} \qty(v_R \Sigma R )  \\
R^2 \pdv{t}(\Omega \Sigma ) &= - \pdv{}{R} \qty(v_R R \Sigma R^2 \Omega ) + \frac{1}{2 \pi } \pdv{G}{R}
\,,
\end{align}
%
so they are simplified to
%
\begin{align}
\pdv{}{R} \qty( v_R \Sigma R) &=0  \\
-\pdv{}{R} \qty(v_R R \Sigma R^2 \Omega ) + \frac{1}{2 \pi } \pdv{G}{R}&= 0
\,.
\end{align}

So, the mass equation can be expressed as \(v_R \Sigma R = \const\), and we can express the constant as \(2 \pi R v_R \Sigma = - \dot{M}\).
The mass accretion rate is \(\dot{M}> 0\), and the minus sign is needed in order to cancel the fact that \(v_R < 0\).

The angular momentum equation can be integrated as well:
%
\begin{align}
2 \pi
\pdv{}{R} \qty(v_R R \Sigma R^2 \Omega )
&= \pdv{G}{R} \\
\underbrace{2 \pi v_R R \Sigma}_{-\dot{M}} R^2 \Omega &= G + \const  \\
- \dot{M} R^2 \Omega  &= G + \const
\,,
\end{align}
%
and now what we need to do is to find out what the constant is. In order to do so, we evaluate the equation at the inner edge of the disk, at \(R = R _{\text{in}}\).
The constant \(\const = C\) reads
%
\begin{align}
C = - \dot{M} R _{\text{in}}^2 \Omega _{\text{in}} - G(R _{\text{in}})
\,,
\end{align}
%
and now we introduce a further hypothesis, called the \textbf{no-torque} condition: \(G (R _{\text{in}}) = 0\).

In the Black Hole case, the inner edge of the disk corresponds to the ISCO, so the edge does not touch any other surface, which means there can be no torque.
In the Neutron Star case this will not be true in general; \(R _{\text{in}}\) will be \(R_{*}\), and the fact that the ring touches the star means that there can be torque as long as \(\Omega (R _{\text{in}}) \neq \Omega _*\).

So, the no-torque condition is fulfilled only in the Black Hole case.
The constant then reads \(C = -\dot{M} R^2 _{\text{in}} \Omega _{\text{in}}\), and the angular momentum equation becomes
%
\begin{align}
-\dot{M} \qty(R^2 \Omega - R^2 _{\text{in}} \Omega _{\text{in}}) = G
\,.
\end{align}

We can then substitute the expression we have found earlier for \(G(R)\), under the further assumption that \(\Omega (R) = \Omega _K (R )= \sqrt{GM / R^3}\).

The derivative of the Keplerian angular velocity reads
%
\begin{align}
\dv{\Omega _K}{R} = - \frac{3}{2} \sqrt{ \frac{GM}{R^5}}
\,.
\end{align}

Therefore, substituting in the expression for \(G\) \eqref{eq:local-torque-explicit} and making \(\pdv*{\Omega }{R}\) explicit we find
%
\begin{align}
2 \pi R \Sigma \nu R^2 \qty(- \frac{3}{2} \sqrt{ \frac{GM}{R}} \frac{1}{R^2}) = - \dot{M} \qty(R^2 \frac{1}{R} \sqrt{ \frac{GM}{R}}- R^2 _{\text{in}} \sqrt{ \frac{GM}{R _{\text{in}}}} \frac{1}{R _{\text{in}}})
\,.
\end{align}

With some manipulations, we get
%
\begin{align}
\Sigma \nu = \frac{\dot{M}}{3 \pi } \qty(1 - \sqrt{\frac{R _{\text{in}}}{R}})
\,.
\end{align}

We can then calculate the radiated power per unit area of the disk, \(D(R)\):
%
\begin{align}
D(R) &= \frac{1}{2} \Sigma \nu \qty(R \dv{\Omega }{R})^2  \\
&= \frac{1}{2} \frac{\dot{M}}{3 \pi } \qty(1 - \sqrt{\frac{R _{\text{in}}}{R}}) \frac{9}{4} \frac{GM}{R^3}  \\
&= \frac{3}{8 \pi } \frac{G M \dot{M}}{R^3} \qty(1 - \sqrt{ \frac{R _{\text{in}}}{R}})
\,.
\end{align}

This turns out to be completely independent of \(\nu \)!

If the disk extends from \(R _{\text{in}}\) to \(R _{\text{out}}\) then we can calculate the total luminosity (from both sides of the disk)
%
\begin{align}
L &= 2\int_{R _{\text{in}}}^{R _{\text{out}}} 2 \pi R D(R) \dd{R}  \\
&= 2\int_{R _{\text{in}}}^{R _{\text{out}}} 2 \pi R \frac{3}{8 \pi } \frac{G M \dot{M}}{R^3} \qty(1 - \sqrt{ \frac{R _{\text{in}}}{R}})
\dd{R}  \\
&=
\frac{3}{4} \frac{GM \dot{M}}{R _{\text{in}}}
\int_{x_1 }^{x_2 }
x^{-2} \qty(1 - x^{-1/2}) \dd{x}
\marginnote{Introduced \(x = R / R _{\text{in}}\).}  \\
&= \frac{3}{2} \frac{GM \dot{M}}{ R _{\text{in}}}
\qty[ \frac{1}{x_1 } - \frac{1}{x_2 } - \frac{2}{3} \qty( \frac{1}{x_1^{3/2}} - \frac{1}{x_2^{3/2}})]
\,.
\end{align}

Here \(x_1 = R _{\text{in}} / R _{\text{in}} = 1\), while \(x_2\) is very large; we can send it to infinity. This yields \(1 - 2/3\) inside the square bracket, so
%
\begin{align}
L = \frac{3}{2} \frac{GM \dot{M}}{ R _{\text{in}}}
\frac{1}{3} = \frac{1}{2} \frac{GM \dot{M}}{R _{\text{in}}}
\,.
\end{align}

% This is half of \(L _{\text{acc}}= G M \dot{M} / R _{\text{in}}\), since

\subsection{Self-consistency checks}

\subsubsection{The azimuthal velocity is supersonic}

To first order, the gravitational force in the planar direction is
%
\begin{align}
F_g = \frac{GM}{r^2} = \frac{GM}{R^2 + z^2}
\,,
\end{align}
%
while the force in the vertical direction is
%
\begin{align}
F_{g, z} = \frac{GM}{R^2 + z^2} \sin \theta = \frac{GMz}{(R^2 + z^2)^{3/2}} \approx \frac{GMz}{R^3}
\,.
\end{align}

We can assume that the plasma does not move in the \(z\) direction, the pressure gradient along the \(z\) direction must balance the gravitational force:
%
\begin{align}
\frac{1}{\rho } \pdv{P}{z} = - \frac{GMz}{R^3}
\,,
\end{align}
%
and we can make a rough approximation \(\pdv*{P}{z} \approx P / H\). This then means that
%
\begin{align}
\frac{1}{\rho } \frac{P}{H} \approx \frac{GMH}{R^3}
\,.
\end{align}

Further, since \(c_s^2 \approx P / \rho \) we will have
%
\begin{align} \label{eq:speed-of-sound-disk-shape}
c_s^2 \approx \frac{GM H^2}{R^3}
\,.
\end{align}

However, since \(H \ll R\) we have
%
\begin{align}
\frac{H}{R} \approx c_s \sqrt{ \frac{R}{GM}} \ll 1
\,,
\end{align}
%
therefore
%
\begin{align}
c_s \ll \sqrt{ \frac{GM}{R}} = v_\phi
\,.
\end{align}

With this simple and rough line of reasoning we have shown that \textbf{the azimuthal Keplerian velocity of the gas must be supersonic} if we want the disk to be geometrically thin.

The azimuthal velocity can be written as
%
\begin{align}
v_\phi = \sqrt{ \frac{GM}{R}} = \frac{c}{\sqrt{2}} \sqrt{ \frac{R_S}{R}}
\,,
\end{align}
%
so even if we are at \(R = 2500 R_s\), quite far from the ISCO, we are already moving at \(c/ 50 \sqrt{2} \approx \SI{4000}{km/s}\), very fast.

\subsubsection{The azimuthal velocity is Keplerian}

Let us start with the Euler equation in the radial direction, written in a frame which is corotating with \(\Omega = \Omega (R)\), so we will need to account for fictitious forces: the centrifugal force \(\vec{\Omega} \wedge \qty(\vec{\Omega} \wedge \vec{r})\) yields a term \(v_\phi^2 / R\).
We find
%
\begin{align}
v_R \pdv{v_R}{R} - \frac{v_\phi^2}{R} + \frac{1}{\rho } \pdv{P}{r} + \frac{GM}{R^2} = 0
\,,
\end{align}
%
and now we will make some approximations:
%
\begin{align}
\frac{1}{\rho } \pdv{P}{R} \approx \frac{1}{\rho } \frac{P}{R} \approx \frac{c_s^2}{R}
\,,
\end{align}
%
and \(c_s^2 \ll GM/R \): thus, we can completely neglect the pressure gradient term compared to the gravitational term.
This yields
%
\begin{align}
v_R \pdv{v_R}{R} - \frac{v_\phi^2}{R} + \frac{GM}{R^2} \approx 0
\,.
\end{align}

We approximate \(\pdv*{v_R}{R} \approx v_R / R\), and write \(v_R\) using the mass continuity equation \(-2 \pi R \Sigma v_R = \dot{M}\), with
%
\begin{align}
\Sigma = \frac{\dot{M}}{3 \pi \nu } \qty(1 - \sqrt{\frac{R _{\text{in}}}{R}})
\,,
\end{align}
%
therefore
%
\begin{align}
v_R &= \frac{\dot{M}}{2 \pi R \Sigma } = - \frac{3 \nu }{2 R} \frac{1}{1 - \sqrt{R _{\text{in}} / R}}
\,.
\end{align}

This means that the typical \(v_R\) will be \(v_R \approx \nu /R\).
Using the \(\alpha \)-prescription, this reads
%
\begin{align}
v_R \approx \frac{\nu}{R} = \frac{\alpha c_s H}{R} \ll c_s
\,,
\end{align}
%
since both \(\alpha < 1\) and \(H < R\).
Then,
%
\begin{align}
\frac{v_R^2}{R} \ll \frac{c_s^2}{R} \ll \frac{GM}{R^2}
\,,
\end{align}
%
so  the inertial term is negligible compared to the gravitational one: this means that
%
\begin{align}
- \frac{v_\phi^2}{R} + \frac{GM}{R^2} \approx 0
\,,
\end{align}
%
the gravitational force is balanced by the centrifugal force: this means that the azimuthal velocity is indeed Keplerian, our model is self-consistent.

\end{document}