% -*- latex -*-
% FILE: "/home/evmik/jobs/wm/2015_fall_practical_computing_for_scientists/midterm02/midterm02_2015.tex"
% LAST MODIFICATION: "Sun, 18 Oct 2015 21:23:58 -0400 (evmik)"
% (C) 2010 by Eugeniy Mikhailov, <evgmik@gmail.com>
% $Id:$
\documentclass[letter,12pt]{article}

%---------------------------------------------------------------
\usepackage{listings}
\usepackage{color}
\usepackage{fullpage}
%---------------------------------------------------------------

\begin{document}
\newcommand{\problem}[1]{%
	{\flushleft  \bf #1\\}
}

%---------------------------------------------------------------
%\title{Midterm 02}
%\author{Eugeniy E. Mikhailov}
%\date{\today}
%\date{}
%\maketitle
\begin{center}
{ \Large{Midterm 02}} 
\end{center}

%---------------------------------------------------------------

%\flushleft
{Discuss relevant  equations, describe you solution, show
results.
All Matlab code/scripts must be present in the carbon copy as well, put
them near relevant parts of the text (avoid appendixes).
The text part of your report should not exceed 10 pages.}

\vskip .5in

{\flushleft \bf Bio hazard spread problem (100 points total)}

%---------------------------------------------------------------

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 is the same and equals to 0.15 per day
	\item probability of dying from this virus is .05 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 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.



{\flushleft \bf Task 1 (40 points):}

Program a probabilistic model of the virus spread on the given territory for a 
given population. 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}

{\flushleft \bf Task 2 (20 points):}

How many people will you take under your command?
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.

{\flushleft \bf Task 3 (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 2nd}  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}


{\flushleft \bf Task 4 (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{document}
%---------------------------------------------------------------
