\chapter{Deadlock}

\epigraph{No, you can't always get what you want
	\\You can't always get what you want
	\\You can't always get what you want
	\\But if you try sometimes you find
	\\You get what you need}{The philosophers Jagger \& Richards}

Deadlock is defined as when a system cannot make any forward progress.
We define a system for the rest of the chapter as a set of rules by which a set of processes can move from one state to another, where a state is either working or waiting for a particular resource.
Forward progress is defined as if there is at least one process working or we can award a process waiting for a resource that resource.
In a lot of systems, Deadlock is avoided by ignoring the entire concept \cite[P.237]{silberschatz2006operating}.
Have you heard about turn it on and off again?
For products where the stakes are low (User Operating Systems, Phones), it may be more efficient to allow deadlock.
But in the cases where ``failure is not an option'' - Apollo 13, you need a system that tracks, breaks, or prevents deadlocks.
Apollo 13 didn't fail because of deadlock, but it wouldn't be good to restart the system on liftoff.

Mission-critical operating systems need this guarantee formally because playing the odds with people's lives isn't a good idea.
Okay so how do we do this? We model the problem.
Even though it is a common statistical phrase that all models are wrong, the more accurate the model is to the system the higher the chance the method will work.

\section{Resource Allocation Graphs}

\begin{figure}[H]
	\centering
	\includegraphics[width=.6\textwidth]{deadlock/drawings/rag.eps}
	\caption{Resource allocation graph}
	\label{ragfigure}
\end{figure}

One such way is modeling the system with a resource allocation graph (RAG).
A resource allocation graph tracks which resource is held by which process and which process is waiting for a resource of a particular type.
It is a simple yet powerful tool to illustrate how interacting processes can deadlock.
If a process is \emph{using} a resource, an arrow is drawn from the resource node to the process node.
If a process is \emph{requesting} a resource, an arrow is drawn from the process node to the resource node.
If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only one instance, then the processes will deadlock.
For example, if process 1 holds resource A, process 2 holds resource B and process 1 is waiting for B and process 2 is waiting for A, then processes 1 and 2 will be deadlocked \ref{ragfigure}.
We'll make the distinction that the system is in deadlock by definition if all workers cannot perform an operation other than waiting.
We can detect a deadlock by traversing the graph and searching for a cycle using a graph traversal algorithm, such as the Depth First Search (DFS).
This graph is considered as a directed graph and we can treat both the processes and resources as nodes.

\begin{minted}{C}
	typedef struct {
		int node_id; // Node in this particular graph
		Graph **reachable_nodes; // List of nodes that can be reached from this node
		int size_reachable_nodes; // Size of the List
	} Graph;

	// isCyclic() traverses a graph using DFS and detects whether it has a cycle
	// isCyclic() uses a recursive approach
	// G points to a node in a graph, which can be either a resource or a process
	// is_visited is an array indexed with node_id and initialized with zeros (false) to record whether a particular node has been visited
	int isCyclic(Graph *G, int* is_visited) {
		if (this graph has been visited) {
			// Oh! the cycle is found
			return true;
			} else {
			1. Mark this node as visited
			2. Traverse through all nodes in the reachable_nodes
			3. Call isCyclic() for each node
			4. Evaluate the return value of isCyclic()
		}
		// Nope, this graph is acyclic
		return false;
	}
\end{minted}

\begin{figure}[H]
	\centering
	\includegraphics[width=.5\textwidth]{deadlock/drawings/deadlock.eps}
	\caption{Graph based Deadlock}
\end{figure}

\section{Coffman Conditions}

Surely cycles in RAGs happen all the time in an OS, so why doesn't it grind to a halt?
You may not see deadlock because the OS may \textbf{preempt} some processes breaking the cycle but there is still a chance that your three lonely processes could deadlock.

There are four \emph{necessary} and \emph{sufficient} conditions for deadlock -- meaning if these conditions hold then there is a non-zero probability that the system will deadlock at any given iteration.
These are known as the Coffman Conditions \cite{coffman1971system}.

\begin{itemize}
	\tightlist
	\item
	      Mutual Exclusion: No two processes can obtain a resource at the same time.
	\item
	      Circular Wait: There exists a cycle in the Resource Allocation Graph, or there exists a set of processes \{P1, P2,\ldots{}\} such that P1 is waiting for resources held by P2, which is waiting for P3,\ldots{}, which is waiting for P1.
	\item
	      Hold and Wait: Once a resource is obtained, a process keeps the resource locked.
	\item
	      No pre-emption: Nothing can force the process to give up a resource.
\end{itemize}

\begin{proof} Deadlock can happen if and only if the four Coffman conditions are satisfied.

	$\rightarrow$ If the system is deadlocked, the four Coffman conditions are apparent.

	\begin{itemize}
		\item For contradiction, assume that there is no circular wait. If not then that means the resource allocation graph is acyclic, meaning that there is at least one process that is not waiting on any resource to be freed. Since the system can move forward, the system is not deadlocked.
		\item For contradiction, assume that there is no mutual exclusion. If not, that means that no process is waiting on any other process for a resource. This breaks circular wait and the previous argument proves correctness.
		\item For contradiction, assume that processes don't hold and wait but our system still deadlocks. Since we have circular wait from the first condition at least one process must be waiting on another process. If that and processes don't hold and wait, that means one process must let go of a resource. Since the system has moved forward, it cannot be deadlocked.
		\item For contradiction, assume that we have preemption, but the system cannot be un-deadlocked. Have one process, or create one process, that recognizes the circular wait that must be apparent from above and break one of the links. By the first branch, we must not have deadlocked.
	\end{itemize}

	$\leftarrow$ If the four conditions are apparent, the system is deadlocked.
	We will prove that if the system is not deadlocked, the four conditions are not apparent.
	Though this proof is not formal, let us build a system with the three requirements not including circular wait.
	Let assume that there is a set of processes $P = \{p_1, p_2, ..., p_n\}$ and there is a set of resources $R = \{r_1, r_2, ..., r_m\}$.
	For simplicity, a process can only request one resource at a time but the proof can be generalized to multiple.
	Let assume that the system is a state at time $t$.
	Let us assume that the state of the system is a tuple $(h_t, w_t)$ where there are two functions $h_t: R \rightarrow P \cup \{\text{unassigned}\}$ that maps resources to the processes that own them (this is a function, meaning that we have mutual exclusion) and or unassigned and $w_t: P \rightarrow R \cup \{\text{satisfied}\}$ that maps the requests that each process makes to a resource or if the process is satisfied.
	If the process is satisfied, we consider the work trivial and the process exits, releasing all resources -- this can also be generalized.
	Let $L_t \subseteq P \times R$ be a set of lists of requests that a process uses to release a resource at any given time.
	The evolution of the system is at each step at every time.

	\begin{itemize}
		\item Release all resources in $L_t$.
		\item Find a process that is requesting a resource
		\item If that resource is available give it to that process, generating a new $(h_{t+1}, w_{t+1})$ and exit the current iteration.
		\item Else find another process and try the same resource allocation procedure in the previous step.
	\end{itemize}

	If all processes have been surveyed and if all are requesting a resource and none can be granted a resource, consider it deadlocked.
	More formally, this system is deadlocked means if $\exists t_0, \forall t \geq t_0, \forall p \in P, w_t(p) \neq \text{satisfied} \text{ and } \exists q, q \neq p \rightarrow h_t(w_t(p)) = q$ (which is what we need to prove).

	Mutual exclusion and no pre-emption are encoded into the system.
	Circular wait implies the second condition, a resource is owned by another process which is owned by another process meaning at this state $\forall p \in P, \exists q \neq p \rightarrow h_t(w_t(p)) = q$.
	Circular wait also implies that at this current state, no process is satisfied, meaning at this state $\forall p \in P, w_t(p) \neq \text{satisfied}$.
	Hold and wait simply proves the condition that from this point onward, the system will not change, which is all the conditions that we needed to show.
\end{proof}

If a system breaks any of them, it cannot have deadlock!
Consider the scenario where two students need to write both pen and paper and there is only one of each.
Breaking mutual exclusion means that the students share the pen and paper.
Breaking circular wait could be that the students agree to grab the pen then the paper.
As proof by contradiction, say that deadlock occurs under the rule and the conditions.
Without loss of generality, that means a student would have to be waiting on a pen while holding the paper and the other waiting on a pen and holding the paper.
We have contradicted ourselves because one student grabbed the paper without grabbing the pen, so deadlock fails to occur.
Breaking hold and wait could be that the students try to get the pen and then the paper and if a student fails to grab the paper then they release the pen.
This introduces a new problem called \textit{livelock} which will be discussed later.
Breaking preemption means that if the two students are in deadlock the teacher can come in and break up the deadlock by giving one of the students a held item or tell both students to put the items down.

livelock relates to deadlock.
Consider the breaking hold-and-wait solution as above.
Though deadlock is avoided, if the philosopher picks up the same device again and again in the same pattern, no work will be done.
Livelock is generally harder to detect because the processes generally look like they are working to the outside operating system whereas in deadlock the operating system generally knows when two processes are waiting on a system-wide resource.
Another problem is that there are necessary conditions for livelock (i.e. deadlock fails to occur) but not sufficient conditions -- meaning there is no set of rules where livelock has to occur.
You must formally prove in a system by what is known as an invariant.
One has to enumerate each of the steps of a system and if each of the steps eventually -- after some finite number of steps -- leads to forward progress, the system fails to livelock.
There are even better systems that prove bounded waits; a system can only be livelocked for at most $n$ cycles which may be important for something like stock exchanges.

\section{Approaches to Solving Livelock and Deadlock}

Ignoring deadlock is the most obvious approach.
Quite humorously, the name for this approach is called the ostrich algorithm.
Though there is no apparent source, the idea for the algorithm comes from the concept of an ostrich sticking its head in the sand.
When the operating system detects deadlock, it does nothing out of the ordinary, and any deadlock usually goes away.
An operating system preempts processes when stopping them for context switches.
The operating system can interrupt any system call, potentially breaking a deadlock scenario.
The OS also makes some files read-only thus making the resource shareable.
What the algorithm refers to is that if there is an adversary that specifically crafts a program -- or equivalently a user who poorly writes a program -- that the OS deadlocks.
For everyday life, this tends to be fine.
When it is not we can turn to the following method.

Deadlock detection allows the system to enter a deadlocked state.
After entering, the system uses the information to break deadlock.
As an example, consider multiple processes accessing files.
The operating system can keep track of all of the files/resources through file descriptors at some level either abstracted through an API or directly.
If the operating system detects a directed cycle in the operating system file descriptor table it may break one process' hold through scheduling for example and let the system proceed.
Why this is a popular choice in this realm is that there is no way of knowing which resources a program will select without running the program.
This is an extension of Rice's theorem \cite{rice} that says that we cannot know any semantic feature without running the program (semantic meaning like what files it tries to open).
So theoretically, it is sound.
The problem then gets introduced that we could reach a livelock scenario if we preempt a set of resources again and again.
The way around this is mostly probabilistic.
The operating system chooses a random resource to break \keyword{hold-and-wait}.
Now even though a user can craft a program where breaking hold and wait on each resource will result in a livelock, this doesn't happen as often on machines that run programs in practice or the livelock that does happen happens for a couple of cycles.
These systems are good for products that need to maintain a non-deadlocked state but can tolerate a small chance of livelock for a short time.

In addition, we have the \textit{Banker's Algorithm}. Which the basic premise is the bank never runs dry, which prevents livelock. Feel free to check out the appendix for more details.

\section{Dining Philosophers}

The Dining Philosophers problem is a classic synchronization problem.
Imagine we invite $n$ (let's say 6) philosophers to a meal.
We will sit them at a table with 6 chopsticks, one between each philosopher.
A philosopher alternates between wanting to eat or think.
To eat the philosopher must pick up the two chopsticks either side of their position.
The original problem required each philosopher to have two forks, but one can eat with a single fork so we rule this out.
However, these chopsticks are shared with his neighbor.

\begin{figure}[H]
	\centering
	\includegraphics[width=.5\textwidth]{deadlock/drawings/dining.eps}
	\caption{Dining Philosophers}
\end{figure}

Is it possible to design an efficient solution such that all philosophers get to eat?
Or, will some philosophers starve, never obtaining a second chopstick?
Or will all of them deadlock?
For example, imagine each guest picks up the chopstick on their left and then waits for the chopstick on their right to be free.
Oops - our philosophers have deadlocked!
Each philosopher is essentially the same, meaning that each philosopher has the same instruction set based on the other philosopher i.e. you can't tell every even philosopher to do one thing and every odd philosopher to do another thing.

\subsection{Failed Solutions}

\begin{lstlisting}[language=C]
void* philosopher(void* forks){
  info phil_info = forks;
  pthread_mutex_t* left_fork = phil_info->left_fork;
  pthread_mutex_t* right_fork = phil_info->right_fork;
  while(phil_info->simulation){
    pthread_mutex_lock(left_fork);
    pthread_mutex_lock(right_fork);
    eat(left_fork, right_fork);
    pthread_mutex_unlock(left_fork);
    pthread_mutex_unlock(right_fork);
  }
}
\end{lstlisting}

This looks good but.
What if everyone picks up their left fork and is waiting on their right fork? We have deadlocked the program.
It is important to note that deadlock doesn't happen all the time and the probability that this solution deadlock goes down as the number of philosophers goes up.
What is important to note is that eventually that this solution will deadlock, letting threads starve which is bad.
Here is a simple resource allocation graph that shows how the system could be deadlocked

\begin{figure}[H]
	\centering
	\includegraphics[width=.9\textwidth]{deadlock/drawings/dining_naive.eps}
	\caption{Left right dining philosopher cycle}
\end{figure}

So now you are thinking about breaking one of the Coffman Conditions.
Let's break Hold and Wait!

\begin{lstlisting}[language=C]
void* philosopher(void* forks){
  info phil_info = forks;
  pthread_mutex_t* left_fork = phil_info->left_fork;
  pthread_mutex_t* right_fork = phil_info->right_fork;
  while(phil_info->simulation){
    int left_succeed = pthread_mutex_trylock(left_fork);
    if (!left_succeed) {
      sleep();
      continue;
    }
    int right_succeed = pthread_mutex_trylock(right_fork);
    if (!right_succeed) {
      pthread_mutex_unlock(left_fork);
      sleep();
      continue;
    }
    eat(left_fork, right_fork);
    pthread_mutex_unlock(left_fork);
    pthread_mutex_unlock(right_fork);
  }
}
\end{lstlisting}

Now our philosopher picks up the left fork and tries to grab the right.
If it's available, they eat.
If it's not available, they put the left fork down and try again.
No deadlock! But, there is a problem.
What if all the philosophers pick up their left at the same time, try to grab their right, put their left down, pick up their left, try to grab their right and so on.
Here is what a time evolution of the system would look like.

\begin{figure}[H]
	\centering
	\includegraphics[width=.9\textwidth]{deadlock/drawings/dining_livelock.eps}
	\caption{Livelock Failure}
\end{figure}

We have now livelocked our solution! Our poor philosophers are still starving, so let's give them some proper solutions.

\section{Viable Solutions}

The naive arbitrator solution has one arbitrator a mutex for example.
Have each of the philosophers ask the arbitrator for permission to eat or trylock an arbitrator mutex.
This solution allows one philosopher to eat at a time.
When they are done, another philosopher can ask for permission to eat.
This prevents deadlock because there is no circular wait! No philosopher has to wait for any other philosopher.
The advanced arbitrator solution is to implement a class that determines if the philosopher's forks are in the arbitrator's possession.
If they are, they give them to the philosopher, let him eat, and take the forks back.
This has the bonus of being able to have multiple philosophers eat at the same time.

There are a lot of problems with these solutions.
One is that they are slow and have a single point of failure.
Assuming that all the philosophers are good-willed, the arbitrator needs to be fair.
In practical systems, the arbitrator tends to give forks to the same processes because of scheduling or pseudo-randomness.
Another important thing to note is that this prevents deadlock for the entire system.
But in our model of dining philosophers, the philosopher has to release the lock themselves.
Then, you can consider the case of the malicious philosopher (let's say Descartes because of his Evil Demons) could hold on to the arbitrator forever.
He would make forward progress and the system would make forward progress but there is no way of ensuring that each process makes forward progress without assuming something about the processes or having true preemption -- meaning that a higher authority (let's say Steve Jobs) tells them to stop eating forcibly.

\begin{proof} The arbitrator solution doesn't deadlock

	The proof is about as simple as it gets. Only one philosopher can request resources at a time. There is no way to make a cycle in the resource allocation graph with only one philosopher acting in pickup the left then the right fork which is what we needed to show.

\end{proof}

\begin{figure}[H]
	\centering
	\includegraphics[width=.9\textwidth]{deadlock/drawings/dining_arbitrator.eps}
	\caption{Arbitrator Diagram}
\end{figure}

\subsection{Leaving the Table (Stallings' Solution)}

Why does the first solution deadlock?
Well, there are $n$ philosophers and $n$ chopsticks.
What if there is only 1 philosopher at the table?
Can we deadlock?
No.
How about 2 philosophers?
3?
You can see where this is going.
Stallings' \cite[P. 280]{stalling} solution removes philosophers from the table until deadlock is not possible -- think about what the magic number of philosophers at the table.
The way to do this in the actual system is through semaphores and letting a certain number of philosophers through.
This has the benefit that multiple philosophers can be eating.

In the case that the philosophers aren't evil, this solution requires a lot of time-consuming context switching.
There is also no reliable way to know the number of resources beforehand.
In the dining philosophers case, this is solved because everything is known but trying to specify an operating system where a system doesn't know which file is going to get opened by what process can lead to a faulty solution.
And again since semaphores are system constructs, they obey system timing clocks which means that the same processes tend to get added back into the queue again.
Now if a philosopher becomes evil, then the problem becomes that there is no preemption.
A philosopher can eat for as long as they want and the system will continue to function but that means the fairness of this solution can be low in the worst case.
This works best with timeouts or forced context switches to ensure bounded wait times.

\begin{proof} Stallings' Solution Doesn't Deadlock.
	Let's number the philosophers $\{p_0, p_1, .., p_{n-1}\}$ and the resources $\{r_0, r_1, .., r_{n-1}\}$.
	A philosopher $p_i$ needs resource $r_{i-1 \mod n}$ and $r_{i + 1 \mod n}$.
	Without loss of generality, let us take $p_i$ out of the picture.
	Each resource had exactly two philosophers that could use it.
	Now resources $r_{i-1 \mod n}$ and $r_{i + 1 \mod n}$ only have on philosopher waiting on it.
	Even if hold and wait, no preemption, and mutual exclusion or present, the resources can never enter a state where one philosopher requests them and they are held by another philosopher because only one philosopher can request them.
	Since there is no way to generate a cycle otherwise, circular wait cannot hold.
	Since circular wait cannot hold, deadlock cannot happen.
\end{proof}

Here is a visualization of the worst-case.
The system is about to deadlock, but the approach resolves it.

\begin{figure}[H]
	\centering
	\includegraphics[width=.9\textwidth]{deadlock/drawings/dining_stalling.eps}
	\caption{Stalling solution almost deadlock}
\end{figure}

\subsection{Partial Ordering (Dijkstra's Solution)}

This is Dijkstra's solution \cite[P. 20]{EWD:EWD310}. He was the one to propose this problem on an exam.
Why does the first solution deadlock? Dijkstra thought that the last philosopher who picks up his left fork (causing the solution to deadlock) should pick up his right.
He accomplishes it by number the forks $1..n$, and tells each of the philosophers to pick up his lower number fork.
Let's run through the deadlock condition again.
Everyone tries to pick up their lower number fork first.
Philosopher $1$ gets fork $1$, Philosopher $2$ gets fork $2$, and so on until we get to Philosopher $n$.
They have to choose between fork $1$ and $n$.
fork $1$ is already held up by philosopher $1$, so they can't pick up that fork, meaning he won't pick up fork $n$.
We have broken \keyword{circular wait}! Meaning deadlock isn't possible.

Some problems are that an entity either needs to know the finite set of resources in advance or be able to produce a consistent partial order such that circular wait cannot happen.
This also implies that there needs to be some entity, either the operating system or another process, deciding on the number and all of the philosophers need to agree on the number as new resources come in.
As we have also seen with previous solutions, this relies on context switching.
This prioritizes philosophers that have already eaten but can be made fairer by introducing random sleeps and waits.

\begin{proof} Dijkstra's Solution Doesn't Deadlock

	The proof is similar to the previous proof.
	Let's number the philosophers $\{p_0, p_1, .., p_{n-1}\}$ and the resources $\{r_0, r_1, .., r_{n-1}\}$.
	A philosopher $p_i$ needs resource $r_{i-1 \mod n}$ and $r_{i + 1 \mod n}$.
	Each philosopher will grab $r_{i-1 \mod n}$ then $r_{i + 1 \mod n}$ but the last philosopher will grab in the reverse order.
	Even if hold and wait, no preemption, and mutual exclusion or present.
	Since the last philosopher will grab $r_{n-1}$ then $r_0$ there are two cases either the philosopher has the first lock or the philosopher doesn't.

	If the last philosopher $p_{n-1}$ holds the first lock meaning the previous philosopher $p_{n-2}$ is waiting on $r_{n-1}$ meaning $r_{n-2}$ is available.
	Since no other blockers, the philosopher previous $p_{n-3}$ will grab her first lock.
	This is now a reduction to the previous proof of stalling because we now have $n$ resources but only $n-1$ philosophers, meaning this cannot deadlock.

	If the philosopher doesn't obtain that first lock, then we have a reduction to Stalling's proof above because now have $n-1$ philosophers vying for $n$ resources.
	Since we can't reach deadlock in either case, this solution cannot deadlock which is what we needed to show.

\end{proof}

\begin{figure}[H]
	\centering
	\includegraphics[width=.9\textwidth]{deadlock/drawings/dining_partial.eps}
	\caption{Stalling solution partial deadlock}
\end{figure}

There are a few other solutions (clean/dirty forks and the actor model) in the appendix.

\section{Topics}

\begin{itemize}
	\item Coffman Conditions
	\item Resource Allocation Graphs
	\item Dining Philosophers
	\item Failed DP Solutions
	\item Livelocking DP Solutions
	\item Working DP Solutions: Benefits/Drawbacks
	\item \href{http://adit.io/posts/2013-05-11-The-Dining-Philosophers-Problem-With-Ron-Swanson.html}{Ron Swanson Deadlock}
\end{itemize}

\section{Questions}

\begin{itemize}
	\item
	      What are the Coffman conditions?
	\item
	      What does each of the Coffman conditions mean? Define each one.
	\item
	      Give a real-life example of breaking each Coffman condition in turn. A situation to consider: Painters, Paint, Paint Brushes etc. How would you assure that work would get done?
	\item
	      Which Coffman condition is unsatisfied in the following snippet?

	      \begin{lstlisting}[language=C]
// Get both locks or none
pthread_mutex_lock(a);
if(pthread_mutex_trylock( b )) { /* failure */
  pthread_mutex_unlock( a );
}
	      \end{lstlisting}
	\item
	      The following calls are made

	      \begin{lstlisting}[language=c]
// Thread 1
pthread_mutex_lock(m1) // success
pthread_mutex_lock(m2) // blocks

// Thread 2
pthread_mutex_lock(m2) // success
pthread_mutex_lock(m1) // blocks
	      \end{lstlisting}

	      What happens and why? What happens if a third thread calls
	      \keyword{pthread\_mutex\_lock(m1)} ?
	\item
	      How many processes are blocked? As usual, assume that a process can complete if it can acquire all of the resources listed
	      below.

	      \begin{itemize}
	      	\tightlist
	      	\item
	      	      P1 acquires R1
	      	\item
	      	      P2 acquires R2
	      	\item
	      	      P1 acquires R3
	      	\item
	      	      P2 waits for R3
	      	\item
	      	      P3 acquires R5
	      	\item
	      	      P1 waits for R4
	      	\item
	      	      P3 waits for R1
	      	\item
	      	      P4 waits for R5
	      	\item
	      	      P5 waits for R1
	      \end{itemize}
\end{itemize}

Draw out the resource graph!

\bibliographystyle{plainnat}
\bibliography{deadlock/deadlock}


%https://tex.stackexchange.com/questions/114219/add-notes-to-latex-beamer
\documentclass[14pt, aspectratio=169]{beamer}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{xltxtra}
\usepackage{dsfont}
\usepackage{multicol}
% \usepackage{pgfpages}
% \setbeameroption{show notes}
%\setbeameroption{hide notes} % Only slides
%\setbeameroption{show only notes} % Only notes
% \setbeameroption{show notes on second screen=right} % Both
%% USAGE:
% \usepackage[logo="sklogo.png"]{beamerskoltech}
%   if you have a stand-alone image file for Sk logo
% or
% \usepackage[logo]{beamerskoltech}
%   if you has no logo-file, but want LaTeX to generate it.
%   In this case you probably will need to use `--enable-write18 -interaction=nonstopmode` arguments running the latex command.
%   in papeeria and overleaf all works fine
% or
% \usepackage{beamerskoltech}
%   In case you don't want logo at all
%
% provided commands:
% color `skoltechgreen` -- the dark-green color for structure elements
% command `\logoname` -- the name of logo file if exist
% command `{\csk <text>}` -- the shortcut from `\color{skoltechgreen}`
% command `skfootnote{text}` -- put some text for current slide
% `\renewcommand{\skbeforetitle}{\vspace{-3ex}}` is useful in case you use `aspectratio=169`
%%%%%%%%%%%%%%%%
\usepackage[logo]{beamerskoltech}
\usepackage{lecturessk}
\renewcommand{\skbeforetitle}{\vspace{-3ex}}

\usepackage{xecyr}
\usepackage{hyperref}

\usepackage{polyglossia}
\usepackage{graphicx}
\usepackage{listings}

\begin{document}

\title{\LaTeX:\\ \Large from dummy to \TeX nician}
\subtitle{The bases of TikZ}
\author{Anton Lioznov}
\institute{Skoltech}
\date{ISP 2019,\\ \textit{lesson 3}}
\frame{\titlepage}
\begin{frame}\frametitle{What we will know?}
\tableofcontents[hideallsubsections]
\end{frame}
\begin{frame}{Acknowledgments}\relax
We acknowledge
\begin{description}
    \item[Dmitry Barashev] for useful comments, that was included in the presentation
    \item[Alexander Kulikov] for useful comments, that was included in the presentation
\end{description}
\end{frame}

\begin{frame}{Agreements}{I}\relax
{ \Large Footnotes }
\smash{
\raisebox{-5.4cm}{
\begin{tikzpicture}
\draw[white] (0,0) -- (0, 0.0);
\draw[->,ultra thick] (0,0) to[out=0,in=45] (8, -1.5) -- (4,-5.5);
\end{tikzpicture}}
}

\skfootnote{Like this}
\begin{itemize}
    \item Only in the ``out-class'' version
     \item For second reading
     \item Containe advanced usage of the command
     \item Containe references to read more
     \begin{itemize}
         \item to the exact chapter
         \item (often) with the href to exact page
     \end{itemize}
     \item Containe some comments
\end{itemize}
\end{frame}

% \renewcommand{\skfootnote}[1]{}

\begin{frame}{Agreements}{II}\relax
{ \Large Addition information -- ``magic'' }
\smash{\begin{tikzpicture}
\draw[white] (0,0) -- (0, 0.6);
\draw[->,ultra thick] (0,0.3) --(3,1.8);
\end{tikzpicture}}
\magicPage

\begin{itemize}
     \item To have the full picture
     \item Not to analyze or to puzzle out in class
\end{itemize}
\end{frame}

\inclassFrame{
\begin{frame}{Agreements}{III}\relax
\centering
     {\LARGE This slide means it is ``in-class task time''\par
     \inclassFrag{This box means it is ``in-class task time''}}
     These slides and boxes will be removed in the ``out-class'' version. However, they still be in the source code.
\end{frame}
}

\AtBeginSection[]
{
  \begin{frame}{What we will know?}
  \tableofcontents[currentsection,hideallsubsections]
  \end{frame}
}

\AtBeginSubsection[]
{
  \begin{frame}{What we will know?}
  \tableofcontents[currentsubsection, hideothersubsections, sectionstyle=show/hide, subsectionstyle=show/shaded/hide]

  \end{frame}
}

\begin{frame}\frametitle{What we have learned today?}
\tableofcontents
\end{frame}

\section{Introduction: on approaches to TikZ}
\input{sec01/sec01}

% general -> path - draw, fill, circle, rectangle, coordintates. Nodes.
% graphs
\section{``General usage''}
\input{sec02/sec02}

\section{``Graphs''}
\input{sec03/sec03}

\section{``Arrangment''}
\input{sec04/sec04}

\begin{frame}[allowframebreaks]{references}
color from the footnotes corresponds to references' color.
    \begin{itemize}
        % \item \knuthc{Knuth ``The \TeX Book''}
        % \item \lvoc{L'vovsky ``Nabor i verstka v sisteme \LaTeX''}
        % \item \lamc{Lamport. ``\LaTeX. A Document Preparation System, User’s Guide and Reference Manual''}
        % \item \lmanc{``\LaTeX 2e: An unofficial reference manual''} also at website \url{https://latexref.xyz/}
        \item \stExC{https://tex.stackexchange.com/questions} : \url{https://tex.stackexchange.com/questions}
        \item \wikiC{https://en.wikibooks.org/wiki/LaTeX} : \url{https://en.wikibooks.org/wiki/LaTeX}
        \item \overC{https://www.overleaf.com/learn/latex} : \url{https://www.overleaf.com/learn/latex}
        % \item \tugC{https://www.tug.org/utilities/plain/cseq.html} : \url{https://www.tug.org/utilities/plain/cseq.html}
        \item \tikzc{TikZ manual} (1161 pages :))
    \end{itemize}
\end{frame}

\begin{frame}{Distribution}\relax
\begin{itemize}
     \item the pdf-version of the presentation and all printed materials can be distributed under license Creative Commons Attribution-ShareAlike 4.0 \url{https://creativecommons.org/licenses/by-sa/4.0/}
     \item The source code of the presentation is available on {\csk\url{https://github.com/Lavton/latexLectures}} and can be distributed under the MIT license \url{https://en.wikipedia.org/wiki/MIT_License\#License_terms}
\end{itemize}

\end{frame}
\end{document}


\documentclass{article} % For LaTeX2e
% We will use NIPS submission format
\usepackage{nips13submit_e,times}
% for hyperlinks
\usepackage{hyperref}
\usepackage{url}
% For figures
\usepackage{graphicx}
\usepackage{subfigure}
% math packages
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsopn}
\usepackage{ifthen}
\usepackage{natbib}

\title{Project-I by Group OSLO}

\author{
Etienne Pot\\
EPFL \\
\texttt{etienne.pot@epfl.ch} \\ \And
Lucile Madoulaud\\
EPFL \\
\texttt{lucile.madoulaud@epfl.ch} \\
}

% 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.

\nipsfinalcopy

\begin{document}

\maketitle

\begin{abstract}
This report present our method and prediction results of for the regression and classification tasks. For the regression task, we have decided to train three different models and a model selector, we obtain respectable performance except for some outliers which have too much impact on our RMSE. We discuss about some improvement possible to solve this problem.\newline
For the classification task, we have decided to train two different models this time, but here the results are less good, we discuss about some possibilities to improve them.
\end{abstract}

\section{Data Description}
Our training data $\mathbf{X}$ which consists in $N=2800$ data of 65 variables for the regression and $N=1500$ data of 29 variables for the classification task. Our testing set is composed of $1200$ and $1500$ sample for the regression and classification.
\newline
Our goal is to produce predictions for the test sample, as well as an approximation of the test-error.

\section{Data visualization and cleaning}
First of all, we have done some exploration of our data before we start to apply some machine learning algorithms.

\subsection{Regression}

On the regression task, by plotting the histogram of the output (Figure \ref{fig:histY}), we can clearly see three distinct clusters around 2000, 6000 and 12000.

We have also plotted each variable in function of the output to see if there was some variable directly correlated to our output. We have see for instance that the variables 16 and 38 could help us to discriminate between the three clusters (Figure \ref{fig:clusters}) and that for each cluster, there was a variable which was strongly linearly correlated with the output (12, 26 and 61), those could eventually be used to have a first really simple model with only one dimension.

\begin{figure}[!h]
\center
\subfigure[Histogram of $\mathbf{y}$. We clearly see three clusters.]{\includegraphics[width=2.5in]{figures/histY.png} \label{fig:histY}}
\hfill
\subfigure[Output in function of two interesting variables. We have separated our output in three different clusters]{\includegraphics[width=2.5in]{figures/clusters.png} \label{fig:clusters}}
\caption{}
\end{figure}

\subsection{Classification}

Similarly, for the classification task, by plotting the histogram of the $\mathbf{X}$ (for instance column 10), we see that our input seems to be the sum of two Gaussian distributions. We have also used those histogram to isolate and remove some outliers which were really far from the Gaussian distributions.

We have also notice that when we separate the two Gaussian distributions, some variables where strongly correlated with the output class, we have also visualized the combination of two variables using gplotmatrix to see if we could see some pattern between the variables, we didn't.

\begin{figure}[!h]
\center
{\includegraphics{figures/histX10.png} \label{fig:histX10}}
\caption{Histogram of variable 10 of $\mathbf{X}$. We see the two gaussian and some outliers.}
\end{figure}

We also remark that our training set possess more sample of class +1 than class -1 (65\% vs 35\%).

\section{Regression models}
At first, we have try to train all our data with a single model. It was not going well (huge RMSE) so we have decided to separate our data into three different model for the regression task corresponding to the three clusters we have seen in our training data. In order to do that, we will also need to another model which will choose which one of the three model apply to the testing data.

To avoid bias, we have separated our training set in two parts (90\%-10\%): the first one to train (and test using cross validation) the three (regression) / two (classification) models, and the other one to test the model selection and estimate the global error.

Our input matrix being ill-conditioned, we have decided to focus on using ridge regression which will lift the eigenvalue to avoid the problem. And selecting the best value of lambda for each of our model (with cross validation, we found $\lambda_{model1} = 0.0335$, $\lambda_{model1} = 0.1016$ and $\lambda_{model1} = 0.0335;$).

We have separated the first part of our training values (the 90\%) into three groups corresponding to the output clusters. We have then trained and tested each model independently.

After feature transformation, we have obtain the following results presented in Figure \ref{fig:learningCurves}.

\begin{figure}[!h]
\center
{\includegraphics{figures/learningCurves.png} \label{fig:learningCurves}}
\caption{Histogram of variable 10 of $\mathbf{X}$. Learning curves for each of our models}
\end{figure}

Here are our three model results on the testing set for a 15-cross validation:
\begin{enumerate}
        \item Model 1:  rmse = 19.6      ; std = 1.8
        \item Model 2:  rmse = 19.0      ; std = 3.1
        \item Model 3:  rmse = 27.4      ; std = 5.8
\end{enumerate}

Each cluster size being large from about 4000, having a rmse of about 15-30, is not perfect but is still ok to have a good estimation of y. Our model 3 has the worst results, and a higher variance, our model 1 provide the best results and has the smaller variance. Luckily, there is much more points in our model 1 which perform the best and few in the one which perform the worst.

\section{Classification models}

Similarly, for the classification task, we have separated our training data into two parts corresponding to the two Gaussian distributions. We have done that by using the column 12 where the Gaussian distributions were already separated, and we have then checked that the data where also separated for the other columns.

For our classification models, after having try without success to solve our ill-conditioning input matrix by reducing the dimension we conclude we had to use the penalized logistic regression. In order to have the better results possible, we needed a small alpha with a great number of iteration, which made the convergence really slow (in addition we also needed to compute the best lambda using cross-validation, which made the program even slower).

To improve the convergence speed, as well as the global performances, we also have try to apply Iterative Recursive Least-Squares, but the ill-conditioning made our beta diverge to infinity. After few try, we have stopped using this method and went back to penalized logistic regression. We have used cross validation to compute the optimal parameters for lambda, it gives us: $\lambda=1.92$ for model 1 and $\lambda = 2.45$ for our model 2.

The results predicted with our model were not good (30-45\% of mistake on the global error). A lack of times prevent us to do some diagnostic to identify were was the problem with our results. Our model had a bias toward the -1 class. Even if this class was minority into our training set, our model predict more -1 than +1.

\section{Feature transformation}

We have tested different features transformations to see if those could improve our results.

First for the categorical data, on both regression and classification model, we have try dummy encoding, keeping only the binary data or removing all. Each test has few impact on our results (even slightly worse in case of dummy encoding) so we have decided to remove the categorical data.

We also have try data reduction by removing the features which had the strongest negative impact on our RMSE. In order to do that, we have created a script which remove one of the input column and record the result, we have repeated the process 200 times for each column to see if there was significant differences. The results were not concluding so we have stopped the experimentation here.

Finally, we have tested simple and usual global features transformations apply to all input column. Here are the transformation we have tested: , $x^2$, $x^3$, $x^4$, $exp(x)$, $1/x$, $sqrt(x)$, $log(x)$, $abs(x)$.

For the regression task, we remarked that $x^2$ and in a higher degree $x^3$ had a big impact on the results (RMSE divided by a factor 2-4 for each of our model).

We did not have the time to test all those transformation on the classification task.

\section{Model selection}

\subsection{Regression}

At first, to decide which model to apply to our points, we have apply a naive threshold separation method (on the columns 16 and 38). It was not going very well because the “borderlines” point where easily confused with the wrong model.

This part is critical because even if few points are incorrectly predicted with the wrong model, the error is to huge and it has a really big impact on the rmse.

In order to improve the result of the model selection, we have try KNN. It was simple enough to implement and has greatly improved our results. From our different tests, 3-NN seems the one which produced the better results (2-3\% of wrong classification).

When we evaluate the global error (including the model selection), we remark that most points are correctly predicted except for those few points which are incorrectly classified (fig.\ref{fig:predErrors}), because we apply the wrong model to predict them. One way to solve this issue could have been to change the cost function (by using for instance the tukey biweight loss). The errors would still be there but will be less penalized. Of course it’s depend of our application and if it’s okay to have those kind of mistake.

\begin{figure}[!h]
\center
{\includegraphics{figures/predErrors.png} \label{fig:predErrors}}
\caption{For our regression model: all our predicted points have a low error value except for the four points which have been wrongly classified}
\end{figure}

With this model, we can also estimate the confidence of the prediction for each point. Indeed, for most of the points, we are confident to use the right model (all neighbours belongs to the same class), and if we remove the few points for which we are not sure (have neighbour in two different classes), it drastically improve our results (RMSE goes from 600-800 to 21 by removing the 6\% of our points for which we are not sure which model to apply).

In order to correct those outliers, we have tried to apply a basic outlier detection by looking at the distance of each points to the mean of his cluster and see if some points where outside this Gaussian distribution. Unfortunately, even outliers point where not distant enough of the mean to allowing us to detect them. We did not have enough time to try more complex method, though it would have been necessary.

\subsection{Classification}

Separate the models on the testing set was easier on the classification task because it was not dependant of the output, so we separate the testing set the same way that the training set, by putting a threshold on the variable 12 of our input vector. We have checked that we obtain the same distribution for the testing set that for our training set.

\section{Final prediction}

\subsection{Regression}

For the final prediction, we have retrain our data set on all the given data. Even if we could not check if our prediction are correct or not, we can compare the histogram of our predicted values (fig. \ref{fig:histPredY}) with the histogram of the output value of our training set (fig.\ref{fig:histY}). Those two were similar. We also have checked that the proportion of data in each cluster was similar to the one in our training set (70\% for cluster 1, 20\% for cluster 2 and 10\% for cluster 3).

\begin{figure}[!h]
\center
{\includegraphics{figures/histPredY.png} \label{fig:histPredY}}
\caption{The distribution of our predicted Y on the testing set is really similar to the one given on the training set (fig \ref{fig:histY})}
\end{figure}

With those informations, we can suppose that the testing predictions will be quite similar to the one given with our training set. Unfortunately, that also means that some point will be incorrectly classified (about 36 of 1200 points, we can even know which one are potentially wrong), we also know that it will have a strong negative impact on our final RMSE. After running several times our algorithm, we predict the final error: 630 +/- 200 . The high variance of the prediction is due to the fact that our model is really sensitive to outliers so just one more or one less outlier can have a big impact on our rmse.

\begin{figure}[!h]
\center
{\includegraphics{figures/histPred.png} \label{fig:histPred}}
\caption{After running 200 times our algorithm, we can have a pretty good estimation of our rmse. (fig \ref{fig:histPred})}
\end{figure}

\subsection{Classification}

Even after having separated the data into two model and apply the predictions on each model individually, the results seems still pretty random. By applying the same method that above on the optimal computed lambda for each of the models, we predict an error of 40\% +/- 10\%.

\section{Conclusion}
\subsection{Regression}
We could have spend more time focusing on improving each one of the three model performances (for selecting the best parameters) but we have decided instead to focus more on the model selection part, indeed, the three models performances was already quite good and the main problem with our final results is that the few outliers points ruin all our performances.

\subsection{Classification}
An unidentified problem make the results of our classification not good. In addition, the penalised logistic regression was really slow, which create a big delay for each test before we could have a feedback. For this reason among other, we have tested much less configurations for the classification that for the regression. And in conclusion, we did not find any satisfying model for the classification task.

\subsubsection*{Acknowledgments}
All the code and the report was written by us, except for the small parts provided during the lab sessions (for instance, some part of the code for the IRLS came from the lecture slide on logistic regression (p8)).

\end{document}


\subsection{Interpreting Copilot}
To use Copilot's interpreter, one must first invoke the GHC Interpreter via
the Cabal sandbox. Invoking the interpreter with the \texttt{cabal
repl} command starts a session that looks like:

%
\begin{lstlisting}[language = Copilot]
cabal repl
Preprocessing library copilot-2.1.2...
GHCi, version 7.10.1: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Language.Copilot ( src/Language/Copilot.hs, interpreted )
Ok, modules loaded: Language.Copilot.
*Language.Copilot>
\end{lstlisting}
%

A copilot file \texttt{file} is loaded as follows:
%
\begin{lstlisting}[language = Copilot]
Language.Copilot> :l file
\end{lstlisting}

Notice that \texttt{file} has to be located
in the directory \emph{Copilot}.

The copilot interpreter is invoked as follows:
%
\begin{lstlisting}[language = Copilot]
Language.Copilot> interpret 10 propTempRiseShutOff
\end{lstlisting}
%
The first argument to the function \emph{interpret} is the number of iterations
that we want to evaluate. The third argument is the specification (of type {\tt
Spec}) that we wish to interpret.

The interpreter outputs the values of the arguments passed to the trigger, if
its guard is true, and {\tt --} otherwise. For example, consider the following
Copilot program:
%
\begin{lstlisting}[language = Copilot]
spec = do
  trigger "trigger1" (even nats) [arg nats, arg $ odd nats]
  trigger "trigger2" (odd nats) [arg nats]
\end{lstlisting}
% $
where {\tt nats} is the stream of natural numbers, and {\tt even} and {\tt odd}
are functions that take a stream and return whether the point-wise values are
even or odd, respectively. The output of
%
\begin{lstlisting}[language = Copilot]
interpret 10 spec
\end{lstlisting}
%
is as follows:
%
\begin{code}
trigger:   trigger2:
(0,false)  --
--         (1)
(2,false)  --
--         (3)
(4,false)  --
--         (5)
(6,false)  --
--         (7)
(8,false)  --
--         (9)
\end{code}
%

Sometimes it is convenient to observe the behavior of a stream without defining
a trigger. We can do so declaring an \emph{observer}. For example:
%
\begin{lstlisting}[language = Copilot]
spec :: Spec
spec = observer "obs" nats
\end{lstlisting}
%
can be interpreted using
%
\begin{lstlisting}[language = Copilot]
interpret 5 spec
\end{lstlisting}
%
as usual. Observers can be combined in larger Copilot programs. For example,
consider the following:
%
\begin{lstlisting}[language = Copilot]
spec :: Spec
spec = do
  let x = externW8 "x" (Just [0..])
  trigger "trigger" true [arg $ x < 3]
  observer "debug_x" x
\end{lstlisting}
% $
Interpreting {\tt spec} as follows
%
\begin{lstlisting}[language = Copilot]
interpret 10 spec
\end{lstlisting}
%
yields
%
\begin{code}
trigger:  debug_x:
(true)    0
(true)    1
(true)    2
(false)   3
(false)   4
(false)   5
(false)   6
(false)   7
(false)   8
(false)   9
\end{code}

\subsection{Compiling Copilot} \label{sec:compiling}

Compiling the engine controller from Example~\ref{exm:engine} is
straightforward. First, we pick a back-end to compile to. Currently, two
back-ends are implemented, both of which generate constant-time and
constant-space C code. One back-end is called \emph{copilot-c99} and targets
the Atom language,\footnote{\url{http://hackage.haskell.org/package/atom}}
originally developed by Tom Hawkins at Eaton Corp.\ for developing control
systems (in the process of depreciation). The second back-end is called \emph{copilot-sbv} and targets the SBV
language,\footnote{\url{http://hackage.haskell.org/package/sbv}} originally
developed by Levent Erk\"{o}k. SBV is primarily used as an interface to SMT
solvers~\cite{smt} and also contains a C-code generator. Both languages are
open-source.

The two back-ends are installed with Copilot, and they can be imported,
respectively, as

\begin{lstlisting}[language = Copilot]
import Copilot.Compile.C99
\end{lstlisting}
\noindent
and
\begin{lstlisting}[language = Copilot]
import Copilot.Compile.SBV
\end{lstlisting}

After importing a back-end, the interface for compiling is as
follows:
%
\footnote{Two explanations are in order: (1) {\tt reify} allows sharing in the
expressions to be compiled~\cite{DSLExtract}, and {\tt >>=} is a higher-order
operator that takes the result of reification and ``feeds'' it to the compile
function.}
%
\begin{lstlisting}[language = Copilot]
reify spec >>= compile defaultParams
\end{lstlisting}
%
\noindent
(The compile function takes a parameter to rename the generated C files; {\tt
defaultParams} is the default, in which there is no renaming.)

The compiler now generates several files in a separate-subfolder, some of which are called:

\begin{itemize}
\item ``driver.c'' ---
\item ``copilot.h'' ---
\end{itemize}

The file named ``copilot.h'' contains prototypes for all external variables,
functions, and arrays, and contains a prototype for the ``step''-function,
which evaluates a single iteration.

\begin{code}
/* Generated by Copilot Core v. 0.1 */

#include <stdint.h>
#include <stdbool.h>

/* Triggers (must be defined by user): */

void over_temp_rise();

/* External variables (must be defined by user): */

extern float temp;
extern bool running;

/* Step function: */

void step();
\end{code}

Using the prototypes in ``copilot.h'' we can build a controller as follows:

\begin{code}
/* main.c */
#include <stdio.h>
#include "copilot.h"

bool running = true;
float temp = 1.1;

void over_temp_rise()
{
  printf("The trigger has been evoked!\n");
}

int main (int argc, char const *argv[])
{
  int i;

  for (i = 0; i < 10; i++)
  {
    printf("iteration: %d\n", i);
    temp = temp * 1.3;
    step();
  }

  return 0;
}
\end{code}

Running {\tt gcc *.c -o prop} gives a program {\tt prop}, which,
when executed, yields the following output:
%
\begin{code}
iteration: 0
iteration: 1
iteration: 2
iteration: 3
iteration: 4
iteration: 5
iteration: 6
iteration: 7
The trigger has been evoked!
iteration: 8
The trigger has been evoked!
iteration: 9
The trigger has been evoked!
\end{code}
%

\subsection{Correctness of the generated code}~\label{sec:correctness}

\subsubsection{Frama-c}~\label{subsec:frama-c}
For this, make sure the following is installed:

\begin{itemize}
	\item GNU parallel
	\item frama-c (version at least sodium)
	\item why3
	\item cvc4 prover
\end{itemize}

It is possible to generate C code optimized for frama-c by using the \texttt{proofACSL}
function from SBV.Compile (instead of \texttt{compile} as shown
in~\ref{sec:compiling}). It will then generate the C source code (which should be in
the \texttt{copilot-sbv-codegen} folder). Go into that folder and just run
\texttt{make fwp} to use frama-c with the WP plugin. This should generate two
log files: a first exhaustive one called \texttt{logfwp}, and another which
summaries the former which is called \texttt{logfwpcompact}. The command make
fwp is detailed in Figure~\ref{fig:fwp}.

\begin{figure*}[!htb]
	\begin{lstlisting}[frame=none, language=bash]
parallel frama-c -wp -wp-out . -wp-timeout 20 -wp-prover CVC4 -wp-split {} ::: *.c | tee >logfwp >(grep 'Proved\|Unknown\|Timeout\|Failed\|Qed:\s\|CVC4:\s\|Parsing .*\.c' > logfwpcompact) >(grep 'Proved\|Qed:\s\|CVC4:\s\|Unknown\|Timeout\|Failed\|Parsing .*\.c')

	\end{lstlisting}
	\caption{The bash command.}
	\label{fig:fwp}
\end{figure*}

It is also possible to run frama-c with the value analysis plugin by running
\texttt{make fval}. Be careful, the value analysis plugin requires to preload
all the C sources files together, which requires a lot of RAM memory for big
projects.

It is recommended to refactor your code before trying to use frama-c on it, by for example splitting non-trivial expressions using magic labels, deleting all
local variables (use functions) and bitwise operators. If this step is
neglected, it may happen that frama-c would start swapping all your memory
resulting in a system crash.

\subsubsection{Splint}~\label{subsec:splint}

If you have splint installed on your computer, you can also try running splint on the C source code generated (with the command \texttt{make splint}), which will be a good starting point for manual code optimization or refactoring. The same problem as for frama-c may occur if you are working on a very big project (system crash).

\textbf{This tools does not guarantee that the generated C source code is compliant to ISO 9899:1999.}

\subsubsection{Dot}~\label{subsec:dot}

It is also possible to generate dot graphs for each C source file generated, in
order to make the files more understandable. For this, you should check that
you have dot installed. Extract the dot code from a C source file, and then
just run \texttt{dot -Tps code.gv -o code.ps}
%
\footnote{A script doing that automatically is provided here
\url{https://raw.githubusercontent.com/Copilot-Language/examplesForACSL/master/WCV/scriptdot.cpp}}.

\subsection{Cross compiling}~\label{sec:cross}

If you go in the \texttt{copilot-sbv-codegen} folder, and run {\tt make all}
inside it, it should generate an archive file named \texttt{internal.a}, using
the compiler defined in the file \texttt{copilot.mk} (default is compcert, you
have to install it with its standard library). This can create some problems
during the linking process if you change the compiler for the final compilation
(typically \texttt{gcc -m32 -lm main.c internal.a} where \texttt{main.c} is
where your controller code is). For this purpose, we recommend installing first
the cross compiler (example \texttt{arm-none-eabi-gcc}), and compile the
\emph{whole} project using that very same compiler using the command \texttt{arm-none-eabi-gcc -m32 -lm [options] *.c}.


%------------------------------------------------------------------------------
% Author(s):
% Arjun Mohammed, Brad Bachu
%
% Copyright:
%  Copyright (C) 2020 Brad Bachu, Arjun Mohammed, Nicholas Sammy, Kerry Singh
%
%  This file is part of Applied-Mathematics-Unit2 and is distributed under the
%  terms of the MIT License. See the LICENSE file for details.
%
%  Description:
%     Module 1 notes.
%------------------------------------------------------------------------------

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Module 1 Notes and Definitions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Linear Programming} \label{Module1:LinearProgramming}

\begin{defn}\label{mod1:defn:TourOfVertices}
	\textbf{Tour of Vertices}: In a 2-dimensional graph, the set of vertices of the feasible region must contain the optimal solution. The Tour of Vertices method checks each vertex of the feasible region to find the optimum solution for the problem.
\end{defn}

\section{Assignment Models} \label{Module1:AsgnmtModels}

\begin{defn} \label{mod1:defn:HungAlgStep4}
	\textbf{Step 4}: This is the step of the Hungarian algorithm that subtracts the matrix minimum from all unshaded elements and adds double the matrix minimum to all elements that are shaded twice. Using this as the reference until the structured Hungarian notes are placed.
\end{defn}

\section{Graph Theory and Critical Analysis} \label{Module1:GraphTheoryCriticalAnalysis}

\textcolor{red} {DIAGRAMS FOR GRAPH DEFINITIONS  AND CRITICAL ANALYSIS ALGORITHMS}\\
\textcolor{red}{HUNGARIAN ALGO}
\begin{defn}\label{mod1:defn:Vertex}
   \textbf{Vertex}: The Vertex is the fundamental unit from which graphs are formed.
\end{defn}

\begin{defn}\label{mod1:defn:Edge}
   \textbf{Edge}: In an undirected graph, an unordered pair of nodes that signify  a line joining  two nodes are said to form an edge. For a directed graph, the edge is an ordered pair of nodes. An edge may also connect a node to itself.
\end{defn}

\begin{defn}\label{mod1:defn:Path}
    \textbf{Path}: A path in a graph is a sequence of edges which joins multiple vertices, in which all vertices (and edges) are distinct.
\end{defn}

\begin{defn}\label{mod1:defn:Degree}
   \textbf{Degree}: The degree of a vertex  in a graph is the number of edges that are incident to the vertex. A loop is counted as two incident edges.
\end{defn}

\begin{defn}\label{mod1:defn:FloatTime}
	\textbf{Float Time}: The float time of an activity in an activity network is the time difference between the latest start time of the activity and the earliest start time of the activity.
\end{defn}

\begin{defn}\label{mod1:defn:CritPath}
	\textbf{Critical Path}: A crtical path is a path which joins activities in an Activity Network whose Float Time is $0$. It is the path which determines the minimum time for some set of operations to be completed.
\end{defn}

\section{Logic and Boolean Algebra}

\begin{defn}\label{mod1:defn:Proposition}
   \textbf{Propsition}: A proposition is a declarative statment which is either true or false.
\end{defn}

\begin{defn}\label{mod1:defn:LogicSymbols}
   Propositional Logical Symbols

   \begin{table}[ht]
      \centering
      \begin{tabular}{|c|c|c|}
         \hline
            Symbol & Name & Read as\\
         \hline
         $\land$ & Conjunction & And \\
         $\lor$ &   Disjunction & Or \\
         $\lnot$ & Negation & Not\\
         $\Rightarrow$ & Conditional & If ... then ...\\
         $\Leftrightarrow$ & Bi-conditional & If and only if; iff\\
         \hline
      \end{tabular}
      \label{mod1:tab:LogicSymbols}
   \end{table}

\end{defn}

\begin{defn}\lbl{mod1:defn:TruthTables}
\begin{table}[!htb]
   \centering
   \begin{minipage}{0.2\textwidth}
   \centering
      \begin{tabular}{|c |c|}
         \hline
         p &  $\lnot$p\\
         \hline
         T & F\\
         F & T\\
         \hline
      \end{tabular}
      \caption{\label{mod1:tab:Negation}Negation}
   \end{minipage}
   \begin{minipage}{0.38\textwidth}
      \centering
      \begin{tabular}{|c c|c|}
            \hline
            p & q & p $\land$ q\\
            \hline
            T & T & T\\
            T & F & F\\
            F & T & F\\
            F & F & F\\
            \hline
      \end{tabular}
      \caption{\label{mod1:tab:Conjunction}Conjunction}
   \end{minipage}
   \vspace{1cm}
   \begin{minipage}{0.38\textwidth}
   \centering
            \begin{tabular}{|c c|c|}
         \hline
         p & q & p $\lor$ q\\
         \hline
         T & T & T\\
         T & F & T\\
         F & T & T\\
         F & F & F\\
         \hline
      \end{tabular}
      \caption{\label{mod1:tab:Disjunction}Disjunction}
   \end{minipage}
   \hspace{6cm}
   \begin{minipage}{0.38\textwidth}
      \centering
      \begin{tabular}{|c c|c|}
         \hline
         p & q & p $\Rightarrow$ q\\
         \hline
         T & T & T\\
         T & F & F\\
         F & T & T\\
         F & F & T\\
         \hline
      \end{tabular}
      \caption{\label{mod1:tab:Conditional}Conditional}
   \end{minipage}
   % \hspace{1cm}
   \begin{minipage}{0.38\textwidth}
      \centering
      \begin{tabular}{|c c|c|}
         \hline
         p & q & p $\Leftrightarrow$ q\\
         \hline
         T & T & T\\
         T & F & F\\
         F & T & F\\
         F & F & T\\
         \hline
      \end{tabular}
      \caption{\label{mod1:tab:Bi-conditional}Bi-conditional}
   \end{minipage}
\end{table}
\end{defn}

\noindent Suppose we have the proposition $p \Rightarrow q$, we make the following three definitions:
\begin{defn}\label{mod1:defn:Inverse}
   \textbf{Inverse}: $\lnot p \Rightarrow \lnot q$
\end{defn}

\begin{defn}\label{mod1:defn:Converse}
   \textbf{Converse}: $q \Rightarrow p$
\end{defn}

\begin{defn}\label{mod1:defn:Contrapositive}
   \textbf{Contrapositive}: $\lnot q \Rightarrow \lnot p$
\end{defn}

\begin{defn}\label{mod1:defn:Tautology}
   \textbf{Tautology}: A tautology is a proposition which is true in every possible interpretation.
\end{defn}

\begin{defn}\label{mod1:defn:Contradiction}
   \textbf{Contradiction}: A contradiction is a proposition which is false in every possible interpretation.
\end{defn}

\subsection{Laws of Boolean Algebra}\label{mod1:section:BooleanAlgebraLaws}

\noindent Consider the propsitional variables $p$, $q$ and $r$

\begin{law}\label{mod1:law:Annulment}
   \textbf{Annulment}
   \begin{align}
   p \land F &= F  \\
   p \lor T &= T
   \end{align}
\end{law}

\begin{law}\label{mod1:law:Identity}
   \textbf{Identity}
   \begin{align}
   p \land T &= p  \\
   p \lor F &= p
   \end{align}
\end{law}

\begin{law}\label{mod1:law:Idempotent}
   \textbf{Idempotent}
   \begin{align}
   p \lor p &= p\\
   p \land p &= p
   \end{align}
\end{law}

\begin{law}\label{mod1:law:Complement}
   \textbf{Complement}
   \begin{align}
   p \lor \lnot p &= T\\
   p \land \lnot p &= F
   \end{align}
\end{law}

\begin{law}\label{mod1:law:DoubleNegation}
   \textbf{Double Negation}
   \begin{align}
   \lnot(\lnot p) &= p
   \end{align}
\end{law}

\begin{law}\label{mod1:law:DeMorgan}
   \textbf{De Morgan's}
   \begin{align}
   \lnot (p \land q) &= \lnot p \lor \lnot q \\
   \lnot (p \lor q) &= \lnot p \land \lnot q
   \end{align}
\end{law}

\begin{law}\label{mod1:law:Associative}
   \textbf{Associative}
   \begin{align}
   (p \lor q) \lor r &= p \lor (q \lor r) \\
   (p \land q) \land r &= p \land (q \land r)
   \end{align}
\end{law}

 \begin{law}\label{mod1:law:Commutative}
   \textbf{Commutative}
   \begin{align}
   p \land q &= q \land p \\
   p \lor q &= q \lor p
   \end{align}
\end{law}

\begin{law}\label{mod1:law:Distributive}
   \textbf{Distributive}
   \begin{align}
   p \land (q \lor r) &= (p \land q) \lor (p \land r) \\
   p \lor (q \land r) &= (p \lor q) \land (p \lor r)
   \end{align}
\end{law}

\begin{law}\label{mod1:law:Absorptive}
   \textbf{Absorptive}
   \begin{align}
   p \land (p \lor q) &= p\\
   p \lor (p \land q) &= p
   \end{align}
\end{law}

\subsection{Logic Circuits}\label{mod1:section:LogicCircuits}

\textcolor{red}{DIAGRAMS OF LOGIC CIRCUITS}


\subsection{Single accelerating object in space}
 Accelerating objects like pulsars can create gravitational wave \cite{Creighton:2011zz}. According to general relativity, mass creates stress in space-time and thus can change the geometry of it by bending and changing the curvature. Then if this object moves, then the curvature also moves along with it. But if the object accelerates in space-time in a circular manner, then the ripples will be created in space-time which is which radiates gravitational waves. This is similar to creation of water waves when we move our finger in a circular fashion in water. So higher the mass of object and it's acceleration, stronger is the gravitational wave it produces \cite{Linear}.

 Such continuously spinning bodies produces continuous gravitational waves, where it's nature is sinusoidal for a longer period of time. This happens only if spin rate of this object is constant. Such gravitational waves have same frequency and amplitude. Such types of gravitational waves are not yet discovered.\\

\begin{figure}[h]
    \centering
    \includegraphics[scale = 0.35]{images.tex/continuous_gw.jpg}
    \caption{Computer simulation of Continuous gravitational wave by a pulsar. \\
    \textbf{Source :-} \url{https://earthsky.org/space/}}
\end{figure}

\begin{figure}[h]
    \centering
    \includegraphics[scale = 0.38]{images.tex/pulsar.jpg}
    \caption{A rotating pulsar.   \textbf{Source}:- \url{https://astronomy.com/news/2018/02/}}
\end{figure}

\pagebreak

gw-physics-and-ligo/4 Source.tex/4.1 Single accelerating object.tex


\documentclass{beamer}
%\usetheme{AnnArbor}
%\usetheme{Antibes}
%\usetheme{Bergen}
%\usetheme{Berkeley}
%\usetheme{Berlin}
%\usetheme{Boadilla}
%\usetheme{boxes}
%\usetheme{CambridgeUS}
\usetheme{Copenhagen}
%\usetheme{Darmstadt}
%\usetheme{default}
%\usetheme{Frankfurt}
%\usetheme{Goettingen}
%\usetheme{Hannover}
%\usetheme{Ilmenau}
%\usetheme{JuanLesPins}
%\usetheme{Luebeck}
%\usetheme{Madrid}
%\usetheme{Malmoe}
%\usetheme{Marburg}
%\usetheme{Montpellier}
%\usetheme{PaloAlto}
%\usetheme{Pittsburgh}
%\usetheme{Rochester}
%\usetheme{Singapore}
%\usetheme{Szeged}
%\usetheme{Warsaw}

\title{Introduction to Python}
\usepackage{listings}
\subtitle{Functional Programming in Python}
\date{\today}

\begin{document}

\lstset{language=Python}

\begin{frame}
  \titlepage
\end{frame}

\section{Introduction}

\subsection{Introduction}
\begin{frame}[fragile]{Lambda Calculus}
  \begin{itemize}
  \item {
  \textbf{Idea:} Create a syntax to describe computation and study it formally as an abstract process.
}

  \pause
  \item {
  First formulated by Alonzo Church just as Turing was inventing Turing machines.
 }
 \pause
 \item {
Formally equivalent to Turing machines (proved by Turing). Led to the Church-Turing Thesis.
  }
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Lambda Calculus in the Real World}

\begin{itemize}
\item{ \textbf{1958:} Lisp invented - a realisation of the Lambda Calculus. Nowadays: Common Lisp, Scheme, Clojure}
\pause
\item{\textbf{1970s:} ML invented at University of Edinburgh as a language to prove theorems with. Nowadays: OCaml, Standard ML.
}
\pause
\item{\textbf{1987:} Haskell language invented, taught at Edinburgh to first years}
\pause
\item{\textbf{Present day:} Erlang, Python, C++ (ish), Swift and many more!}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Concepts}
	\begin{itemize}
	\item { \textbf{Functions are First-Class Citizens:} Functions are just examples of data - they can be parameters in functions and return values to functions
	}
	\pause
	\item{ \textbf{Pure functions:} Functions have no side effects. In particular every function is idempotent.
	}
\pause
\item{\textbf{Recursion:} Because of the ``no mutation" philosophy, recursion is always preferred over iteration.
}
\pause
\item{\textbf{Python is \emph{not} a pure-functional language. It's up to you what you use and ignore from the above
}}
\end{itemize}
\end{frame}

\section{Functional Programming In Action}
\subsection{Helper Functions and Closures}
\begin{frame}[fragile]{Helper Functions}
	\begin{itemize}
	\item { You can define functions inside functions. These will be inaccessible to the outside world.
  \begin{block}{}
  \begin{lstlisting}{frame=single}
  def f(n):
  	def g(m):
	return m*m #latex won't let me indent  :(
  	return g(n)
  \end{lstlisting}
\end{block}
	}
\item{ This is useful for de-cluttering your code and hiding functionality you don't want to be public.}
	\end{itemize}
\end{frame}
\begin{frame}[fragile]{Closures}
	\begin{itemize}
	\item { Helper functions are more powerful than at first they seem. Remember functions can be return values as well!
	}
	\pause
	\item { When you return a function Python will \emph{capture} local variables needed to use later.
  \begin{block}{}
  \begin{lstlisting}{frame=single}
  def powerFactory(n):
  	def g(x):
		return x ** n
  	return g
  \end{lstlisting}
\end{block}
}
\pause
\item{
\large{\textbf{ Demo Time!}}
}
\end{itemize}
\end{frame}
\subsection{Anonymous Functions}
\begin{frame}[fragile]{Lambdas}
	\begin{itemize}
	\item { Sometimes it can be annoying to come up with names for functions you will only use once.
	}
	\pause
	\item{Use Lambdas to get around this
  \begin{block}{}
  \begin{lstlisting}{frame=single}
  def powerFactory(n):
  	return (lambda x: x ** n)
  \end{lstlisting}
\end{block}
}
	\end{itemize}
\end{frame}

\subsection{Demo}
\begin{frame}[fragile]{Demo}
	 \huge{\textbf{Demo Time!}}
\end{frame}

\section{Map, Reduce and Filter}
\begin{frame}[fragile]{Introduction}
	\begin{itemize}
	\item {Map, Reduce and Filter are three common functions from functional programming.
	}
	\item{Each function acts on a list. Since these are functional they do not mutate the list!
}
\pause
\item{The names have since become famous because of the 'MapReduce' framework invented by Google for Big Data calculations
}
	\end{itemize}
\end{frame}
\begin{frame}[fragile]{Map}
	\begin{itemize}
	\item {Map takes a function of one variable and a list and acts componentwise on the list.
	}
	\pause
	\item{For example:
  \begin{block}{}
  \begin{lstlisting}{frame=single}
  x = [1,2,3]
  print map(lambda z: z ** 2, x)
  # prints [1, 4, 9]
  \end{lstlisting}
\end{block}
}
	\end{itemize}
\end{frame}

\begin{frame}[fragile]{Reduce}
	\begin{itemize}
	\item {Reduce takes a function of two variables, a list and an initial value and 'folds' down the list.
	}
	\pause
	\item{For example:
  \begin{block}{}
  \begin{lstlisting}{frame=single}
  x = [1,2,3]
  print reduce(lambda y,z: y + z, x, 0)
  # prints 6
  \end{lstlisting}
\end{block}
}
	\end{itemize}
\end{frame}
\begin{frame}[fragile]{Filter}
	\begin{itemize}
	\item {Filter takes a function of one variable that returns True or False, a list and returns the list with only
	those elements that the function returns True on.
	}
	\pause
	\item{For example:
  \begin{block}{}
  \begin{lstlisting}{frame=single}
  x = [1,2,3]
  print reduce(lambda z: z \% 2, x)
  # prints [1,3]
  \end{lstlisting}
\end{block}
}
	\end{itemize}
\end{frame}
\end{document}


\documentclass{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{subcaption}

\title{My first document}
\date{2021-10-27}
\author{Zion Umoh}

\begin{document}
	\section{Practice-1}
	Hello world!
	\pagenumbering{gobble}
	\maketitle
	\newpage
	\pagenumbering{arabic}
	Hello Class of 2021

	\section{Practice(4)-Section}
	Hello world!
	\subsection{Subsection}
	Structuring a document is easy
	\subsubsection{Subsubsection}
	More text
	\paragraph{Paragraph}
	Some more text
	\subparagraph{Subparagraph}
	Even more text
	\section{Another section}

	\section{Practice(5)Science Discoveries}
	Some of the \textbf{greatest}
	discoveries in \underline{Science}
	were made by
	\textbf{\textit{accident}}

	\paragraph{}
	Some of the greatest discoveries in science were made by \emph{accident}.

	\section{Practice-6}
		\begin{itemize}
		\item Ayomide
		\item Kamsi
		\item Eboseta
	\end{itemize}

\section{Practice-7}
\begin{enumerate}
	\item David
	\item Precious
	\item Tabitha
\end{enumerate}
\section{Practice-8}
	\begin{equation*}
	f(x)=x^2
\end{equation*}
\section{Practice-9}
	\begin{equation*}
	1 + 2 = 3
\end{equation*}
\begin{equation*}
	1 = 3 - 2
\end{equation*}
\begin{align*}
	1 + 2 &= 3\\
	1 &= 3 - 2
\end{align*}
\section{Practice-10}
	\begin{align*}
	f(x) &= x^2\\
	g(x) &= \frac{1}{x}\\
	F(x) &= \int^a_b
	\frac{1}{3}x^3
\end{align*}

\section{Practice-11}
	\begin{equation}
	\begin{matrix}
		1 & 0\\ 0 & 1
	\end{matrix}
\end{equation}
\section{Practice-12}
\begin{equation}
	[
	\begin{matrix}
		1 & 0\\
		0 & 1
	\end{matrix}
	]
\end{equation}
\section{Practice-13}
	\begin{equation*}
	\left[
	\begin{matrix}
		1 & 0\\
		0 & 1
	\end{matrix}
	\right]
\end{equation*}
\section{Practice-14}
	\begin{figure}
	\includegraphics[width=\linewidth]{boat.jpg}
	\caption{A boat.}
	\label{fig:boat1}
\end{figure}
Figure \ref{fig:boat1} shows a boat

\section{Practice-15}
	\begin{figure}[h!]
	\centering
	\begin{subfigure}[b]{0.4\linewidth}
		\includegraphics[width=\linewidth]{coffe.jpg}
		\caption{Coffee.}
	\end{subfigure}
	\begin{subfigure}[b]{0.4\linewidth}

		\includegraphics[width=\linewidth]{coffe.jpg}
		\caption{More coffee.}
	\end{subfigure}
	\caption{The same cup of coffee. Two times.}
	\label{fig:coffee}
\end{figure}

\section{Practice-16}
	\begin{figure}[h!]
	\centering
	\begin{subfigure}[b]{0.4\linewidth}
		\includegraphics[width=\linewidth]{coffe.jpg}
		\caption{Coffee.}
	\end{subfigure}
	\begin{subfigure}[b]{0.4\linewidth}

		\includegraphics[width=\linewidth]{coffe.jpg}
		\caption{More coffee.}
	\end{subfigure}
	\caption{The same cup of coffee. Two times.}
	\label{fig:coffee}
\end{figure}
\end{document}


\section[image=trams]{Two-phase commit}
\begin{frame}[plain]{}
    \sectionpage
\end{frame}

\begin{frame}
    \frametitle{Specifying two-phase commit}

    Our goal is to model a system with one \alert{transaction manager} and
    several \alert{resource managers} that need to perform a distributed
    transaction.
    The transaction may:
    \begin{itemize}
        \item \textbf{commit} if all resource managers have committed
        \item \textbf{abort} in any other case
    \end{itemize}

    \begin{center}
        \begin{tikzpicture}[
            >=stealth,
            edge from parent/.style={draw,<->},
            every node/.style={ellipse,draw}
        ]
            \node[rectangle, inner sep=.75em] (tm) {$tm$}
            child foreach \x in {rm1,rm2,rm3} {
                node {$\x$}
            };
        \end{tikzpicture}
    \end{center}
\end{frame}

\subsection{TCommit}
\begin{frame}
    \frametitle{Specifying transaction commit}
    We start with a simple setting, in which we only consider the
    \alert{resource managers} and their \alert{states}.
    We do \emph{not} model
    \begin{itemize}
        \item the transaction manager
        \item the communication channels
        \item the transaction itself
    \end{itemize}
    \uncover<2->{
    We write the specification in module $TCommit$, which will be refined later

    \begin{center}
        \begin{tlatex}
            \moduleLeftDash{ {\MODULE} $TCommit$}\moduleRightDash
        \end{tlatex}
    \end{center}
    }
\end{frame}

\setbeamercovered{transparent}

\begin{frame}
    \frametitle{Constants and variables}

    We declare $RM$ as the set of all resource managers and $rmState$ as the
    state of each resource manager.
    \begin{tlabox}
        &\CONSTANT RM \\
        &\VARIABLE rmState
    \end{tlabox}

    Every constant value is a set -- even 0, 1 and the string "xyz" --
    but for the former their elements are simply undefined, so we
    can't test if $a \in 0$.

    We will assign elements to $RM$ when performing model checking.
\end{frame}

\begin{frame}
    \frametitle{Type checking}
    \begin{uncoverenv}<1>
        \tlap doesn't provide explicit typing of variables. It is customary to
        define an invariant $TypeOK$ to specify the \alert{domain} of each variable.
    \end{uncoverenv}
    \begin{uncoverenv}<2>
        We specify that $rmState$ is an array indexed by $RM$ with values in
        the set of strings below.
    \end{uncoverenv}
    \begin{uncoverenv}<2->
        \begin{tlabox}
            TCTypeOK \defeq rmState \in
            [RM \rightarrow \{&\str{working}, \str{prepared}, \\
                            &\str{committed},\str{aborted}\}]
        \end{tlabox}
    \end{uncoverenv}
    \begin{uncoverenv}<3>
        You can think of it as a mathematical function
        \[
            rmState: RM \rightarrow \{\str{working}, \dots\}
        \]
        We can refer to a value with $rmState[r]$, where $r \in
        RM$.
    \end{uncoverenv}
\end{frame}

\begin{frame}
    \frametitle{The temporal formula of the specification}
    \begin{uncoverenv}<1->
            We specify the behavior with a temporal formula
        \begin{tlabox}
            TCSpec \defeq TCInit \land \Box [TCNext\,]_{rmState}
        \end{tlabox}
    \end{uncoverenv}
    \begin{uncoverenv}<1->
        where initially each resource manager is working
        \begin{tlabox}
            TCInit \defeq [ r \in RM \mapsto \str{working}]
        \end{tlabox}
        and at each step either all resource managers remain in their
        current state (stuttering step) or they take a $Next$-step
    \end{uncoverenv}
\end{frame}

\begin{frame}
    \frametitle{Evolution of the system}

    \begin{columns}[c]
    \begin{column}{0.65\textwidth}
        \uncover<1>{What is a possible step of our system?}

        \uncover<2,3>{A resource manager may}
        \begin{itemize}
            \item<2> Finish working and become prepared
            \item<3> Decide to commit or abort
        \end{itemize}
    \end{column}
    \begin{column}{0.35\textwidth}
        \centering
        \scalebox{.75}{
            \begin{tikzpicture}[
                >=stealth,
                sibling distance=7em,
                edge from parent/.style={draw,->},
                every node/.style={ellipse,draw}
            ]
                \node (w) {$working$}
                child {
                    node (p) {$prepared$}
                    child {
                        node[inner xsep=0] (c) {$committed$}
                    }
                    child {
                        node (a) {$aborted$}
                    }
                };
                \path [draw, ->] (w.south east) to [bend left=30] (a.north);
            \end{tikzpicture}
        }
    \end{column}
    \end{columns}
    \vspace{\baselineskip}
    \begin{tlabox}
        TCNext \defeq \uncover<2,3>{\E r \in RM :}
        \uncover<2>{Prepare(r\,)} \uncover<3>{\lor Decide(r\,)}
    \end{tlabox}
\end{frame}

\begin{frame}
    \frametitle{The Prepare action}

    Resource manager $r$ evolves from \str{working} to \str{prepared},
    while all the other ones remain in the same state.
    \begin{tlabox}
        Prep&are(r) \defeq \\
            &\uncover<1>{%
            \land rmState[r\,] = \str{working}} \\
            &\uncover<2>{%
            \land rmState' = [rmState \EXCEPT \bang[r\,] = \str{prepared}]}
    \end{tlabox}

\end{frame}

\begin{frame}
    \frametitle{The Decide action}

    \only<1>{%
    First subformula: transition \alert{$prepared \rightarrow committed$}}
    \only<2>{%
    Second subformula: transition \alert{$working, prepared \rightarrow aborted$}}
    \begin{tlabox}
        De&cide(r) \defeq \\
            &\lor \uncover<1>{%
                \land rmState[r\,] = \str{prepared}} \\
            &\phantom{\.{\lor}} \uncover<1>{%
                \land canCommit} \\
            &\phantom{\.{\lor}}\uncover<1>{%
                \land rmState' = [rmState \EXCEPT \bang[r\,] = \str{committed}]} \\
            &\lor \uncover<2>{%
                \land rmState[r\,] \in \{\str{working}, \str{prepared}\}} \\
            &\phantom{\.{\lor}}\uncover<2>{
                \land notCommitted} \\
            &\phantom{\.{\lor}}\uncover<2>{%
                \land rmState' = [rmState \EXCEPT \bang[r\,] = \str{aborted}]}
    \end{tlabox}

    \begin{onlyenv}<1>
        \begin{tlabox}
            canCommit \defeq \A r \in RM : rmState[r\,] \in \{\str{prepared},
            \str{comm.}\}
        \end{tlabox}
    \end{onlyenv}

    \begin{onlyenv}<2>
        \begin{tlabox}
            notCommitted \defeq \A r \in RM : rmState[r\,] \neq \str{committed}
        \end{tlabox}
    \end{onlyenv}

\end{frame}

\begin{frame}
    \frametitle{Checking the specification}

    Once we have modeled how the system \emph{behaves}, we can use TLC to check
    if desired properties hold.

    In particular we are interested in checking that \alert{no two RMs have
    arrived at conflicting decisions}. This \emph{safety property} can be
    expressed with the invariant $TCConsistent$:

    \begin{tlabox}
        TC&Consistent \defeq \\
        &\A r1, r2 \in RM : \neg
            \land rmState[r1] = \str{aborted} \\
        &\phantom{\A r1, r2 \in RM : \.{\neg}}
            \land rmState[r2] = \str{committed}
    \end{tlabox}

\end{frame}

\subsection{TwoPhase}

\begin{frame}
    \frametitle{Refining the specification}

    Now we create module $TwoPhase$ where we refine our specification,
    adding the following variables
    \begin{itemize}
        \item $tmState$ -- control state of the tr. manager (init or done)
        \item $tmPrepared$ -- set of r.m.s that t.m. knows are prepared
        \item $msgs$ -- set of all messages ever sent
    \end{itemize}

    while $rmState$ is defined as in the previous module.

    \begin{center}\scalebox{.75}{
        \begin{tikzpicture}[
            >=stealth,
            ent/.style={draw, rectangle, align=center},
            pnt/.style={circle, fill, inner sep=0.15em}
        ]
           \node[ent] at (0,2)(tm) {$tmState$ \\ $tmPrepared$};

           \node[ent] (rm1) at (-3,0) {$rmState[rm1]$};
           \node[ent] (rm2) at (0,0) {$rmState[rm2]$};
           \node[ent] (rm3) at (3,0) {$rmState[rm3]$};

           \draw (-3,1) -- (0,1) -- (3,1);
           \draw (rm1) -- (-3,1);
           \draw (rm2) -- (tm);
           \draw (rm3) -- (3,1);
           \node[above] (msgs) at (2,1) {$msgs$};
        \end{tikzpicture}
    }\end{center}

\end{frame}

\begin{frame}
    \frametitle{Refining the specification}

    The specification for this model is
    \begin{tlabox}
        TPSpec \defeq TPInit \land \Box [TPNext\,]_{\langle
        rmState,\, tmState,\, tmPrepared,\, msgs \rangle}
    \end{tlabox}

    Now we want to check if $TPSpec$ implements $TCSpec$, so we import the
    definitions from module $TCommit$
    \begin{tlabox}
        \INSTANCE TCommit \textcolor{gray!50}{\WITH rmState \leftarrow rmState}
    \end{tlabox}
    and check that \alert{$TPSpec \implies TCSpec$} with TLC.

    \begin{center}
        \scriptsize
        \begin{tikzpicture}[
            >=stealth,
            block/.style={draw, rectangle,
                            minimum height=3em, minimum width=3em}
        ]

            \node[block] (tp) {$TPSpec$};
            \node[block, right of=tp, node distance=10 em] (tc) {$TCSpec$};

            \draw[draw,->] (tp) -- node[above] {$rmState$} (tc);

        \end{tikzpicture}
    \end{center}

\end{frame}

\begin{frame}
    \frametitle{A common step}

    \begin{tlabox}
        TPSpec \defeq TPInit \land \Box [TPNext\,]_{\langle
        rmState,\, tmState,\, tmPrepared,\, msgs \rangle}
    \end{tlabox}

    {\scriptsize
    \setlength\abovedisplayskip{0pt}
    \setlength\belowdisplayskip{0pt}
    \begin{alignat*}{2}
        &\begin{bmatrix}
            tmState : \str{init} \\
            tmPrepared : \{\} \\
            msgs : \{\} \\
            rmState[rm1] : \str{work.}
        \end{bmatrix}
        &\xrightarrow{RMPrepare(rm1)}&
        \begin{bmatrix}
            tmState : \str{init} \\
            tmPrepared : \{\} \\
            \alert{
            msgs = \{[type :\str{Prep}, rm : rm1]\}} \\
            \alert{
            rmState[rm1] : \str{prep.}}
        \end{bmatrix}
        \\[\baselineskip]
        &\begin{bmatrix}
            rmState[rm1] : \str{work.} \\
        \end{bmatrix}
        &\xrightarrow{\phantom{R}Prepare(rm1)\phantom{M}}&
        \begin{bmatrix}
            \phantom{msgs \ \ }
            \alert{
            rmState[rm1] : \str{prep.}}
            \phantom{msgs \ \ }
        \end{bmatrix}
    \end{alignat*}
    }

    \begin{tlabox}
        TCSpec \defeq TCInit \land \Box [TCNext\,]_{rmState}
    \end{tlabox}

\end{frame}

\begin{frame}
    \frametitle{A stutter step}

    \begin{tlabox}
        TPSpec \defeq TPInit \land \Box [TPNext\,]_{\langle
        rmState,\, tmState,\, tmPrepared,\, msgs \rangle}
    \end{tlabox}

    {\scriptsize
    \setlength\abovedisplayskip{0pt}
    \setlength\belowdisplayskip{0pt}
    \begin{alignat*}{2}
        &\begin{bmatrix}
            tmState : \str{init} \\
            tmPrepared : \{\} \\
            msgs = \{\dots\} \\
            rmState[rm1] : \str{prep.}
        \end{bmatrix}
        &\xrightarrow{TMRcvPrepared(rm1)}&
        \begin{bmatrix}
            tmState : \str{init} \\
            \alert{
            tmPrepared : \{rm1\}} \\
            msgs : \{\dots\} \\
            rmState[rm1] : \str{prep.}
        \end{bmatrix}
        \\[\baselineskip]
        &\begin{bmatrix}
            rmState[rm1] : \str{prep.}
        \end{bmatrix}
        &\xrightarrow{\phantom{T} \UNCHANGED rmState \phantom{T}}&
        \begin{bmatrix}
            rmState[rm1] : \str{prep.} \\
        \end{bmatrix}
    \end{alignat*}
    }

    \begin{tlabox}
        TCSpec \defeq TCInit \land \Box [TCNext\,]_{rmState}
    \end{tlabox}

\end{frame}

\begin{frame}
    \frametitle{Taking advantage of symmetry}

    TLC is an explicit-state model checker, so it suffers from state
    explosion. Consider the following possible states according to $TPSpec$:

    {\setlength\abovedisplayskip{0pt}
    \setlength\belowdisplayskip{0pt}
    \begin{gather*}
        s =
        \begin{bmatrix}
            rmState[\textcolor{redPolimi}{rm1}] : \str{prep.} \\
            rmState[\textcolor{bluePolimi}{rm2}] : \str{prep.} \\
            rmState[\textcolor{yellowPolimi}{rm3}] : \str{com.} \\
            \dots
        \end{bmatrix}
        \quad
        s^{\pi} =
        \begin{bmatrix}
            rmState[\textcolor{yellowPolimi}{rm3}] : \str{prep.} \\
            rmState[\textcolor{bluePolimi}{rm2}] : \str{prep.} \\
            rmState[\textcolor{redPolimi}{rm1}] : \str{com.} \\
            \dots
        \end{bmatrix} \\
        \pi = [
            rm1 \mapsto rm3,\,
            rm2 \mapsto rm2,\,
            rm3 \mapsto rm1
        ]
    \end{gather*}}

    Since permutations such as $\pi$ are irrelevant for us, we can take
    advantage of \emph{symmetry}.

\end{frame}

\begin{frame}
    \frametitle{Taking advantage of symmetry}

    \begin{block}{Symmetric specification}
        \setlength\belowdisplayskip{0pt}
        A specification $Spec$ is symmetric with respect to a permutation $\pi$
        over a set of values $S$ if and only if for each behavior $\sigma$:
        \[
             \sigma \models Spec \iff \sigma^{\pi} \models Spec
        \]
    \end{block}

    $TPSpec$ is indeed symmetric w.r.t. the permutations of set $RM$,
    since all resource managers are equal.

    After stating that \alert{$RM$ is a symmetry set} in our model, if TLC
    has already visited a state $s$, it will avoid visiting a state $s^{\pi}$,
    where $\pi$ is a permutation of values in $RM$.

    A reduction of a factor $\lvert RM \rvert !$ can be observed for the number
    of states to be checked.

\end{frame}

\setbeamercovered{invisible}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\setlength{\parindent}{0em}
\setlength{\parskip}{0.5em}
\usepackage{graphicx}
\usepackage{tabto}
\usepackage[table]{xcolor}
\usepackage{subcaption}
\usepackage{float}
\usepackage{wrapfig}

\title{CTA200 2020 Assignment 2}
\author{Yansong Qian}
\date{\today}

\begin{document}

\maketitle

\section*{Question 1}

\begin{figure}[H]

    \begin{subfigure}{7cm}
    \centering
    \includegraphics[width=7cm]{1.1.png}
    \caption{$-2 < x < 2$ and $-2 < y < 2$}
    \label{fig:my_label}
     \end{subfigure}
     \begin{subfigure}{7cm}
    \centering
    \includegraphics[width=7cm]{1.2.png}
    \caption{$0 < x < 0.5$ and $-1 < y < 1$}
    \label{fig:my_label}
     \end{subfigure}
     \caption{Divergence graph}
\end{figure}

\indent To produce the sequence $z_n$, I write a recursive python function. The function has two parameters n and c, where c is a complex number described in question. There are two possible types of output. If $z_n$ is finite, the function will return the complex number $z_n$. If $z_n$ is infinite, the function will return a list with only one element i, where i is the smallest integer between 0 and n where $z_i$ is infinite. To plot my result, I create a 2d array. One dimension is for real part and another dimension is for imaginary part. And I use my recursive function to calculate the iteration number where it converges for each point.\\
\\
\indent Figure 1(a) shows that the sequence will diverge if real part and imaginary part is large, but it is not likely to diverge if the real part of c is negative. Figure 1(b) is a zooming in version if (a) in the boundary. We can see if the real part is larger than 0.25, the sequence will diverge very quickly(less than 50 iteration). But if real part is smaller than 0,25, the sequence will not diverge after 100 iteration.

\section*{Question 2}

\begin{figure}[H]

    \begin{subfigure}{7cm}
    \centering
    \includegraphics[width=7cm]{0.1&0.01.png}
    \caption{$\beta = 0.1, \gamma = 0.01$}
    \label{fig:my_label}
     \end{subfigure}
     \begin{subfigure}{7cm}
    \centering
    \includegraphics[width=7cm]{0.1&0.2.png}
    \caption{$\beta = 0.1, \gamma = 0.2$}
    \label{fig:my_label}
     \end{subfigure}
     \\
     \begin{subfigure}{7cm}
    \centering
    \includegraphics[width=7cm]{0.3&0.01.png}
    \caption{$\beta = 0.3, \gamma = 0.01$}
    \label{fig:my_label}
     \end{subfigure}
     \begin{subfigure}{7cm}
    \centering
    \includegraphics[width=7cm]{0.5&0.2.png}
    \caption{$\beta = 0.5, \gamma = 0.2$}
    \label{fig:my_label}
     \end{subfigure}
     \caption{SIR model solutions}
\end{figure}

\indent I use scipy.integrate.odeint to solve the ODE system. And use four different combination of $\beta$ and $\gamma$ to show different cases.\\
\\
\indent Overall, $\beta$ is the parameter related to infection rate and $\gamma$ is the parameter related to recovery rate. By comparing figure 2(a) and 2(c), we know higher infection rate will cause more rapid increase in infection number and also higher infection peak. Figure 2(b) shows that if recovery rate is higher than infection rate, the disease will not spread.

\end{document}