\lab{The Inverted Pendulum}{The Inverted Pendulum}
\label{lab:inverted_pendulum}

\objective{We will set up the LQR optimal control problem for the inverted pendulum and compute the solution numerically.
% We will add a stochastic component to the system.
}

Think back to your childhood days when, for entertainment purposes, you'd balance objects: a book on your head, a spoon on your nose, or even a broom on your hand.
 Learning how to walk was likely your initial introduction to the inverted pendulum problem.

A pendulum has two rest points: a stable rest point directly underneath the pivot point of the pendulum, and an unstable rest point directly above.
The generic pendulum problem is to simply describe the dynamics of the object on the pendulum (called the `bob').
The inverted pendulum problem seeks to guide the bob toward the unstable fixed point at the top of the pendulum.
Since the fixed point is unstable, the bob must be balanced relentlessly to keep it upright.

The inverted pendulum is an important classical problem in dynamics and control theory, and is often used to test different control strategies. One application of the inverted pendulum is the guidance of rockets and missiles. Aerodynamic instability occurs because the center of mass of the rocket is not the same as the center of drag. Small gusts of wind or variations in thrust require constant attention to the orientation of the rocket.

\section*{The Simple Pendulum}
We begin by studying the simple pendulum setting.
Suppose we have a pendulum consisting of a bob with mass $m$ rotating about a pivot point at the end of a (massless) rod of length $l$.
Let $\theta(t)$ represent the angular displacement of the bob from its stable equilibrium.
By Hamilton's Principle, the path $\theta$ that is taken by the bob minimizes the functional
\begin{align}
J[\theta] = \int_{t_0}^{t_1}	L,
\end{align}
where the Lagrangian $L = T - U$ is the difference between the kinetic and potential energies of the bob.

The kinetic energy of the bob is given by $mv^2/2$, where $v$ is the velocity of the bob.
In terms of $\theta$, the kinetic energy becomes
\begin{align}
	\begin{split}
	T &= \frac{m}{2}v^2  = \frac{m}{2}(\dot{x}^2 + \dot{y}^2),\\
	&= \frac{m}{2}((l\cos(\theta)\dot{\theta})^2 + (l\sin(\theta)\dot{\theta})^2),\\
	&= \frac{ml^2\dot{\theta}^2}{2}.
	\end{split}
\end{align}
The potential energy of the bob is $U = mg(l-l\cos \theta)$.
From these expressions we can form the Euler-Lagrange equation, which determines the path of the bob:
\begin{align}
	\begin{split}
	0 &= L_{\theta} - \frac{d}{dx}L_{\dot{\theta}},\\
	&= -mgl\sin \theta - m l^2 \ddot{\theta},\\
	&= \ddot{\theta} + \frac{g}{l}\sin \theta.
	\end{split}
\end{align}
Since in this setting the energy of the pendulum is conserved, the equilibrium position $\theta = 0$ is only Lyapunov stable. When forces such as friction and air drag are considered $\theta = 0$ becomes an asymptotically stable equilibrium.

\begin{figure}
\centering
\includegraphics[width=8cm]{Simple_gravity_pendulum.png}
\caption{The frame of reference for the simple pendulum problem.
}
\label{fig:inverted_pendulum:simple_gravity_pendulum}
\end{figure}

\section*{The Inverted Pendulum}
\subsection*{The Control System}
We consider a gift suspended above a rickshaw by a (massless) rod of length $l$.
The rickshaw and its suspended gift will have masses $M$ and $m$ respectively, $M > m$.
Let $\theta $ represent the angle between the gift and its unstable equilibrium, with clockwise orientation.
Let $v_1$ and $v_2$ represent the velocities of the rickshaw and the gift, and $F$ the force exerted on the rickshaw.
The rickshaw will be restricted to traveling along a straight line (the $x$-axis).

By Hamilton's Principle, the path $(x,\theta)$ of the rickshaw and the present minimizes the functional
\begin{align}
J[x,\theta] = \int_{t_0}^{t_1}	L,
\end{align}
where the Lagrangian $L = T - U$ is the difference between the kinetic energy of the present on the pendulum, and its potential energy.

Since the position of the rickshaw and the present are $(x(t),0)$ and $(x-l\sin \theta, l\cos \theta)$ respectively, the total kinetic energy is
\begin{align}
	\begin{split}
	T &= \frac{1}{2}Mv_1^2 +  \frac{1}{2}mv_2^2,\\
	&= \frac{1}{2}M\dot{x}^2 +  \frac{1}{2}m((\dot{x} - l\dot{\theta}\cos \theta)^2 + (- l\dot{\theta}\sin \theta)^2),\\
	&= \frac{1}{2}(M+m)\dot{x}^2 +\frac{1}{2}m l^2\dot{\theta}^2-ml\dot{x}\dot{\theta}\cos \theta.
	\end{split}
\end{align}
% The potential energy $U$ is composed of two parts: the first is the gravitational potential energy of the bob/present on the pendulum, and is given by $(mg)(l\cos \theta)$.
% The second part is the potential energy in the system due to any force $F$ exerted on the rickshaw.
% Recalling that whenever a force field $\mathbf{F}$ has the form $\mathbf{F} = \nabla \phi(\mathbf{x})$, where $\phi(\mathbf{x})$ is a scalar field, the potential energy derived from that force is $ -\phi(\mathbf{x})$.
The total potential energy is
\begin{align*}
U &= mgl\cos \theta.
\end{align*}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{rickshaw_img.png}
\caption{The inverted pendulum problem on a mobile rickshaw with a present suspended above.
}
\label{fig:inverted_pendulum:rickshaw_diagram}
\end{figure}

The path $(x,\theta)$ of the rickshaw and the present satisfy the Euler-Lagrange differential equations, but the problem involves a nonconservative force $F$ acting in the $x$ direction.
By way of D'Alambert's Principle, our normal Euler-Lagrange equations now include the nonconservative force $F$ on the right side of the equation:

\begin{align}
	\begin{split}
\frac{\partial L}{\partial x} - \frac{d}{dt} \frac{\partial L}{\partial \dot{x}} &= F,\\
\frac{\partial L}{\partial \theta} - \frac{d}{dt} \frac{\partial L}{\partial \dot{\theta}} &= 0.
	\end{split}\label{inverted_pendulum:langrange_eqns}
\end{align}
After expanding \eqref{inverted_pendulum:langrange_eqns} we see that $x(t)$ and $\theta(t)$ satisfy
\begin{align}
	\begin{split}
		F &= (M + m)\ddot{x} - ml\ddot{\theta} \cos \theta + ml \dot{\theta}^2 \sin \theta,\\
		l \ddot{\theta} &= g \sin \theta + \ddot{x} \cos \theta.
	\end{split}\label{inverted_pendulum:langrange_eqns_explicit}
\end{align}

At this point we make a further simplifying assumption.
If $\theta$ starts close to $0$, we may assume that the corresponding force $F$ will keep $\theta$ small.
In this case, we linearize \eqref{inverted_pendulum:langrange_eqns_explicit} about $(\theta, \dot{\theta}) = (0,0)$, obtaining the equations
\begin{align*}
	\begin{split}
		F &= (M + m)\ddot{x} - ml\ddot{\theta},\\
		l \ddot{\theta} &= g \theta + \ddot{x}.
	\end{split}%\label{inverted_pendulum:langrange_eqns_explicit2}
\end{align*}
These equations can be further manipulated
 % \eqref{inverted_pendulum:langrange_eqns_explicit2}
to obtain
\begin{align}
	\begin{split}
		\ddot{x} &= \frac{1}{M}F - \frac{m}{M}g\theta,\\
		\ddot{\theta} &= \frac{1}{Ml}F + \frac{g}{Ml} (M+m) \theta.
	\end{split}\label{inverted_pendulum:langrange_eqns_explicit3}
\end{align}

We will now write \eqref{inverted_pendulum:langrange_eqns_explicit3} as a first order system.
Making the assignments $x_1 = x$, $x_2 = x_1'$, $\theta_1 = \theta$, $\theta_2 = \theta_1'$, letting $u = F$ represent the control variable, we obtain
\begin{align*}
\begin{bmatrix}
x_1\\
x_2 \\
\theta_1 \\
\theta_2
\end{bmatrix}' &=
\begin{bmatrix}
0 & 1 & 0 & 0\\
0 & 0 & \frac{mg}{M} & 0 \\
0 & 0 & 0 & 1 \\
0 & 0 & \frac{g}{Ml}(M+m) & 0
\end{bmatrix}
\begin{bmatrix}
x_1\\
x_2 \\
\theta_1 \\
\theta_2
\end{bmatrix} + u
\begin{bmatrix}
0\\
\frac{1}{M} \\
0 \\
\frac{1}{Ml}
\end{bmatrix},
\end{align*}
which can be written more concisely as
\[z' = Az + Bu.\]

\subsection*{The infinite time horizon LQR problem}
We consider the cost function
\begin{align}
\begin{split}
J[z] &= \int_0^{\infty} (q_1x_1^2 + q_2x_2^2  + q_3\theta_1^2 + q_4\theta_2^2 + ru^2)\, dt\\
&= \int_0^{\infty} z^TQz + u^TRu \, dt
\end{split} \label{inverted_pendulum:LQR}
\end{align}
where $q_1, q_2, q_3, q_4$, and $r$ are nonnegative weights, and
\[
Q =
\begin{bmatrix}
q_1 & 0 & 0 & 0 \\
0 & q_2 & 0 & 0\\
0 & 0 & q_3 & 0 \\
0 & 0 & 0 & q_4
\end{bmatrix}, R = \begin{bmatrix} r \end{bmatrix}.
\]

\begin{problem}
Write a function that returns the matrices $A, B, Q$, and $R$ given above. Let $g = 9.8\text{ m}/\text{s}^2$.

\begin{lstlisting}
def linearized_init(M, m, l, q1, q2, q3, q4, r):
	'''
	Parameters:
	----------
	M, m: floats
          masses of the rickshaw and the present
	l 	: float
          length of the rod
	q1, q2, q3, q4, r : floats
         relative weights of the position and velocity of the rickshaw, the
		 angular displacement theta and the change in theta, and the control

	Return
	-------
	A : ndarray of shape (4,4)
	B : ndarray of shape (4,1)
	Q : ndarray of shape (4,4)
	R : ndarray of shape (1,1)
	'''
	pass
\end{lstlisting}
\end{problem}

The optimal control problem \eqref{inverted_pendulum:LQR} is an example of a Linear Quadratic Regulator (LQR), and is known to have an optimal control $\tilde{u}$ described by a linear state feedback law:
\begin{align*}
\tilde{u} &= -R^{-1}B^TP\tilde{z}.
\end{align*}
Here $P$ is a matrix function that satisfies the Riccati differential equation (RDE)
\[
\dot{P}(t) = PA + A^TP + Q - PBR^{-1}B^T P.
\]
Since this problem has an infinite time horizon, we have $\dot{P} = 0$. Thus $P$ is a constant matrix, and can be found by solving the algebraic Riccati equation (ARE)
\begin{align}
 PA + A^TP + Q - PBR^{-1}B^T P = 0.  \label{inverted_pendulum:ARE}
\end{align}
The evolution of the optimal state vector $\tilde{z}$ can then be described by \footnote{See Calculus of Variations and Optimal Control Theory, Daniel Liberzon, Ch.6}
\begin{align}
\dot{\tilde{z}} = (A - BR^{-1}B^TP)\tilde{z}. \label{inverted_pendulum:optimal_state}
\end{align}

\begin{problem}
Write the following function to find the matrix $P$.
Use \li{scipy.optimize.root}.
Since \li{root} takes in a vector and not a matrix, you will have to reshape the matrix $P$ before passing it in and after getting your result, using \li{np.reshape(16)} and \li{np.reshape((4,4))}.
\begin{lstlisting}
def find_P(A, B, Q, R):
	'''
	Parameters:
	----------
	A, Q 	: ndarrays of shape (4,4)
	B		: ndarray of shape (4,1)
	R		: ndarray of shape (1,1)

	Returns
	-------
	P		: the matrix solution of the Riccati equation
	'''
	pass

\end{lstlisting}
Using the values
\begin{lstlisting}
M, m = 23., 5.
l = 4.
q1, q2, q3, q4 = 1., 1., 1., 1.
r = 10.
\end{lstlisting}
compute the eigenvalues of $A - BR^{-1}B^TP$.
Are any of the eigenvalues positive?
Consider differential equation \eqref{inverted_pendulum:optimal_state} governing the optimal state $\tilde{z}$.
Using this value of $P$, will we necessarily have $\tilde{z} \to 0$?
\end{problem}

Notice that we have no information on how many solutions \eqref{inverted_pendulum:ARE} possesses.
In general there may be many solutions.
We hope to find a unique solution $P$ that is \textit{stabilizing}: the eigenvalues of $A - BR^{-1}B^TP$ have negative real part.
To find this $P$, use the function \li{solve_continuous_are} from \li{scipy.linalg}.
This function is designed to solve the continuous algebraic Riccati equation.

\begin{problem}
	Write the following function that implements the LQR solution described earlier.  For the IVP solver, you can use your own or you may use the function \li{odeint} from \li{scipy.integrate}.
\begin{lstlisting}
def rickshaw(tv, X0, A, B, Q, R, P):
	'''
	Parameters:
	----------
	tv 	: ndarray of time values, with shape (n+1,)
	X0 	: Initial conditions on state variables
	A, Q: ndarrays of shape (4,4)
	B	: ndarray of shape (4,1)
	R	: ndarray of shape (1,1)
	P	: ndarray of shape (4,4)

	Returns
	-------
	Z : ndarray of shape (n+1,4), the state vector at each time
	U : ndarray of shape (n+1,), the control values
	'''
	pass
\end{lstlisting}
\label{prob:inverted_pendulum3}
\end{problem}

\begin{problem}
Test the function made in Problem \eqref{prob:inverted_pendulum3} with the following inputs:
\begin{lstlisting}
M, m = 23., 5.
l = 4.
q1, q2, q3, q4 = 1., 1., 1., 1.
r = 10.
tf = None
X0 = np.array([-1, -1, .1, -.2])
\end{lstlisting}
Find the matrix P using the \li{scipy.optimize.root} method with \li{tf=6} as well as the \li{solve_continuous_are} method with \li{tf=60}.
Plot the solutions $\tilde{z}$ and $\tilde{u}$.
Compare your results as shown in Figure \ref{fig:inverted_pendulum:4}.
% and \ref{fig:inverted_pendulum:prob4_stable} respectively.
\label{prob:inverted_pendulum:4}
\end{problem}

\begin{figure}
\begin{minipage}[b]{.47\linewidth}
\centering
\includegraphics[width=\textwidth]{prob4_unstable.pdf}
\caption*{$P$ is found using \li{scipy.optimize.root}.}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.47\linewidth}
\centering
\includegraphics[width=\textwidth]{prob4_stable.pdf}
\caption*{$P$ is found using \li{solve_continuous_are}.}
\end{minipage}
\caption{The solutions of Problem \ref{prob:inverted_pendulum:4}.}
\label{fig:inverted_pendulum:4}
\end{figure}

\begin{comment}
\begin{problem}
Consider the following inputs:
\begin{lstlisting}
M, m = 23., 5.
l = 4.
q1, q2, q3, q4 = 1., 1., 1., 1.
r = 10.
tf = 60
X0 = np.array([-1, -1, .1, -.2])
\end{lstlisting}
Vary the entries of \li{X0} responsible for $\theta(0)$ and $\dot{\theta}(0)$ to determine the sensitivity of the control $u$ to the initial conditions.  What initial conditions lead to a reasonable, physical control $u$? (Plot several solutions with various values of $\theta(0)$ and $\dot{\theta}(0)$.)
\end{problem}
\end{comment}


%bisection diagram

\section{Bisection Diagrams}

\subsection{2-D Bisection of Terminal Triangle Pair}

\begin{figure}
\begin{center}

\vnode{0} \vnode{1} \vnode{2}
\psfrag{vh}{$\widehat{V}$}

\psfrag{longedge}{(longest edge)}

\psfrag{ar1}{\Huge{$\Downarrow$}}
\psfrag{ar2}{\Huge{$\Rightarrow$}}

\psfrag{t0}{$t_0$}
\psfrag{t1}{$t_1$}
\psfrag{s0}{$s_0$}
\psfrag{t0d}{$\tilde{t}_0$}

\psfrag{A}{$A$}
\psfrag{B}{$B$}
\psfrag{C}{$C$}
\psfrag{D}{$D$}

\includegraphics[width=4.0in]{Bisect_Terminal_Triangle_Pair}
\caption{Diagram of bisection process for terminal triangle pair.  The initial pair consists of two triangles $t_0, t_1$ that share a longest edge.  The initial neighbors of $t_0$ are the triangles labeled $A, B, t_1$.  Assuming that $t_1$ has already been bisected into triangles $C, D$, we see an intermediate stage for bisecting $t_0$.  We then replace $t_0$ by triangles $\tilde{t}_0$ and $s_0$ whose neighbors are adjusted accordingly.} \label{fig:Bisect_Terminal_Pair_2D}
\end{center}
\end{figure}

Assume we are given a terminal pair of triangles that share a longest edge (see Figure \ref{fig:Bisect_Terminal_Pair_2D}).  Bisecting the longest edge adds a new vertex with global index denoted $\widehat{V}$.  To better explain the bisection of the adjacent triangles, we assume that $t_1$ is already bisected into two triangles labeled $C, D$.

To bisect the triangle $t_0$, we must replace the triangle connectivity data of $t_0$ with the connectivity data of $\tilde{t}_0$, followed by adding a new triangle to the mesh, namely $s_0$. We then change the neighbors of $t_0$ to those of $\tilde{t}_0$, and add the neighbors of $s_0$ to the neighbor list.  Finally, we adjust the neighbors of $A$ and $B$ to correctly correspond to the new mesh.  This is summarized as follows.
\begin{equation}\label{eqn:tri_connectivity_and_neighbors}
\begin{split}
    \text{triangle~connectivity~of} ~\tilde{t}_0 &= [\widehat{V}, V_1, V_2], \quad \text{neighbor~connectivity~of} ~\tilde{t}_0 = [A, s_0, D] \\
    \text{triangle~connectivity~of} ~s_0 &= [\widehat{V}, V_2, V_0], \quad \text{neighbor~connectivity~of} ~s_0 = [B, C, \tilde{t}_0]
%    \text{neighbor~connectivity~of} ~\tilde{t}_0 &= [A, s_0, D] \\
%    \text{neighbor~connectivity~of} ~s_0 &= [B, C, \tilde{t}_0]
\end{split}
\end{equation}

Note that the global triangle index of $t_0$ and $\tilde{t}_0$ is identical, since we simply \emph{replaced} $t_0$ by $\tilde{t}_0$.  This means that the neighbor data for triangle $A$ does not need to be changed.  However, we must update the neighbors of triangle $B$, i.e. if $B$'s $k$th neighbor was $t_0$ before the bisection, then $B$'s $k$th neighbor after bisection should be $s_0$.

Note: the process for bisecting $t_1$ is exactly the same as for $t_0$; just rotate the diagrams in Figure \ref{fig:Bisect_Terminal_Pair_2D} by $180^\circ$.  In other words, triangle $C$ is really $\tilde{t}_1$, and $D$ is $s_1$.

%%%%%%


% !Mode:: "TeX:UTF-8"
% !TEX program  = xelatex
\begin{abstract}
    We introduce projectile motion and quadratic equation through a real world problem --- \emph{what angle should we throw a football for maximum range}, then we give a proper and accurate definition of quadratic equation, from which we derive quadratic formula for solving quadratic equations. Furthermore, we list some useful applications of the quadratic equation and implement them in \texttt{Python}.
\end{abstract}
\paragraph{Keywords:} Projectile motion; Quadratic equation; \texttt{Python} simulation.


Calculating means and other simple statistics is a matter of using the
right function in R.  The \pkg{mosaic} package --- which you should
load in to R as shown in Section \ref{sec:loading-mosaic} --- makes it
straightforward to calculate either a ``grand'' statistic or a
``group-wise'' statistic.   To illustrate:

\noindent Load the \pkg{mosaic} package, needed only once per session:
\begin{Schunk}
\begin{Sinput}
> require(mosaic)
\end{Sinput}
\end{Schunk}

\noindent Read in data you are interested in analyzing, for instance
the Cherry-Blossom 2008 data described earlier: \datasetCherryBlossomEight
\begin{Schunk}
\begin{Sinput}
> runners = fetchData("Cherry-Blossom-2008.csv")
> names( runners )
\end{Sinput}
\begin{Soutput}
[1] "position" "division" "total"    "name"     "age"
[6] "place"    "net"      "gun"      "sex"
\end{Soutput}
\end{Schunk}

\noindent Calculate a grand mean on the ``gun'' time --- the time from
the start of the race, signalled by a gun, and when each runner
crossed the finish line:
\begin{Schunk}
\begin{Sinput}
> mean( gun, data=runners )
\end{Sinput}
\begin{Soutput}
[1] 93.7
\end{Soutput}
\end{Schunk}
\noindent Or other ``grand'' statistics.

\begin{Schunk}
\begin{Sinput}
> median( gun, data=runners )
\end{Sinput}
\begin{Soutput}
[1] 93.7
\end{Soutput}
\begin{Sinput}
> sd( gun, data=runners )
\end{Sinput}
\begin{Soutput}
[1] 15
\end{Soutput}
\end{Schunk}

To tell R that you want to break the statistics down by groups, use
the \verb+~+ notation, pronounced ``\newword{tilde}.''  You will be
using this notation frequently in building models.  It means, ``model
by'' or ``broken down by'' or ``versus.''
\begin{Schunk}
\begin{Sinput}
> mean( gun ~ sex, data=runners )
\end{Sinput}
\begin{Soutput}
  sex    S    N Missing
1   F 98.8 6397       0
2   M 88.3 5905       0
\end{Soutput}
\end{Schunk}

Other statistics work the same way, for instance,
\begin{Schunk}
\begin{Sinput}
> sd( gun ~ sex, data=runners )
\end{Sinput}
\begin{Soutput}
  sex    S    N Missing
1   F 13.3 6397       0
2   M 14.7 5905       0
\end{Soutput}
\end{Schunk}

Another example ... wage broken down by sector of the economy, using
data :
\begin{Schunk}
\begin{Sinput}
> cps = fetchData("cps.csv")
> mean( wage ~ sector, data=cps )
\end{Sinput}
\begin{Soutput}
    sector     S   N Missing
1 clerical  7.42  97       0
2    const  9.50  20       0
3    manag 12.70  55       0
4    manuf  8.04  68       0
5    other  8.50  68       0
6     prof 11.95 105       0
7    sales  7.59  38       0
8  service  6.54  83       0
\end{Soutput}
\end{Schunk}

In the Whickham smoking data example, the outcome for each person was
not a number but a category: Alive or Dead at the time of the
follow-up.  \datasetWhickham
\begin{Schunk}
\begin{Sinput}
> w = fetchData("whickham.csv")
> names(w)
\end{Sinput}
\begin{Soutput}
[1] "outcome" "smoker"  "age"
\end{Soutput}
\begin{Sinput}
> levels(w$outcome)
\end{Sinput}
\begin{Soutput}
[1] "Alive" "Dead"
\end{Soutput}
\end{Schunk}

To find the proportion of people who were alive at the end of the
20-year follow-up period, you can use a computational trick.  Convert
the \VN{outcome} variable to \texttt{TRUE} or \texttt{FALSE} to
indicate whether an individual is alive, then take the mean of the
true/false values.  R, like many computer languages, treats
\texttt{TRUE} as 1 and \texttt{FALSE} as 0 for the purposes of doing arithmetic.
\begin{Schunk}
\begin{Sinput}
> mean( outcome=="Alive", data=w )
\end{Sinput}
\begin{Soutput}
[1] 0.719
\end{Soutput}
\end{Schunk}

Here's the breakdown according to smoking status:
\begin{Schunk}
\begin{Sinput}
> mean( outcome=="Alive" ~ smoker, data=w )
\end{Sinput}
\begin{Soutput}
  smoker     S   N Missing
1     No 0.686 732       0
2    Yes 0.761 582       0
\end{Soutput}
\end{Schunk}

A more meaningful question is whether smokers are different from
non-smokers when holding other variables constant, such as age.  To
address this question, you need to add age into the model.

It might be natural to consider
each age --- 35, 36, 37, and so on --- as a separate group, but you
won't get very many members of each group.  And, likely, the data for
35 year-olds has quite a lot to say about 36 year-olds, so it doesn't
make sense to treat them as completely separate groups.

You can use the \function{cut} function to divide up a quantitative
variable into groups.  You get to specify the breaks between groups.
\begin{Schunk}
\begin{Sinput}
> w$ageGroups = cut(w$age, breaks=c(0,30,40,53,64,75,100) )
> mean( outcome=="Alive" ~ ageGroups, data=w )
\end{Sinput}
\begin{Soutput}
  ageGroups     S   N Missing
1    (0,30] 0.979 288       0
2   (30,40] 0.948 252       0
3   (40,53] 0.832 280       0
4   (53,64] 0.625 251       0
5   (64,75] 0.201 169       0
6  (75,100] 0.000  74       0
\end{Soutput}
\begin{Sinput}
> mean( outcome=="Alive" ~ smoker + ageGroups, data=w )
\end{Sinput}
\begin{Soutput}
   smoker ageGroups     S   N Missing
1      No    (0,30] 0.982 165       0
2     Yes    (0,30] 0.976 123       0
3      No   (30,40] 0.955 134       0
4     Yes   (30,40] 0.941 118       0
5      No   (40,53] 0.876 113       0
6     Yes   (40,53] 0.802 167       0
7      No   (53,64] 0.669 127       0
8     Yes   (53,64] 0.581 124       0
9      No   (64,75] 0.214 131       0
10    Yes   (64,75] 0.158  38       0
11     No  (75,100] 0.000  62       0
12    Yes  (75,100] 0.000  12       0
\end{Soutput}
\end{Schunk}

With modeling techniques, to be introduced in later chapters, you can use
quantitative variables without the need to divide them into groups.

\subsection{Model Values and Residuals}

A group-wise model tells you a model value for each group, but often
you will need these in a case-by-case format: the model value for each
case in a data set.  The \function{fitted} function carries out
this simple calculation, taking each case in turn, figuring out which
group it belongs to, and then returning the set of model values for
all the cases.  It requires two arguments: the group-wise model and
the data on which the model was based.  For example:

\index{P}{fitted}
\index{P}{fetchData}

\begin{Schunk}
\begin{Sinput}
> kids = fetchData("kidsfeet.csv")
> mod = mean( width ~ sex, data=kids )
> fitted( mod, kids )
\end{Sinput}
\begin{Soutput}
 [1] 9.19 9.19 9.19 9.19 9.19 9.19 9.19 8.78 8.78 9.19 9.19
[12] 9.19 9.19 9.19 8.78 8.78 8.78 8.78 8.78 8.78 9.19 9.19
[23] 8.78 8.78 8.78 9.19 8.78 9.19 9.19 9.19 8.78 8.78 8.78
[34] 9.19 9.19 8.78 8.78 8.78 8.78
\end{Soutput}
\end{Schunk}

The residuals are found by subtracting the case-by-case model value
from the actual values for each case.
\begin{Schunk}
\begin{Sinput}
> res = kids$width - fitted(mod, kids)
\end{Sinput}
\end{Schunk}
Take care to use the same quantitative variable (\VN{width} in this case) from the
data as was used in constructing the model.

The \function{var} function will calculate the variance:
\begin{Schunk}
\begin{Sinput}
> var( kids$width )  # overall variation
\end{Sinput}
\begin{Soutput}
 var
0.26
\end{Soutput}
\begin{Sinput}
> var( fitted(mod, kids) ) # variation in model values
\end{Sinput}
\begin{Soutput}
   var
0.0422
\end{Soutput}
\begin{Sinput}
> var( kids$width - fitted(mod, kids) ) # residual variation
\end{Sinput}
\begin{Soutput}
  var
0.217
\end{Soutput}
\end{Schunk}


\subsubsection{ParetoFrontier}
\label{ParetoFrontierPP}
The \textbf{ParetoFrontier} PostProcessor is designed to identify the points lying on the Pareto Frontier in a multi-dimensional trade-space.
This post-processor receives as input a \textbf{DataObject} (a PointSet only) which contains all data points in the trade-space space and it
returns the subset of points lying in the Pareto Frontier as a PointSet.

It is here assumed that each data point of the input PointSet is a realization of the system under consideration for a
specific configuration to which corresponds several objective variables (e.g., cost and value).

%
\ppType{ParetoFrontier}{ParetoFrontier}
%
\begin{itemize}
  \item   \xmlNode{objective},\xmlDesc{string, required parameter}, ID of the objective variable that represents a dimension of the trade-space space.
          The \xmlNode{costID} requires one identifying attribute:
          \begin{itemize}
            \item \xmlAttr{goal}, \xmlDesc{string, required field}, Goal of the objective variable characteristic: minimization (min) or maximization (max)
            \item \xmlAttr{upperLimit}, \xmlDesc{string, optional field}, Desired upper limit of the objective variable for the points in the Pareto frontier
            \item \xmlAttr{lowerLimit}, \xmlDesc{string, optional field}, Desired lower limit of the objective variable for the points in the Pareto frontier
          \end{itemize}
\end{itemize}

The following is an example where a set of realizations (the ``candidates'' PointSet) has been generated by changing two parameters
(var1 and var2) which produced two output variables: cost (which it is desired to be minimized) and value (which it is desired to be maximized).
The \textbf{ParetoFrontier} post-processor takes the ``candidates'' PointSet and populates a Point similar in structure
(the ``paretoPoints'' PointSet).

\textbf{Example:}
\begin{lstlisting}[style=XML,morekeywords={anAttribute},caption=ParetoFrontier input example (no expand)., label=lst:ParetoFrontier_PP_InputExample]
  <Models>
    <PostProcessor name="paretoPP" subType="ParetoFrontier">
      <objective goal='min' upperLimit='0.5'>cost</objective>
      <objective goal='max' lowerLimit='0.5'>value</objective>
    </PostProcessor>
  </Models>

  <Steps>
    <PostProcess name="PP">
      <Input     class="DataObjects"  type="PointSet"        >candidates</Input>
      <Model     class="Models"       type="PostProcessor"   >paretoPP</Model>
      <Output    class="DataObjects"  type="PointSet"        >paretoPoints</Output>
    </PostProcess>
  </Steps>

  <DataObjects>
    <PointSet name="candidates">
      <Input>var1,var2</Input>
      <Output>cost,value</Output>
    </PointSet>
    <PointSet name="paretoPoints">
      <Input>var1,var2</Input>
      <Output>cost,value</Output>
    </PointSet>
  </DataObjects>
\end{lstlisting}

\nb it is possible to specify both upper and lower limits for each objective variable.
When one or both of these limits are specified, then the Pareto frontier is filtered such that all Pareto frontier points that
satisfy those limits are preserved.


\subsection*{Name}
series -- generate an additive series of numbers

\subsection*{Usage}

{\bf series} start end [stepsize]

\subsection*{Description}

{\bf series} prints the real numbers from
{\bf start} to {\bf end}, one per line.
{\bf series} begins with {\bf start}
to which {\bf stepsize} is repeatedly added or subtracted,
as appropriate, to approach, possibly meet, but not pass
{\bf end}.

If all arguments are integers, only integers are
produced in the output.
The {\bf stepsize} must be nonzero; if it is not specified,
it is assumed to be of unit size (1).
In all other cases,
{\bf series} prints an appropriate error message.

\subsection*{Example}
To count from 1 to 100:
\begin{verbatim}
	series 1 100
\end{verbatim}
To do the same, but backwards:
\begin{verbatim}
	series 100 1
\end{verbatim}

\subsection*{Limitations}

The reported number of significant digits is limited.
If the ratio of the series range to the
{\bf stepsize} is too large, several numbers in a row will be equal.

The maximum length of a series is limited to the size of the
maximum long integer that can be represented on the machine in use.
Exceeding this value has undefined results.

\subsection*{Author}

Gary Perlman


% !TEX root = ../../main.tex
\subsection{Kramers-Moyal expansion of the Master equation}

For the most part master equations are hard to deal with beacause of their
integro-differential nature. A way around this issue is to simplify the master
equation using Taylor expansions. This expansion goes by name of Kramers-Moyal
expansion, and the truncated version of the expansion is known in physics
literature as the Fokker-Planck equation, and in the probability theory
literature as the Kolmogorov forward equation. Regardless of the fancy names
behind these manipulations the idea is very straight-forward and very common in
physics in general: we take a complicated function and use its derivatives to
come up with an approximation that is good enough at least locally.

To perform the expansion we need to express the transition probabilities
$\phi_t(x \mid x')$ in \eref{eq_master_eq_full} as a function of the jump size
$r \equiv x - x'$ rather than as conditional probabilities. We therefore
express the transition probabilities as
\begin{equation}
  \phi_t(x \mid x') = \phi_t(x'; r).
  \label{eq_trans_jump_size}
\end{equation}
By performing this change of variables for the integral in
\eref{eq_master_eq_full} we need to compute the Jacobian which is of the form
\begin{equation}
  dr = dx' {dr \over dx'} = -dx'.
\end{equation}

Furthermore since the integration now is done over values of $r$ rather than
$x'$ we must update the integration limits as well. We then have for the lower
limit $x' = 0$ that upon substitution on the definition of $r$ this gives
\begin{equation}
  r(x' = 0) = x - 0 = x.
\end{equation}
Doing the same for the upper limit of $x' = 1$ results in
\begin{equation}
  r(x' = 1) = x - 1.
\end{equation}
Using these changes results in a master equation of the form
\begin{equation}
  \dt{P(x, t)} = \int_f^{x - 1} \underbrace{(-1)}_{\text{Jacobian}} dr \;
  \left[
  \phi_t(x - r; r) P(x - r, t) -
  \phi_t(x; -r) P(x, t)
  \right].
\end{equation}
For the second term inside the square brackets we wrote $\phi_t(x; -r)$ due to
the change of reference point. Since the original term was $\phi_t(x' \mid x)$
that means that the jump took place from $x$ to $x'$ instead of the other way,
therefore the jump rather than being of size $r$ it must be of size $-r$ to
compensate for this change. Flipping the limits of integration because of the
negative sign that the Jacobian gave results in
\begin{equation}
  \dt{P(x, t)} = \int^x_{x - 1} dr \;
  \phi_t(x - r; r) P(x - r, t) -
  \int^x_{x - 1} dr \;
  \phi_t(x; -r) P(x, t).
  \label{eq_master_eq_jump}
\end{equation}
Having showed what the integration limits \textit{should look like} we come
back to the issue mentioned before. Nowhere in the literature I have come
across there has been an explicit account for these limits. From Kimura to Rice
to even Lassig, everyone seems to overlook what these integrations should be.
In an attempt to be more explicit and thorough, while still readable and
intuitive I wanted to show the limits for the integral in the master equation.
Nevertheless these limits will have to be modify for our approximations as we
will see in a bit.

As mentioned before, dealing with \eref{eq_master_eq_jump} is extremely
complicated due to its integro-differential nature. To simplify things now we
will make use of one of the most handful techniques ever given to physics - the
Taylor expansion. This expansion will allow us to cast the nasty
integro-differential equation into a partial differential equation of infinite
order. This again is difficult to deal with, but we can always compromise some
precision for simplicity by truncating the expansion up to some arbitrary order
that is easier to handle. In particular is common practice to truncate
expansions up to second order, giving in this case one of Motoo Kimura's most
fundamental results, and the basis of all of diffusion theory in population
genetics. To perform the expansion of the master equation we have to assume the
following:
\begin{itemize}
  \item Changes on $x$ occur via very small jumps; i.e. the transition
  probability $\phi_t(x; r)$ is sharply peaked as a function of $r$, but at the
  same time varies slowly enough with $x$. This is a reasonable assumption in
  the limit of large population sizes since changes in the allele frequency
  will be slow for this regime.
  \item The probability of an allele frequency $P(x, t)$ also varies slowly
  with $x$. This smoothness requirement for the distribution is necessary since
  the expansion requires computing derivatives.
\end{itemize}
Both of these assumptions are necessary for the expansion to be valid at least
locally around the point we expand at.

\subsubsection{Issues with the integration limits}

As mentioned before, the integration limits on \eref{eq_master_eq_jump} must be
modified if we want to perform the Kramers-Moyal expansion. To show why this is
the case we will first attempt the expansion keeping the limits. Given the
assumptions we listed we can expand the first integrand in
\eref{eq_master_eq_jump} in powers of $x$ obtaining
\begin{equation}
  \begin{split}
    \ddt{P(x, t)} &= \int^x_{x - 1} dr \;
    \left[
    \phi_t(x; r) P(x, t) -
    {\partial \over \partial f} \left( \phi_t(x; r) P(x, t) \right) r \right. \\
    &+ \left.
    {1 \over 2} {\partial^2 \over \partial f^2}
    \left( \phi_t(x; r) P(x, t) \right) r^2 -
    {1 \over 3!} {\partial^3 \over \partial f^3}
    \left( \phi_t(x; r) P(x, t) \right) r^3 + \ldots
    \right] \\
    &-
    P(x, t) \int_{x - 1}^x dr \; \phi_t(x, -r).
  \end{split}
\end{equation}
If we now distribute the integral and write the general form of the expansion
we obtain
\begin{equation}
  \begin{split}
    \ddt{P(x, t)} &= P(x, t)\int^x_{x - 1} dr \; \phi_t(x; r) \\
    &+
    \sum_{k=1}^\infty {(-1)^k \over k!} {\partial^k \over \partial f^k}
    \left[
    P(x, t) \int_{x-1}^x dr \; r^k \phi_t(x; r)
    \right] \\
    &-
    P(x, t) \int_{x - 1}^x dr \; \phi_t(x, -r).
  \end{split}
\end{equation}
The problem with this expansion is that all integrals of the form
\begin{equation}
  \Phi(x, k) = \int_{x-1}^x dr \; r^k \phi_t(x; r),
  \text{ for } k \in \mathbb{N},
\end{equation}
have the wrong integration limits. Recall that we set these limits to cover the
entire domain of values that the jump size could take. Before the expansion
these limits allowed $\phi_t(x - r; r)$ to cover all jumps from any point
between zero and one towards $x$; after the expansion this is not true anymore.
For example if we were to substitute the lower integration limit into the
transition probability $\phi_t(x; r = x-1)$, the resulting final position $x +
x -1 = 2x -1$ would be outside of the range $[0, 1]$ for all $x < 1$. That is
why the Kramers-Moyal expansion is generally done for cases in which the
boundaries are irrelevant, for example Einstein's Brownian motion problem
didn't have this issue since the integration was taken from $-\infty$ to
$\infty$.

So how do we overcome this issue? The way to overcome this difficulty is to
set the integration limits to some irrelevant boundary. But in order to do so
we need to define our transition probability $\phi_t(x; r)$ to be zero if the
jump falls outside of the range $[0, 1]$. In other words, we could define the
transition probability as a piecewise function
\begin{equation}
  \phi_t(x; r) =
  \begin{cases}
    \phi_t(x + r \mid x)& \text{xor } (x + r), x \in [0, 1] \\
    0& \text{otherwise}
  \end{cases}.
\end{equation}
This definition allow us to send the integration limits of
\eref{eq_master_eq_jump} from $-\infty$ to $\infty$ to still cover all possible
jumps. With this definition we then have
\begin{equation}
  \begin{split}
    \ddt{P(x, t)} &= P(x, t)\int^\infty_{- \infty} dr \; \phi_t(x; r) \\
    &+
    \sum_{k=1}^\infty {(-1)^k \over k!} {\partial^k \over \partial f^k}
    \left[
    P(x, t) \int_{-\infty}^\infty dr \; r^k \phi_t(x; r)
    \right] \\
    &-
    P(x, t) \int_{-\infty}^\infty dr \; \phi_t(x, -r).
  \end{split}
\end{equation}
Notice that the first and third term on the right-hand side of the equation are
integrating the transition probability over all possible jumps. Because of the
change of integration limits we proposed, these two terms cancel each other and
we are left with
\begin{equation}
  \ddt{P(x, t)} = \sum_{k=1}^\infty {(-1)^k \over k!}
  {\partial^k \over \partial f^k}
  \left[
  P(x, t) \int_{-\infty}^\infty dr \; r^k \phi_t(x; r)
  \right].
  \label{eq_almost_km}
\end{equation}
Let us now define the moments of the jump size distribution $\phi_t(x; r)$ to
be
\begin{equation}
  a^{(k)}(x, t) \equiv \int_{-\infty}^\infty dr \; r^k \phi_t(x; r),
  \label{eq_jump_mom}
\end{equation}
Then we can substitute this into \eref{eq_almost_km} to finally obtain the
Kramers-Moyal expansion of the master equation
\begin{equation}
  \ddt{P(x, t)} = \sum_{k=1}^\infty {(-1)^k \over k!}
  {\partial^k \over \partial f^k}
  \left[
  P(x, t) a^{(k)}(x, t)
  \right].
  \label{eq_km_expansion}
\end{equation}
Notice that on \eref{eq_transition_short_time} we used already the zero$\tth$
moment of the distribution; that's why the use of the notation $a^{(0)}(x, t)$.

So far we converted the complicated integro-differential form of the master
equation in \eref{eq_master_eq_jump} into an equally difficult partial
differential equation of infinite order. Therefore our ability to handle the
equation hasn't been simplified. In the next section we will deal with how to
actually improve our chances of making sense of this by truncating the
expansion up to second order.


\subsubsection{Global Convolutional Network}
Global Convolutional Network (GCN) proposed by Peng et al.
~\cite{Peng2017} addressed the importance to have large kernels for both
localisation and classification for semantic segmentation to enlarge
respective fields.
However, a contradiction arises when performing classification and localisation
tasks.
For instance, classification tasks require the models to be invariant for
different transformations such as rotation and translation.
On the other hand, localisation tasks require the models to be sensitive for
any transformation, to accurately assign each pixel for its semantic category.
Accordingly, to solve this contradiction, two design principles were suggested:
\begin{enumerate}
	\item For the classification task, in order to improve the capability of
	the model to handle different transformations, a large kernel size must be
	used to enable dense connections between feature maps and per-pixel
	classifiers.
	\item For localisation task, the model must be fully convolutional.
	Additionally, fully connected or global pooling layers are not applied as
	these layers will discard the localisation information.
\end{enumerate}

Figure~\ref{fig:gcn} presents the proposed GCN module for semantic segmentation
utilised for delamination identification.
\begin{figure} [h!]
	\begin{center}
		\includegraphics[scale=1.0]{GCN.png}
	\end{center}
	\caption{Global Convolution Network whole architecture.}
	\label{fig:gcn}
\end{figure}
As shown in the Fig.~\ref{fig:gcn}, a residual network was utilised as a backbone for
feature maps extraction, the residual block is shown in
Fig.~\ref{fig:res_gcn_br}a.
After each residual block, a GCN block is inserted
(Fig.~\ref{fig:res_gcn_br}b), which employs a combination of \((1\times
k)\)+\((k\times 1)\) and \((k\times 1)\)+\((1\times k)\) convolutions which
enables a dense connections within a large \((k\times k)\) region in the
feature map.
In this work, we implemented the model with \(k=7\).
This is followed by a boundary refinement (BR) block shown in Fig.~\ref{fig:res_gcn_br}c, which can be considered as an additional residual block to refine the predictions near the object boundaries ended up generating a lower resolution score map.
Furthermore, the upsampling operation is done recursively, it upsamples the low
resolution score maps then concatenate it with a higher one to produce a new
score maps.
The deconvolution operation is repeated until the original image size is
obtained.
\begin{figure} [h!]
	\begin{center}
		\includegraphics[scale=1.0]{res_gcn_br.png}
	\end{center}
	\caption{(a) Residual block, (b) Global Convolution Network block, (c)
		Boundary Refinement}
	\label{fig:res_gcn_br}
\end{figure}


\documentclass{yellowpaper}
\usepackage{graphicx}
\usepackage{balance}  % for  \balance command ON LAST PAGE  (only there!)
\usepackage{pgf-pie}
\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows}

\begin{document}

\title{Emotiq Yellowpaper}

\numberofauthors{1}

\author{
\alignauthor
The team\\
       %\affaddr{Emotiq AG}\\
       \email{info@emotiq.ch}
}
\date{22 March 2018}

\maketitle

\section{UTX Transfer Proof}
When a transaction is submitted with cloaked values, the use of Pedersen commitments enables transfer of tokens to other parties. But those recipients must then be able to use them in future transactions that they initiate.

Proof of ownership is shown by evidence that they know the cloaking and hiding terms in the commitments that originally referred to their tokens. That information must have been transmitted to them through a secret side channel from the originator of the output UTX transferred to them.

Recall that a Pedersen commitment with cloaking refers to a commitment that looks like this:
$$ C = \gamma \, A + (k_{rand} + x) \, B$$
Where $A$ and $B$ are independent generators from group $G_1$, $\gamma$ is a hiding factor for the commitment, $k_{rand}$ is a cloaking factor to protect knowledge of transaction amount $x$.

This commitment is always accompanied by terms
 $$L = (k_{rand} + x) \, A$$
 and
$$R = \gamma \, B$$
  for use in verification.

  For a Fiat-Shamir challenge, computed as the hash of the public transcript:
$$z = Z_r(H(A, B, C, L, R))$$
the verifier computes a new generator in $G_1$ as
$$G = \frac{1}{z} \, A + z\, B$$
and then verifies that
$$ \alpha \, G = \frac{1}{z^2} \, L + C + z^2\, R$$
where $\alpha$ was transmitted to verifier in response to challenge $z$ as
$$\alpha = z \, \gamma + \frac{1}{z} (k_{rand} + x)$$

This proves that the commitment quantities $\gamma$ and $(k_{rand} + x)$ were known by the committer, without having to reveal the actual values involved.

Now when $x$ is transmitted via secret channel to the new owner, we must also transmit the values of $\gamma$ and $k_{rand}$ to them so that they can utilize this output commitment in their own future transaction proofs. Only the person with full knowledge of $\gamma$, $k_{rand}$, and $x$ can produce verifiable proofs on their values when used again.

In effect, it is the value of this output commitment which ties the output UTX to its use as input in a future transaction, and the output commitment should be repeated in input position in that new transaction. That provides a record of transfer.

There is no need for identification with public keys in this process. Nothing needs to tie a commitment to any particular key. It is sufficient to prove knowledge of the commitment quantities, which only the owner of the UTX could possibly know.

Hence complete anonymity is provided in the public ledger. The only person needing knowledge of the public keys of recipients is the initiator of the transaction, for purposes of secret channel communications regarding the commitment items.

A transaction need not even be signed with keyed signatures, since it either validates properly through all the cryptographic validation proofs, or it doesn't. There is no requirement in transfer of ownership to show public information about who originated the transaction, and who now owns output UTX tokens.

This further implies that we should choose generators $A$ and $B$, in $G_1$, once and for all to use, so that prior commitment values, $C$, can be used in a chain of transactions. There should be no need to select different independent generators. If new generators are ever utilized, then we need additional proofs that can verify the chain of transactions through the change of generators. It is simpler just to use previous, already publicly known, values for $A$ and $B$ throughout the entire chain.

\subsection{Protection of UTX Owner}
With the above arrangement, two people know everything they need to know to support spending proofs on a newly created UTX -- the creator of the UTX output, and the new owner.

In order to prevent a dishonest creator from turning around and spending the output UTX for themselves, the output UTX must be declared as directed at some new owner. We need not identify them in the public record, but some public key needs to be made up to support the output and we publish the hash of the output commitment, $C$, and public key, $P$, as $h = H(C, P)$.

Then when the UTX is next spent, the input UTX is presented as a tuple $(h, C, P, Sig(h, P))$, where $Sig(h, P)$ is a valid signature on $h$, corresponding to public key $P$.

Value $h$ could serve as a unique identifier for the UTX, and double spending can be prevented by allowing only one time for $h$ to be used as input UTX in an otherwise valid transaction. Everyone can validate the spending attempt as unique, verify that $h = H(C, P)$, and verify the validity of signature $Sig(h, P)$.

The public key used in the transactions, $P$, can be freshly created and need not correspond to any known public key for the recipient. That preserves a measure of anonymity in these transactions.

So a full transaction record must consist of an input $UTX_{in}$ and one or more $UTX_{out}$ where
$$UTX_{in} = (h, C, P, Sig(h, P))$$
$$UTX_{out, i} = (C_i, H(C_i, P_i))$$
$$Trans = (UTX_{in}, UTX_{out,1}, UTX_{out,2}, ...)$$

The transaction must also include Pedersen commitment proofs on all the quantities involved, along with range proofs on each.

In order for recipients to locate their new UTX, we can store the them along with encrypted vital quantities, in the blockchain blocks, tagged by the hash of the public key used in forming the directed commitments. Only the recipient knows what to look for. The actual public key is never shown until, just once, at the start of a new spend transaction involving that UTX.

\subsection{Bulletproofs}

Bulletproofs are used to provide range checks on token amounts involved in transactions. Every token amount mentioned in a transaction is accompanied by its own Bulletproof.

A Bulleproof includes an uncloaked Pedersen commitment
$$C = \gamma \, A + x \, B$$
 as one of its items, along with a recursively structured short proof of every bit value in the binary encoding of amount $x$. The $\gamma$ blinding factor is randomly generated for each commitment.

In order to produce a verifiable transaction, we take all of those Pedersen commitments, add them all up for the input side, and subtract all of the output side UTXO commitments. Then we add a correction factor in the $A$ curve to show a well recognized encoding of zero amount.

If the sum of the input amounts equals the sum of the output amounts, this process will produce a zero $B$ value. But the sum and difference of the $\gamma$ factors in each Pedersen commitment will likely not be zero, since they are randomly generated. So to that sum and difference of UTX commitments, we must add a correction factor that zeros out only the $A$ component, and carry that correction factor along in the transaction, so that anyone else can verify that the transaction sums to zero.

If we set the correction factor

$$\gamma_{corr} = \sum_{outs} \gamma_{out} - \sum_{ins} \gamma_{in}$$

then we have, for any valid transaction,

$$
\begin{align}
C_{trans} &= \sum_{ins} C_{in} - \sum_{outs} C_{out} + \gamma_{corr} \, A \\
&= 0 \, A + 0 \, B \\
&= \infty
\end{align}
$$

When someone wishes to spend a UTXO arising from this process, they need to produce a signature on the hash of their public key and the Pedersen commitment for that UTXO. Only the recipient can do this, since a signature requires the corresponding private key.

But more than that, they need to start with the same Pedersen commitment value that the UTXO had when it was created. And they need to know the $\gamma$ value used in that commitment so that they too can produce a correction factor in the $A$ curve for the overall new transaction.

Since the correction factor applies only to the $A$ curve, and there is no known relationship between $A$ and $B$, and likewise, there is no known relation between the amounts $x$ and the $\gamma$ factors used in each Pedersen commitment, this suffices as proof that the transaction is valid.

We need to know the value $\gamma$ used when the UTXO Pedersen commitment was created, in order to be able to compute the overall correction factor $\gamma_{corr}$ for the transaction we are creating to spend that UTX.

Knowing only the value of the Pedersen commitment, which anyone can see in plain sight, is not sufficient. And knowing only that commitment value tells you nothing about the $\gamma$ value that was used, even if you know the amount $x$ involved in that UTXO.

\end{document}


\documentclass[12pt, letterpaper, twoside]{article}
\usepackage{nopageno,epsfig, amsmath, amssymb}
\usepackage{physics}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{xcolor}
\hypersetup{
    colorlinks,
    linkcolor={blue},
    citecolor={blue},
    urlcolor={blue}
}
\usepackage{empheq}

\usepackage[letterpaper,
            margin=0.8in]{geometry}

\title{working out kick coordinates}
\author{\textbf{Tom Wagg}}

\newcommand{\question}[1]{{\noindent \it #1}}
\newcommand{\answer}[1]{
    \par\noindent\rule{\textwidth}{0.4pt}#1\vspace{0.5cm}
}
\newcommand{\todo}[1]{{\color{red}\begin{center}TODO: #1\end{center}}}

% custom function for adding units
\makeatletter
\newcommand{\unit}[1]{%
    \,\mathrm{#1}\checknextarg}
\newcommand{\checknextarg}{\@ifnextchar\bgroup{\gobblenextarg}{}}
\newcommand{\gobblenextarg}[1]{\,\mathrm{#1}\@ifnextchar\bgroup{\gobblenextarg}{}}
\makeatother

\newcommand{\avg}[1]{\left\langle #1 \right\rangle}
\newcommand{\angstrom}{\mbox{\normalfont\AA}}
\allowdisplaybreaks

\begin{document}

\section*{kick thoughts}

There's no way I'm going to get this right if I don't write it down haha. Okay so starting from COSMIC we are given the change in velocities due to kicks in the same coordinates system as BSE, which looks like this
\begin{center}
    \includegraphics[width=0.5\textwidth]{static/bse_coordinates.png}
\end{center}
which means that the axes are defined as
\begin{itemize}
    \item \textbf{$x$-axis}: positive $x$ direction points in the direction of motion of the star that is \textit{not} going supernova
    \item \textbf{$y$-axis}: positive $y$ direction points from the non-supernova star to the supernova star
    \item \textbf{$x$-axis}: positive $z$ direction points in the direction of angular momentum vector of the system
\end{itemize}

We are given by COSMIC the change in systemic velocity, which I'm just going to call $\va{v}_{\rm kick}$ due to the kick in this coordinate system. This seems to account for the Blauuw kick and natal kick \textbf{but doesn't take the orbital motion into consideration}. Therefore, we need to add on the orbital motion of the supernova star $\va{v}_{\rm orb}$. From the definition of the coordinate system, this vector points along the negative $x$ axis, such that
\begin{equation}
    \va{v}_{\rm kick, eff} = \mqty[v_{\rm kick, x} - v_{\rm orb} \\ v_{\rm kick, y} \\ v_{\rm kick, z}]
\end{equation}
The orbital speed is calculated just as follows if we assume that the orbit is circular
\begin{equation}
    v_{\rm orb}^2 = \frac{G (M_1 + M_2)}{a}
\end{equation}
Now we just need to move this coordinate system into the Galactocentric frame so that we can add it to the system velocity, $v_{\rm sys}$. I \textit{think} that we consider this as basically a rotation about the $z$ and $x$ axis due to the random orbital phase and inclination to the Galactic plane respectively. Let's call these angles $\theta$ and $\phi$. In this case we can make a rotation matrix
\begin{align}
    \mathcal{R} &= \mqty[ \cos \theta & - \sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 ] \mqty[ 1 & 0 & 0 \\ 0 & \cos \phi & - \sin \phi \\ 0 & \sin \phi & \cos \phi ] \\
                &= \left[
                    \begin{array}{ccc}
                     \cos \theta & - \sin \theta \cos \phi & \sin \theta \sin \phi \\
                     \sin \theta & \cos \theta \cos \phi & -\cos \theta \sin \phi \\
                     0 & \sin \phi & \cos \phi \\
                    \end{array}
                    \right]
\end{align}
Which means that the final systemic velocity should be
\begin{equation}
    v_{\rm sys, final} = \mqty[v_{\rm sys, R} \\ v_{\rm sys, T} \\ v_{\rm sys, z}] + \mathcal{R} \cdot \va{v}_{\rm kick, eff},
\end{equation}
where
\begin{equation}
    \mathcal{R} \cdot \va{v}_{\rm kick, eff} = \left(
        \begin{array}{c}
         \cos \theta (v_{\rm kick, x}-v_{\rm orb})-v_{\rm kick, y} \sin \theta \cos \phi
           +v_{\rm kick, z} \sin \theta \sin \phi \\
         \sin \theta (v_{\rm kick, x}-v_{\rm orb})+v_{\rm kick, y} \cos \theta \cos \phi
           -v_{\rm kick, z} \cos \theta \sin \phi \\
         v_{\rm kick, y} \sin \phi+v_{\rm kick, z} \cos \phi \\
        \end{array}
        \right)
\end{equation}

\end{document}


\documentclass[8pt,oneside]{extarticle}

%\usepackage{subfigure}
\usepackage{subcaption}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{hyperref}
\usepackage{adjustbox}
\usepackage{listings}
\usepackage{optidef}
\usepackage{cleveref}
\usepackage{threeparttable}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{enumitem}
\usepackage{mathrsfs}
\usepackage[driver=pdftex]{geometry}
\usepackage{import}
%\usepackage{titleformat{\section
%        {\normalfont\normalzie\bfseries}{Helo.}{1em}{}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\crefname{table}{table}{table}
\setlength{\parindent}{0em}
\setlength{\parskip}{0.7em}

\counterwithin{table}{section}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}

\newtheorem{theorem}{Theorem}
\newtheorem{definition}{Definition}
\newtheorem{proof}{Proof}

\lstset{style=mystyle}

%\usepackage[margin=0.5in]{geometry}
\usepackage{inputenc}

\newcommand{\Real}{\mathbb{R}}
\newcommand{\Int}{\mathbb{Z}}
\newcommand{\Nat}{\mathbb{N}}
\newcommand{\Complex}{\mathbb{C}}
\newcommand{\vect}[1]{\boldsymbol{#1}}

%\renewcommand{\TPTminimum}{\textwidth}

\renewcommand{\Re}[1]{\mathfrak{Re}\left\lbrace{#1}\right\rbrace}
\renewcommand{\Im}[1]{\mathfrak{Im}\left\lbrace{#1}\right\rbrace}

%\DeclareMathOperator*{\minimize}{minimize}
%\DeclareMathOperator*{\maximize}{maximize}

\title{{\bf MATH 3172 3.0\\ Combinatorial Optimization}\\\vspace{10pt} \large Workshop 5
    \author{Jacques Nel}
}

\begin{document}

\maketitle

\thispagestyle{empty}

\newpage

\pagenumbering{arabic}

\section*{A quick note about revision:}

This is the $3^\mathrm{rd}$ revision of this assignment.

\newpage

\section{A-4 Cane sugar production}

This problem is taken from\footnote{C. Guéret, C. Prins, M. Sevaux, \textit{Applications of optimization with Xpress-MP}. %
Paris: Dash Optimization Ltd., 2007. Page 74.}.

\subsection{Parameters}

Let $W = \left\lbrace 1,\ldots, m\right\rbrace$ enumerate $m=11$ wagons or lots, and
$S=\left\lbrace 1, \ldots, n\right\rbrace$ enumerate $n$ time slots. The refinery has $k=3$ equivalent processing lines.
$n$ timeslots are required to process $m$ wagons where $n$ is given by $n=\texttt{ceil}(m/k)=4$.
Each lot $w\in W$ has an associated hourly loss $\Delta_w$ and remaining lifespan $l_w$ until total loss.
Furthermore, a single lot takes $D=2$ hours to process on any given production line.

\subsection{Decision variable}

Let $\vect{x} = \left[ x_{ws}\right] \in \left\lbrace 0, 1\right\rbrace^{m\times n}$
where for $w\in W$ and $s\in S$,

$$x_{ws} = \begin{cases}
    1 & \text{lot }w\text{ is processed in slot }s\\
    0 & \text{otherwise}
\end{cases}.
$$
\subsection{Model}

We seek to minimize the loss in raw material resulting from fermentation due
to delayed processing of a lot. The model is

\begin{mini!}
    {\vect{x}}{f\left(\vect{x}\right)=\sum_{w\in W}\sum_{s\in S} sd\Delta_{w}x_{ws} \protect\label{eq:a4-obj}}{\label{eq:a4}}{}
    \addConstraint{\sum_{s\in S}x_{ws}}{=1, \forall w\in W \protect\label{eq:a4-cstr1}}
    \addConstraint{\sum_{w\in W}x_{ws}}{\leq k, \forall s\in S \protect\label{eq:a4-cstr2}}
    \addConstraint{\sum_{s\in S}sx_{ws}}{\leq l_w / d, \forall w\in W \protect\label{eq:a4-cstr3}}
\end{mini!}

The objective function \cref{eq:a4-obj} is the total loss in raw material resulting from delayed processing
summed over all lots and wagons. All lots must be assigned to exactly one slot as enforced by \cref{eq:a4-cstr1}.
Next, \cref{eq:a4-cstr2} guarantees that at most $k=3$ lots can be processed in any one timeslot. Finally,
\cref{eq:a4-cstr3} ensures that a lot is processed before its total loss occurs. Observe that total loss of a lot
occurs after $l_w / d$ slots.

\newpage

\subsection{Results}

The optimal solution results in a loss of $f\left(\vect{x}^*\right) = 1620$ kg with the following
time slot assignments:

\begin{table}[h]
    \centering
    \caption{Optimal time slot allocations for each lot}\label{table:a4-results}
    \begin{tabular}{cccc}
        \hline
        \textbf{Slot 1} & \textbf{Slot 2} & \textbf{Slot 3} & \textbf{Slot 4} \\
        \hline
        lot 3   & lot 1 & lot 10 & lot 2 \\
        lot 6   & lot 5 & lot 8 & lot 4 \\
        lot 7   & lot 8 & &\\
        \hline

    \end{tabular}

    \medskip
    \emph{Note:} Column $j$ is generated with $\left\lbrace w : x_{wj} = 1, \forall w \in
            \mathrm{W}\right\rbrace$.
\end{table}

\section{A-6 Production of electricity}

This problem is taken from\footnote{C. Guéret, C. Prins, M. Sevaux, \textit{Applications of optimization with Xpress-MP}. %
Paris: Dash Optimization Ltd., 2007. Page 78.}.

\subsection{Parameters}

Let $T=\lbrace 1,\ldots, n\rbrace$ enumerate $n=7$ time periods (of varying length) and $P=\lbrace 1,\ldots, m\rbrace$ enumerate
$m=4$ generator types. For a time period $t\in T$ let $l_t$
denote the length of time period in hours, and
let $d_t$ denote the forecasted power demand given by $d_t$.

\begin{table}[h]
    \center
    \caption{Length and forecasted demand of time periods}\label{table:a6-periods}
    \begin{tabular}{c c c}
        \hline
        \textbf{Period} $t$ & \textbf{Length} $l_t$ & \textbf{Demand} $d_t$ \\
        \hline
        1 & 6 & $1.2 \times 10^4$ \\
        2 & 3 & $3.2 \times 10^4$ \\
        3 & 3 & $2.5 \times 10^4$ \\
        4 & 2 & $3.6 \times 10^4$ \\
        5 & 4 & $2.5 \times 10^4$ \\
        6 & 4 & $3.0 \times 10^4$ \\
        7 & 2 & $1.8 \times 10^4$ \\
        \hline
    \end{tabular}
\end{table}

For each generator type $p\in P$, there are $a_p$ units available. Each unit has
a minimum base power output $\theta_p$ (if it is running) and can scale up to a
maximum output denoted $\psi_p$, but incurring additional operating cost.

\begin{table}[h]
    \center
    \caption{Number of available units and power output capacity}\label{table:a6-generators}
    \begin{tabular}{cccc}
        \hline
        \textbf{Type} & \textbf{Num. available} $a_p$ & \textbf{Min. output} $\theta_p$ & \textbf{Max output} $\psi_p$ \\
        \hline
        1 & 10 & $7.5\times 10^2$ & $1.75\times 10^3$ \\
        2 & 4 & $1.0\times 10^3$ & $1.5\times 10^3$ \\
        3 & 8 & $1.2\times 10^3$ & $2.0\times 10^3$ \\
        4 & 3 & $1.8\times 10^3$ & $3.5\times 10^3$ \\
        \hline
    \end{tabular}
\end{table}

\newpage

Starting a generator unit of type $p\in P$ incurs a startup cost $\lambda_p$. Running the generator
incurs a fixed cost per hour $\mu_p$. Additional scalable output, on top of
the base output, incurs a hourly cost $\nu_p$ that is proportional to the additional output.

\begin{figure}[h]
    \protect\label{fig:a6-costs}
    \center
    \caption{Various costs associated with generator types}
    \begin{tabular}{cccc}
        \hline
        \textbf{Type} & \textbf{Start cost} $\lambda_p$ & \textbf{Run cost} $\mu_p$ & \textbf{Add. cost} $\nu_p$ \\
        \hline
        1 & 5000 & 2250 & 2.7 \\
        2 & 1600 & 1800 & 2.2 \\
        3 & 2400 & 3750 & 1.8 \\
        4 & 1200 & 4800 & 3.8 \\
        \hline
    \end{tabular}
\end{figure}

\subsection{Decision variables}

Suppose $p\in P$ and $t\in T$. Let
$0 \leq x_{pt} \in\Int$ denote the number of generators of type $p$ started in
period $t$, and
$0 \leq y_{pt} \in\Int$ be the number of generators of type $p$ running in
period $t$. Finally, let $0 \leq z_{pt}\in\Real$ denote the additional power generated by unit of type $p$ during
period $t$.

To simplify notation, let $\vect{x} = \left[x_{pt}\right] \in
\Int^{m\times n}, \vect{y} = \left[y_{pt}\right] \in \Int^{m\times n}$,
and $\vect{z} = \left[z_{pt}\right] \in \Real^{m\times n}$.

\subsection{Model}

\begin{mini!}
    {\vect{x}, \vect{y}, \vect{z}}{\sum_{t\in T}\sum_{p\in P} \lambda_{p}x_{pt} + l_t\left(\mu_p y_{pt} + \nu_p z_{pt}\right) \protect\label{eq:a6-obj}}{\label{eq:a6}}{}
    \addConstraint{x_{p1}}{\geq y_{p1} - y_{pn}, \forall p\in P \protect\label{eq:a6-cstr1}}
    \addConstraint{x_{pt}}{\geq y_{pt} - y_{p(t-1)}, \forall p\in P, 1 < t\in T \protect\label{eq:a6-cstr2}}
    \addConstraint{z_{pt}}{\leq \left(\psi_p - \theta_p\right)y_{pt}, \forall (p, t)\in P\times T \protect\label{eq:a6-cstr3}}
    \addConstraint{\sum_{p\in P} \theta_p y_{pt} + z_{pt}}{\geq d_t, \forall t\in T \protect\label{eq:a6-cstr4}}
    \addConstraint{\sum_{p\in P} \psi_p y_{pt}}{\geq 1.2 d_t, \forall t\in T \protect\label{eq:a6-cstr5}}
    \addConstraint{y_{pt}}{\leq a_p, \forall (p,t)\in P\times T \protect\label{eq:a6-cstr6}}
    \addConstraint{x_{pt}}{\geq 0, \forall (p,t)\in P\times T \protect\label{eq:a6-cstr7}}
    \addConstraint{y_{pt}}{\geq 0, \forall (p,t)\in P\times T \protect\label{eq:a6-cstr8}}
    \addConstraint{z_{pt}}{\geq 0, \forall (p,t)\in P\times T \protect\label{eq:a6-cstr9}}
\end{mini!}

The cost function \cref{eq:a6-obj} is simply the startup cost, running cost, and
additional power cost summed over all decision variables. The number of generators started
for $t=1$ is related to the number of generators running by \cref{eq:a6-cstr1}. This relationship
depends on the numbers generators running at the end period $t=n$. The next family of
constraints \cref{eq:a6-cstr2} is similar to the above, but deals with the relationship
for $1< t \leq n$.

Additional power output $z_{pt}$ is bounded by the difference between the maximum and
the base output, ie. $\psi_p - \theta_p$, as expressed
by \cref{eq:a6-cstr3}. Next, \cref{eq:a6-cstr4} ensures that the total ouput of all
generator units meets forecasted demand $d_t$ for all $t\in T$. Furthermore, a $20\%$
safety buffer is required at all times. \Cref{eq:a6-cstr5} ensures that, if demand
were to suddenly spike, a minimum of $20\%$ of $d_t$ of additional capacity can instantly be
made available, by increasing additional output $z_{pt}$ up its maximum $\psi_p-\theta_p$.

The family of constraints \cref{eq:a6-cstr6} simply places an upper bound on the number
of units running, in a given period, equal to the given available number of units
$a_p$ of each type. Finally, \cref{eq:a6-cstr7}, \cref{eq:a6-cstr8}, and
\cref{eq:a6-cstr9} simply enforce the canonical non-negativity of $x_{pt}$, $y_{pt}$, and $z_{pt}$
respectively.

\subsection{Results}

The optimal solution was found with a total operating cost of
$f\left(\vect{x}^*, \vect{y}^*, \vect{z}^*\right) = \$1,456,810$.

\begin{table}[ht]
    \centering
    \caption{Optimal power generation schedule for 4 generator types over 7 planning periods}
\begin{tabular}{clrrrrrrr}
    \hline
    \textbf{Type} & & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} \\
\hline
    \textbf{1} & No. used & 3 & 4 & 4 & 7 & 3 & 3 & 3\\
 & Tot. output & 2250 & 4600 & 3000 & 8600 & 2250 & 2600 & 2250\\
 & Add. output & 0 & 1600 & 0 & 3350 & 0 & 350 & 0\\
 \hline
    \textbf{2} & No. used & 4 & 4 & 4 & 4 & 4 & 4 & 4\\
 & Tot. output & 5750 & 6000 & 4200 & 6000 & 4950 & 6000 & 5950\\
 & Add. output & 1750 & 2000 & 200 & 2000 & 950 & 2000 & 1950\\
 \hline
    \textbf{3} & No. used & 2 & 8 & 8 & 8 & 8 & 8 & 4\\
 & Tot. output & 4000 & 16000 & 16000 & 16000 & 16000 & 16000 & 8000\\
 & Add. output & 1600 & 6400 & 6400 & 6400 & 6400 & 6400 & 3200\\
 \hline
    \textbf{4} & No. used & 0 & 3 & 1 & 3 & 1 & 3 & 1\\
 & Tot. output & 0 & 5400 & 1800 & 5400 & 1800 & 5400 & 1800\\
 & Add. output & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
 \hline
\end{tabular}

\medskip
\emph{Note:} The above table was generated from the solution values $x_{pt}^*,
y_{pt}^*$, and $z_{pt}^*$ with \texttt{a-6\_report.py}.
\end{table}

\section{C-2 Production of drinking glasses}

This problem is taken from\footnote{C. Guéret, C. Prins, M. Sevaux, \textit{Applications of optimization with Xpress-MP}. %
Paris: Dash Optimization Ltd., 2007. Page 106.}.

\subsection{Parameters}

Let $W =\left\lbrace 1,\ldots, n\right\rbrace$ enumerate $n=12$ week-long planning
periods, and let $P=\left\lbrace 1,\ldots, m\right\rbrace$ enumerate the $m=6$
product variants. Each product $p\in P$ has a predicted demand $d_{pt}$ during week
$t\in W$ as given in \cref{table:c2-demands}.

\begin{table}[h]
    \center
    \caption{Predicted weekly demand for each product variant}\label{table:c2-demands}
    \begin{tabular}{c cccccccccccc}
        \hline
        \textbf{Week} & \textbf{1} &\textbf{2} &\textbf{3} &\textbf{4} &\textbf{5} &\textbf{6} &\textbf{7} &\textbf{8} &\textbf{9} &\textbf{10} &\textbf{11} &\textbf{12} \\
        \hline
        \textbf{V1} & 20 & 22 & 18 & 35 & 17 & 19 & 23 & 20 & 29 & 30 & 28 & 32 \\
        \textbf{V2} & 17 & 19 & 23 & 20 & 11 & 10 & 12 & 34 & 21 & 23 & 30 & 12 \\
        \textbf{V3} & 18 & 35 & 17 & 10 & 9  & 21 & 23 & 15 & 10 & 0 & 13 & 17 \\
        \textbf{V4} & 31 & 45 & 24 & 38 & 41 & 20 & 19 & 37 & 28 & 12 & 30 & 37 \\
        \textbf{V5} & 23 & 20 & 23 & 15 & 10 & 22 & 18 & 30 & 28 & 7 & 15 & 10 \\
        \textbf{V6} & 22 & 18 & 20 & 19 & 18 & 35 & 0 & 28 & 12 & 30 & 21 & 23 \\
        \hline
    \end{tabular}
\end{table}

Each product variant $p\in P$ has an associated basic production cost $\lambda_p$
and an inventory storage cost $\mu_p$ incurred on product in inventory over
given period. Production requires a known amount worker labour time $\delta_p$,
machine time $\pi_p$, and production area $\gamma_p$. In every period there
is a limited amount of worker time $\Delta$, available machine time $\Pi$, and
production area $\Gamma$. Lastly, at the start of planning period there exists volume
$I_p$ of item $p$ in the inventory, and it is required that there is $F_p$ of item
$p$ at the end of the planning period in the inventory. All parameters are given in
\cref{table:c2-params}.

\begin{table}[h]
    \center
    \caption{Given costs, production resources, and inventory of product variants}
    \label{table:c2-params}
    \begin{tabular}{cccccccc}
        \hline
        & \textbf{prod. cost} & \textbf{inv. cost}  &
        \textbf{init. stock}  & \textbf{fin. stock}  &
        \textbf{labour}  & \textbf{mach. time}  &
        \textbf{area} \\
        \hline
        \textbf{V1} & 100 & 25 & 50 & 10 & 3 & 2 & 4 \\
        \textbf{V2} & 80 & 28 & 20 & 10 & 3 & 1 & 5 \\
        \textbf{V3} & 110 & 25 & 0 & 10 & 3 & 4 & 5 \\
        \textbf{V4} & 90 & 27 & 15 & 10 & 2 & 8 & 6 \\
        \textbf{V5} & 200 & 10 & 0 & 10 & 4 & 11 & 4 \\
        \textbf{V6} & 150 & 20 & 10 & 10 & 4 & 9 & 9 \\
        \hline
    \end{tabular}
\end{table}

\newpage

\subsection{Decision variables}

For a given product $p\in P$ and week $t\in W$ let
$0 \leq x_{pt} \in\Int$ denote the production volume.
Also let $0\leq y_{pt}\in\Int$ denote the amount of product stored in the inventory
at the end of period $t$.

To simplify notation let $\vect{x} = \left[ x_{pt} \right] \in \Int^{m\times x}$
and $\vect{y} = \left[ y_{pt} \right] \in \Int^{m\times x}$.

\subsection{Model}

\begin{mini!}
    {\vect{x}, \vect{y}}{f\left(\vect{x}, \vect{y}\right)=\sum_{p\in P}\sum_{t\in W} \lambda_p x_{pt} + \mu_p y_{pt} \protect\label{eq:c2-obj}}{\label{eq:c2}}{}
    \addConstraint{y_{p1}}{= I_p + x_{pt} - d_{pt}, \forall p\in P \protect\label{eq:c2-cstr1}}
    \addConstraint{y_{pt}}{= y_{p(t-1)} + x_{pt} - d_{pt}, \forall p\in P, t\in W \protect\label{eq:c2-cstr2}}
    \addConstraint{y_{pn}}{= F_p, \forall p\in P, \protect\label{eq:c2-cstr3}}
    \addConstraint{\sum_{p\in P} \delta_p x_{pt}}{\leq \Delta, \forall t\in W, \protect\label{eq:c2-cstr4}}
    \addConstraint{\sum_{p\in P} \pi_p x_{pt}}{\leq \Pi, \forall t\in W, \protect\label{eq:c2-cstr5}}
    \addConstraint{\sum_{p\in P} \gamma_p x_{pt}}{\leq \Gamma, \forall t\in W, \protect\label{eq:c2-cstr6}}
    \addConstraint{x_{pt}}{\geq 0 \forall p\in P, t\in W, \protect\label{eq:c2-cstr7}}
    \addConstraint{y_{pt}}{\geq 0 \forall p\in P, t\in W, \protect\label{eq:c2-cstr8}}
\end{mini!}

We seek to minimize total production cost. \Cref{eq:c2-obj} is a cost function which simply sums the total production and storage
costs over all decision variables.

\Cref{eq:c2-cstr1} and \cref{eq:c2-cstr2} states that the inventory at time $t$ is
equal the previous inventory plus the product minus the demand. \Cref{eq:c2-cstr1}
makes special consideration for the initial inventory $I_p$. \Cref{eq:c2-cstr3}
ensures that the final inventory for product $p$ is equal to $F_p$ at the end of the
planning period.

\Cref{eq:c2-cstr4}, \cref{eq:c2-cstr5} and \cref{eq:c2-cstr6} ensures that limited
production factors: worker time capacity $\Delta$, machine time capacity
$\Pi$, and production area $\Gamma$ constraints are respected. For example,
production of product $p$ during period $t$ requires $\pi_p x_{pt}$ machine hours, the total
of which shall not exceed $\Pi$ for the given period.

Lastly \cref{eq:c2-cstr7} and \cref{eq:c2-cstr8} are simply the cononical non-negativity
constraints on both decision variables $x_{pt}$ and $y_{pt}$.

\newpage

\subsection{Results}

A optimal solution $\left(\vect{x}^*, \vect{t}^*\right)$ is found with a total
production cost of $f\left(\vect{x}^*, \vect{y}^*\right) = \$186,076$.

\begin{table}[h]
    \center
    \caption{Production and storage quantities for each product type}
\begin{tabular}{cccccccccccccc}
    \hline
    & \textbf{Week} & \textbf{1} &\textbf{2} &\textbf{3} &\textbf{4} &\textbf{5} &\textbf{6} &\textbf{7} &\textbf{8} &\textbf{9} &\textbf{10} &\textbf{11} &\textbf{12} \\
    \hline
    \textbf{1} & Prod. & 0 & 0 & 11 & 34 & 29 & 7 & 23 & 21 & 29 & 29 & 29 & 41\\
& Store & 30 & 8 & 1 & 0 & 12 & 0 & 0 & 1 & 1 & 0 & 1 & 10\\
\textbf{2} & Prod. & 7 & 21 & 14 & 17 & 11 & 10 & 12 & 34 & 21 & 23 & 30 & 22\\
& Store & 10 & 12 & 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 10\\
\textbf{3} & Prod. & 18 & 35 & 17 & 11 & 8 & 21 & 23 & 15 & 10 & 0 & 13 & 27\\
& Store & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 10\\
\textbf{4} & Prod. & 16 & 45 & 24 & 38 & 41 & 20 & 20 & 36 & 29 & 11 & 31 & 46\\
& Store & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 10\\
\textbf{5} & Prod. & 47 & 16 & 34 & 14 & 23 & 24 & 43 & 0 & 26 & 4 & 0 & 0\\
& Store & 24 & 20 & 31 & 30 & 43 & 45 & 70 & 40 & 38 & 35 & 20 & 10\\
\textbf{6} & Prod. & 14 & 17 & 20 & 18 & 18 & 35 & 1 & 27 & 12 & 49 & 28 & 7\\
& Store & 2 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 19 & 26 & 10\\
\hline
\end{tabular}
\end{table}

\emph{Note:} I choose to solve this as an integer programming model. This is
the reason why my results differ slightly from the book. The above table simply states the optimal
solution values for $x_{pt}^*$ and $y_{pt}^*$ for all $p\in P$ and $t\in W$.

\section{D-5 Cutting sheet metal}

This problem is taken from\footnote{C. Guéret, C. Prins, M. Sevaux, \textit{Applications of optimization with Xpress-MP}. %
Paris: Dash Optimization Ltd., 2007. Page 134.}.

\subsection{Parameters}

Let $S =\left\lbrace 1, \ldots, n\right\rbrace$ enumerate $n=4$ different sizes, ie.
$\left\lbrace \texttt{36x50}, \texttt{24x36}, \texttt{20x60}, \texttt{18x30}\right\rbrace$.
Also, let $P=\left\lbrace 1,\ldots, m\right\rbrace$ enumerate $m$ different cutting patterns.
For $s\in S$ and $p\in P$ let $c_{sp}$ denote number of pieces of size $s$ yielded
by pattern $p$. The values of $c_{sp}$ are given by \cref{table:d5-yield}.

\begin{table}[h]
    \center
    \caption{Yields of various cutting patterns}\label{table:d5-yield}
    \begin{tabular}{ccccccccccccccccc}
        \hline
        \textbf{Pattern} & \textbf{1} & \textbf{2} &\textbf{3} &\textbf{4} &\textbf{5}&\textbf{6}&\textbf{7}&\textbf{8}&\textbf{9}&\textbf{10}&\textbf{11}&\textbf{12}&\textbf{13}&\textbf{14}&\textbf{15}&\textbf{16} \\
        \hline
        \texttt{36x50} & 1 & 1 & 1 & 0 & 0 & 0 &0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        \texttt{24x36} & 2 & 1 & 0 & 2 & 1 & 0 & 3 & 2 & 1 & 0 & 5 & 4 & 3 & 2 & 1 & 0 \\
        \texttt{20x60} & 0 & 0 & 0 & 2 & 2 & 2 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
        \texttt{18x30} & 0 & 1 & 3 & 0 & 1 & 3 & 0 & 2 & 3 & 5 & 0 & 1 & 3 & 5 & 6 & 8 \\
        \hline
    \end{tabular}
\end{table}

Each cut size $s\in S$ has a given demand $\vect{d} =\left( d_s : s \in S\right)^T
= \left( 8, 13, 5, 15\right)^T$. Finaly, each pattern has equivalent cost $\kappa = 1$,
or simply the cost of each sheet of raw material.

\subsection{Decision variable}

Let $0 \leq x_{p} \in \Int$ denote the number of times pattern $p$ is used. To simplify
notation let $\vect{x} = \left( x_p : p \in P\right)$.

\subsection{Model}

\begin{mini!}
    {\vect{x}}{f(\vect{x}) = \sum_{p\in P} x_p\kappa \protect\label{eq:d5-obj}}{\label{eq:d5}}{}
    \addConstraint{\sum_{p\in P} c_{sp} x_p}{\geq d_s, \forall s\in S \protect\label{eq:d5-cstr1}}
    \addConstraint{x_p}{\geq 0, \forall p\in P \protect\label{eq:d5-cstr2}}
\end{mini!}

We seek to minimize the total cost which is given by \cref{eq:d5-obj}. This is simply
the total number of sheets of raw material used. \Cref{eq:d5-cstr1} is the family
of demand constraints, which guarantee that demand is met for each size $s\in S$.
Finaly \cref{eq:d5-cstr2} is simply the canonical non-negativity constraint on
the decision variable $x_p$.

\subsection{Results}

An optimal solution is found which uses 11 sheets of raw material to satisfy
demand, with a cost function value of $f\left(\vect{x}^*\right)= 11$. The following quantities of each pattern
are used:

\texttt{pattern 1} = 3,
\texttt{pattern 3} = 5,
\texttt{pattern 4} = 2,
\texttt{pattern 7} = 1,
and all others are unused. These values are simply the optimal non-zero values of the decision variable
$x_p^*, \forall p\in\mathrm{P}$.

\section{F-1 Flight connections at a hub}

This problem is taken from\footnote{C. Guéret, C. Prins, M. Sevaux, \textit{Applications of optimization with Xpress-MP}. %
Paris: Dash Optimization Ltd., 2007. Page 157.}.

\subsection{Parameters}

Let $P = \left\lbrace 1, \ldots, n\right\rbrace$ enumerate both $n=6$ incoming flights
and then $n$ outgoing flights. For $i\in P$ and $j\in P$, let $\mu_{ij}$ denote
the number of passengers arriving on flight $i$ from origin $i$ seeking to continue
on to destination $j$ given by \cref{table:f1-passengers}.

\begin{table}[h]
    \center
    \caption{Arriving passengers and destinations}\label{table:f1-passengers}
    \begin{tabular}{c|cccccc}
        \hline
        \textbf{City} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} \\
        \textbf{1} & 35 & 12 & 16 & 38 & 5 & 2 \\
        \textbf{2} & 25 & 8 & 9 & 24 & 6 & 8 \\
        \textbf{3} & 12 & 8 & 11 & 27 & 3 & 2 \\
        \textbf{4} & 38 & 15 & 14 & 30 & 2 & 9 \\
        \textbf{5} & - & 9 &   8 & 25 &  10 & 5 \\
        \textbf{6} & - & - & - & 14 &  6 & 7 \\
        \hline
    \end{tabular}
\end{table}

\subsection{Decision variable}

Let $x_{ij} \in\lbrace 0, 1\rbrace$ indicate that aircraft
    from origin $i\in P$ travels to destination $j\in P$ for next flight when
    $x_{ij} = 1$. To simplify notation let $\vect{x} = \left[x_{ij}\right]
    \in \left\lbrace 0, 1\right\rbrace^{n\times n}$.

\subsection{Model}

We seek to minimize the number of passengers requiring to disembark and transfer to another plane for their
next flight. In other words, we wish to maximize the number of passengers staying on their arriving aircraft.

\begin{mini!}
    {\vect{x}}{f(\vect{x}) = \sum_{i\in P} \sum_{j\in P} \mu_{ij}x_{ij} \protect\label{eq:f1-obj}}{\label{eq:f1}}{}
    \addConstraint{\sum_{j\in P} \mu_{ij}x_{ij}}{= 1, \forall i\in P \protect\label{eq:f1-cstr1}}
    \addConstraint{\sum_{i\in P} \mu_{ij}x_{ij}}{= 1, \forall j\in P. \protect\label{eq:f1-cstr2}}
\end{mini!}

\subsection{Results}

The optimal solution has a total of $f\left(\vect{x}^*\right)= 112$ passengers remaining on their arrival flights for the
remainder of their journeys. The following assignment of aircraft to destination, minimizes passenger
inconvenience:
\medskip

Bordeaux $\rightarrow$ London 38

Clermon-Ferrand $\rightarrow$ Bern 8

Marseille $\rightarrow$ Brussels 11

Nantes $\rightarrow$ Berlin 38

Nice $\rightarrow$ Rome 10

Toulouse $\rightarrow$ Vienna 7

\medskip
\emph{Note:} The above mapping is simply constructed by the permutation matrix given by $\vect{x}^*$.

Refer to \texttt{f-1\_report.py}
for implementation details.

\end{document}