% =====================================
% Purpose: Create a Robert Bringhurst style thesis paper using the %
% classicthesis package and some custom enhancements - this is % the default
% template for most of my documents
% =====================================

% =====================================
% Document Class and main packages
% =====================================

\documentclass[10pt,a4paper, hidelinks]{article} % KOMA-Script article scrartcl
\usepackage[nochapters, pdfspacing, drafting]{classicthesis} % nochapters %drafting (puts date/time at bottom) beramono (changed mono spaced font)

% =====================================
% Packages in Use
% =====================================

%Math Packages
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{nicefrac}  % For typsetting inline fractions
\usepackage{mathtools} % For substack and mathclap (underbrace helper commands)

%% Typography enhancements
\usepackage{microtype} % For awesome typographical improvements
\usepackage{booktabs}  % Pretty \begin{tabular}
\usepackage{multicol}  % For pretty multi-columns environments
\usepackage{xspace}    % For use in a command to ensure proper spacing
%\usepackage{geometry} % Uncomment this is you want full page documentation
\usepackage{graphicx}  % For allowing pictures
\usepackage{float}     % For the purpose of adding \begin{figure} [H]
\usepackage{lipsum}    % For adding filler text

% Font Choices - Some of the fonts in this package are great and are my favorite
% 1) ebgaramond - Garamond, this is the top choice for fonts for the resume. It
% is classy, full-proof for professional work, and looks great up close and %
% afar
\usepackage[osf, scale=1.02]{ebgaramond}
% 2) cochineal - based off of the Crimson fonts, this is almost like Minion,
% but is a solid workhorse font and has clean lines - use for the most clean
% appearance
% \usepackage[osf]{cochineal}
% 3) Minion Pro - You have to use XeTex to get this font working (and often
% have to install it) but this is a workhorse font, and one of the cleanest
% fonts around. Use it when you d:vsp on't want the font to be noticed
% \usepackage{fontspec}
% \setmainfont[Numbers=OldStyle]{Minion Pro}
% 4) kpfonts - the Kepler font project, these are based off of Palatino fonts
% and are wide but very easy to read and fill the page well. Use this if you
% think that the resume looks too white
% http://ctan.math.utah.edu/ctan/tex-archive/fonts/kpfonts/doc/kpfonts.pdf
% \usepackage[oldstylenums]{kpfonts}

% Honorable Mentions:
% 5) fbb - based off of Bembo, this is a delicate font and a great substitute
% for when you are sick of garamond but need something professional
% \usepackage[osf,scale=0.95]{fbb}
% 6) erewhon - based off of Utopia, this is a no-nonsense font and holds a
% great modern look while being very stark
% \usepackage[osf]{erewhon} % Erewhon font based on utopia
% 7) sourcesanspro - this is the only font that is approved for sans-serif work
% and it is a great workhorse of a font when legibility is the chief concern
% \usepackage[default]{sourcesanspro} % Only Approved Sans Serif

% Use of libertine or alegreya for open source fonts
% \usepackage[osf]{libertine} % replacement for times and gorgeous font
% \usepackage[libertine]{newtxmath}
% Playful and professional - a great super family and the sans-serif is superb
% \usepackage[osf]{alegreya}

% Use the following for the baskerville fonts ----
% This, combined with Mrs Eaves, are some of the most gorgeous old school fonts
% especially the italic. These two were on all of the menus at Mackinac Island
% \usepackage{lmodern} % monospace font
% \usepackage[scale=0.89]{tgheros} % Helvetica is too big
% \usepackage[osf]{Baskervaldx} % tosf in text, tlf in math
% \usepackage[baskervaldx,cmintegrals,bigdelims,vvarbb]{newtxmath} % math italic letters from Baskervaldx
% ------------------------------------------------

% Run this if you are using a sans-serif font
%\renewcommand{\familydefault}{\sfdefault}

% For commenting on incomplete or new items
\usepackage{todonotes} % \missingfigure{} is the best command

% =====================================
% Graphics
% =====================================

% For quick graphics insert -- Full Line --
\newcommand{\qpic}[2]{
	\begin{figure}[H]
		\centering
		\includegraphics[width=1\linewidth]{./#1}
		\caption{#2}
		\label{fig:#1}
	\end{figure}
}

% For quick graphics insert -- Normal Size --
\newcommand{\qpics}[2]{
	\begin{figure}[H]
		\centering
		\includegraphics[width=0.7\linewidth]{./#1}
		\caption{#2}
		\label{fig:#1}
	\end{figure}
}

% =====================================
% Custom Macros that make life easier
% =====================================

% Description Enviroment Item Helper Commands
\newcommand{\im}[1]{\item[#1] \xspace}
\newcommand{\imp}[1]{\item[(#1)] \xspace}

% Auto-commas for long nominal and dollar amounts
\RequirePackage{siunitx}
\newcommand{\commasep}[1]{\num[group-separator={,}]{#1}}
\newcommand{\money}[1]{\$\commasep{#1}}

% Borrowing from tufte, this is the \newthought command that is
% often used to bring about the change from one subsubsection
% to another and is a good way to bring things up logically into smaller
% bites
\newcommand{\newthought}[1]{
\vspace{11pt} \noindent
\spacedlowsmallcaps{#1}
}

% Code to produce spaced small caps in real text
\newcommand{\mysmallcaps}[1]{\spacedlowsmallcaps{#1}\xspace}

% =====================================
% For handling code blocks and other text
% =====================================

% Code to handle inputting code segments in R
\usepackage{listings} % To import code use: \lstinputlisting[language=R]{h1code.r}
\lstset{language=R} % \lstinputlisting[language={}]{file.txt} for  unformatted code
\usepackage{color}
\definecolor{mygreen}{rgb}{0, 0.6, 0}
\definecolor{mygray}{rgb}{0.5, 0.5, 0.5}
\definecolor{mymauve}{rgb}{0.58, 0, 0.82}

\lstset{ %
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\footnotesize,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,	                   % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=R,                      % the language of the code
  otherkeywords={*,...},           % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=2,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{mymauve},     % string literal style
  tabsize=2,	                     % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

% =====================================
% Beginning the main document
% =====================================

\begin{document}
\pagestyle{plain}
\title{\rmfamily\normalfont\spacedallcaps{the title}}
\author{\spacedlowsmallcaps{Mark Kurzeja}}
\date{} % no date or \today if you want to insert a date

\maketitle

\begin{abstract}
\end{abstract}

\tableofcontents

\section{A Section}
\subsection{A Subsection}
\lipsum[1-1]
\newthought{My name is} \lipsum[1-1]
\lipsum[1-1]

\end{document}


\documentclass[12pt,letter]{article}
\usepackage{geometry}\geometry{top=0.75in}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{xcolor}	% Color words
\usepackage{cancel}	% Crossing parts of equations out
\usepackage{tikz}    	% Drawing
\usepackage{pgfplots}   % Other plotting
\usepgfplotslibrary{colormaps,fillbetween}
\usepackage{placeins}   % Float barrier
\usepackage{hyperref}   % Links
\usepackage{tikz-qtree} % Trees
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{multicol}
\usepackage{graphicx}   % For graphics
\usepackage{parcolumns}
\usepackage{listings}   % lstlisting

%\tikzset{
%    treenode/.style = {shape=rectangle, rounded corners, draw, align=center}
%    root/.style     = {treenode, font=\Large}
%    env/.style      = {treenode, font=\ttyfamily\normalsize},
%    dummy/.style    = {circle,draw}
%}

%\tikzset{every tree node/.style={circle,align=center, anchor=west, grow=right}
%	}
\tikzset{every tree node/.style={align=center,minimum width=2em},%, draw},%, circle},
	 grow=right,
	 level distance=1.75cm}

% Don't indent
\setlength{\parindent}{0pt}
% Function to replace \section with a problem name specifically formatted
\newcommand{\problem}[1]{\vspace{3mm}\Large\textbf{{Problem {#1}\vspace{3mm}}}\normalsize\\}
% Formatting function, like \problem
\newcommand{\ppart}[1]{\vspace{2mm}\large\textbf{\\Part {#1})\vspace{2mm}}\normalsize\\}
\newcommand{\documentation}[1]{\vspace{2mm}\large\textbf{\\Documentation{#1}\vspace{2mm}}\normalsize\\}
% Formatting
\newcommand{\condition}[1]{\vspace{1mm}\textbf{{#1}:}\normalsize\\}

\begin{document}
\title{CIS 510: Competitive Super Mario Kart\\
\large Deep Q Reinforcement Learning for Multiplayer Super Mario Kart}
\author{Steven Walton}
\maketitle

\section{Project Description}
In this project we will be creating two competitive agents that will be able to
play Super Mario Kart against one another. An extra goal is to be able to play
against these agents in a match. A full description of Super Mario Kart can be
found in the \href{https://www.nintendo.co.jp/clvs/manuals/common/pdf/CLV-P-SAAFE.pdf}
{official documentation}.

\section{Problem Formulation}
In this section we will discuss the various tools and break down how we will
formulate the project. In the next section we will discuss our proposition of
how to achieve these goals.

\subsection{The Quality Matrix and Q Learner}
Reinforcement learning depends on what is called a quality matrix, or q matrix
for short. The q matrix is composed of states and actions, where each row
represents a different state of the game and each column represents a different
action. In a learning environment a q matrix will be initialized where all elements
are 0 or a random number. A learner will then navigate the space and iteratively
adjust the elements of this matrix. The learner will do this in two different ways:
they will explore, that is making ``random" moves testing out the environment
and outcome of certain actions; and by relying on historical solutions from the
previous iteration of the q matrix. We can define this as the following
\begin{equation}
    Q(s_t,a_t) = Q(s_t, a_t) + \alpha(r_{t+1} + \gamma\max_a(Q(s_{t+1},a)-Q(s_t,a_t)))
\end{equation}
Where
\begin{align*}
    Q(s_t,a_t)  \hspace{1cm} & \textsf{is the quality matrix at time t}\\
    \alpha      \hspace{1cm} & \textsf{is the learning rate}\\
    r_{t+1}     \hspace{1cm} & \textsf{is the reward}\\
    \gamma      \hspace{1cm} & \textsf{is the discount}\\
    \max_a      \hspace{1cm} & \textsf{is the maximal reward from Q given a specific action in the
                                       action set of } s_{t+1}\\
    Q(s_{t+1},a)\hspace{1cm} & \textsf{is the Q matrix at the next state, } s_{t+1}
                   \textsf{, and a is the action set at that state}
\end{align*}
We can represent problems with a Markovian Decision Process (MDP) and this fills
our Q matrix.

A simple example of a q learner and the associated MDP can be found in this
\href{https://gist.github.com/stevenwalton/4e4b664d494b35be2d6882c7e50eb893}
{gist} that I created. It also contains a more detailed explanation of a
Q learner and Deep Q Learner.

\subsection{Retro}
For this problem we will be using OpenAI's \href{https://github.com/openai/retro/}
{retro gym}. Similar to \href{https://github.com/openai/gym}{gym}, retro gym
creates and environment that allows us to play ``retro" video games. It handles
all the emulation and provides us with bindings to the buttons that would be
pressed on an actual controller. Retro also provides some states and actions for
common games, though not Super Mario Kart (more on this later). Using this
module a user can focus on just creating a reinforcement learner to learn to
play the game and they do not have to think much about the environment or action
states, that is: they do not need to formulate the quality matrix to create a
basic learner.

\subsection{Super Mario Kart}
This game was chosen for a few reasons. Besides the fact that it is extremely
popular, though less popular that its successor (Mario Kart 64), the environment
is a nice balance of complication for the allotted time of this course.

Super Mario Kart is a racing game that has different speeds,
\href{https://strategywiki.org/wiki/Super_Mario_Kart/Characters}{characters with
unique traits}, \href{https://strategywiki.org/wiki/Super_Mario_Kart/Items}
{power items}, and has a multiplayer aspect. Though the reward
function is difficult to define, there is a clear goal to the game: place first.
Excluding glitches, it is always optimal to have forward progression. We have
also speculated that by having multiple agents compete against one another within
the same race that learning will happen faster and not be limited by only being
better than the game's AI.

\section{Proposed Solution}
Completing this project will require several tasks. First it will require importing
the game into retro's engine. Next it requires defining the reward function, which
means that the state and action set needs to be well defined. Lastly, the agents
need to be trained to complete the tasks.

\subsection{Integration into Retro}
Currently we have completed the task of integrating the Super Mario Kart package
into retro's engine, but this is not enough to play the game. Retro relies on
different ``states", which can be defined as different starting states.
This is placed in quotes because the state will continuously be changing as
the agent progresses through the level, more on this later. For
this project we are starting with the scenario where there is a single agent
playing the 50cc Mushroom Cup. Essentially we set a place in the game where we
want our agent to start. Retro provides a UI where we can interactively play
the game and save specific states. For this example our initial state looks like
this:
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.8\textwidth]{1P_GrandPrix_50cc_MushroomCup.png}
    \caption{Initial state for 1P\_GrandPrix\_50cc\_MushroomCup.state}
\end{figure}
\FloatBarrier
From this we can see that in the upper half we see our player and the world
around us. In the bottom half we see the entire map and can actually identify
certain features like coins and items. We will be focusing on the top half.

Now that we have where we want to start, we need to define everything else.
Retro uses json files to define many different parts to the game. There are
two important files for each game: data.json and scenario.json. Data defines
information about the level, lives, and score. Scenario defines the
ending condition and the reward structure. An example with the Nintendo
Entertainment System's (NES) Mario Brothers are shown below.
\\
\begin{figure}[h!]
\begin{minipage}{0.45\textwidth}
    \begin{lstlisting}[caption=data.json, frame=tlrb]{Name}
{
   "info": {
      "level": {
         "address": 65,
         "type": "|d1"
      },
      "lives": {
         "address": 72,
         "type": "|u1"
      },
      "score": {
         "address": 148,
         "type": ">d4"
      }
   }
}
    \end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
    \begin{lstlisting}[caption=scenario.json, frame=tlrb]{Name}
{
   "done": {
      "variables": {
         "lives": {
            "op": "equal",
            "reference": 0
        }
     }
   },
   "reward": {
      "variables": {
         "score": {
            "reward": 1
        }
     }
   }
}
    \end{lstlisting}
\end{minipage}
    \caption{data.json and scenario.json for NES Mario Brothers}
\end{figure}
Additionally, scenario.json can contain information about the action set. We can
either restrict our action set to a specific set of buttons, and button
combinations, or we can expose all possible set of actions to the agent. Initially
we will be limiting our agent, since certain actions like ["DOWN","UP"] are
not likely to be useful, excluding glitches. Super Mario Kart has the following
set of actions: Steering with the D-Pad, B for Accelerate, A to use an item,
Y for Brakes, X for the rear-view, L or R for Hop/Power-side (same action),
Start to pause the game, and select for Rear-View. We will limit the agent
so that it may not look behind itself, cannot pause the game, and we will select
R for hop/power-slide (because that is what I use when playing). So our new full
action set for the steering will be \{\{UP\},\{DOWN\},\{LEFT\},\{RIGHT\},
\{UP,LEFT\}, \{UP, RIGHT\}, \{DOWN,RIGHT\}, \{DOWN,LEFT\}\}.
This represents the left side of the controller. For the right side of the
controller we will assume that the agent can play like a human does
(this will also limit glitches that the agent might be able to exploit).
The controls action set will be defined as \{\{B\},\{A\},\{Y\},
\{R\},\{A,B\},\{B,R\},\{A,R\}\}. The full action set is the combination of
actions from the steering set and the control set.

To summarize the integration: we need to fully define how our agent will receive
rewards, when the agent ends, and our action set.

\subsection{Rewards}
Defining rewards is always a challenging problem in any machine learning task.
There are many fantastic examples where a well thought out reward function leads
to outcomes not expected by the machine learning engineer. For this game there
are several criteria that will help us create different reward functions to test
out.

First off, we need to have a clear goal. Since Super Mario Kart is a competitive
racing game, each player is given a ranking when they finish the race. This
gives us a well defined goal: be first. Since we will be training over a cup,
multiple tracks, we do not need to place first in every race to win the cup, but
placing first in every race will always result in placing first in the cup. The
position can be seen in the bottom right of the screen, in our picture we start
in 8th place (starting position changes depending on finishing position of the
previous race).

The second factor that is key is that we have a clock, seen in the upper right
of the screen. While we want to place first every time, setting a reward function
purely based on position will only train the agent to beat the game's AI. Instead
we want the agent to finish the race as fast as possible.

There are other factors to include that we will not consider until combinations
of the first two are tested. Such factors include that the number of coins,
with maximum of 10, determine how fast one can go. So having more coins helps
optimize the first two, but may be harder to incorporate. Additionally, there
are pieces on the may that boost the player. We hope that the agent will
recognize these features on its own and that we do not need to assign a reward
for using these. Including features like these have a higher potential to create
an undesirable reward function. So first we will keep the reward system simple
and iterate as needed.

\subsection{Determining Finish}
As mentioned previously, by incorporating a game that is not already defined by
retro we need to tell retro when we are done. To accomplish this we will actually
need to play this game in an emulator and record the state when finishing a race
as well as when we finish a cup. This task is not yet accomplished but is not
expected to be exceedingly difficult.

\subsection{Seeing the Environment}
We have mentioned that we will only be using the top half of the screen, the
reason being is that this is where the agent gains the most information about
their environment. Additionally, when playing in 2 player mode, the bottom half
will be used for that agent's view. Therefore to train properly we need to only
rely on the top half.

Retro does not create a way for the agent to ``see" its current state. To
accomplish this we need to turn this Q learner into a Deep Q learner. To do that
we first create a Convolutional Neural Network (CNN) that will determine our
state. Using this we will have a simple network which will then determine the
action that needs to be taken at any given state, position on the track.

A simple way to think about this flow is with the following image.
\begin{figure}[h]
    \centering
    \includegraphics[width=0.8\textwidth]{conv_agent.png}
    \caption{Example flow of network with Mario Brothers}
\end{figure}
Using the position of Mario and other objects on the screen, the agent needs
to determine if it will run or jump. Our action set will be slightly more
complicated.

\end{document}


%!TEX root = ../thesis.tex

\cleardoublepage
\chapter{Technologies and Concepts of a Larger Context}

This appendix chapter contains definitions for technologies and concepts that are mentioned in the thesis, but are not of higher importance for it. Section \ref{sec:paxos} introduces a consensus algorithm for distributed systems.

\section{Finding Consensus in a Distributed System}
\label{sec:paxos}

The Paxos algorithm has first been described by Leslie Lamport in his work about the parliament on the island of Paxos \cite{paxos1998}. The parliament's part-time legislators had been able to maintain consistent copies of their records by following the algorithm protocol.  While the original work's main focus has not been the application in computer science, the author simplified his explanations later in \cite{paxos2001} and described how Paxos can be used to find consensus in fault-tolerant distributed systems.

Even though the Paxos algorithm cannot mitigate Byzantine failures (see appendix \ref{app:dic}), it can mitigate the effects of different processing speeds of participants and reordered, delayed, or lost messages. The only requirement is that at least half of the participants can somehow communicate.

In Paxos, four different roles exist. The \textbf{client} issues requests and waits for responses. Based on the client's request, multiple \textbf{proposer} propose a value. All these proposals are processed by the \textbf{acceptors} which will agree upon one at the end. Finally, there are the \textbf{learners} which guarantee persistence of agreed decisions and respond to client reads. The usual setup used in practice, in which every machine participating fulfills each of the roles besides the client role, is shown in figure \ref{fig:paxosSetup}.

\begin{figure}[ht]
    \centering
	\includegraphics[width=0.8\textwidth]{fig/PaxosSetup.pdf}
	\caption{Example Setup of Three Machines Agreeing on Values Based on the Paxos Algorithm}
	\label{fig:paxosSetup}
\end{figure}

\section{Detecting Mutual Inconsistency}
\label{sec:versionVector}

Parker et al. claim that a system must ensure the mutual consistency of data copies by applying all changes made to one copy to all others correspondingly \cite{versionVektor}. Each time two copies of the same original data item have a different set of modification applied to them, they become incompatible and this conflict must be detected. However, this is not trivial, because \enquote{network partitioning can completely destroy mutual consistency in the worst case}. Nevertheless, Parker et al. state that the efficient detection of conflicts that lead to mutual inconsistency can be done by a concept they call \textit{version vectors}. They define a version vector for an item f as \enquote{a sequence of n pairs, where \textit{n} is the number of sites at which f is stored. The \textit{i}th pair ($S_i$: $v_i$) gives the index of the latest version of \textit{f} made at site $S_i$}. An example vector for an item stored at the sites A, B and D is $<$A:2, B:4, D:3$>$, which translates in a file that has been modified twice on site A, four times on site B, and thrice on site D.

A set of vectors is \enquote{compatible when one vector is at least as large as any other vector in every site component for which they have entries}. Otherwise the vectors conflict and are incompatible. E.g., the two vectors $<$A:2, B:4, D:3$>$ and $<$A:4, B:5, D:3$>$ are compatible because the second one dominates the other one. $<$A:3, B:4, D:3$>$ and $<$A:2, B:5, D:3$>$ conflict, because the first one indicates that the data item was modified one more time on node A, while the second one indicates one more modification occurred on node B. However, if we add a third vector $<$A:3, B:5, D:4$>$, no conflict exists anymore, because it dominates the two others. The consequences of an operation performed on a data item for a data item's vector is depicted in table \ref{tab:vectorOperations}.

\begin{table}[ht]
    \centering
    \begin{tabularx}{\linewidth}{@{}>{\bfseries}l@{\hspace{.5em}}X@{}}
        \toprule
        \textbf{Operation related to data item} & \textbf{Consequence for vector of data item}\\ \midrule
        Update on site $S_i$ & Increment $v_i$ by one\\
        Delete or rename on site $S_i$ & Keep vector and increment $v_i$ by one, remove data item value\\
        Reconcile version conflict & Set each $v_i$ to maximum $v_i$ from all vectors used for reconciliation. In addition, increment $v_i$ of site that initiated reconciliation by one\\
        Copy to new site & Augment vector to include new site\\
        \bottomrule
    \end{tabularx}
    \caption{Influence of Operations on a Data Item's Version Vector}
    \label{tab:vectorOperations}
\end{table}

By using version vectors, one can detect version conflicts and initiate (automatic) reconciliation. However, Parker et al. warn that two identical updates made on separate partitions will result in a conflict, even though none is present. Thus, they recommend to additionally check two data items on differences before a conflict is raised in certain applications. Furthermore, the reconciliation is most times not trivial, that's why tools such as Cassandra delegate this task to the application layer \cite{cassandra2010}.


\title{Computer Architecture - CS 301} % You may change the title if you want.

\author{Rishit Saiya - 180010027, Assignment - 8}

\date{\today}

\documentclass[12pt]{article}
\usepackage{fullpage}
\usepackage{enumitem}
\usepackage{amsmath,mathtools}
\usepackage{amssymb}
\usepackage[super]{nth}
\usepackage{textcomp}
\usepackage{hyperref}
\begin{document}
\maketitle

%----------------------------------------------------------------
\section{}

\textbf{\textit{Temporal Locality}} \\
Temporal locality refers to the reuse of specific data, and/or resources, within a relatively small time duration. The processor tends to access memory locations that have been used recently. So the instruction is kept in cache memory such that it can be fetched easily and takes no time in searching for the same instruction. It is generally exploited by keeping recently used instruction and data values in cache memory and by exploiting a cache hierarchy. \\

Let us consider a stack where, whenever we access an address, we bring it to the top of the stack. The distance from the top till where element was found is defined as the stack distance. It actually counts the frequency of reuse of the addresses, thus its an example of temporal locality. Most stack distances are very low and thus have high temporal locality in general. \\

\textbf{\textit{Example:}} \\
Let's say that in a case, we are recursively updating a variable. Consider the following code snippet:
\begin{verbatim}
    int sum = 0;
        for (int i = 0; i < 10; i++){
            sum += i;
        }
\end{verbatim}
In here, we update the variable sum, continuously and hence this demonstrates Spatial Temporal Locality case. \\

\textbf{\textit{Spatial Locality}} \\
Spatial locality (also termed data locality[3]) refers to the use of data elements within relatively close storage locations. The processor tends to execute and involve a number of memory locations that are clustered. So, the data elements (instructions) are used which are relatively close in storage locations. It is generally exploited by using larger cache blocks and by incorporating prefetching mechanisms (fetching items of anticipated use) into the cache control logic.

Let us consider maintaining a sliding window of the last K memory addresses. The $i^{th}$ address distance as the difference in the memory addresses of the $i^{th}$ memory access, and the closest address in the set of last K memory access. This shows the similarity in addresses. The address distances within $\pm$ 25 bytes are most likely to be accessed frequently and hence they have high spatial locality. \\

\textbf{\textit{Example:}} \\

Now, let us consider a case where we want to read array elements from array \textbf{arr}. If we want to read values from arr[i], where i is index, such memory locations are nearby (spaced with 4 gaps of byte). Thus, this demonstrates Spatial Locality case.

%----------------------------------------------------------------
\section{}

If the registers are removed from any given architecture, then we are compelled to store the operands of the instructions in the processors' memory or other processing unit's memory. However, we know that Memory involved operations are quite slower than the operations performed by registers. The process of operand fetching the memory and then writing, involves more time and computations. \\

A processing unit operates on register contents at the rate of more than one operations per cycle but such multiple operations are not possible in case of memory. So, if only memory is used with no availability of caches (which register provide), then the execution time is naturally going to increase. \\

The registers provide a faster computing interim internal calculations of processing units and if for such calculations, if only memory is involved then the time computation will be verbose for programs as mentioned above. \\

So, No, such a processor would be very slow if only memory is used as registers are very fast. So such a processor is not recommended.

%----------------------------------------------------------------
\end{document}


\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage{chngcntr}
\usepackage{tikz}

\lhead{Evan Kohilas - z5114986}
\rhead{COMP3821 - Assignment 3}
\pagestyle{fancy}
\title{A17S1N3}
\counterwithin*{equation}{section}
\usepackage{parskip}
\begin{document}

\begin{center}
    \begin{LARGE}
        COMP3121\\
        Assignment 3\\
        A17S1N3\\
        \hrulefill\\
        Evangelos Kohilas\\
        z5114986\\
        \hrulefill
    \end{LARGE}

    \begin{large}
        By submitting this document you are confirming that all the answers are your work and are not take from any other sources unless clearly mentioned.
    \end{large}

\end{center}

\section*{Question 1}
We can find the minimal tree by recursing down the tree and balancing a subtree by increasing the lengths on each side such that the path sums on both sides are equal.

When we reach the final lengths, we set the smaller length to be equal to the larger length.

By doing this, as all the base lengths will first become equal, all the other lengths will be balanced to their possible minimum.

\begin{verbatim}
def recurse(node):
    if node.left is leaf and node.right is leaf:
        new_length = max(node.left_length, node.right_length)
        node.left_length = new_length
        node.right_length = new_length
        return new_length
    else:
        right_sum = recurse(node.right)
        left_sum = recurse(node.left)
        max_sum_path = max(right_sum + node.right_length, left_sum + node.left_length)
        node.left_length = max_sum_path - left_sum
        node.right_length = max_sum_path - right_sum
        return max_sum_path
\end{verbatim}

\pagebreak
\section*{Question 2}
The objects should be machined in descending order of polishing time, and then ascending order of machining time.

\textbf{Proof of optimality:}\\
let $A$ be our greedy solution produced by our algorithm and $O$ be the optimal solution.\\
let $(m, p)$ be the machining and polishing time of some item and $(m', p')$ be the machining and polishing time of another item.

In the case where $p' < p$ and $m' = m$,\\
we let the time taken for two items done in order $a$ and $b$ be $a_m + b_m + \max(a_p - b_m, b_p)$

If $A$ selects $(m, p)$ to be machined first, we let $total_A = m + m' + \max(p - m', p')$

In the first case where $p > m$:
\begin{align*}
    total_{A}^1 &= m + m' + p - m'\\
              &= m + p\\
\intertext{Otherwise:}
    total_{A}^2 &= m + m' + p'\\
              &= 2m + p'
\intertext{%
If we violate our greedy choice and $O$ selects $(m', p')$ to be machined first,\newline
we let $total_O = m' + m + \max(p' - m, p)$\endgraf
In the first case where $p > m$:%
}
    total_{O}^1 &= m' + m + p\\
              &= 2m + p
\intertext{Otherwise:}
    total_{O}^2 &= m' + m + p' - m\\
              &= m + p'
\end{align*}

Then we see that $total_{A}^1 < total_{O}^1$ and $total_{A}^2 < total_{O}^2$.\\
Therefore the optimal solution $O$ is worse than the greedy solution $A$.

In the case where $p = p'$ and $m' < m$,\\
we let the time taken for two activities done in order $a$ and $b$ be $a_m + \max(a_p, b_m + b_p)$

Using $A$, we select $(m, p)$ to be machined first, so we get:
\begin{align*}
    total &= m' + \max(p', m + p)\\
          &= m' + \max(p', m + p')\\
          &= m' + m + p'
\end{align*}

If we violate our greedy choice and $O$ selects $(m', p')$ to be machined first, we get:
\begin{align*}
    total' &= m + \max(p, m' + p')\\
           &= m + \max(p, m' + p)\\
           &= m + m' + p
\end{align*}

Then we see $total = total'$ showing no difference between $O$ and $A$.

Since a change in machining time sees no difference, then there will be no difference in every other case.

Therefore, $A$ is optimal as we cannot create a better solution than the greedy one produced by our algorithm.

\section*{Question 3}
If Alice makes a graph connecting people that know each other, Alice can continuously remove all people of degree less than 5 until either everyone is removed (in which case the constraints cannot hold for any amount of invitees) or until no more people can be removed (in which case the largest number of invitees have been found).

\textbf{Proof of optimality:}\\
let $S = \{p_1, p_2, p_3, \ldots, p_n\}$\\
let $O$ be the set of the optimal solution.\\
let $S_i$ be the subset of $S$ after $i$ iterations where $0 \leq i \leq n$.

We prove by induction that $O$ will always be a subset of $S_i$.

When $i = 0$, no people have been removed, and so $O$ remains a subset of $S_0$

Assume $i = k$ such that after $k$ iterations, $O$ is a subset of $S_k$

let $i = k + 1$:\\
$O$ will still remain as a subset of $S_{k+1}$ as $p_{k+1}$ will only be removed if $p_{k+1}$ doesn't satisfy the two constraints.

Therefore by induction, since $O$ is still a subset of $S$, $S$ is optimal since $|S| \geq |O|$, as we are trying to maximise the number of invitees.

\section*{Question 4}
\begin{enumerate}[label=\alph*)]
    \item
    Given the following arrangement,
    \begin{center}
    \begin{tikzpicture}
        \tikzset{circles/.style={draw, circle, very thick, minimum size=5mm}}
        \node [circles, fill=black] (1) at (0, 0){};
        \node [circles, fill=black] (2) at (2, 0){};
        \node [circles]             (3) at (4, 0){};
        \node [circles]             (4) at (6, 0){};
        \node [circles, fill=black] (5) at (8, 0){};
        \node [circles]             (6) at (10,0){};
    \end{tikzpicture}
    \end{center}

    By using the closest pair method (the proposed algorithm) our total length will be $5 + 1 + 1 = 7$
    \begin{center}
    \begin{tikzpicture}
        \tikzset{circles/.style={draw, circle, very thick, minimum size=5mm}}
        \node [circles, fill=black] (1) at (0, 0){};
        \node [circles, fill=black] (2) at (2, 0){};
        \node [circles]             (3) at (4, 0){};
        \node [circles]             (4) at (6, 0){};
        \node [circles, fill=black] (5) at (8, 0){};
        \node [circles]             (6) at (10,0){};
        \draw [very thick] (1) |- +(0,  1) -| (6);
        \draw [very thick] (2) |- +(0,0.6) -| (3);
        \draw [very thick] (4) |- +(0,0.6) -| (5);
    \end{tikzpicture}
    \end{center}

    This is not optimal as the following solution has a shorter length of size $2 + 2 + 1 = 5$
    \begin{center}
    \begin{tikzpicture}
        \tikzset{circles/.style={draw, circle, very thick, minimum size=5mm}}
        \node [circles, fill=black] (1) at (0, 0){};
        \node [circles, fill=black] (2) at (2, 0){};
        \node [circles]             (3) at (4, 0){};
        \node [circles]             (4) at (6, 0){};
        \node [circles, fill=black] (5) at (8, 0){};
        \node [circles]             (6) at (10,0){};
        \draw [very thick] (1) |- +(0, 0.6) -| (3);
        \draw [very thick] (2) |- +(0,-0.6) -| (4);
        \draw [very thick] (5) |- +(0, 0.6) -| (6);
    \end{tikzpicture}
    \end{center}

    \item
        Going from left to right, we can produce an optimal solution by matching each dot with its earliest possible match.

        \textbf{Proof of optimality:}\\
        Let $A$ be our greedy solution produced by our algorithm and $O$ be the optimal solution.

        If we violate our greedy algorithm, then there are two cases that can occur.

        If we swap two pairs in $A$ such that a pair finishes in the position of an earlier pair, then the earlier pair will finish in the position of the later pair, but the total length will not increase as both will have changed by the same amount.

        If we swap two pairs in $A$ such that a pair finishes in the position of a later pair, then the length of that pair will increase, but the length of the later pair will either decrease by an equal amount such that the total length will not increase, or not decrease, such that the total length has increased.

        Therefore $A$ is optimal as $O$ does not offer a better solution than the one produced by $A$.

\end{enumerate}

\section*{Question 5}
To complete $n$ tasks with as low total penalty as possible, we pick the highest penalty task and schedule it to be completed as late as possible before incurring a penalty. If the picked task is scheduled to be completed in such a way that would incur a penalty, we ignore it, scheduling it to be completed after all tasks without penalty have been completed. We repeat this until all tasks have been scheduled.

\textbf{Proof of optimality:}\\
Let $A$ be our greedy solution produced by our algorithm and $O$ be the optimal solution.

We can violate our greedy algorithm by swapping tasks from $A$ to $O$, such that they are either scheduled earlier, or scheduled later.

In both cases, by doing so we have either:
\begin{enumerate}
    \item Scheduled the tasks such that there is no change to the total penalty.
    \item Scheduled one of the tasks to be past its due date, and thus increasing our penalty.
    \item Scheduled a lower cost task to be before its due date, but scheduled a higher cost task after its due date, thus increasing out total penalty.
\end{enumerate}

Therefore $A$ is optimal as all cases cannot create a better solution than the greedy one produced by our algorithm.

\section*{Question 6}
To calculate the first 10 books that we need to borrow, we iterate through the sequence of books, adding each book to a set and continuing until our set is of length 10. Whenever we come across a book we need that is not in our set, we make another trip to the library, creating a new set and iterating as before.

\textbf{Proof of optimality:}\\
let $B$ be the sequence of books.\\
let $A$ be the set of the first 10 unique books from $B$ to keep produced by our algorithm.

Suppose $A$ is not optimal, and assume $O$ be the optimal solution.\\
If we violate the greedy algorithm, taking one less book, then $|O| = 9$.

If $|O| = 9$ then one of two things have occurred:

Either we have moved a book to the next set such as the next set will contain the $20^{th}$ book that we will need, as it will be the same as the book removed.

Or we have removed a book such that we require an additional trip to compensate for the $20^{th}$ book that could not be added to our set.

Therefore, $A$ is optimal as we cannot create a better solution than the greedy one produced by our algorithm.

\end{document}


\section{Controller tuning}
\subsection{}

\begin{frame}
\frametitleTC{Outline}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We stick to the PI case, adding D afterwards is easy.
 \item We first address the pure DT (not sampled-signals) case: % with two frequently encountered structures for $P(z).
 \item the purpose here is to strengthen the comprehension already achieved, while looking also at the behaviour of $u$ and not only $y$.
 \item Then we move to the sampled-signals case:
 \item the purpose is to understand how ``$T_s$-independent'' parameters\\
       can be defined.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Pure DT case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We already saw this:
       \begin{displaymath}
        \begin{array}{l}
          P(z)              = \frac{\mu}{z-p} \\
          G_{yw}^{\circ}(z) = \frac{1-\alpha}{z-\alpha}
          \text{ or }
          G_{yd}^{\circ}(z) = \frac{z-1}{z-\alpha}
        \end{array}
        \quad \Rightarrow \quad
        C_{PI}(z) = K\,\frac{z-\zeta}{z-1}, \quad
        K         = \frac{1-\alpha}{\mu}, \;
        \zeta     = p.
       \end{displaymath}
 \item The tuning above fulfils our desires on $y$; what about $u$?
 \item We have
       \begin{displaymath}
        \begin{array}{rclclcl}
         G_{uw}(z) &=& \frac{C(z)}{1+L(z)}
                   &=& \frac{\frac{1-\alpha}{\mu}\frac{z-p}{z-1}}{1+\frac{1-\alpha}{z-1}}
                   &=& \frac{1-\alpha}{\mu}\frac{z-p}{z-\alpha}, \\
         G_{ud}(z) &=& \frac{-C(z)}{1+L(z)}
                   &=& -G_{uw}(z)
                   &=& \frac{\alpha-1}{\mu}\frac{z-p}{z-\alpha}. \\
        \end{array}
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Pure DT case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Turning $G_{uw}(z)$ to time domain we get
       \begin{displaymath}
         \begin{array}{rcl}
          \frac{u(k)}{w(k)}  &=& \frac{1-\alpha}{\mu}\frac{z-p}{z-\alpha} \\
          (z-\alpha) u(k)    &=& \frac{1-\alpha}{\mu}(z-p) w(k) \\
          u(k+1)-\alpha u(k) &=& \frac{1-\alpha}{\mu} \left( w(k+1)-pw(k) \right)
         \end{array}
       \end{displaymath}
 \item and finally, rearranging and scaling the time index,
       \begin{displaymath}
        u(k) = \alpha u(k-1)+\frac{1-\alpha}{\mu} \left( w(k)-pw(k-1) \right).
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Pure DT case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We now analyse the response of $u$ to a step on $w$ (the $d$ case differs only\\
       by the sign):
       {\small
       \begin{displaymath}
         \begin{array}{rclclcl}
          u(0) &=& \alpha u(-1)   + \frac{1-\alpha}{\mu} \left( w(0) - p       w(-1) \right)
               &=& \alpha \cdot 0 + \frac{1-\alpha}{\mu} \left( 1    - p \cdot 0     \right)
               &=& \frac{1-\alpha}{\mu} \\
          u(1) &=& \alpha u(0)    + \frac{1-\alpha}{\mu} \left( w(1) - p       w(0)  \right)
               &=& \alpha \cdot \frac{1-\alpha}{\mu} + \frac{1-\alpha}{\mu} \left( 1-p\right)
               &=& \frac{1-\alpha}{\mu} (1+\alpha-p) \\
          \cdots\\
         \end{array}
       \end{displaymath}
       }
 \item For $k\rightarrow\infty$, then,
       \begin{displaymath}
        u(\infty) = G_{uw}(1) \cdot 1
                  =  \frac{\cancel{1-\alpha}}{\mu}\frac{1-p}{\cancel{1-\alpha}}
                  = \frac{1-p}{\mu}.
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Pure DT case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item The response of $u$ to a $w$ (or $d$ if not for the sign) step, thus, can have an initial\\
       overshoot or not:
       \begin{center}
        \includegraphics[width=0.55\columnwidth]{./Unit-06/img/ControlStepResponses-1.pdf}
       \end{center}
 \item The analysis we carried out, provides \TC{dynamic} actuator sizing clues:
       \begin{itemize}[<+-| alert@+>]
       \item to sustain \TC{steady state}, must be capable of exerting $u_{max}=\frac{1-p}{\mu} w_{max}$;
       \item if control has to \TC{accelerate} the system ($\alpha<p$) must transiently exert\\
             the \TC{larger} value $u_{max}=\frac{1-\alpha}{\mu} w_{max}$.
       \end{itemize}
 \item Analogous considerations based on the maximum amplitude of $d$,\\
       and the required speed for its rejection, are left as a simple exercise.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Pure DT case}
\framesubtitleTC{One-step delayed integrator}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item A process structure quite frequently encountered in computer-related controls,\\
       is the \TC{one-step delayed integrator}
       \begin{displaymath}
        P(z) = \frac{\mu}{z-1}.
       \end{displaymath}
 \item The name is motivated by the time-domain formulation
       \begin{displaymath}
        y(k) = y(k-1) + \mu u(k-1),
       \end{displaymath}
       showing that at step $k$, $y$ is the sum (DT integral) of $\mu u$ up to the\\
       previous step $k-1$.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Pure DT case}
\framesubtitleTC{One-step delayed integrator}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item If we apply the same policy we used for the stable asymptotically case just setting $p=1$, we get
       \begin{displaymath}
        \begin{array}{l}
          P(z)              = \frac{\mu}{z-1} \\
          G_{yw}^{\circ}(z) = \frac{1-\alpha}{z-\alpha}
          \text{ or }
          G_{yd}^{\circ}(z) = \frac{z-1}{z-\alpha}
        \end{array}
        \quad \Rightarrow \quad
        C_{PI}(z)  = K\,\frac{z-\zeta}{z-1}, \quad
        K          = \frac{1-\alpha}{\mu}, \;
        \red{\zeta = 1}.
       \end{displaymath}
 \item But we are NOT realising a controller with \red{a zero/pole cancellation on the circle},\\
       as this would create a non asymptotically stable hidden part in the closed-loop\\
       system.
\item Hence we only use the P controller
       \begin{displaymath}
        C(z) = \frac{1-\alpha}{\mu}.
       \end{displaymath}
      and we are back to the case we already treated.
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Sampled-signal case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Just a bit of CT: if we take the first-order LTI system
       \begin{displaymath}
        \dot{y}(t) = ay(t)+bu(t)
       \end{displaymath}
 \item and set $y(0)=0$, $u(t)=1$ for $t \geq 0$ to compute its unit step response, we get
       \begin{displaymath}
        y(t) = \frac{b}{a} \left( e^{at}-1 \right)
       \end{displaymath}
 \item Check (wxMaxima):
       \begin{verbatim}
  y: b/a*(exp(a*t)-1);
  u: 1;
  ratsimp(diff(y,t)-(a*y+b*u)); /* -> zero, OK */
  subst(t=0,y);                 /* -> zero, OK */
       \end{verbatim}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Sampled-signal case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Incidentally, the response converges for $a<0$ (system asymptotically stable),\\
       is constant for $a=0$ (simply stable) and diverges for $a>0$ (unstable).
 \item Generalising to orders above one, there is a stability theorem for CT systems\\
       identical to the one we know for DT ones, if not for replacing ``magnitude\\
       of eigenvalue of $A$ $\lesseqgtr$ 1'' with ``real part of eigenvalue of $A$ $\lesseqgtr$ 0''.\\
 \item Enough for us here.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Sampled-signal case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item If we re-write the CT system above with differently named parameters, i.e.,
       \begin{displaymath}
        \dot{y}(t) = -\frac{1}{T}y(t) + \frac{\mu}{T} u(t)
       \end{displaymath}
 \item[] where asymptotic stability means $T>0$, the unit step response takes the form
       \begin{displaymath}
        y(t) = \mu \left( 1-e^{-t/T} \right).
       \end{displaymath}
 \item Taking also $\mu>0$ for simplicity and without loss of generality,\\
       we can READ the system parameters on a step response plot.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Sampled-signal case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{center}
  \includegraphics[width=0.55\columnwidth]{./Unit-06/img/FOstepResponse.pdf}
 \end{center}
 \begin{itemize}[<+-| alert@+>]
 \item \vspace{-4mm}When you know nothing about the system you need to control, but you know\\
       what is the input and what is the output, a viable way to get a model,\\
       is to perform a \TC{step test}.
 \item If the response resembles the first-order one above, you can read $\mu$\\
       and $T$ on the response record.
 \item Recall however that the $t$ axis is a CT one: seconds, minutes,...\\
       NOT samples, we still have to decide $T_s$ for our control.
 \end{itemize}
\end{frame}

\begin{frame}[label={pag:FOsystem-DT}]
\frametitleTC{Sampled-signal case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Now let us turn the system to DT as we already did:
       \begin{displaymath}
        \dot{y}(t) = -\frac{1}{T}y(t) + \frac{\mu}{T} u(t) \quad \Rightarrow \quad
        \frac{y(k)-y(k-1)}{T_s} = -\frac{1}{T}y(k) + \frac{\mu}{T} u(k).
       \end{displaymath}
 \item This allows us to compute the DT transfer function as
       \begin{displaymath}
        \frac{1-z^{-1}{T_s}}y(k) = -\frac{1}{T}y(k) + \frac{\mu}{T} u(k) \; \Rightarrow \;
        P(z) = \frac{y(k)}{u(k)} = \frac{\mu\frac{T_s}{T+T_s}z}{z-\frac{T}{T+T_s}}.
       \end{displaymath}
 \item Quite expectedly, we have a first-order DT system whose parameters\\
       depend on $T_s$, i.e.,
       \begin{displaymath}
        P(z) = \frac{\rho z}{z-p}, \qquad
        \rho = \mu\frac{T_s}{T+T_s}, \quad
        p    = \frac{T}{T+T_s}.
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Sampled-signal case}
\framesubtitleTC{Dominantly first-order asymptotically stable process}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Results for different values of $T_s$ versus the CT case (dash-dot line):
       \begin{center}
        \includegraphics[width=0.60\columnwidth]{./Unit-06/img/CTvsDTexample-step.pdf}
       \end{center}
 \item \TC{Rule of thumb} to select $T_s$ for a representative DT approximation:\\
       at most 1/5 of $T$ (in our case $T=1$, $T_s=0.2$ is fine).
 \item Plenty of theory omitted, if interested ask for references...
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{A CT interpretation of the PI law}
\framesubtitleTC{useful for tuning}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Can we interpret the PI control law
       \begin{displaymath}
        C_{PI}(z) = K \frac{z-\zeta}{z-1}
       \end{displaymath}
       in the continuous time, so as to derive $T_s$-independent parameters for it as well?
 \item Of course. To this end, let us split the P and the I action again.
 \item The P action has no dynamics, for it the DT and the CT domains\\
       are the same:
       \begin{displaymath}
        u_P(k) = K_P e(k), \quad
        u_P(t) = K_P e(t).
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{A CT interpretation of the PI law}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item The integral action in the CT domain is conversely a true integral:
       \begin{displaymath}
        u_I(t) = K_I \int_0^t e(\tau)d\tau.
       \end{displaymath}
 \item As an immediate consequence, then,
       \begin{displaymath}
        \dot{u}_I(t) = K_I e(t),
       \end{displaymath}
 \item and once again, we relate CT to DT by replacing the time derivative\\
       with the incremental ratio over one step of length $T_s$, which yields
       \begin{displaymath}
        \frac{u_I(k)-u_I(k-1)}{T_s}= K_I e(k).
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{A CT interpretation of the PI law}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item It is a convention of the PI(D) literature and practice to express $K_I$ as the\\
       proportional control gain divided by a time, which is dimensionally consistent,\\
       i.e., to write in the CT
       \begin{displaymath}
        u_P(t)       = K e(t), \quad
        \dot{u}_I(t) = \frac{K}{T_i} e(t)
       \end{displaymath}
       where $K$ is called (somehow improperly) the \TC{gain}, and $T_i$ the \TC{integral time}.
 \item This reflects in the DT controller
       \begin{displaymath}
        \begin{array}{rcl}
         u_P(k) &=& K e(k) \\
         u_I(k) &=& u_I(k-1) + \frac{KT_s}{T_i} e(k)
        \end{array}
       \end{displaymath}
 \item In transfer function form, then,
       {\small
       \begin{displaymath}
        C_{PI}(z) = \frac{u_P(k)+u_I(k)}{e(k)}
                  = K + \frac{zKT_s}{T_i(z-1)}
                  = \frac{K(T_i+T_s)}{T_i} \, \frac{z-\frac{T_i}{T_i+T_s}}{z-1}.
       \end{displaymath}
       }
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{A CT interpretation of PI and 1st order model}
\framesubtitleTC{again in a view to controller tuning}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Let us look at the first-order process model and the PI controller, once their\\
       $T_s$-independent parameters -- $(\mu,T)$ for $P$, $(K,T_i)$ for $C$ -- are evidenced:
       \begin{displaymath}
        P(z)      = \mu \frac{\frac{T_s}{T+T_s} z}{z-\frac{T}{T+T_s}}, \qquad
        C_{PI}(z) = \frac{K(T_i+T_s)}{T_i} \, \frac{z-\frac{T_i}{T_i+T_s}}{z-1}.
       \end{displaymath}
 \item Since $T>0$ by hypothesis and so is obviously $T_s$ as well, the process pole\\
       is within the unit circle.
 \item We can therefore tune the PI by cancellation, but now to do this we\\
       set $T_i=T$, i.e., \TC{we operate in the CT domain}. Doing so produces
       \begin{displaymath}
        L(z) = \mu \frac{\frac{T_s}{\ccancel[green]{T+T_s}} z}{\ccancel[red]{z-\frac{T}{T+T_s}}}
               \frac{K\ccancel[green]{(T+T_s)}}{T} \, \frac{\ccancel[red]{z-\frac{T}{T+T_s}}}{z-1}
             = \frac{\mu K T_s}{T} \frac{z}{z-1}.
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{A CT interpretation of control objectives}
\framesubtitleTC{}
\myPause
 \begin{center}
  \includegraphics[width=0.45\columnwidth]{./Unit-06/img/FOstepResponse.pdf}
 \end{center}
 \begin{itemize}[<+-| alert@+>]
 \item If we want $y$ to reach $u$ within a certain time, the $y \rightarrow w$ continuous time system\\
       has to share the structure of the first-order process, but with $\mu=1$\\
       and a value of $T$ reflecting the desired speed.
 \item Since in the response above $y$ reaches its final value in about $5T$,\\
       if we want the closed-loop system to make $y$ reach $w$ and settle\\
       in a time $t_{set}$, then the required $T$ is $t_{set}/5$. Let us call this quantity\\
       $\lambda$ for brevity.
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Joining the pieces}
\framesubtitleTC{to the purpose of tuning}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Substituting $\mu=1$ and $T=\lambda$ in the expression we found above for $P(z),$\\
       we get the objective $y/w$ transfer function as
       \begin{displaymath}
        G_{yw}^{\circ}(z) = \frac{\frac{T_s}{\lambda+T_s} z}{z-\frac{\lambda}{\lambda+T_s}}, \quad
        \lambda           = \frac{t_{set}}{5}
       \end{displaymath}
 \item We can then apply direct synthesis (wxMaxima):
       \begin{verbatim}
  P   : mu*Ts/(T+Ts)*z/(z-T/(T+Ts));
  Gywo: Ts/(lambda+Ts)*z/(z-lambda/(lambda+Ts));
  Csol: rhs(solve(P*C/(1+P*C)=Gywo,C)[1]);
       \end{verbatim}
 \end{itemize}
\end{frame}

\begin{frame}[fragile,label={pag:PItuning-lambda}]
\frametitleTC{Tuning}
\framesubtitleTC{direct synthesis for set point tracking -- CT setting}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item The result corresponds to the expression we already found for $C$, with
       \begin{displaymath}
        K   = \frac{T}{\lambda\mu}, \quad
        T_i = T.
       \end{displaymath}
 \item check (wxMaxima):
       {\small
       \begin{verbatim}
  K   : T/(mu*lambda);
  Ti  : T;
  Cchk: K*(Ti+Ts)/Ti*(z-Ti/(Ti+Ts))/(z-1); /* The C we got */
  ratsimp(Cchk-Csol);                      /* -> zero, OK  */
       \end{verbatim}
       }
 \item The ``direct synthesis for disturbance rejection'' case is left as a\\
       simple exercise (use wxMaxima). We now observe our result to\\
       make some important remarks.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Remarks}
\framesubtitleTC{direct synthesis for set point tracking -- CT setting}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We showed that the idea of tuning by cancellation can be somehow applied\\
       in the CT domain as well.
 \item We have to say ``somehow'' because we do not possess the knowledge of\\
       \TC{CT transfer function}.
 \item Such an object can be defined, however, and the DT theory we saw can be\\
       completed with its CT counterpart.
 \item In fact, just to notice, the CT side of the matter was born far before.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Remarks}
\framesubtitleTC{general ideas to remember}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item You can derive a ``model for control'' that is VERY SIMPLE, yet enough\\
       for tuning, even just by looking at recorded data.
 \item So simple models are often flagged as ``too simple to provide realistic\\
       guarantees''...
 \item ...but basically by researchers who do not know about the Systems and Control\\
       Theory (no judgement, just mentioning facts).
 \item Be careful about ``realism'', and always consider the PURPOSE of\\
       your model.
 \item In the end, for example, temperature is molecular motion, but luckily\\
       we do not need to follow individual molecules to do temperature\\
       control \smiley.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Remarks}
\framesubtitleTC{General ideas to remember}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item On the symmetric front, be careful about the REPRESENTATIVENESS\\
       of your model.
 \item If you have knowledge of the phenomenon, that is a great advantage.
 \item If you need to rely on data, be sure to explore all the relevant dynamics.
 \item Experiment design is itself a discipline, explore it and/or get in touch with\\
       experts of the Identification Theory.
 \item And in general, be cautious about benchmarks that assert they are ``general''\\
       just because e.g. they contain a ``large'' number of ``heterogeneous''\\
       applications.
 \item By themselves, such statements provide NO guarantee that the\\
       tuned control will not have to face a totally unforeseen dynamics.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Remarks}
\framesubtitleTC{Even more general ideas to remember}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item To set up a control \TC{you need a model}.
 \item A model for that purpose is a \TC{dynamic system}.
 \item If you need formal guarantees on control, you need the Systems Theory.
 \item []{\small
       \begin{quote}
        There is no royal road to geometry.\\
        \vspace{1mm}\hspace{70mm}Euclid
       \end{quote}
       }
 \item However, never stretch a model beyond its purpose.
 \item []{\small
       \begin{quote}
        Since all models are wrong the scientist cannot obtain a ``correct'' one\\
        by excessive elaboration. On the contrary following William of Occam\\
        he should seek an economical description of natural phenomena.\\
        Just as the ability to devise simple but evocative models\\
        is the signature of the great scientist so overelaboration\\
        and overparameterization is often the mark of mediocrity.\\
        \vspace{1mm}\hspace{70mm}George Box
       \end{quote}
       }
 \item []...often summarised as ``all models are wrong, some are useful''.
 \end{itemize}
\end{frame}


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

\usepackage{graphicx,caption}
\graphicspath{ {./images/} }
\usepackage{float}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[unicode]{hyperref}
\usepackage{amsmath}
\usepackage[shortlabels]{enumitem}

\title{Homework 4 - Theory/Laboratory}
\author{Dainese Fabio, 857661}
\date{March 29, 2020}

\begin{document}

\maketitle

\section{Reading and Comprehension}
    \begin{enumerate}
        \item The algorithm provided in the \textit{Blondel et al. (2008)} to find the optimal value of \(Q(C)\) can be described by the following two steps:
        \begin{enumerate}
            \item First, each node in the network is assigned to its own community. Then for each node \(i\), the change in modularity is calculated by removing \(i\) from its own community and moving it into the community of each neighbor \(j\) of \(i\).\newline
            Then, once the \(\Delta Q\) value is calculated for all the communities which are directly connected to \(i\), \(i\) is placed into the community that resulted in the greatest modularity increase. If no increase is possible, \(i\) remains in its original community. This process is applied repeatedly and sequentially to all nodes until no modularity increase can occur.

            \item In the second phase of the algorithm, it groups all of the nodes in the same community and builds a new network where nodes are the communities from the previous phase. Any links between nodes of the same community are now represented by self-loops on the new community node and links from multiple nodes in the same community to a node in a different community are represented by weighted edges between communities. Once the new network is created and the second phase has ended, the first phase can be re-applied to the new network.
        \end{enumerate}

        \noindent The previous steps are iterated until there are no more changes and a maximum of modularity is attained.

        \item The modularity \(Q(C)\) is upper bounded by \(1\) and lower bounded by \(-1\).\newline

        \par\noindent To prove that \(Q(C)\) is lower bounded by \(-1\) we have considered the case of having a complete network, which means having the adjacent matrix '\(A\)' equals to all ones except for the diagonal (of zeros - no self-loop allowed in an undirected graph).\newline
        In this scenario we have:

        \begin{align*}
            d_{i} &= \sum_{j=1}^{n} = A_{ij} = (n-1) \\
            d &= \frac{1}{2} \sum_{i=1}^{n} \sum_{j=1}^{n} A_{ij} = \frac{(n^{2}-n)}{2}
        \end{align*}

        \noindent Now we are going to use these previous quantities in the modularity formula:

        \begin{align*}
            Q(C) &= \frac{1}{2d} \sum_{i=1}^{n}\sum_{j=1}^{n} \left( A_{ij} - \frac{d_{i}d_{j}}{2d} \right) \delta (c_{i},c_{j}) \\
            &= \frac{1}{n^{2}-n} \left[\sum_{i=1}^{n}\sum_{j=1}^{n} \left( A_{ij} - \frac{(n-1)^{2}}{n^{2}-n}\right) \delta (c_{i},c_{j}) \right] \\
            &\leq \frac{1}{n^{2}} \left[\sum_{i=1}^{n}\sum_{j=1}^{n} \left( A_{ij} - \frac{n^{2}}{n^{2}}\right) \delta (c_{i},c_{j}) \right] \\
            &= \frac{1}{n^{2}} \left[\sum_{i=1}^{n}\sum_{j=1}^{n} \left( A_{ij} - 1\right) \delta (c_{i},c_{j}) \right] \\ \intertext{In this particular case of complete graph the value of \(A_{ij}\) will be equals to 1 every time \(i \neq j\), resulting \(( A_{ij} - 1)=(1-1)=0\). The only time in which the multiplication inside the summations is different from zero will be when \(i = j\) \((( A_{ij} - 1)=(0-1)=-1)\) and this happens \(n\) times.\newline Moreover we consider the quantity \(\delta (c_{i},c_{j})\) be equals to \(1\) since we are dealing with an unique giant partition.\newline Then we apply these knowledge into the formula, resulting:}
            &= \frac{1}{n^{2}} \left[n(-1)\right] = - \frac{n}{n^{2}} = - \frac{1}{n}
        \end{align*}

        \noindent Finally, we consider the following cases:
        \begin{itemize}
            \item \(n = 1\) : \(Q(C) = - \frac{1}{n} = -1\)
            \item \(n \to \infty\): \(Q(C) = \displaystyle{\lim_{n\to\infty} - \frac{1}{n}} = 0\)
        \end{itemize}

        Thus, we have proved that the \(Q(C)\) is lower bounded by \(-1\).\newline\newline

        \par\noindent Meanwhile, to prove that \(Q(C)\) is upper bounded by \(1\) we have considered the case of having a network with '\(n\)' isolated \textit{cliques} each of one having \(m\) nodes (meaning that \(|V| = n \cdot m\)). To recall also that the number of edges in a clique of \(m\) nodes is equal to \(\binom{m}{2}\).\newline

        \par\noindent As before, we applied these knowledge into the modularity formula, resulting:

        \begin{align*}
            Q(C) &= \frac{1}{2d} \sum_{i=1}^{n}\sum_{j=1}^{n} \left( A_{ij} - \frac{d_{i}d_{j}}{2d} \right) \delta (c_{i},c_{j}) \\
            &= \frac{1}{2d} \sum_{k=1}^{n} \left[\binom{m}{2}-\frac{(m-1)^{2}}{2d}\right] \\
            &= \frac{1}{2d} \sum_{k=1}^{n} \left[\frac{m(m-1)}{2}-\frac{(m-1)^{2}}{2d}\right] \\
            &= \frac{1}{2d} \sum_{k=1}^{n} \left[\frac{(m^2-m)d-(m-1)^{2}}{2d}\right] \\
            &= \frac{1}{2d} \sum_{k=1}^{n} \left[\frac{((d-1)m+1)(m-1)}{2d}\right] \\
            &= \frac{((d-1)m+1)(m-1)n}{4d^{2}} \\
            &= \frac{((\frac{nm(m-1)}{2}-1)m+1)(m-1)n}{4\left(\frac{nm(m-1)}{2}\right)^{2}} \\
            &= \frac{((\frac{nm(m-1)}{2}-1)m+1)(m-1)n}{4 \frac{n^{2}m^{2}(m-1)^{2}}{4}} \\
            &= \frac{nm^{2}(m-1)-2m+2}{nm^{2}(m-1)} \\
            &= 1 - \frac{2m-2}{nm^{2}(m-1)} \\
            &= 1 - \frac{2(m-1)}{nm^{2}(m-1)} \\
            &= 1 - \frac{2}{nm^{2}} = 1 - \frac{2}{|V|m}
        \end{align*}

        \noindent Finally, if we consider the case of \(|V| \to \infty\), we have:
        \begin{align*}
            Q(C) = \displaystyle{\lim_{|V|\to\infty} \left(1-\frac{2}{|V|m}\right)} = 1 - 0 = 1
        \end{align*}
        Thus, we have proved that the \(Q(C)\) is upper bounded by \(1\).

        \item Consider the new partition defined in the given task, we have to prove that:
        \begin{align*}
            Q(\Tilde{C}) - Q(C) &= \frac{k_{2h}}{d} - \frac{k_{1h}}{d} + \frac{d_{h}k_{1}}{2d^{2}} - \frac{d_{h}k_{2}}{2d^{2}} \\
            \intertext{We start by recalling the definition of \(Q(C)\):}
            Q(C) &= \frac{1}{2d} \sum_{i=1}^{n}\sum_{j=1}^{n} \left( A_{ij} - \frac{d_{i}d_{j}}{2d} \right) \delta (c_{i},c_{j})
            = \sum_{i=1}^{R} Q(C_{i})
            \intertext{Where \(Q(C_{i})\) is defined as follows:}
            Q(C_{x}) &= \frac{1}{2d} \sum_{i,j \in C_{x}} \left(A_{ij}-\frac{d_{i}d_{j}}{2d}\right)
        \end{align*}
        \noindent Now we apply these definition to prove the desired result:
        \begin{align*}
            Q(\Tilde{C}) - Q(C) &= \sum_{i=1}^{R} (Q(\Tilde{C_{i}}) - Q(C_{i})) \\
            &= Q(\Tilde{C_{1}}) - Q(C_{1}) + Q(\Tilde{C_{2}}) - Q(C_{2}) + \sum_{i=3}^{R} (Q(\Tilde{C_{i}}) - Q(C_{i})) \\
            \intertext{Since the summation is equals to \(0\), the equation becomes:}
            &= Q(\Tilde{C_{1}}) - Q(C_{1}) + Q(\Tilde{C_{2}}) - Q(C_{2}) \\
            \intertext{Now we substitute the terms with their definitions:}
            \intertext{\[= \frac{1}{2d} \left[ \sum_{i,j \in \Tilde{C_{1}}} \left(A_{ij}-\frac{d_{i}d_{j}}{2d}\right) - \sum_{i,j \in C_{1}} \left(A_{ij}-\frac{d_{i}d_{j}}{2d}\right) + \sum_{i,j \in \Tilde{C_{2}}} \left(A_{ij}-\frac{d_{i}d_{j}}{2d}\right) - \sum_{i,j \in C_{2}} \left(A_{ij}-\frac{d_{i}d_{j}}{2d}\right)\right]\]}
            \intertext{And finally we apply the given definition of \(k_{r}\) and \(k_{rh}\) into the equation, resulting:}
            &= \frac{1}{2d} \left[ 2k_{2h} - 2k_{1h} + \frac{2d_{h}k_{1}}{2d} - \frac{2d_{h}k_{2}}{2d} \right] \\
            &= \frac{k_{2h}}{d} - \frac{k_{1h}}{d} + \frac{d_{h}k_{1}}{2d^{2}} - \frac{d_{h}k_{2}}{2d^{2}}
        \end{align*}

        \item Consider the new partition defined in the given task, we have to prove that:
        \begin{align*}
            Q(C) - Q(\Tilde{C}) &=  \frac{d_{h}k_{2}}{2d^{2}} - \frac{k_{2h}}{d}
            \intertext{We start by multiplying all by \(-1\), obtaining:}
            Q(\Tilde{C}) - Q(C) \\
            \intertext{Using the knowledge of the previous point we can substitute the values, resulting:}
            Q(\Tilde{C}) - Q(C) &= \frac{k_{2h}}{d} - \frac{k_{1h}}{d} + \frac{d_{h}k_{1}}{2d^{2}} - \frac{d_{h}k_{2}}{2d^{2}} \\
            \intertext{Since \(\Tilde{C}=\O\) we have that \(k_{1h} = k_{1} = 0\), thus:}
            Q(\Tilde{C}) - Q(C) &= \frac{k_{2h}}{d} - \frac{d_{h}k_{2}}{2d^{2}}
            \intertext{Finally, we multiply all again by \(-1\) and we obtain the expected result, as reported below:}
            Q(C) - Q(\Tilde{C}) &=  \frac{d_{h}k_{2}}{2d^{2}} - \frac{k_{2h}}{d}
        \end{align*}
    \end{enumerate}

\section{Interlocking Directorate}
    \subsection{Subgraph Extraction}
        \begin{figure}[H]
            \centering
            \includegraphics[width=1\textwidth]{1.png}
            \caption{Network}
            \label{fig:figure-1}
        \end{figure}

    \subsection{Connected Components}
        In the network represented in 'Figure \ref{fig:figure-1}' there are:
        \begin{itemize}
            \item 533 nodes;
            \item 2251 edges;
            \item 83 weakly connected components.
        \end{itemize}

        \noindent Moreover, in the 'Figure \ref{fig:figure-2.0}' are highlighted in red the first and the second largest connected components.
        \begin{figure}[H]
            \centering
            \includegraphics[width=1\textwidth]{2.0.png}
            \caption{}
            \label{fig:figure-2.0}
        \end{figure}

        \noindent Just to make it more clear, here the isolated two most largest connected components:
        \begin{figure}[H]
            \centering
            \begin{minipage}[b]{0.4\textwidth}
              \includegraphics[width=\textwidth]{2.1.png}
                \subcaption{Largest connected component}
            \label{fig:figure-2.1}
            \end{minipage}
            \begin{minipage}[b]{0.55\textwidth}
              \includegraphics[width=\textwidth]{2.2.png}
                \subcaption{Second largest connected component}
            \label{fig:figure-2.2}
            \end{minipage}
            \label{fig:figure-2}
        \end{figure}

        \noindent To isolate the largest connected component it has been used the '\textit{Giant Component}' filter, meanwhile to find the second largest connected component it has been applied the following filters: "Giant Component" \(\to\) "NOT (Nodes)" \(\to\) "Giant Component" (where "\(\to\)" means to applying as a sub-filter).

        \subsection{Communities}
        In order to find the communities in the network it has been used the '\textit{Modularity}' statistic  with different resolutions (and without \textit{edge weights}).\newline

        \par\noindent With resolution \(0.5\) the results are:
        \begin{itemize}
            \item 89 communities;
            \item 8 communities belonging to the giant component;
            \item By using as a node size the \textit{betweenness centrality} (size between 1 and 16) and colouring each community with a different colour, the result is:
            \begin{figure}[H]
                \centering
             \includegraphics[width=1\textwidth]{3.1.png}
                \caption{}
                \label{fig:figure-3.1}
            \end{figure}

        \end{itemize}

        \par\noindent Meanwhile with resolution \(1\) the results are:
        \begin{itemize}
            \item 85 communities;
            \item 7 communities belonging to the giant component;
            \item By using as a node size the \textit{betweenness centrality} (size between 1 and 16) and colouring each community with a different colour, the result is:
            \begin{figure}[H]
                \centering
             \includegraphics[width=1\textwidth]{3.2.png}
                \caption{}
                \label{fig:figure-3.2}
            \end{figure}
        \end{itemize}

        \par\noindent Finally with resolution \(2\) the results are:
        \begin{itemize}
            \item 85 communities;
            \item 6 communities belonging to the giant component;
            \item By using as a node size the \textit{betweenness centrality} (size between 1 and 16) and colouring each community with a different colour, the result is:
            \begin{figure}[H]
                \centering
             \includegraphics[width=1\textwidth]{3.3.png}
                \caption{}
                \label{fig:figure-3.3}
            \end{figure}
        \end{itemize}

        \noindent The number of communities and the number of connected components can be different because the definition of \textit{communities} doesn't imply that there must not be any edges between communities, meanwhile that's a requisite to identify a single connected component.\newline

        \par\noindent So, for example, in case of a network composed by a series of isolated cliques, the number of communities and the one of connected components are equal. At the same time if the network is composed by a series of cliques weakly connected between each other, the number of communities and the one of connected components are different.

\end{document}


\documentclass{article}[12pt]
\setlength{\evensidemargin}{0in}
\setlength{\oddsidemargin}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9in}
\setlength{\topmargin}{-.75in}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{arrows, decorations.markings}
\usepackage{enumerate}
\usepackage{datetime}
\usepackage{natbib}
\usepackage{array}
\usepackage{hyperref}
\hypersetup{
colorlinks= true,
allcolors = blue
}
\usepackage{arev} % excellent sans-serif font that has its own math mode font!
\newtheorem{theorem}{Theorem}
\usepackage{floatrow}

\title{171.107 Quiz 2 Solutions}
\author{Michael Busch \& Alexander de la Vega}
\date{September 23, 2016}

\begin{document}
\maketitle

\tikzset{->-/.style={decoration={
  markings,
  mark=at position #1 with {\arrow{>}}},postaction={decorate}}}

\section*{Quiz Question}
\begin{large} A soccer ball is kicked with an initial horizontal velocity of 4 m/s and an initial vertical velocity of 3 m/s.
\begin{enumerate}
\item What is the initial speed of the ball?
\item What is the maximum height the ball goes above the ground?
\item How far from where it was kicked will the ball land?
\end{enumerate}

\end{large}

\section*{Quiz Solution}

\begin{figure}[h!]
\centering

%\resizebox{5in}{!}{ % to resize
\begin{tikzpicture}

% grid lines
%\draw [help lines] (-8, -3) grid (8, 5);

% plot trajectory
\draw [domain=-3:3.1, thick=1] plot (\x, {0.1 + 5.9 * (0.2 * (\x + 3)) - 0.5 * 9.81 * (0.2 * (\x + 3))^2});
\draw [domain=-3:-1, -triangle 45=0.5] plot (\x, {0.1 + 5.9 * (0.2 * (\x + 3)) - 0.5 * 9.81 * (0.2 * (\x + 3))^2});

% angle
\draw [thick=1] (-3, 0) ++(0:.75) arc(0:50:0.75);
\node at (-2.1, 0.4) {$\theta$};

% soccer ball
\node at (-3, .17) {\includegraphics[height=0.15in]{soccer.jpg}};

% 'ground'
\draw [thick=1.5pt] (-6, 0) -- (6, 0);

% brackets
\draw [thick=1pt] (0, -1) to [out=90, in=180] (1.5, -0.5) to [out=0, in=-150] (3, -.25);
\draw [thick=1pt] (0, -1) to [out=90, in=0] (-1.5, -0.5) to [out=180, in=-30] (-3, -.25);

\draw [thick=1pt] (0.5, 0.9) to [out=180, in=90] (0.25, 0.45) to [out=-90, in=30] (0, 0);
\draw [thick=1pt] (0.5, 0.9) to [out=180, in=-90] (0.25, 1.35) to [out=90, in=-30] (0, 1.8);

% labels
\node at (0.75, 0.9) {$h$};
\node at (0, -1.2) {$d$};
\end{tikzpicture} %}

\caption{Trajectory of the soccer ball. We denote the max height it travels $h$ and the distance crossed $d$.}
\label{fig:diagram}
\end{figure}

\begin{large}

\noindent This problem is a straightforward 2D kinematics problem in projectile motion. We didn't allow calculators for this quiz,
so to simplify calculations, we let $g = -10 \ \text{m/s}^2$. The trajectory and values of interest are shown in Figure \ref{fig:diagram}.

\begin{enumerate}

% 1
\item For some initial velocity $\text{{\it V}}_{\text{{\it 0}}}$, we know that the {\it y} - and {\it x} - components of the velocity are
$$ V_{0_y} = V_0 \sin \theta = \text{3 m/s}$$
$$ V_{0_x} = V_0 \cos \theta = \text{4 m/s},$$

so using the either the Pythagorean theorem or the trigonometric identity $\sin^2 \theta + \cos^2 \theta = 1$, we find

\begin{align*} V_0 &= \sqrt{V_{0_y}^2 + V_{0_x}^2} \\
&= \sqrt{3^2 + 4^2} \\
&= 5 \ \text{m/s}  \qquad \qquad \begin{small} \framebox[1.1\width]{3 pts + $\frac{1}{3}$ pts for units} \end{small}
\end{align*}

% 2
\item \label{part2} There are two ways to go about solving this part: solving for the time to maximum height and finding $h$ afterwards, or using
$V_f^2 = V_0^2 + 2ah$. For the first method, we write

$$ V_{f_y} = V_{0_y} + \frac{1}{2} g t, \qquad \qquad \begin{small} \framebox[1.1\width]{1 pt} \end{small}$$

for which we find time to max height (when $V_{f_y} = 0$)

$$ t = \frac{V_{0_y}}{2g} = 0.3 \ \text{s} \qquad \qquad \begin{small} \framebox[1.1\width]{1 pt} \end{small}$$

Plugging this time into $y = V_{0_y} t + \frac{1}{2} g t^2$, we find
$$ h = \frac{9}{20} = 0.45 \ \text{m}. \qquad \qquad \begin{small} \framebox[1.1\width]{1 pt + $\frac{1}{3}$ pts for units} \end{small} $$

Finding $h$ with the second method is more direct: we write

$$ V_{f_y}^2 = V_{0_y}^2 + 2gh, \qquad \qquad \begin{small} \framebox[1.1\width]{2 pts} \end{small}$$

and noting $V_{f_y} = 0$ at $h$, we solve for $h$ and find

$$ h = \frac{9}{20} = 0.45 \ \text{m}. \qquad \qquad \begin{small} \framebox[1.1\width]{1 pt + $\frac{1}{3}$ pts for units} \end{small} $$

% 3
\item To find out how far horizontally the ball travels, we note that the $x$-component of the velocity is constant throughout the motion of the soccer ball.
Thus to find the maximum horizontal distance traveled, we write

$$ d = V_{0_x} t_d, \qquad \qquad \begin{small} \framebox[1.1\width]{1 pt} \end{small}$$

where $t_d$ is the time to travel to max distance $d$. If the first method had been applied in Part \ref{part2}, then $t_d = 2 \times 0.3 = 0.6 \ \text{s}$, as
the time to the maximum height is half that of the time to the max horizontal distance covered
(as the ball is launched from and lands at the same height). Had the first method
in the last part not been applied, one could find the time by using the quadratic formula for the equation $0 = V_{0_y} t + \frac{1}{2}gt^2$ or
$-5t^2 + 3t = 0$, which has roots 0 and 0.6 (note here that we find the times where $y=0$).
\framebox[1.1\width]{1 point} was assigned for finding the time. Thus we find for $d$:

$$d = V_{0_x} t_d = 4 \times 0.6 = 2.4 \ \text{m}. \qquad \qquad \begin{small} \framebox[1.1\width]{1 pt + $\frac{1}{3}$ pts for units} \end{small}$$

\end{enumerate}

\noindent Note that, as we're given the two components of the initial velocity, the angle $\theta$ need not play any role in any of the questions asked here.
For those who used the equation for maximum height,

$$ h = \frac{V_0^2 \sin^2 \theta}{2g},$$

and that of the horizontal distance covered,

$$ d = \frac{V_0^2 \sin 2\theta}{g},$$

we also assigned full points, but we would prefer if these equations were derived on future quizzes.

\end{large}

\end{document}