[projects] add random-walk project
This commit is contained in:
parent
64161be6a8
commit
6710224e30
10
projects/project_random_walk/Makefile
Normal file
10
projects/project_random_walk/Makefile
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
latex:
|
||||||
|
pdflatex *.tex > /dev/null
|
||||||
|
pdflatex *.tex > /dev/null
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf *.log *.aux *.zip *.out auto
|
||||||
|
rm -f `basename *.tex .tex`.pdf
|
||||||
|
|
||||||
|
zip: latex
|
||||||
|
zip `basename *.tex .tex`.zip *.pdf *.dat *.mat
|
116
projects/project_random_walk/random_walk.py
Normal file
116
projects/project_random_walk/random_walk.py
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import matplotlib.mlab as mlab
|
||||||
|
import scipy.io as scio
|
||||||
|
from IPython import embed
|
||||||
|
|
||||||
|
|
||||||
|
def create_food_blotch(size_x=51, size_y=51, noise=0.01, threshold=0.008):
|
||||||
|
x = np.arange(-np.round(size_x/2), np.round(size_x/2)+1)
|
||||||
|
y = np.arange(-np.round(size_y/2), np.round(size_y/2)+1)
|
||||||
|
X, Y = np.meshgrid(x, y)
|
||||||
|
Z = mlab.bivariate_normal(X, Y, sigmax=10, sigmay=10)
|
||||||
|
food = (np.random.rand(size_x, size_y) * noise) + Z
|
||||||
|
return food
|
||||||
|
|
||||||
|
|
||||||
|
def create_world(width=100, height=100, d=0.1, food_sources=100, noise=0.01):
|
||||||
|
my_world = np.zeros((int(width/d), int(height/d)))
|
||||||
|
print("placing food sources ...")
|
||||||
|
for i in range(food_sources):
|
||||||
|
x = np.random.randint(0, width/d)
|
||||||
|
y = np.random.randint(0, height/d)
|
||||||
|
food = create_food_blotch(noise=noise)
|
||||||
|
if (x + food.shape[1]) > my_world.shape[1]:
|
||||||
|
x -= food.shape[1]
|
||||||
|
if (y + food.shape[0]) > my_world.shape[0]:
|
||||||
|
y -= food.shape[0]
|
||||||
|
my_world[y:y+food.shape[0], x:x+food.shape[1]] += food
|
||||||
|
return my_world
|
||||||
|
|
||||||
|
|
||||||
|
def get_step():
|
||||||
|
options = [-1, 1]
|
||||||
|
step = np.asarray([options[np.random.randint(0, 2)], options[np.random.randint(0, 2)]])
|
||||||
|
return step
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_step(pos, step, max_x, max_y):
|
||||||
|
new_pos = pos + step
|
||||||
|
if (new_pos[0] < max_y) & (new_pos[1] < max_x) & (new_pos[0] >= 0) & (new_pos[1] >= 0):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def do_valid_step(pos, max_x, max_y):
|
||||||
|
valid = False
|
||||||
|
while not valid:
|
||||||
|
step = get_step()
|
||||||
|
valid = is_valid_step(pos, step, max_x, max_y)
|
||||||
|
new_pos = pos + step
|
||||||
|
return new_pos, step
|
||||||
|
|
||||||
|
|
||||||
|
def random_walk(world, steps=10000):
|
||||||
|
x_positions = np.zeros(steps)
|
||||||
|
y_positions = np.zeros(steps)
|
||||||
|
start_x = np.random.randint(0, world.shape[1])
|
||||||
|
start_y = np.random.randint(0, world.shape[0])
|
||||||
|
eaten_food = 0
|
||||||
|
new_pos = np.asarray([start_y, start_x])
|
||||||
|
for i in range(steps):
|
||||||
|
new_pos, step = do_valid_step(new_pos, world.shape[1], world.shape[0])
|
||||||
|
x_positions[i] = new_pos[1]
|
||||||
|
y_positions[i] = new_pos[0]
|
||||||
|
eaten_food += world[new_pos[0], new_pos[1]]
|
||||||
|
world[new_pos[0], new_pos[1]] = 0.0
|
||||||
|
return x_positions, y_positions, eaten_food
|
||||||
|
|
||||||
|
|
||||||
|
def not_so_random_walk(world, steps=10000):
|
||||||
|
x_positions = np.zeros(steps)
|
||||||
|
y_positions = np.zeros(steps)
|
||||||
|
start_x = np.random.randint(0, world.shape[1])
|
||||||
|
start_y = np.random.randint(0, world.shape[0])
|
||||||
|
previous_food = 0.0
|
||||||
|
current_food = 0.0
|
||||||
|
eaten_food = 0
|
||||||
|
pos = np.asarray([start_y, start_x])
|
||||||
|
step = None
|
||||||
|
for i in range(steps):
|
||||||
|
gradient = current_food - previous_food
|
||||||
|
if (gradient <= 0) or ((step is not None) and \
|
||||||
|
(not is_valid_step(pos, step, world.shape[1], world.shape[0]))):
|
||||||
|
pos, step = do_valid_step(pos, world.shape[1], world.shape[0])
|
||||||
|
else:
|
||||||
|
pos += step
|
||||||
|
x_positions[i] = pos[1]
|
||||||
|
y_positions[i] = pos[0]
|
||||||
|
previous_food = current_food
|
||||||
|
current_food = world[pos[0], pos[1]]
|
||||||
|
eaten_food += current_food
|
||||||
|
world[pos[0], pos[1]] -= current_food
|
||||||
|
return x_positions, y_positions, eaten_food
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("create world... ")
|
||||||
|
world = create_world(noise=0.0, food_sources=50)
|
||||||
|
trials = 10
|
||||||
|
gain_random = np.zeros(trials)
|
||||||
|
gain_nsr = np.zeros(trials)
|
||||||
|
print("run")
|
||||||
|
for i in range(trials):
|
||||||
|
x, y, gain_random[i] = random_walk(world.copy(), 100000)
|
||||||
|
x2, y2, gain_nsr[i] = not_so_random_walk(world.copy(), 100000)
|
||||||
|
|
||||||
|
print("random walk yields: %.2f +- %.2f food per 100000 steps" % (np.mean(gain_random),
|
||||||
|
np.std(gain_random)))
|
||||||
|
print("not so random walk yields: %.2f +- %.2f food per 100000 steps" % (np.mean(gain_nsr),
|
||||||
|
np.std(gain_nsr)))
|
||||||
|
|
||||||
|
scio.savemat('random_world.mat', {'world': world})
|
||||||
|
plt.imshow(world)
|
||||||
|
plt.scatter(x[::2], y[::2], s=0.5, color='red')
|
||||||
|
plt.scatter(x2[::2], y2[::2], s=0.5, color='green')
|
||||||
|
plt.show()
|
68
projects/project_random_walk/random_walk.tex
Normal file
68
projects/project_random_walk/random_walk.tex
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
\documentclass[addpoints,11pt]{exam}
|
||||||
|
\usepackage{url}
|
||||||
|
\usepackage{color}
|
||||||
|
\usepackage{hyperref}
|
||||||
|
|
||||||
|
\pagestyle{headandfoot}
|
||||||
|
\runningheadrule
|
||||||
|
\firstpageheadrule
|
||||||
|
\firstpageheader{Scientific Computing}{Project Assignment}{WS 2016/17}
|
||||||
|
%\runningheader{Homework 01}{Page \thepage\ of \numpages}{23. October 2014}
|
||||||
|
\firstpagefooter{}{}{{\bf Supervisor:} Jan Grewe}
|
||||||
|
\runningfooter{}{}{}
|
||||||
|
\pointsinmargin
|
||||||
|
\bracketedpoints
|
||||||
|
|
||||||
|
%\printanswers
|
||||||
|
%\shadedsolutions
|
||||||
|
|
||||||
|
|
||||||
|
\begin{document}
|
||||||
|
%%%%%%%%%%%%%%%%%%%%% Submission instructions %%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
\sffamily
|
||||||
|
% \begin{flushright}
|
||||||
|
% \gradetable[h][questions]
|
||||||
|
% \end{flushright}
|
||||||
|
|
||||||
|
\begin{center}
|
||||||
|
\input{../disclaimer.tex}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%% Questions %%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
\section*{Random walk with memory.}
|
||||||
|
|
||||||
|
Some animals perform a random walk when searching for food. In some
|
||||||
|
cases this random walk is not completely random. In fact, sometimes
|
||||||
|
there is some memory involved. Whenever there is a positive gradient
|
||||||
|
in food gain between successive steps the animal will continue in the
|
||||||
|
very same direction as before. When the next step leads to a decrease
|
||||||
|
in food gain the animal switches back to a random walk.
|
||||||
|
|
||||||
|
|
||||||
|
\begin{questions}
|
||||||
|
\question{} The accompanying dataset (random\_world.mat) contains a
|
||||||
|
single variable stored. This is the world (10000\,m$^2$ area with
|
||||||
|
10\,cm spatial resolution) in which there are randomly distributed
|
||||||
|
food sources (Gaussian blotches of food).
|
||||||
|
|
||||||
|
\begin{parts}
|
||||||
|
\part{} Create a plot of the world.\\[0.5ex]
|
||||||
|
\part{} Create a model animal that performs a pure random walk. The
|
||||||
|
agent can walk in eight different directions (the cardinal and
|
||||||
|
diagonal directions) with a stepsize of 10\,cm
|
||||||
|
(approximately). Let the agent start at a random location in the
|
||||||
|
world and count how much food it eats in 10000 steps (eaten food
|
||||||
|
disappears from the world, of course). If the agent bumps into the
|
||||||
|
borders of the world choose a different direction.\\[0.5ex]
|
||||||
|
\part{} Plot a typical example walk. (You can also make an animation
|
||||||
|
with MATLAB)\\[0.5ex]
|
||||||
|
\part{} Same as above, but create a model animal that has some memory,
|
||||||
|
i.e. the direction is kept constant as long as there is a positive
|
||||||
|
gradient in the food gain. Otherwise a random walk is performed\\[0.5ex]
|
||||||
|
\part{} Plot a typical example walk also for this agent.\\[0.5ex]
|
||||||
|
\part{} Compare the performance of the two agents. Create appropriate
|
||||||
|
plots and apply statistical methods.
|
||||||
|
\end{parts}
|
||||||
|
\end{questions}
|
||||||
|
|
||||||
|
\end{document}
|
BIN
projects/project_random_walk/random_world.mat
Normal file
BIN
projects/project_random_walk/random_world.mat
Normal file
Binary file not shown.
Reference in New Issue
Block a user