% -*- latex -*-
\documentclass[letter,12pt]{article}
%---------------------------------------------------------------
%\usepackage{listings}
\usepackage[framed]{matlab-prettifier}
\usepackage{color}
\usepackage{fullpage}
\usepackage{enumitem} % control over itemize, list environment
%---------------------------------------------------------------

\begin{document}
\newcommand{\hwtitle}[1]{%
	\begin{center}
		\Large  \bf Midterm #1%
	\end{center}%
}

\newcommand{\mat}[1]{% matlab code
%{\color{blue}\texttt{#1}}%
	% this one does not work inside of captions
	{\lstinline[style = Matlab-editor, basicstyle = \mlttfamily, columns=fixed]!#1!}%
	% this one work but style is different
	%{\lstinline[columns=fixed]!#1!}%
}
%---------------------------------------------------------------
\newcommand{\problem}[1]{% homework problem
	%{\item  \bf #1%}
\item {\bf #1{}:}% the next blank line is important

}

\newenvironment{homework}[2]%
{%
	\hwtitle{#1}
	#2% Prerequisits

	\begin{enumerate}[
		label={\bf Problem\ \arabic*}, 
		leftmargin=0pt,
		start,
		labelindent=0pt,
		widest=20, align=left, itemindent=!
		]
}%
{%
	\end{enumerate}
}%
%---------------------------------------------------------------

\begin{homework}{02 Bio hazard spread problem (100 points)}{
%Prerequisites

\begin{itemize}
	\item 
		One report per team is enough, but make sure everyone is
		listed in the authors list.
	\item
		Discuss the relevant physics equations, describe your solution, show
		results. Report page limit is 10 pages excluding listings which should be
		in appendixes, font size to be no less than 12pt. 
		The emailed submission must have all relevant listings in the attachments.
\end{itemize}

\vskip .5in
%Problem statement
%---------------------------------------------------------------

Imagine that  aliens are invading the  Earth. Your team's job  is to organize
the most  productive resistance (i.e. the  one which generates the  most
counter alien equipment  for a given number of days). 
You spread your team members randomly and uniformly across the available area (with only
1 person per cell).

The  aliens do  not  care  about the Geneva  convention  and  actively use  bio
weapons. On the zero day (and only this day), aliens  were able to infect 200 people (or
all of your group if its size is less then 200 people) at random places with a
virus. Luckily, it happens only once. However, every day aliens bomb the
planet with bio bombs, which results in 0.006 probability to get infected
with the same virus in that day for every human. The virus has following properties

\begin{itemize}
	\item the probability to transfer the virus to a next cell neighbor
		in all 4 directions (up, down, left, and right) 
		is the same and equals to 0.16 per interaction
	\item probability of dying from this virus is .04 per turn/day
	\item there is no cure for this virus
	\item virus cannot jump to an empty (unpopulated) cell
\end{itemize}

We will make 
a reasonably simple model of the virus spread and the production
of counter-alien equipment.

You are in charge  of the square territory  of 100$\times$100 cells.
Each cell  might have  a human or be empty.  At the beginning of each  day
all alive members generate 1 unit of the counter-alien equipment
per capita (it  does not matter if  a person is ill or  healthy). Then they
talk to the next cell neighbors to exchange the news and updated blueprints
(radio is jammed by aliens) in  the four directions (north, south, west and
east). Notice that each neighbor speaks or interacts with another twice, for example
north one goes to south and then south one goes the north to speak again.
If one of  the neighbors has the alien's virus  it has a probability
to transfer  to the other one  according to the virus  specification. 
Then this virus may kill infected people.  
If a person dies in  a certain cell that cell is treated as empty from the
next day forward.
After this the aliens bomb again trying to infect more people.
}

\problem{(40 points)}
%---------------------------------------------------------------

Program a probabilistic model of the virus spread on the given territory for a 
given population according to the above specification.
Write a function which calculates how many equipment
units will be produced for 1200 days of resistance.

Book keeping: Keep population statistic in the 2D array called 'AreaMap'
with the size of the area dimensions. Assign 1 to the empty cell, 2 to the
healthy human, 3 to the ill/infected human. I would suggest to have a
current population map and another map reflecting a new situation with newly
infected people and so on, since a human who was not infected at the
beginning of the current day cannot infect another one due to an incubation
period.

Hint: it is very useful to look at the area map from time to time during the debugging
to see how the virus has spread. Use the following code to output the
image of the area.
\begin{lstlisting} [language=matlab]
% assigns the black color to the empty cell, 
% green to the healthy human, and red to the ill person
population_colormap=[ 0,0,0; 0,1,0; 1,0,0]; 
image(AreaMap), colormap(population_colormap); % outputs the map image 
\end{lstlisting}

\problem{(20 points)}
%---------------------------------------------------------------

How many people will you take under your command to maximize the number of
units 
of the counter-alien equipment produced in 1200 days?
Using the function optimization algorithm (golden search or matlab built
in), find the optimal resistance group size. Run your algorithm several
times to estimate error bars on this number.

\problem{(20 points)}
%---------------------------------------------------------------
Make a movie of your group map evolution for the optimal group size during
the first 200 days. 
At the beginning of the every {\bf 5th}  day  execute the following
\begin{lstlisting} [language=matlab]
population_colormap=[ 0,0,0; 0,1,0; 1,0,0]; 
image(AreaMap), colormap(population_colormap); % outputs the map image 
% frame_counter must start from 1
map_evolution_movie(frame_counter)=getframe;  
frame_counter=frame_counter+1;
% this movie can be played with the 'movie' command
% i.e. movie(map_evolution_movie);
\end{lstlisting}

Save this movie at the very end of the simulation of the virus spread.
\begin{lstlisting} [language=matlab]
save('movie_file.mat', 'map_evolution_movie');
\end{lstlisting}
Attach this movie file 'movie\_file.mat' to your electronic submission.

You can check the validity of the movie with the following commands
\begin{lstlisting} [language=matlab]
load('movie_file.mat');
movie(map_evolution_movie);
\end{lstlisting}


\problem{(10 points)}
%---------------------------------------------------------------

For the optimal group size, plot the accumulated production of the counter-alien equipment vs. number of days since the
virus introduction. Make sure that you  show the
interesting transient behavior in the zoomed-in version.

{\flushleft \bf Task 5 (10 points):}

For the optimal group size, plot the accumulated number of people killed by
the virus vs. the day number. Make sure that you  show the
interesting transient behavior in the  zoomed-in version.



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