Skip to content

Commit d56fe4f

Browse files
committed
add rbbits3mine
1 parent 49f0701 commit d56fe4f

2 files changed

Lines changed: 224 additions & 1 deletion

File tree

‎code/rabbits3mine.org‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#+name: pre
22
#+BEGIN_SRC python
3-
from modsim import *
3+
from modsim import (TimeSeries, TimeFrame, System, State, linrange)
44

55
init = State(juveniles=0, adults=10)
66
system = System(init=init, t0 = 0, t_end=20, birth_rate=0.90,
@@ -77,3 +77,69 @@
7777
21 40.546917
7878
dtype: float64
7979
#+end_example
80+
81+
#+name: frame
82+
#+BEGIN_SRC python :noweb yes :results output
83+
<<pre>>
84+
85+
def run_simulation(system):
86+
"""Runs a proportional growth model.
87+
88+
Adds TimeSeries to `system` as `results`.
89+
90+
system: System object
91+
"""
92+
frame = TimeFrame(columns=system.init.index)
93+
frame.loc[system.t0] = system.init
94+
95+
for t in linrange(system.t0, system.t_end):
96+
maturations = system.mature_rate * frame['juveniles'][t]
97+
births = system.birth_rate * frame['adults'][t]
98+
deaths = system.death_rate * frame['adults'][t]
99+
100+
if frame['adults'][t] > 30:
101+
market = frame['adults'][t] - 30
102+
else:
103+
market = 0
104+
105+
frame.loc[t + 1] = [frame['juveniles'][t] + births - maturations,
106+
frame['juveniles'][t] + maturations - deaths - market]
107+
108+
system.results = frame
109+
run_simulation(system)
110+
print(system.results)
111+
#+END_SRC
112+
113+
#+RESULTS: frame
114+
#+begin_example
115+
juveniles adults
116+
0 0.000000 10.000000
117+
1 9.000000 -5.000000
118+
2 1.530000 14.470000
119+
3 14.048100 -5.200100
120+
4 4.732137 21.284023
121+
5 22.326152 -4.348269
122+
6 11.045080 31.867917
123+
7 36.081329 -3.111920
124+
8 21.373763 49.544128
125+
9 58.910136 -15.889088
126+
10 25.169612 86.295025
127+
11 94.529162 -65.966953
128+
12 3.964281 158.707262
129+
13 145.492605 -202.788399
130+
14 -85.029514 294.899364
131+
15 208.439653 -525.438299
132+
16 -333.239902 539.943888
133+
17 262.678765 -1223.124901
134+
18 -924.817639 960.925208
135+
19 245.204869 -2641.395271
136+
20 -2212.968481 1646.820111
137+
21 -0.550783 -5383.478247
138+
#+end_example
139+
140+
#+BEGIN_SRC python :noweb yes
141+
<<pre>>
142+
143+
def plot_results(sytsem):
144+
system.results.plot()
145+
#+END_SRC

‎code/rabbits3mine.tex‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
% Created 2017-10-03 Tue 19:55
2+
% Intended LaTeX compiler: pdflatex
3+
\documentclass[11pt]{article}
4+
\usepackage[utf8]{inputenc}
5+
\usepackage[T1]{fontenc}
6+
\usepackage{graphicx}
7+
\usepackage{grffile}
8+
\usepackage{longtable}
9+
\usepackage{wrapfig}
10+
\usepackage{rotating}
11+
\usepackage[normalem]{ulem}
12+
\usepackage{amsmath}
13+
\usepackage{textcomp}
14+
\usepackage{amssymb}
15+
\usepackage{capt-of}
16+
\usepackage{hyperref}
17+
\usepackage{minted}
18+
\author{Kawin Nikomborirak}
19+
\date{\today}
20+
\title{}
21+
\hypersetup{
22+
pdfauthor={Kawin Nikomborirak},
23+
pdftitle={},
24+
pdfkeywords={},
25+
pdfsubject={},
26+
pdfcreator={Emacs 25.3.1 (Org mode 9.1.1)},
27+
pdflang={English}}
28+
\begin{document}
29+
30+
\tableofcontents
31+
32+
\begin{minted}[frame=single]{python}
33+
from modsim import (TimeSeries, TimeFrame, System, State, linrange)
34+
35+
init = State(juveniles=0, adults=10)
36+
system = System(init=init, t0 = 0, t_end=20, birth_rate=0.90,
37+
mature_rate=0.33, death_rate=0.5)
38+
\end{minted}
39+
40+
\begin{minted}[frame=single]{python}
41+
<<pre>>
42+
43+
def run_simulation(system):
44+
"""Runs a proportional growth model.
45+
46+
Adds TimeSeries to `system` as `results`.
47+
48+
system: System object
49+
"""
50+
juveniles = TimeSeries()
51+
juveniles[system.t0] = system.init.juveniles
52+
53+
adults = TimeSeries()
54+
adults[system.t0] = system.init.adults
55+
56+
for t in linrange(system.t0, system.t_end):
57+
maturations = system.mature_rate * juveniles[t]
58+
births = system.birth_rate * adults[t]
59+
deaths = system.death_rate * adults[t]
60+
61+
if adults[t] > 30:
62+
market = adults[t] - 30
63+
else:
64+
market = 0
65+
66+
juveniles[t + 1] = juveniles[t] + births - maturations
67+
adults[t + 1] = adults[t] + maturations - deaths - market
68+
69+
system.adults = adults
70+
system.juveniles = juveniles
71+
\end{minted}
72+
73+
\begin{minted}[frame=single]{python}
74+
from modsim import (TimeSeries, TimeFrame, System, State, linrange)
75+
76+
init = State(juveniles=0, adults=10)
77+
system = System(init=init, t0 = 0, t_end=20, birth_rate=0.90,
78+
mature_rate=0.33, death_rate=0.5)
79+
80+
def run_simulation(system):
81+
"""Runs a proportional growth model.
82+
83+
Adds TimeSeries to `system` as `results`.
84+
85+
system: System object
86+
"""
87+
juveniles = TimeSeries()
88+
juveniles[system.t0] = system.init.juveniles
89+
90+
adults = TimeSeries()
91+
adults[system.t0] = system.init.adults
92+
93+
for t in linrange(system.t0, system.t_end):
94+
maturations = system.mature_rate * juveniles[t]
95+
births = system.birth_rate * adults[t]
96+
deaths = system.death_rate * adults[t]
97+
98+
if adults[t] > 30:
99+
market = adults[t] - 30
100+
else:
101+
market = 0
102+
103+
juveniles[t + 1] = juveniles[t] + births - maturations
104+
adults[t + 1] = adults[t] + maturations - deaths - market
105+
106+
system.adults = adults
107+
system.juveniles = juveniles
108+
run_simulation(system)
109+
print(system.adults)
110+
\end{minted}
111+
112+
\begin{minted}[frame=single]{python}
113+
from modsim import (TimeSeries, TimeFrame, System, State, linrange)
114+
115+
init = State(juveniles=0, adults=10)
116+
system = System(init=init, t0 = 0, t_end=20, birth_rate=0.90,
117+
mature_rate=0.33, death_rate=0.5)
118+
119+
def run_simulation(system):
120+
"""Runs a proportional growth model.
121+
122+
Adds TimeSeries to `system` as `results`.
123+
124+
system: System object
125+
"""
126+
frame = TimeFrame(columns=system.init.index)
127+
frame.loc[system.t0] = system.init
128+
129+
for t in linrange(system.t0, system.t_end):
130+
maturations = system.mature_rate * frame['juveniles'][t]
131+
births = system.birth_rate * frame['adults'][t]
132+
deaths = system.death_rate * frame['adults'][t]
133+
134+
if frame['adults'][t] > 30:
135+
market = frame['adults'][t] - 30
136+
else:
137+
market = 0
138+
139+
frame.loc[t + 1] = [frame['juveniles'][t] + births - maturations,
140+
frame['juveniles'][t] + maturations - deaths - market]
141+
142+
system.results = frame
143+
run_simulation(system)
144+
print(system.results)
145+
\end{minted}
146+
147+
\begin{minted}[frame=single]{python}
148+
from modsim import (TimeSeries, TimeFrame, System, State, linrange)
149+
150+
init = State(juveniles=0, adults=10)
151+
system = System(init=init, t0 = 0, t_end=20, birth_rate=0.90,
152+
mature_rate=0.33, death_rate=0.5)
153+
154+
def plot_results(sytsem):
155+
system.results.plot()
156+
\end{minted}
157+
\end{document}

0 commit comments

Comments
 (0)