\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{dsfont}
\usepackage{graphicx}
\usepackage{interval}
\usepackage{color}
\usepackage{xcolor,colortbl}
\usepackage[style=authoryear, maxcitenames=11, mincitenames=11, natbib=true, uniquename=false, uniquelist=false]{biblatex}
\addbibresource{main.bib}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing,angles,quotes}
\usepgflibrary{arrows.meta,shapes.geometric}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}
\usepackage{dirtytalk}
\usepackage{hyperref}

\beamertemplatenavigationsymbolsempty%
\addtobeamertemplate{navigation symbols}{}{%
    \usebeamerfont{footline}%
    \usebeamercolor[fg]{footline}%
    \hspace{1em}%
    \insertframenumber/\inserttotalframenumber%
}

\author{Florian Fontan}
\title{Advanced Models and Methods in Operations Research \\ Dynamic Programming}
\date{November 9, 2021}

\begin{document}

\newcommand{\customcite}[1]{\citetitle{#1}, \citeauthor{#1}, \citeyear{#1}}

\AtBeginSection[]
{%
  \begin{frame}<beamer>
    \frametitle{Table of contents}
    \tableofcontents[currentsection]
  \end{frame}
}

\maketitle

\section{Introduction}

\begin{frame}
  \frametitle{Me}

  \begin{itemize}
    \item Former student of ORCO (2015--2016)
    \item PhD in Operations Research
    \item Engineer at Artelys: \url{https://www.artelys.com/}
      \begin{itemize}
        \item Artelys is an independent company specialized in optimization, decision support and modeling
        \item Design and implementation of optimization algorithms for industrial clients
        \item Development for the NLP/MINLP solver Artelys Knitro \url{https://www.artelys.com/solvers/knitro/}
      \end{itemize}
    \item My GitHub: \url{https://github.com/fontanf/}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Organization}
  \begin{itemize}
    \item 4 classes with me:
      \begin{itemize}
        \item Dynamic Programming
        \item Heuristic Tree Search
        \item Column Generation Heuristics
        \item Project Presentations
      \end{itemize}
    \item 1.5 hours lecture / 1.5 hours practical training
      \item Goal of the classes: understanding the theory of the methods and being able to implement them to solve practical problems
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Organization}
  \begin{itemize}
    \item Evaluation:
      \begin{itemize}
        \item Not in the final exam
        \item Project by groups of 3 or 4
        \item Implementation of the algorithms studied in the classes
        \item First deadline with feedbacks
        \item Second deadline with final grade
        \item Please do not put your code on a public repository
        \item You can keep it on a private repository. It can be valuable if you decide to apply for a company some day
      \end{itemize}
    \item All materials (slides, projects\dots) are online \url{https://github.com/fontanf/teaching}
    \item E-mail: \href{mailto:dev@florian-fontan.fr}{dev@florian-fontan.fr}
  \end{itemize}
\end{frame}

\section{The Partition Problem and the Subset Sum Problem}

\begin{frame}
  \frametitle{Problem definition}

  \begin{block}{Partition Problem}
    \begin{itemize}
      \item Instance: $n$ items with weight \alert{$w_j$}, $j = 1, \dots, n$.
      \item Question: is it possible to partition the set of items into two subsets of equal weights?
    \end{itemize}
  \end{block}

  \pause

  We consider a slightly more general variant:
  \begin{block}{Subset Sum Problem (decision version)}
    \begin{itemize}
      \item Instance:
        \begin{itemize}
          \item $n$ items with weight \alert{$w_j$}, $j = 1, \dots, n$
          \item a capacity \alert{$C$}
        \end{itemize}
      \item Question: is there a subset of items with total weight $C$?
    \end{itemize}
  \end{block}

  \pause
  Link between the Partition Problem and the Subset Sum Problem?
  \pause
  The Partition Problem is a Subset Sum Problem with capacity $C = \frac{1}{2} \sum_{j = 1}^n w_j$.
\end{frame}

\begin{frame}
  \frametitle{Recursive function}

  For all $j = 0, \dots, n$, $c = 0, \dots, C$, let us define:
  \begin{displaymath}
    F(j, c) =
    \left\{
      \begin{array}{ll}
        \text{True} & \text{if among items $1, \dots, j$, there exists} \\
        & \text{a subset of items with total weight $c$} \\
        \text{False} & \text{otherwise}
      \end{array}
    \right.
  \end{displaymath}

  \pause
  What is the value of
  \begin{itemize}
    \item $F(0, 0)$? \pause True \pause
    \item $F(0, c)$? \pause True for $c = 0$, False otherwise \pause
    \item $F(j, 0)$? \pause True \pause
  \end{itemize}

  What is the relation between the Subset Sum Problem and $F$? \pause

  The Subset Sum Problem is equivalent to determining the value of $F(n, C)$.
\end{frame}

\begin{frame}
  \frametitle{Computing $F(n, C)$}

  We compute $F(j, c)$ with the following recursive formula:
  \begin{displaymath}
    F(j, c) =
    \left\{
      \begin{array}{ll}
        \text{True} & \text{if $j = 0$ and $c = 0$} \\
        \text{False} & \text{if $j = 0$ and $c \neq 0$} \\
        F(j - 1, c) & if \text{$j \neq 0$ and $c < w_j$} \\
        F(j - 1, c) & \text{otherwise} \\
        \text{or } F(j - 1, c - w_j) &
      \end{array}
    \right.
  \end{displaymath}
\end{frame}

\begin{frame}
  \frametitle{Divide-and-conquer}

  \small
  We can now solve the Subset Sum Problem with a divide-and-conquer algorithm:

  \begin{minipage}[t]{0.68\linewidth}
    \begin{algorithmic}
      \Function{F}{$w$, $j$, $c$}
      \If {$j == 0$}
      \If {$c == 0$}
      \State\Return$True$
      \Else{}
      \State\Return$False$
      \EndIf
      \ElsIf {$c < w_j$}
      \State\Return$F(j - 1, c)$
      \Else{}
      \State\Return$F(j - 1, c) \text{ or } F(j - 1, c - w[j])$
      \EndIf
      \EndFunction{}
      \Function{subsetsum}{$w$, $C$}
      \State\Return{} $F(w, n, C)$
      \EndFunction{}
    \end{algorithmic}
  \end{minipage}
  \hfill
  \begin{minipage}[t]{0.28\linewidth}
    \begin{itemize}
      \item \pause Time complexity? \pause $O(2^n)$
      \item \pause Space complexity? \pause $O(n)$
    \end{itemize}
  \end{minipage}

  \bigskip

  \pause Is there a way to improve the time complexity?
\end{frame}

\begin{frame}
  \frametitle{Divide-and-conquer example}
  Consider an instance $I$ of the Subset Sum Problem with
  \begin{itemize}
    \item $n = 103$
    \item $C = 1000$
    \item $w_{101} = 30$, $w_{102} = 20$, $w_{103} = 10$.
  \end{itemize}

  \begin{center}
    \begin{tikzpicture}
      \onslide<1->{%
        \node[draw=black, thick, rectangle] (103-1000) at (0,0) {$F(103, 1000)$};
      }

      \onslide<2->{%
        \node[draw=black, thick, rectangle] (102-1000) at (-2,-1) {$F(102, 1000)$};
        \node[draw=black, thick, rectangle] (102-990) at (2,-1) {$F(102, 990$)};
        \draw [thick, draw=black, ->] (103-1000) -- (102-1000);
        \draw [thick, draw=black, ->] (103-1000) -- (102-990);
      }

      \onslide<3->{%
        \node[draw=black, thick, rectangle] (101-1000) at (-4,-2) {$F(101, 1000)$};
        \node[draw=black, thick, rectangle] (101-980) at (-1.5,-2) {$F(101, 980$)};
        \draw [thick, draw=black, ->] (102-1000) -- (101-1000);
        \draw [thick, draw=black, ->] (102-1000) -- (101-980);

        \node[draw=black, thick, rectangle] (101-990) at (1.5,-2) {$F(101, 990)$};
        \node[draw=black, thick, rectangle] (101-970) at (4,-2) {$F(101, 970$)};
        \draw [thick, draw=black, ->] (102-990) -- (101-990);
        \draw [thick, draw=black, ->] (102-990) -- (101-970);
      }

      \onslide<4->{%
        \node[draw=black, thick, rectangle] (100-1000) at (-4.5,-3) {$F(100, 1000)$};
        \node[draw=black, thick, rectangle] (100-970) at (-2,-3) {$F(100, 970$)};
        \draw [thick, draw=black, ->] (101-1000) -- (100-1000);
        \draw [thick, draw=black, ->] (101-1000) -- (100-970);

        \node[draw=black, thick, rectangle] (100-970-2) at (2,-3) {$F(100, 970)$};
        \node[draw=black, thick, rectangle] (100-940) at (4.5,-3) {$F(100, 940$)};
        \draw [thick, draw=black, ->] (101-970) -- (100-970-2);
        \draw [thick, draw=black, ->] (101-970) -- (100-940);
      }

      \onslide<5->{%
        \node[draw=red, thick, rectangle] (100-970) at (-2,-3) {$F(100, 970$)};
        \node[draw=red, thick, rectangle] (100-970-2) at (2,-3) {$F(100, 970)$};
      }
    \end{tikzpicture}
  \end{center}

  \onslide<6->{%
    The same subproblems might be solved multiple times!
  }
\end{frame}

\begin{frame}
  \frametitle{Dynamic Programming: recursive implementation (top-down)}

  \footnotesize
  Same algorithm as before, but now we store the results of the subproblems to avoid solving multiple times the same subproblems:
  \begin{minipage}[t]{0.75\linewidth}
    \footnotesize
    \begin{algorithmic}
      \Procedure{F}{w, T, j, c}
      \If {$T[j][c] == \mathrm{NULL}$}
      \If {$j == 0$}
      \If {$c == 0$}
      \State$T[j][c] \gets \text{True}$
      \Else{}
      \State$T[j][c] \gets \text{False}$
      \EndIf
      \ElsIf {$c < w[j]$}
      \State$T[j][c] \gets F(j - 1, c)$
      \Else{}
      \State$T[j][c] \gets F(j - 1, c) \text{ or } F(j - 1, c - w[j])$
      \EndIf
      \EndIf
      \State\Return$T[j][c]$
      \EndProcedure{}
      \Procedure{subsetsum}{w, C}
      \State$T \gets$ array of size $(n + 1) \times (C + 1)$ initialized at NULL
      \State\Return$F(w, T, n, C)$
      \EndProcedure{}
    \end{algorithmic}
  \end{minipage}
  \hfill
  \begin{minipage}[t]{0.24\linewidth}
    \footnotesize
    \begin{itemize}
      \item \pause Time complexity? \pause $O(nC)$
      \item \pause Space complexity? \pause $O(nC)$
    \end{itemize}
  \end{minipage}
\end{frame}

\begin{frame}
  \frametitle{Dynamic Programming}

  \begin{block}{Dynamic Programming}
    Solving a problem recursively and storing the results of the subproblems to avoid recomputing them multiple times.
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{Dynamic Programming: iterative implementation (bottom-up)}

  \begin{algorithmic}
    \Procedure{subsetsum}{w, C}
    \State$T \gets$ array of size $(n + 1) \times (C + 1)$ initialized at NULL
    \State$T[0][0] \gets \text{True}$
    \For {$c = 1, \dots, C$}
    \State$T[0][c] \gets \text{False}$
    \EndFor
    \For {$j = 1, \dots, n$}
    \For {$c = 0, \dots, w[j] - 1$}
    \State$T[j][c] \gets T[j - 1][c]$
    \EndFor
    \For {$c = w[j], \dots, C$}
    \State$T[j][c] \gets T[j - 1][c] \text{ or } T[j - 1][c - w[j]]$
    \EndFor
    \EndFor
    \State\Return{}$T[n, C]$
    \EndProcedure{}
  \end{algorithmic}

  \begin{itemize}
    \item \pause Time complexity? \pause $O(nC)$
    \item \pause Space complexity? \pause $O(nC)$
    \item \pause In practice, $10$ times faster than the recursive implementation
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Dynamic Programming example}

  Instance:
  \begin{itemize}
    \item $n = 5$, $w = \{ 4, 11, 6, 8, 7 \}$
    \item $C = 17$
  \end{itemize}

  \pause

  Reminder:
  \begin{displaymath}
    F(j, c) =
    \left\{
      \begin{array}{ll}
        \text{True} & \text{if $j = 0$ and $c = 0$} \\
        \text{False} & \text{if $j = 0$ and $c \neq 0$} \\
        F(j - 1, c) & if \text{$j \neq 0$ and $c < w_j$} \\
        F(j - 1, c) & \text{otherwise} \\
        \text{or } F(j - 1, c - w_j) &
      \end{array}
    \right.
  \end{displaymath}

  \small
  \begin{center}
    \begin{tabular}{cp{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}}
      \toprule
      j / c & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 \\
      \midrule
      \onslide<3->{0     & T & F & F & F & F & F & F & F & F & F & F  & F  & F  & F  & F  & F  & F  & F } \\
      \onslide<4->{1     & T & F & F & F & T & F & F & F & F & F & F  & F  & F  & F  & F  & F  & F  & F }  \\
      \onslide<5->{2     & T & F & F & F & T & F & F & F & F & F & F  & T  & F  & F  & F  & T  & F  & F }  \\
      \onslide<6->{3     & T & F & F & F & T & F & T & F & F & F & T  & T  & F  & F  & F  & T  & F  & T }  \\
      \onslide<7->{4     & T & F & F & F & T & F & T & F & T & F & T  & T  & T  & F  & T  & T  & F  & T }  \\
      \onslide<8->{5     & T & F & F & F & T & F & T & T & T & F & T  & T  & T  & T  & T  & T  & F  & T }  \\
      \bottomrule
    \end{tabular}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Retrieving the solution with backtracking}

  The algorithms presented in the previous slides only return True or False. In case the answer is True, we would also like to get a solution, \textit{i.e.}\ a subset of items with total weight $C$.

  $w = \{ 4, 11, 6, 8, 7 \}$

  \small
  \begin{center}
    \begin{tabular}{cp{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}}
      \toprule
      j / c & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 \\
      \midrule
      0     & \temporal<11>{T}{\cellcolor{blue!25} T}{\cellcolor{red!25} T} & F & F & F & \temporal<11>{F}{\cellcolor{blue!25} F}{F} & F & F & F & F & F & F  & F  & F  & F  & F  & F  & F  & F  \\
      1     & T & F & F & F & \temporal<9>{T}{\cellcolor{blue!25} T}{\cellcolor{red!25} T} & F & F & F & F & F & F  & F  & F  & F  & F  & F  & F  & F  \\
      2     & T & F & F & F & \temporal<7>{T}{\cellcolor{blue!25} T}{\cellcolor{red!25} T} & F & F & F & F & F & \temporal<7>{F}{\cellcolor{blue!25}F}{F}  & T  & F  & F  & F  & T  & F  & F  \\
      3     & T & F & \temporal<5>{F}{\cellcolor{blue!25} F}{F} & F & T & F & T & F & F & F & \temporal<5>{T}{\cellcolor{blue!25} T}{\cellcolor{red!25} T} & T  & F  & F  & F  & T  & F  & T  \\
      4     & T & F & F & F & T & F & T & F & T & F & \temporal<3>{T}{\cellcolor{blue!25} T}{\cellcolor{red!25} T}  & T  & T  & F  & T  & T  & F  & \temporal<3>{T}{\cellcolor{blue!25} T}{T}  \\
      5     & T & F & F & F & T & F & T & T & T & F & T  & T  & T  & T  & T  & T  & F  & \alt<1>{T}{\cellcolor{red!25} T}  \\
      \bottomrule
    \end{tabular}
  \end{center}

  \onslide<13>{$S = \{5, 3, 1 \}$}
  \pause
\end{frame}

\begin{frame}
  \frametitle{Retrieving the solution with backtracking}

  \begin{algorithmic}
    \Function{SSPbacktracking}{w, C, T}
    \State$S \gets \{ \}$
    \State$c \gets C$
    \State$i \gets n$
    \While{$i > 0$}
    \If {not $T[i-1][w]$}
    \State$S \gets S \cup \{ i \}$
    \State$c \gets c - w[j]$
    \EndIf
    \State {} $i \gets i - 1$
    \EndWhile{}
    \State {} \Return{} S
    \EndFunction{}
  \end{algorithmic}
\end{frame}

\begin{frame}
  \frametitle{Going further}

  \begin{itemize}
    \item Write an algorithm computing $F(n, C)$ which only keeps two lines of the array in memory (spatial complexity $O(C)$)
    \item Write an algorithm computing $F(n, C)$ which only keeps a single line of the array in memory.
    \item How to return a solution when keeping only a single line in memory?
      \begin{itemize}
        \item if the array is stored as an array of integers
        \item if the array is stored as an array of bits
      \end{itemize}
  \end{itemize}

\end{frame}

\section{The Knapsack Problem}

\begin{frame}
  \frametitle{Problem definition}

  \begin{block}{Knapsack Problem}
    \begin{itemize}
      \item Instance:
        \begin{itemize}
          \item $n$ items with weight \alert{$w_j$} and profit \alert{$p_j$}, $j = 1, \dots, n$
          \item a capacity \alert{$C$}
        \end{itemize}
      \item Problem: find a subset of items such that the total weight of the subset is less than or equal to $C$.
      \item Objective: maximize the total profit of the selected items.
    \end{itemize}
  \end{block}

  \pause
  Link between the Subset Sum Problem and the Knapsack Problem?
  \pause
  The Subset Sum Problem is a Knapsack Problem with $p_j = w_j$ for all $j = 1, \dots, n$.
\end{frame}

\begin{frame}
  \frametitle{Recursive function}

  For all $j = 0, \dots, n$, $c = 0, \dots, C$, let us define \alert{$F(j, c)$} the maximum profit of a subset of items $1, \dots, j$ with total weight less than or equal to $c$.

  \bigskip

  \pause
  What is the value of
  \begin{itemize}
    \item $F(0, 0)$? \pause $0$ \pause
    \item $F(0, c)$? \pause $0$ \pause
    \item $F(j, 0)$? \pause $0$ \pause
  \end{itemize}

  \bigskip

  What is the relation between the Knapsack Problem and $F$? \pause

  The Knapsack Problem is equivalent to determining the value of $F(n, C)$.
\end{frame}

\begin{frame}
  \frametitle{Computing $F(n, C)$}

  We compute $F(j, c)$ with the following recursive formula:
  \begin{displaymath}
    F(j, c) =
    \left\{
      \begin{array}{ll}
        0 & \text{if $j = 0$} \\
        F(j - 1, c) & \text{if $j \neq 0$ and $c < w_j$} \\
        \max \left\{
          \begin{array}{ll}
            F(j - 1, c) \\
            F(j - 1, c - w_j) + p_j
          \end{array}
        \right. & \text{otherwise} \\
      \end{array}
    \right.
  \end{displaymath}

  \pause

  \begin{minipage}[t]{0.48\linewidth}
    Example: $C = 7$
    \begin{center}
      \begin{tabular}{ccc}
        \toprule
        Item & Weight & Profit \\
        \midrule
        1 & 3 & 4 \\
        2 & 5 & 6 \\
        3 & 4 & 5 \\
        4 & 2 & 2 \\
        \bottomrule
      \end{tabular}
    \end{center}
  \end{minipage}
  \begin{minipage}[t]{0.48\linewidth}
    \pause
    \begin{center}
      \begin{tabular}{cp{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}p{0.001cm}}
        \toprule
        j / c & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
        \midrule
        \onslide<4->{0 & \temporal<16>{0}{\cellcolor{blue!25} 0}{\cellcolor{red!25} 0} & 0 & 0 & \temporal<16>{0}{\cellcolor{blue!25} 0}{0} & 0 & 0 & 0 & 0 } \\
        \onslide<5->{1 & 0 & 0 & 0 & \temporal<14>{4}{\cellcolor{blue!25} 4}{\cellcolor{red!25} 4} & 4 & 4 & 4 & 4 } \\
        \onslide<6->{2 & 0 & 0 & 0 & \temporal<12>{4}{\cellcolor{blue!25} 4}{\cellcolor{red!25} 4} & 4 & 6 & 6 & \temporal<12>{6}{\cellcolor{blue!25} 6}{6} } \\
        \onslide<7->{3 & 0 & 0 & 0 & 4 & 5 & \temporal<10>{6}{\cellcolor{blue!25} 6}{6} & 6 & \temporal<10>{9}{\cellcolor{blue!25} 9}{\cellcolor{red!25} 9} } \\
        \onslide<8->{4 & 0 & 0 & 2 & 4 & 5 & 6 & 7 & \alt<1-8>{9}{\cellcolor{red!25} 9} } \\
        \bottomrule
      \end{tabular}
    \end{center}

    \onslide<18>{$S = \{3, 1 \}$}
    \pause
  \end{minipage}
\end{frame}

\section{The Single-Night Star Observation Scheduling Problem}

%\mystar{y}{r}{d-r}{t-r}{p}{nom}
\def\mystar#1#2#3#4#5#6#7{
  % 1: y
  % 2: r_s
  % 3: d_s - r_s
  % 4: t_s - r_s
  % 5: p_s
  % 6: nom
  % 7: opacity
  \begin{scope}[shift={(#2,#1)},opacity=#7,transparency group]
    \draw[line width=1pt,color=blue,arrows={Bracket-Bracket}] (0.4,0) -- +(#3,0);
    \draw[line width=5pt,color=red] (0.4,0) ++(#4,0) -- ++(#5,0);
    \def\tmp{#6}\if\tmp\empty\else
    \node[star, star point height=3mm, minimum size=5mm, color=yellow, fill=yellow, text=black,scale=.4] at (0,0) {#6};
    \fi
  \end{scope}
}

\def\mystaronly#1#2#3#4{
  % 1: y
  % 2: r_s
  % 3: nom
  % 4: opacity
  \begin{scope}[shift={(#2,#1)},opacity=#4,transparency group]
    \node[star, star point height=3mm, minimum size=5mm, color=yellow, fill=yellow, text=black,scale=.4] at (0,0) {#3};
  \end{scope}
}

\begin{frame}
  \frametitle{A star}

  \begin{minipage}{.7\linewidth}
    \begin{tikzpicture}
      \def\mypath{(0,0) arc[x radius=4cm, y radius=4cm, start angle=150, end angle=30]};

      \begin{scope}
        \clip\mypath;
        \fill[fill=cyan!20!] (1,0) rectangle (6.5,2.5);
      \end{scope}

      \draw[line width=1pt, color=blue, loosely dotted] \mypath;

      \node[star, star point height=3mm, minimum size=5mm, draw,
      color=yellow,fill=yellow,text=black] at (2.5,1.9) {\LARGE $j$};

      \draw[line width=1pt,color=blue,arrows={Bracket-Bracket}] (1,0)--(6.5,0);
      \node[below,blue] at (1,-.1) {\LARGE $r_j$};
      \node[below,blue] at (6.5,-.1) {\LARGE $d_j$};
      \draw[line width=5pt, color=red] (2.5,0) -- (6,0);
      \node[below,red] at (4.25,-.1) {\LARGE $p_j$};
      \draw[line width=2pt,dotted,darkgray](3.4,-0.3)--(3.4,0.3);
      \node[below,darkgray] at (3.4,-0.3) {\LARGE $m_j$};
    \end{tikzpicture}
  \end{minipage}
  \hfill
  \begin{minipage}{.26\linewidth}\footnotesize
    \begin{itemize}
      \item $\interval[open right]{r_j}{d_j}$: visibility interval
      \item $p_j$: observation time
      \item $w_j$: scientific interest of the observation
    \end{itemize}
  \end{minipage}

  \vfill

  Every observation $j$ has a meridian $m_j \in \interval[open right]{r_j}{d_j}$ which is a \alert{mandatory instant} of the observation.
\end{frame}

\begin{frame}
  \frametitle{Problem definition}

  Instance: a set of stars \alert{$\cal N$}; each star $j\in\cal N$ has a scientific interest \alert{$w_j$}, an observation time \alert{$p_j$} and a time window \alert{$\interval[open right]{r_j}{d_j}$}

  \vfill

  \alt<1>{%
    \begin{tikzpicture}
      % \mystar{y}{r}{d-r}{t-r}{p}{nom}
      \mystar{1.5}{0}{6}{1}{4}{$1$}{1}
      \mystar{1.5}{7}{3}{.5}{2}{$2$}{1}
      \mystar{1.0}{3}{6}{1}{4}{$3$}{1}
      \mystar{0.5}{1}{4}{0.9}{2.2}{$4$}{1}
      \mystar{0.5}{7}{2.5}{0.5}{1.5}{$5$}{1}
      \mystar{0}{4}{5}{1}{3}{$6$}{1}
    \end{tikzpicture}
  }{%
    \begin{tikzpicture}
      % \mystar{y}{r}{d-r}{t-r}{p}{nom}
      \mystar{1.5}{0}{6}{0}{4}{$1$}{1}
      \mystar{1.5}{7}{3}{.5}{2}{$2$}{.2}
      \mystar{1.0}{3}{6}{1}{4}{$3$}{1}
      \mystar{0.5}{1}{4}{0.9}{2.2}{$4$}{.2}
      \mystar{0.5}{7}{2.5}{1}{1.5}{$5$}{1}
      \mystar{0}{4}{5}{1}{3}{$6$}{.1}
    \end{tikzpicture}
  }

  \vfill

  \visible<2->{%
    Problem: find a subset $\cal \alert{N'} \subset N$ as well as the start date \alert{$s_j$} of each selected observation $j \in \cal{N}'$ such that:
    \begin{itemize}
      \item for all $j\in\cal N'$: $\interval[open right]{s_j}{s_j + p_j} \subset \interval[open right]{r_j}{d_j}$
      \item for all $(j_1,j_2)\in{\cal N'}^2: \interval[open right]{s_{j_1}}{s_{j_1} + p_{j_1}} \cap \interval[open right]{s_{j_2}}{s_{j_2} + p_{j_2}} = \emptyset$
    \end{itemize}
    Objective: maximize $\sum_{j\in\cal N'} w_j$ the profit of the selected observations.
  }
\end{frame}

\begin{frame}
  \frametitle{Properties}

  \begin{block}{Property 1}
    There exists an optimal solution in which selected observations are scheduled in non-decreasing order of their mandatory instant.
  \end{block}

  \pause

  \begin{center}%
    \temporal<3>{%
      \begin{tikzpicture}
        \mystar{0.5}{0}{3.5}{0.875}{1.75}{$j_1$}{1}
        \mystar{0.0}{1}{6}{1.5}{3}{$j_2$}{1}
        \draw[line width=2pt,dotted,darkgray]%
        (2.15, 0.2)--(2.15,0.8)
        (4.4,-0.3)--(4.4,0.3);
      \end{tikzpicture}
    }{%
      \begin{tikzpicture}
        \mystar{0.5}{0}{3.5}{1.75}{1.75}{$j_1$}{1}
        \mystar{0.0}{1}{6}{0}{3}{$j_2$}{1}
        \draw[line width=2pt,dotted,darkgray]%
        (2.15, 0.2)--(2.15,0.8)
        (4.4,-0.3)--(4.4,0.3);
        \draw[line width=3pt,black] (2.15,0.5)--+(1.75,0) (2.15,0)--+(1.75,0);
        \draw[line width=2pt,red] (2.27,0.7)--(3.78,-0.2) (2.27,-0.2)--(3.78,0.7);
      \end{tikzpicture}
    }{%
      \begin{tikzpicture}
        \mystar{0.5}{0}{3.5}{0}{1.75}{$j_1$}{1}
        \mystar{0.0}{1}{6}{2.95}{3}{$j_2$}{1}
        \draw[line width=2pt,dotted,darkgray]%
        (2.15, 0.2)--(2.15,0.8)
        (4.4,-0.3)--(4.4,0.3);
      \end{tikzpicture}
    }
  \end{center}

  \onslide<5>{%
    \begin{block}{Property 2}
      Consider a subset $\cal N' \subset N$ and an observation $j_\text{max}$ such that $d_{j_\text{max}} = \max_{j \in \cal N'} d_j$.
      If there exists a feasible solution with selected observations $\cal N'$, then there exists a feasible solution with selected observations $\cal N'$ such that $s_{j_\text{max}} = d_{j_\text{max}} - p_{j_\text{max}}$.
    \end{block}
  }
  \pause
\end{frame}

\begin{frame}
  \frametitle{Recursive function}
  \small

  We consider than the observations are numbered in non-decreasing order of their mandatory instant.

  \bigskip

  \pause
  For all $j = 0, \dots, n$, $t = 0, \dots, T$, let us define \alert{$F(j, t)$} the maximum scientific interest of a subset of observations of $1, \dots, j$ during the interval $\interval[]{0}{T}$.

  \pause
  \begin{displaymath}
    F(j, t) =
    \left\{
      \begin{array}{ll}
        0 & j = 0 \\
        F(j - 1, t) & j \neq 0, t \in \interval[open right]{0}{r_j + p_j} \\
          \max \left\{
            \begin{array}{l}
              F(j - 1, t)	 \\
              F(j - 1, \min\{d_j, t\} - p_j) + w_j
            \end{array}
            \right. & \text{otherwise} \\
      \end{array}
    \right.
  \end{displaymath}

  \pause
  Complexity: $O(nT)$

  \bigskip

  \pause
  Sometimes, a bit of work is needed in order to exhibit the structure to design an efficient algorithm based on Dynamic Programming.
\end{frame}

\section{Dynamic Programming as a Tree Search}

\begin{frame}
  \frametitle{Breadth First Search: Example (Knapsack Problem)}

  \begin{minipage}[t]{0.48\linewidth}
    Example:

    $C = 5$
  \end{minipage}
  \begin{minipage}[t]{0.48\linewidth}
    \tiny
    \begin{center}
      \begin{tabular}{ccc}
        \toprule
        Item & Weight & Profit \\
        \midrule
        1 & 3 & 2 \\
        2 & 2 & 2 \\
        3 & 2 & 3 \\
        4 & 1 & 2 \\
        \bottomrule
      \end{tabular}
    \end{center}
  \end{minipage}

  \pause

  \begin{center}
    \begin{tikzpicture}
      \node[] (N0) at (-4, 0) {$(j, w, p)$};

      \onslide<2>{\node[draw=black, thick, circle, fill=cyan, label=above:{(0,0,0)}] (N1) at (0,0) {};}
        \onslide<3->{\node[draw=black, thick, circle, label=above:{(0,0,0)}] (N1) at (0,0) {};}

      \onslide<3>{%
        \node[draw=black, thick, circle, fill=cyan, label=left:{(1,0,0)}] (N11) at (-3,-1) {};
        \node[draw=black, thick, circle, fill=cyan, label=right:{(1,3,2)}] (N12) at (3,-1) {};
        \draw [thick, draw=black, ->] (N1) -- (N11);
        \draw [thick, draw=black, ->] (N1) -- (N12);
      }
      \onslide<4->{%
        \node[draw=black, thick, circle, label=left:{(1,0,0)}] (N11) at (-3,-1) {};
        \node[draw=black, thick, circle, label=right:{(1,3,2)}] (N12) at (3,-1) {};
        \draw [thick, draw=black, ->] (N1) -- (N11);
        \draw [thick, draw=black, ->] (N1) -- (N12);
      }

      \onslide<4>{%
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,0,0)}] (N111) at (-4,-2) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,2,2)}] (N112) at (-2,-2) {};
        \draw [thick, draw=black, ->] (N11) -- (N111);
        \draw [thick, draw=black, ->] (N11) -- (N112);

        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,3,2)}] (N121) at (2,-2) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,5,4)}] (N122) at (4,-2) {};
        \draw [thick, draw=black, ->] (N12) -- (N121);
        \draw [thick, draw=black, ->] (N12) -- (N122);
      }
      \onslide<5->{%
        \node[draw=black, thick, circle, label=left:{(2,0,0)}] (N111) at (-4,-2) {};
        \node[draw=black, thick, circle, label=left:{(2,2,2)}] (N112) at (-2,-2) {};
        \draw [thick, draw=black, ->] (N11) -- (N111);
        \draw [thick, draw=black, ->] (N11) -- (N112);

        \node[draw=black, thick, circle, label=left:{(2,3,2)}] (N121) at (2,-2) {};
        \node[draw=black, thick, circle, label=left:{(2,5,4)}] (N122) at (4,-2) {};
        \draw [thick, draw=black, ->] (N12) -- (N121);
        \draw [thick, draw=black, ->] (N12) -- (N122);
      }
      \onslide<5>{%
        \node[draw=black, thick, circle, fill=cyan, label=below:{(3,0,0)}] (N1111) at (-5,-3) {};
        \node[draw=black, thick, circle, fill=cyan, label=below:{(3,2,3)}] (N1112) at (-3.5,-3) {};
        \draw [thick, draw=black, ->] (N111) -- (N1111);
        \draw [thick, draw=black, ->] (N111) -- (N1112);

        \node[draw=black, thick, circle, fill=cyan, label=below:{(3,2,2)}] (N1121) at (-2,-3) {};
        \node[draw=black, thick, circle, fill=cyan, label=below:{(3,4,5)}] (N1122) at (-0.5,-3) {};
        \draw [thick, draw=black, ->] (N112) -- (N1121);
        \draw [thick, draw=black, ->] (N112) -- (N1122);

        \node[draw=black, thick, circle, fill=cyan, label=below:{(3,3,2)}] (N1211) at (1,-3) {};
        \node[draw=black, thick, circle, fill=cyan, label=below:{(3,5,5)}] (N1212) at (3,-3) {};
        \draw [thick, draw=black, ->] (N121) -- (N1211);
        \draw [thick, draw=black, ->] (N121) -- (N1212);

        \node[draw=black, thick, circle, fill=cyan, label=below:{(3,5,4)}] (N1221) at (5,-3) {};
        \draw [thick, draw=black, ->] (N122) -- (N1221);
      }
      \onslide<6->{%
        \node[draw=black, thick, circle, label=below:{(3,0,0)}] (N1111) at (-5,-3) {};
        \node[draw=black, thick, circle, label=below:{(3,2,3)}] (N1112) at (-3.5,-3) {};
        \draw [thick, draw=black, ->] (N111) -- (N1111);
        \draw [thick, draw=black, ->] (N111) -- (N1112);

        \node[draw=black, thick, circle, label=below:{(3,2,2)}] (N1121) at (-2,-3) {};
        \node[draw=black, thick, circle, label=below:{(3,4,5)}] (N1122) at (-0.5,-3) {};
        \draw [thick, draw=black, ->] (N112) -- (N1121);
        \draw [thick, draw=black, ->] (N112) -- (N1122);

        \node[draw=black, thick, circle, label=below:{(3,3,2)}] (N1211) at (1,-3) {};
        \node[draw=black, thick, circle, label=below:{(3,5,5)}] (N1212) at (3,-3) {};
        \draw [thick, draw=black, ->] (N121) -- (N1211);
        \draw [thick, draw=black, ->] (N121) -- (N1212);

        \node[draw=black, thick, circle, label=below:{(3,5,4)}] (N1221) at (5,-3) {};
        \draw [thick, draw=black, ->] (N122) -- (N1221);
      }
      \onslide<6->{%
        \node[] (N0) at (0,-4) {\dots};
      }
    \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Breadth First Search (Knapsack Problem)}

  \begin{algorithmic}
    \Procedure{BreadthFirstSearch}{w, C}
    \State$L_0 \gets ((0, 0, 0))$
    \For {$j = 1, \dots, n$}
    \For {$(j, w, p) \in L_{j - 1}$}
    \State {$L_j \gets L_j \cup ((j, w, p)) $}
    \If {$w + w[j] \le C$}
    \State {$L_j \gets L_j \cup ((j, w + w_j, p + p_j)) $}
    \EndIf
    \EndFor
    \EndFor
    \EndProcedure{}
  \end{algorithmic}

  \begin{itemize}
    \item \pause Time complexity? \pause $O(2^n)$
    \item \pause Space complexity? \pause $O(2^n)$
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Dominance rule (Knapsack Problem)}

  We express Dynamic Programming as a dominance rule:
  Consider two nodes $n_1 = (j_1, w_1, p_1)$ and $n_2 = (j_2, w_2, p_2)$.
  If
  \begin{displaymath}
    j_1 \le j_2 \quad \text{and} \quad w_1 \le w_2 \quad \text{and} \quad p_1 \ge p_2
  \end{displaymath}
  then node $n_1$ dominates node $n_2$ and therefore node $n_2$ can be safely pruned.
\end{frame}

\begin{frame}
  \frametitle{Breadth First Search + Dominance rule: Example (Knapsack Problem)}

  \begin{minipage}[t]{0.48\linewidth}
    Example:

    $C = 5$
  \end{minipage}
  \begin{minipage}[t]{0.48\linewidth}
    \tiny
    \begin{center}
      \begin{tabular}{ccc}
        \toprule
        Item & Weight & Profit \\
        \midrule
        1 & 3 & 2 \\
        2 & 2 & 2 \\
        3 & 2 & 3 \\
        4 & 1 & 2 \\
        \bottomrule
      \end{tabular}
    \end{center}
  \end{minipage}

  \pause

  \begin{center}
    \begin{tikzpicture}
      \node[] (N0) at (-4, 0) {$(j, w, p)$};

      \onslide<2>{\node[draw=black, thick, circle, fill=cyan, label=above:{(0,0,0)}] (N1) at (0,0) {};}
        \onslide<3->{\node[draw=black, thick, circle, label=above:{(0,0,0)}] (N1) at (0,0) {};}

      \onslide<3>{%
        \node[draw=black, thick, circle, fill=cyan, label=left:{(1,0,0)}] (N11) at (-2,-1) {};
        \node[draw=black, thick, circle, fill=cyan, label=right:{(1,3,2)}] (N12) at (2,-1) {};
        \draw [thick, draw=black, ->] (N1) -- (N11);
        \draw [thick, draw=black, ->] (N1) -- (N12);
      }
      \onslide<4->{%
        \node[draw=black, thick, circle, label=left:{(1,0,0)}] (N11) at (-2,-1) {};
        \node[draw=black, thick, circle, label=right:{(1,3,2)}] (N12) at (2,-1) {};
        \draw [thick, draw=black, ->] (N1) -- (N11);
        \draw [thick, draw=black, ->] (N1) -- (N12);
      }

      \onslide<4>{%
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,0,0)}] (N111) at (-4,-2) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,2,2)}] (N112) at (-1,-2) {};
        \draw [thick, draw=black, ->] (N11) -- (N111);
        \draw [thick, draw=black, ->] (N11) -- (N112);

        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,3,2)}] (N121) at (1,-2) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,5,4)}] (N122) at (4,-2) {};
        \draw [thick, draw=black, ->] (N12) -- (N121);
        \draw [thick, draw=black, ->] (N12) -- (N122);
      }
      \onslide<5>{%
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,0,0)}] (N111) at (-4,-2) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,2,2)}] (N112) at (-1,-2) {};
        \draw [thick, draw=black, ->] (N11) -- (N111);
        \draw [thick, draw=black, ->] (N11) -- (N112);

        \node[draw=black, thick, circle, fill=orange, label=left:{(2,3,2)}] (N121) at (1,-2) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(2,5,4)}] (N122) at (4,-2) {};
        \draw [thick, draw=black, ->] (N12) -- (N121);
        \draw [thick, draw=black, ->] (N12) -- (N122);
      }
      \onslide<6->{%
        \node[draw=black, thick, circle, label=left:{(2,0,0)}] (N111) at (-4,-2) {};
        \node[draw=black, thick, circle, label=left:{(2,2,2)}] (N112) at (-1,-2) {};
        \draw [thick, draw=black, ->] (N11) -- (N111);
        \draw [thick, draw=black, ->] (N11) -- (N112);

        \node[draw=black, thick, circle, fill=orange, label=left:{(2,3,2)}] (N121) at (1,-2) {};
        \node[draw=black, thick, circle, label=left:{(2,5,4)}] (N122) at (4,-2) {};
        \draw [thick, draw=black, ->] (N12) -- (N121);
        \draw [thick, draw=black, ->] (N12) -- (N122);
      }
      \onslide<6>{%
        \node[draw=black, thick, circle, fill=cyan, label=left:{(3,0,0)}] (N1111) at (-5,-3) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(3,2,3)}] (N1112) at (-3,-3) {};
        \draw [thick, draw=black, ->] (N111) -- (N1111);
        \draw [thick, draw=black, ->] (N111) -- (N1112);

        \node[draw=black, thick, circle, fill=cyan, label=left:{(3,2,2)}] (N1121) at (-1,-3) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(3,4,5)}] (N1122) at (1,-3) {};
        \draw [thick, draw=black, ->] (N112) -- (N1121);
        \draw [thick, draw=black, ->] (N112) -- (N1122);

        \node[draw=black, thick, circle, fill=cyan, label=left:{(3,5,4)}] (N1221) at (4,-3) {};
        \draw [thick, draw=black, ->] (N122) -- (N1221);
      }
      \onslide<7>{%
        \node[draw=black, thick, circle, fill=cyan, label=left:{(3,0,0)}] (N1111) at (-5,-3) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(3,2,3)}] (N1112) at (-3,-3) {};
        \draw [thick, draw=black, ->] (N111) -- (N1111);
        \draw [thick, draw=black, ->] (N111) -- (N1112);

        \node[draw=black, thick, circle, fill=orange, label=left:{(3,2,2)}] (N1121) at (-1,-3) {};
        \node[draw=black, thick, circle, fill=cyan, label=left:{(3,4,5)}] (N1122) at (1,-3) {};
        \draw [thick, draw=black, ->] (N112) -- (N1121);
        \draw [thick, draw=black, ->] (N112) -- (N1122);

        \node[draw=black, thick, circle, fill=orange, label=left:{(3,5,4)}] (N1221) at (4,-3) {};
        \draw [thick, draw=black, ->] (N122) -- (N1221);
      }
      \onslide<8->{%
        \node[draw=black, thick, circle, label=left:{(3,0,0)}] (N1111) at (-5,-3) {};
        \node[draw=black, thick, circle, label=left:{(3,2,3)}] (N1112) at (-3,-3) {};
        \draw [thick, draw=black, ->] (N111) -- (N1111);
        \draw [thick, draw=black, ->] (N111) -- (N1112);

        \node[draw=black, thick, circle, fill=orange, label=left:{(3,2,2)}] (N1121) at (-1,-3) {};
        \node[draw=black, thick, circle, label=left:{(3,4,5)}] (N1122) at (1,-3) {};
        \draw [thick, draw=black, ->] (N112) -- (N1121);
        \draw [thick, draw=black, ->] (N112) -- (N1122);

        \node[draw=black, thick, circle, fill=orange, label=left:{(3,5,4)}] (N1221) at (4,-3) {};
        \draw [thick, draw=black, ->] (N122) -- (N1221);
      }
      \onslide<8>{%
        \node[draw=black, thick, circle, fill=cyan, label=below:{(4,0,0)}] (N11111) at (-6,-4) {};
        \node[draw=black, thick, circle, fill=cyan, label=below:{(4,1,2)}] (N11112) at (-4.5,-4) {};
        \draw [thick, draw=black, ->] (N1111) -- (N11111);
        \draw [thick, draw=black, ->] (N1111) -- (N11112);

        \node[draw=black, thick, circle, fill=cyan, label=below:{(4,2,3)}] (N11121) at (-3,-4) {};
        \node[draw=black, thick, circle, fill=cyan, label=below:{(4,3,5)}] (N11122) at (-1.5,-4) {};
        \draw [thick, draw=black, ->] (N1112) -- (N11121);
        \draw [thick, draw=black, ->] (N1112) -- (N11122);

        \node[draw=black, thick, circle, fill=cyan, label=below:{(4,4,5)}] (N11221) at (0,-4) {};
        \node[draw=black, thick, circle, fill=cyan, label=below:{(4,5,7)}] (N11222) at (2,-4) {};
        \draw [thick, draw=black, ->] (N1122) -- (N11221);
        \draw [thick, draw=black, ->] (N1122) -- (N11222);
      }
    \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Breadth First Search + Dominance rule (Knapsack Problem)}

  \begin{algorithmic}
    \Procedure{BreadthFirstSearch}{w, C}
    \State $L_0 \gets ((0, 0, 0))$
    \For {$j = 1, \dots, n$}
    \For {$(j, w, p) \in L_{j - 1}$}
    \State $L_j \gets L_j \cup ((j, w, p)) $
    \If {$w + w[j] \le C$}
    \State $L_j \gets L_j \cup ((j, w + w_j, p + p_j))$
    \EndIf
    \EndFor
    \State \alert{Remove all dominated nodes from $L_j$}
    \EndFor
    \EndProcedure
  \end{algorithmic}

  \begin{itemize}
    \item \pause Time complexity? \pause The complexity depends on the complexity of applying the dominance rule!

  In this case, it is possible to implement it in $O(C)$ and keep the complexity of the whole algorithm to $O(nC)$ as for the iterative implementation. But in some cases, the complexity might increase.
  \end{itemize}
\end{frame}

\section{Conclusion}

\begin{frame}
  \frametitle{Conclusion}

  \begin{itemize}
    \item Dynamic Programming: solving a problem recursively and storing the results of the subproblems to avoid recomputing them multiple times.
    \item It requires the problem to have a specific structure. It might not be applicable to all problems.
    \item Sometimes, a bit of work is needed in order to exhibit this structure
    \item Multiple possible implementations with their advantages and drawbacks (recursive, iterative, tree search)
    \item \say{Knapsack Problems} (Kellerer, Pferschy et Pisinger, 2004)
    \item Dynamic Programming through path problems: \url{https://moodle.caseine.org/mod/page/view.php?id=30723}
  \end{itemize}

\end{frame}

\maketitle

\end{document}

2021-2022 - M2 ORCO - Advanced OR/1 - Dynamic Programming/slides/slides.tex


%% Template for MLP Coursework 1 / 16 October 2017

%% Based on  LaTeX template for ICML 2017 - example_paper.tex at
%%  https://2017.icml.cc/Conferences/2017/StyleAuthorInstructions

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{amssymb,amsmath}
\usepackage{txfonts}
\usepackage{microtype}

% For figures
\usepackage{graphicx}
\usepackage{subfigure}

% For citations
\usepackage{natbib}

% For algorithms
\usepackage{algorithm}
\usepackage{algorithmic}

% the hyperref package is used to produce hyperlinks in the
% resulting PDF.  If this breaks your system, please commend out the
% following usepackage line and replace \usepackage{mlp2017} with
% \usepackage[nohyperref]{mlp2017} below.
\usepackage{hyperref}
\usepackage{url}
\urlstyle{same}

% Packages hyperref and algorithmic misbehave sometimes.  We can fix
% this with the following command.
\newcommand{\theHalgorithm}{\arabic{algorithm}}

% Set up MLP coursework style (based on ICML style)
\usepackage{mlp2017}
\mlptitlerunning{MLP Coursework 1 (\studentNumber)}
\bibliographystyle{icml2017}

\DeclareMathOperator{\softmax}{softmax}
\DeclareMathOperator{\sigmoid}{sigmoid}
\DeclareMathOperator{\sgn}{sgn}
\DeclareMathOperator{\relu}{relu}
\DeclareMathOperator{\lrelu}{lrelu}
\DeclareMathOperator{\elu}{elu}
\DeclareMathOperator{\selu}{selu}
\DeclareMathOperator{\maxout}{maxout}

%% You probably do not need to change anything above this comment

%% REPLACE this with your student number
\def\studentNumber{s1754321}

\begin{document}

\twocolumn[
\mlptitle{MLP Coursework 1: Activation Functions}

\centerline{\studentNumber}

\vskip 7mm
]

\begin{abstract}
The abstract should be 100--200 words long,  providing a concise summary of the contents of your report.
\end{abstract}

\section{Introduction}
\label{sec:intro}
This document provides a template for the MLP coursework 1 report.  In particular, it structures the document into five sections  (plus an abstract and the references) -- you should keep to this structure for your report.  If you want to use subsections within a section that is fine, but please do not use any deeper structuring.  In this template the text in each section will include an outline of what you should include in each section, along with some practical LaTeX examples (for example figures, tables, algorithms).  Your document should be no longer than \textbf{six pages},  with an additional page allowed for references.

The introduction should place your work in context, giving the overall motivation for the work, and clearly outlining the research questions you have explored -- in this case comparison of the behaviour of the different activation functions,  experimental investigation of the impact of the depth of the network with respect to accuracy, and experimental investigation of different approaches to weight initialisation.  This section should also include a concise description of the MNIST task and  data -- be precise: for example state the size of the training and validation sets.

\section{Activation functions}
\label{sec:actfn}
This section should cover the theoretical methodology -- in this case you should present the four activation functions: ReLU, Leaky ReLU, ELU, and SELU.  I didn't do it in this document, but the first time you use an acronym you should say what it stands for, for example Restricted Linear Unit (ReLU).  You should use equations to concisely describe each activation function.  For example, ReLU:
\begin{equation}
  \relu(x) = \max(0, x) ,
\end{equation}
which has the gradient:
\begin{equation}
  \frac{d}{dx} \relu(x) =
     \begin{cases}
      0      & \quad \text{if } x \leq  0 \\
      1       & \quad \text{if } x > 0 .
    \end{cases}
\end{equation}
The \LaTeX for the derivatives is slightly more complicated.  We provided definitions near the top of the file (the part before \verb+\begin{document}+) for \verb+\relu+, \verb+\lrelu+, \verb+\elu+, and \verb+\selu+.  There is no need to discuss the unit tests for these activation functions in this report.

It is probably not needed in this report, but if you would like to include an algorithm in your report, please use the \verb+algorithm+ and \verb+algorithmic+ environments to format pseudocode (for instance, Algorithm~\ref{alg:example}). These require the corresponding style files, \verb+algorithm.sty+ and \verb+algorithmic.sty+ which are supplied with this package.

\begin{algorithm}[ht]
\begin{algorithmic}
   \STATE {\bfseries Input:} data $x_i$, size $m$
   \REPEAT
   \STATE Initialize $noChange = true$.
   \FOR{$i=1$ {\bfseries to} $m-1$}
   \IF{$x_i > x_{i+1}$}
   \STATE Swap $x_i$ and $x_{i+1}$
   \STATE $noChange = false$
   \ENDIF
   \ENDFOR
   \UNTIL{$noChange$ is $true$}
\end{algorithmic}
  \caption{Bubble Sort}
  \label{alg:example}
\end{algorithm}

\section{Experimental comparison of activation functions}
\label{sec:actexpts}
In this section you should present the results and discussion of your experiments comparing networks using the different activation functions on the MNIST task.  As explained in the coursework document, you should use 2 hidden layers with 100 hidden units per layer for these experiments.  You can compare the learning curves (error vs epoch) for training and/or validation, and the validation set accuracies.

Your experimental sections should include graphs (for instance, figure~\ref{fig:sample-graph}) and/or tables (for instance, table~\ref{tab:sample-table})\footnote{These examples were taken from the ICML template paper.}, using the \verb+figure+ and \verb+table+ environments, in which you use \verb+\includegraphics+ to include an image (pdf, png, or jpg formats).  Please export graphs as
\href{https://en.wikipedia.org/wiki/Vector_graphics}{vector graphics}
rather than \href{https://en.wikipedia.org/wiki/Raster_graphics}{raster
files} as this will make sure all detail in the plot is visible.
Matplotlib supports saving high quality figures in a wide range of
common image formats using the
\href{http://matplotlib.org/api/pyplot_api.html\#matplotlib.pyplot.savefig}{\texttt{savefig}}
function. \textbf{You should use \texttt{savefig} rather than copying
the screen-resolution raster images outputted in the notebook.} An
example of using \texttt{savefig} to save a figure as a PDF file (which
can be included as graphics in a \LaTeX document is given in the coursework document.

If you need a figure or table to stretch across two columns use the \verb+figure*+ or \verb+table*+ environment instead of the \verb+figure+ or \verb+table+ environment.  Use the \verb+subfigure+ environment if you want to include multiple graphics in a single figure.

\begin{figure}[tb]
\vskip 5mm
\begin{center}
\centerline{\includegraphics[width=\columnwidth]{icml_numpapers}}
\caption{Historical locations and number of accepted papers for International
  Machine Learning Conferences (ICML 1993 -- ICML 2008) and
  International Workshops on Machine Learning (ML 1988 -- ML
  1992). At the time this figure was produced, the number of
  accepted papers for ICML 2008 was unknown and instead estimated.}
\label{fig:sample-graph}
\end{center}
\vskip -5mm
\end{figure}

\begin{table}[tb]
\vskip 3mm
\begin{center}
\begin{small}
\begin{sc}
\begin{tabular}{lcccr}
\hline
\abovespace\belowspace
Data set & Naive & Flexible & Better? \\
\hline
\abovespace
Breast    & 95.9$\pm$ 0.2& 96.7$\pm$ 0.2& $\surd$ \\
Cleveland & 83.3$\pm$ 0.6& 80.0$\pm$ 0.6& $\times$\\
Glass2    & 61.9$\pm$ 1.4& 83.8$\pm$ 0.7& $\surd$ \\
Credit    & 74.8$\pm$ 0.5& 78.3$\pm$ 0.6&         \\
Horse     & 73.3$\pm$ 0.9& 69.7$\pm$ 1.0& $\times$\\
Meta      & 67.1$\pm$ 0.6& 76.5$\pm$ 0.5& $\surd$ \\
Pima      & 75.1$\pm$ 0.6& 73.9$\pm$ 0.5&         \\
\belowspace
Vehicle   & 44.9$\pm$ 0.6& 61.5$\pm$ 0.4& $\surd$ \\
\hline
\end{tabular}
\end{sc}
\end{small}
\caption{Classification accuracies for naive Bayes and flexible
Bayes on various data sets.}
\label{tab:sample-table}
\end{center}
\vskip -3mm
\end{table}

\section{Deep neural network experiments}
\label{sec:dnnexpts}
This section should report on your experiments on deeper networks for MNIST.  The two sets of experiments are to explore the impact of the depth of the network (number of hidden layers), and a comparison of different approaches to weight initialisation.

In this section, and in the previous section, you should present your experimental results clearly and concisely, followed by an interpretation and discussion of results. You need to present your results in a way that makes it easy for a reader to understand what they mean. You should facilitate comparisons either using graphs with multiple curves or (if appropriate, e.g. for accuracies) a results table. You need to avoid having too many figures, poorly labelled graphs, and graphs which should be comparable but which use different axis scales. A good presentation will enable the reader to compare trends in the same graph -- each graph should summarise the results relating to a particular research (sub)question.

Your discussion should interpret the results, both in terms of summarising the outcomes of a particular experiment, and attempting to relate to the underlying models. A good report would have some analysis, resulting in an understanding of why particular results are observed, perhaps with reference to the literature. Use bibtex to organise your references -- in this case the references are in the file \verb+example-refs.bib+.  Here is a an example reference \citep{langley00}.

\section{Conclusions}
\label{sec:concl}
You should draw conclusions from the experiments, related to the research questions outlined in the introduction (section~\ref{sec:intro}). You should state the conclusions clearly and concisely. It is good if the conclusion from one experiment influenced what you did in later experiments -- your aim is to learn from your experiments. Extra credit if you relate your findings to what has been reported in the literature.

A good conclusions section would also include a further work discussion, building on work done so far, and referencing the literature where appropriate.

\bibliography{example-refs}

\end{document}

% This document was modified from the file originally made available by
% Pat Langley and Andrea Danyluk for ICML-2K. This version was
% created by Lise Getoor and Tobias Scheffer, it was slightly modified
% from the 2010 version by Thorsten Joachims & Johannes Fuernkranz,
% slightly modified from the 2009 version by Kiri Wagstaff and
% Sam Roweis's 2008 version, which is slightly modified from
% Prasad Tadepalli's 2007 version which is a lightly
% changed version of the previous year's version by Andrew Moore,
% which was in turn edited from those of Kristian Kersting and
% Codrina Lauth. Alex Smola contributed to the algorithmic style files.


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

\section*{1 October 2019}

Figure 22.8 in some PDF: run of adiabatic, radiation gradients vs \(\log T \).

We compute \(\nabla_{\text{ad}}\) and \(\nabla_{\text{rad}}\) and see whether the region is convective or radiative.

We can move from the Eulerian and Lagrangian formalisms using the continuity equation. In the Eulerian formalism:

\begin{equation}
  m(r) = \int_0^\infty 4 \pi r^2 \rho(x) \dd{x} \,.
\end{equation}

We define the mean molecular weight \(\mu \) with:
%
\begin{equation}
  \mu^{-1} = \sum _{i}  (1 + \nu_e (i)) \frac{X_i}{A_i}
\,,
\end{equation}
where \(\nu_{e} (i)\) is the number of free electrons coming from element \(i\), \(X_{i}\) is the abundance by mass fraction of the element \(i\), and \(A_{i}\) is its mass number.

The variables \(X\), \(Y\) and \(Z\) represent the abundances of \(\ce{H}\), \(\ce{He}\) and metals, and satisfy \(X+Y+Z=1\).

We may need to know the metal mixture inside \(Z\), but often we can approximate it as the Sun's distribution.

The time evolution of the various elements' fractions is given by
%
\begin{equation}
  \pdv{X_i}{t} = \frac{m_i}{\rho} \sum _j \qty( r_{ji} - r_{ij})
\end{equation}

\subsubsection{Classification of stars}

\begin{enumerate}
  \item Low mass stars have between \(0.8\) and \(2\) solar masses. They develop an electron-degenerate core after their time on the Main Sequence.
  \item Intermediate mass stars have masses between \(2\) and \(8\) \(M_{\odot}\). They start burning helium in a non-degenerate core, then they develop a degenerate \ce{C-O} core.
  \item Massive stars have masses of over \(8 M_{\odot}\). They start burning carbon in a non-degenerate core.
\end{enumerate}

% \todo[inline]{What does \emph{degenerate} mean in this context?}

\todo[inline]{What are Hayashi lines?}

% Today we will look at typical time-scales, the period-mean density relation, the energy equation and perturbation theory w/ linearization.

% No derivations of the equations in the exam.

\subsection{Time-scales}

The \emph{free fall} time scale is:

\begin{equation}
  \tau_{\text{dyn}} \sim \qty(\frac{R}{g} )^{1/2} = \qty(\frac{R^3}{GM} )^{1/2}\,.
\end{equation}

It is associated with pulsation. It is calculated using the travel time of a mass in free fall across the stellar radius accelerated by constant acceleration equal to surface acceleration.

We note that \(\tau_{\text{dyn}} \propto \overline{\rho} ^{-1/2} \), since \(\overline{\rho} \propto M / R^3\). For the Sun it is about \SI{1.6e3}{s}.

The \emph{thermal} time scale is the relaxation time of deviations from thermal equilibrium:
%
\begin{equation}
  \tau_{\text{th}} \sim E_{\text{th}} / L\,.
\end{equation}

It is calculated as the time required for a star to irradiate all its energy.

\begin{proof}
We use the virial theorem to estimate the thermal (so, local kinetic) energy \(T\): we know that \(T = - V/2\) where \(V\) is the total potential energy of the star. \(V\) can be computed as
%
\begin{align}
V = - \int \frac{Gm}{r(m)} \dd{m}
\,,
\end{align}
%
and since \(r(m) \approx \sqrt[3]{3m / 4 \pi \rho }\) if the density is constant, we get
%
\begin{subequations}
\begin{align}
V &= - \int Gm \sqrt{\frac{4 \pi \rho }{3}} m^{-1/3} \dd{m}  \\
&=- \frac{3}{5} G \sqrt[3]{\frac{4 \pi \rho }{3}} M^{5/3} = -\frac{3}{5} \frac{GM^2}{R} \sim - \frac{GM^2}{R}
\,.
\end{align}
\end{subequations}

Therefore, \(T \sim - GM^2 / 2 R\).
\end{proof}

Typically, \(\tau_{\text{th}} \sim G M^2 /(LR) \sim 10^7 M^2/(LR) \) years.
For the Sun we have \(\tau_{\text{th}} \sim \SI{5e14}{s}\).
It is much larger than the dynamic time scale.

The \emph{nuclear} time scale is even longer: it is calculated using the efficiency of nuclear fusion of \ce{H} to \ce{He}, which is about \(\epsilon \sim \num{.7} \% \), and the fraction of hydrogen in the star, which is about \(M_H = \num{10} \% \times M_{\odot} \). Using these numbers, we get for the Sun:
%
\begin{align}
\tau_{\text{nuc}} = \frac{\epsilon c^2  M_H}{L_{\odot}} \approx \SI{3e17}{s}
\,,
\end{align}
%

This allows us to say that oscillations will not be heavily affected by thermal conduction, and even less by nuclear processes: the pulsations will be almost \emph{adiabatic}.

The best candidate for these oscillations are \emph{sound waves}: is the adiabatic speed of sound roughly right?

The speed of sound is given by:
%
\begin{equation}
  v_s^2 = \pdv{P}{\rho } = \frac{P}{\rho} \pdv{\log P }{\log
  \rho } = \Gamma_1 \frac{P}{\rho}\,,
\end{equation}
%
where \(\Gamma_1 \) is the adiabatic exponent, such that \(P = \rho^{\Gamma_1 }\).

If the gas follows the perfect equation of state

\begin{equation}
  \frac{P}{\rho} = \frac{k_B}{m_H} \frac{T}{\mu}  \,,
\end{equation}
%
where \(\mu \) is the average mass of a gas particle in atomic mass units (units of \(m_H\)), so we have \(\rho = \mu N m_H / V\).
Substituting this in, we get

\begin{equation}
  v_s^2 = \frac{\Gamma_1 k_B T}{m_H \mu}  \,.
\end{equation}

The mean molecular weight is calculated taking account of the fact that the electrons appear in the count of particles, but we can neglect their mass: therefore, we get a \(Z+1\) in the formula, since there are \(Z\) electrons and one nucleus. So the value becomes
%
\begin{align}
\mu = \qty(\sum_{j} \frac{X_j}{A_j} (1+Z_j))^{-1} \approx \qty(2X + 3Y/4 + Z/2)^{-1} \sim 0.6
\,,
\end{align}
%
where we approximated that for each metal \((1+Z) / A \sim \frac[i]{1}{2} \) and we used the values of the mass fractions of the Sun: \(X =\num{.7381}\), \(Y = \num{.2485}\) and \(Z = \num{.0134}\).\footnote{See \url{https://arxiv.org/abs/0909.0948}}

Typical values for the other parameters are \(\Gamma_1 = 5/3\), and \(T_{\text{He}} \sim \SI{4.5e4}{K} \).

So we get \(v_s \sim \SI{32.2}{km/s}\).

The timescale for a perturbation to go from one side of the star to the other is \(\Pi \sim 2R/v_s \sim \SI{22}{d}\), while the observed value is \(\Pi_{\text{obs}} = \SI{5.336}{d} \), so in terms of orders of magnitude it works.
We could say that from a more thorough analysis we would see that the vibration does not actually go all the way from an edge of the star to the other.

We can use the equation for the sound speed in the virial theorem, which for a star relates the gravitational potential energy \(\Omega \) to the integral of the pressure:
%
\begin{align}
\Omega  = - 3 \int P \dd{V} = - 3 \int \frac{P}{\rho } \dd{m}
\,,
\end{align}
%
and substitute in \(P/\rho = v_s^2 / \Gamma_1 \):
%
\begin{equation}
\Omega = - 3 \frac{\int _{M}  v_s^2 / \Gamma_1 \dd{m}}{\int_M \dd{m}}M = -3 \expval{\frac{v_s^2}{\Gamma_1}} M
\,,
\marginnote{We multiply and divide by \(M = \int \dd{m}\)}
\end{equation}
%
where the brackets denote an average weighted by the mass distribution.

If \(\Gamma_1 \) and \(v_s\) are independent, we can compute their averages separately: we approximate and do this. Then, we can substitute in our expression for the average \(\expval{v_s^2} \sim \expval{v_s}^2\) into \(\Pi \sim 2R / v_s\): we get
%
\begin{align}
\expval{v_s}^2 = - \frac{\Omega \Gamma_1 }{3M}
\,,
\end{align}
%
so
%
\begin{align}
\Pi \sim \frac{2R}{\expval{v_s}} = \sqrt{\frac{-4R^2 \times 3M }{\Omega \Gamma_1 }}
\,,
\end{align}
%

This means we are writing the period \(\Pi  \) with respect to something resembling the moment of inertia, \(I = \int_r r^2 \dd{m}(r) \sim R^2M\):

\begin{equation}
  \Pi \sim \qty(\frac{I_{\text{osc}}}{-\Omega} )^{1/2}
\end{equation}

This is further evidence that we are dealing with a dynamical phenomenon.

We can refine our model: the speed of sound changes throughout the interior of the star.
We compute the period as the travel time of sound waves throughout the diameter:
%
\begin{equation}
  \Pi = 2 \int _{0}   ^{R} \dd{t(r)} = 2 \int_0^R \frac{\dd{r} }{\sqrt{\Gamma_1(r) P(r) / \rho(r)}}\,,
\end{equation}
%
since \(\dd{t} = \dd{r} / v_s\); the factor of 2 comes from the fact that the sound wave must go from one side of the star to the other.

We also integrate the hydrostatic balance equation, which reads
%
\begin{align}
\dv{P}{r} = - \frac{Gm \rho }{r^2}
= - \frac{G \rho^2 4 \pi r}{3}
\,,
\end{align}
%
with respect to \(r\): then we get
%
\begin{align}
P(r) - \underbrace{P(R)}_{=0} = \int_{R}^{r} \dv{P}{r} \dd{r}  = \frac{2 \pi \rho^2 G}{3} \qty(R^2 - r^2)
\,,
\end{align}
%
which we can plug into our formula for the period to find:
%
\begin{subequations}
\begin{align}
\Pi &= 2 \int \dd{r} \qty(\Gamma_1 \frac{P}{\rho })^{-1/2}  \\
&= 2 \int \dd{r} \qty(\Gamma_1 \frac{2 \pi \rho G}{3} \qty(R^2 - r^2))^{-1/2}  \\
&= \sqrt{\frac{6}{\Gamma_1 \pi G \overline{\rho}}} \underbrace{\int \frac{ \dd{r}}{\sqrt{R^2- r^2}}}_{\pi /2}  \\
&= \sqrt{\frac{3 \pi }{2\Gamma_1 G \overline{\rho} }}
\,,
\end{align}
\end{subequations}
%
which confirms Ritter's relation \(\Pi \propto \overline{\rho}^{-1/2} \).
Since the product \(\Pi \overline{\rho}^{1/2}\) is approximately constant, we give it the name
%
\begin{align}
\mathcal{Q} = \Pi \sqrt{\overline{\rho}} \approx
\sqrt{\frac{3 \pi }{2 \Gamma_1 G}}
\,.
\end{align}

Ritter's relation is consistent with the statement that the period of the oscillations is of the order of the dynamical characteristic time of the star, since
%
\begin{align}
\tau _{\text{dyn}} = \sqrt{\frac{R^3}{GM}} \qquad \text{while} \qquad \overline{\rho}^{-1/2} \approx \qty(\frac{M}{4 \pi R^3 / 3})^{-1/2} = \sqrt{\frac{4 \pi R^3}{3M}}
\,.
\end{align}

This gives an estimate of \(\Pi \sim \SI{8.5}{d}\) for a \(\delta \)-Cephei star, which we have to compare to the observed period of \(\Pi _{\text{obs}} = \SI{5.466}{d}\). We have definitely improved our estimate.
The prediction of this model is that \emph{dense stars pulstate faster}.

This works for acoustic modes, such as those found in \(\delta \)-Cephei, \(o\)-Ceti and SX-Phe stars, but if we consider non-radial g-modes such as those found in variables of type ZZ Ceti it stops working. Then, the estimate given by this model can be off by three orders of magnitude.

\subsection{The energy equations}

\end{document}


\documentclass[journal,hidelinks]{IEEEtran}
\usepackage[utf8]{inputenc}
\usepackage[
  pdftitle={Assignment \#1},
  pdfauthor={Andrei Purcarus},
  pdfsubject={ECSE-534 -- Analog Microelectronics}
]{hyperref}
\usepackage{graphicx}
\usepackage[all]{hypcap}
\usepackage{cleveref}
\usepackage{indentfirst}
\usepackage[per-mode=symbol]{siunitx}

\title{ECSE-534 \\ Analog Microelectronics \\ Assignment \#1}
\author{Andrei~Purcarus,~260631911,~\IEEEmembership{McGill~University}}

\begin{document}
\sloppy

\maketitle

\begin{abstract}

A CMOS amplifier was analyzed using Cadence and its performance was characterized using DC, small signal AC, and transient analysis. The amplifier was found to meet its specifications, with unity gain stability, a DC gain of $\SI{54}{\deci\bel}$, and a unity gain bandwidth of $\SI{425}{\mega\hertz}$. In addition, the amplifier was found to have a weak phase margin of $\SI{0.8}{\degree}$, suggesting an area of possible improvement.

\end{abstract}

\section{Introduction}

\IEEEPARstart{T}{his} report follows the analysis of a CMOS amplifier that has been designed for unity gain stability in a 0.13 micron process from IBM. The amplifier in question is shown in \Cref{fig:opamp}. Note that all NFET transistors have their body connected to $V_{SS}$ and all PFET transistors have their body connected to $V_{DD}$.

\section{Bias Resistance}

Before the analysis, we must first set the bias resistance $R_{bias}$ such that the drain current in $M_5$ is between $\SI{100}{\micro\ampere}$ and $\SI{150}{\micro\ampere}$. To this end, we sweep the resistance value and simulate the drain current in $M_5$. The results are shown in \Cref{fig:rsweep}. Using this graph, we choose a bias resistance $R_{bias} = \SI{100}{\kilo\ohm}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{rsweep.eps}
  \caption{Drain current in transistor $M_5$ as a function of $R_{bias}$.}
  \label{fig:rsweep}
\end{figure}

\section{DC Analysis}

Next, we measure the DC properties of the amplifier using the setup shown in \Cref{fig:dc_setup}, where the $\SI{1.2}{\volt}$ power supply has been omitted for clarity.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{dc_setup.pdf}
  \caption{A schematic of the setup used for measuring the DC and common mode properties of the amplifier.}
  \label{fig:dc_setup}
\end{figure}

We first sweep the common mode voltage $V_{cm}$ over the entire supply range and measure the output voltage, with $V_d = \SI{0}{\volt}$. The results are shown in \Cref{fig:dc_cm}. We see that the built-in voltage offset causes saturation regardless of common mode voltage.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{dc_cm.eps}
  \caption{DC common mode transfer characteristic of the operational amplifier.}
  \label{fig:dc_cm}
\end{figure}

Then, we sweep the differential voltage $V_d$ from $\SI{-10}{\milli\volt}$ to $\SI{10}{\milli\volt}$, with $V_{cm} = \SI{0.6}{\volt}$ for proper biasing. The results are shown in \Cref{fig:dc_dm}. From this figure, we find that $V_{os} = \SI{-1.4}{\milli\volt}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{dc_dm.eps}
  \caption{DC differential transfer characteristic of the operational amplifier.}
  \label{fig:dc_dm}
\end{figure}

To find the input and output voltage ranges, we plot the incremental voltage gain in \Cref{fig:dc_dm_gain}. We then choose the voltage range as being the range in which the incremental gain is at most $\SI{3}{\deci\bel}$ down from the peak value. Thus, we get an input voltage range of $\SI{-2.3}{\milli\volt}$ to $\SI{-0.5}{\milli\volt}$ and an output voltage range of $\SI{0.18}{\volt}$ to $\SI{1.0}{\volt}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{dc_dm_gain.eps}
  \caption{DC incremental differential gain of the operational amplifier.}
  \label{fig:dc_dm_gain}
\end{figure}

\section{AC Analysis}

Next, we measure the AC properties of the amplifier. First, we measure the common mode frequency response using the setup in \Cref{fig:dc_setup}, keeping the DC component of $V_{cm} = \SI{0.6}{\volt}$ and setting $V_d = \SI{0}{\volt}$. The results are shown in \Cref{fig:ac_cm}. From this figure, we can see that the gain at low frequencies is $\SI{-24}{\deci\bel}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{ac_cm.eps}
  \caption{AC common mode frequency response of the operational amplifier.}
  \label{fig:ac_cm}
\end{figure}

Then, we measure the differential mode frequency response using the setup in \Cref{fig:ac_setup}, where the $\SI{1.2}{\volt}$ power supply has been omitted for clarity. In this setup, we have connected the output to the negative input at DC with a large inductor, ensuring bias stability. We then inject the signal through a large capacitor. These components are chosen so large that they do not affect the results of the AC analysis. $V_{bias}$ is set to $\SI{0.6}{\volt}$ for proper biasing.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{ac_setup.pdf}
  \caption{A schematic of the setup used for measuring the AC properties of the amplifier.}
  \label{fig:ac_setup}
\end{figure}

The results of this analysis are shown in \Cref{fig:ac_dm}. From this graph, we can see that the low frequency gain is $\SI{54}{\deci\bel}$, the unity gain bandwidth is $\SI{425}{\mega\hertz}$, and the phase margin is $\SI{0.8}{\degree}$. We can also see that the amplifier has a pole at $\SI{320}{\kilo\hertz}$, a zero at around $\SI{100}{\mega\hertz}$, and two poles around $\SI{425}{\mega\hertz}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{ac_dm.eps}
  \caption{AC differential frequency response of the operational amplifier.}
  \label{fig:ac_dm}
\end{figure}

Next, we measure the AC noise of the amplifier using the same setup. Using $V_{ac}$ as our noise source, we get the output referred spectral noise density shown in \Cref{fig:ac_noise}. The noise was measured from $\SI{100}{\hertz}$ to $\SI{10}{\mega\hertz}$, with the RMS value calculated to be $\SI{6.3}{\milli\volt}$ over this bandwidth. We note that the noise is dominated by flicker noise.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{ac_noise.eps}
  \caption{Output referred AC noise of the operational amplifier.}
  \label{fig:ac_noise}
\end{figure}

\section{Transient Analysis}

Finally, we measure the closed loop transient behaviour of the amplifier using the setup shown in \Cref{fig:tran_setup}, where the $\SI{1.2}{\volt}$ power supply has been omitted for clarity.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{tran_setup.pdf}
  \caption{A schematic of the setup used for measuring the closed loop transient properties of the amplifier.}
  \label{fig:tran_setup}
\end{figure}

First, we input a fast $\SI{100}{\milli\volt}$ pulse to the circuit, centered around a DC bias point of $\SI{0.6}{\volt}$. The resulting output is shown in \Cref{fig:tran_step}. From this graph, we can measure the slew rate as $\SI{64}{\volt\per\micro\second}$, the $\SI{10}{\percent}$ to $\SI{90}{\percent}$ rise time as $\SI{1.25}{\nano\second}$, and the $\SI{5}{\percent}$ settling time as $\SI{26}{\nano\second}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{tran_step.eps}
  \caption{Transient step response of the operational amplifier.}
  \label{fig:tran_step}
\end{figure}

Next, we measure clipping by using a large $\SI{1}{\kilo\hertz}$ sine wave as input to the circuit. The resulting output is shown in \Cref{fig:tran_clip}. From this, we find that the maximum input and output voltage ranges are both $\SI{0}{\volt}$ to $\SI{1.19}{\volt}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{tran_clip.eps}
  \caption{Transient response of the operational amplifier to a large $\SI{1}{\kilo\hertz}$ sine wave input.}
  \label{fig:tran_clip}
\end{figure}

Finally, we measure both common mode and differential mode distortion using the setup in \Cref{fig:dc_setup}. For this purpose, we add a DC offset equal to $V_{os}$ in series with $E_+$ to ensure adequate biasing. We first set the differential mode voltage $V_d = \SI{0}{\volt}$, and inject a common mode, $\SI{1}{\kilo\hertz}$, $\SI{100}{\milli\volt}$ amplitude sine wave on top of a $\SI{0.6}{\volt}$ DC offset. The resulting output is shown in \Cref{fig:tran_dist_cm}, and the total harmonic distortion was calculated to be $\SI{39.58}{\percent}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{tran_dist_cm.eps}
  \caption{Transient response of the operational amplifier to a $\SI{1}{\kilo\hertz}$, $\SI{100}{\milli\volt}$ amplitude common mode sine wave.}
  \label{fig:tran_dist_cm}
\end{figure}

Then, we fix the common mode voltage $V_{cm}$ at $\SI{0.6}{\volt}$, and set the differential voltage to a $\SI{1}{\kilo\hertz}$, $\SI{1.2}{\milli\volt}$ amplitude sine wave, which was chosen to get an output swing close to the limit. The resulting output is shown in \Cref{fig:tran_dist_dm}, and the total harmonic distortion was calculated to be $\SI{6.04}{\percent}$.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{tran_dist_dm.eps}
  \caption{Transient response of the operational amplifier to a $\SI{1}{\kilo\hertz}$, $\SI{1.2}{\milli\volt}$ amplitude differential sine wave.}
  \label{fig:tran_dist_dm}
\end{figure}

\begin{figure*}[!htb]
  \centering
  \includegraphics[width=\textwidth]{opamp.pdf}
  \caption{Schematic of the operational amplifier.}
  \label{fig:opamp}
\end{figure*}

\section{Conclusions}

In conclusion, the operational amplifier shown in \Cref{fig:opamp} was analyzed and its performance characterized using DC, small signal AC, and transient analysis. The amplifier performs its tasks adequately, with its main flaws being a low phase margin of $\SI{0.8}{\degree}$, and a relatively low gain of $\SI{54}{\deci\bel}$. However, it compensates for this with a high unity gain bandwidth of $\SI{425}{\mega\hertz}$.

\bibliographystyle{IEEEtran}
\bibliography{IEEEabrv,references}

\end{document}