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