-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_sim.py
96 lines (84 loc) · 3.2 KB
/
sample_sim.py
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
"""
Full island simulation with herbivores and carnivores
including movie creation.
"""
__author__ = 'Hans Ekkehard Plesser, NMBU'
import re
import sys
import textwrap
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.path import Path
from biosim.simulation import BioSim
if __name__ == '__main__':
geogr = """\
WWWWWWWWWWWWWWWWWWWWW
WHHHHHLLLLWWLLLLLLLWW
WHHHHHLLLLWWLLLLLLLWW
WHHHHHLLLLWWLLLLLLLWW
WWHHLLLLLLLWWLLLLLLLW
WWHHLLLLLLLWWLLLLLLLW
WWWWWWWWHWWWWLLLLLLLW
WHHHHHLLLLWWLLLLLLLWW
WHHHHHHHHHWWLLLLLLWWW
WHHHHHDDDDDLLLLLLLWWW
WHHHHHDDDDDLLLLLLLWWW
WHHHHHDDDDDLLLLLLLWWW
WHHHHHDDDDDWWLLLLLWWW
WHHHHDDDDDDLLLLWWWWWW
WWHHHHDDDDDDLWWWWWWWW
WWHHHHDDDDDLLLWWWWWWW
WHHHHHDDDDDLLLLLLLWWW
WHHHHDDDDDDLLLLWWWWWW
WWHHHHDDDDDLLLWWWWWWW
WWWHHHHLLLLLLLWWWWWWW
WWWHHHHHHWWWWWWWWWWWW
WWWWWWWWWWWWWWWWWWWWW"""
geogr = textwrap.dedent(geogr)
ini_herbs = [{'loc': (2, 7),
'pop': [{'species': 'Herbivore',
'age': 5,
'weight': 20}
for _ in range(200)]}]
ini_carns = [{'loc': (2, 7),
'pop': [{'species': 'Carnivore',
'age': 5,
'weight': 20}
for _ in range(50)]}]
sim = BioSim(geogr, ini_herbs + ini_carns, seed=1,
hist_specs={'fitness': {'max': 1.0, 'delta': 0.05},
'age': {'max': 60.0, 'delta': 2},
'weight': {'max': 60, 'delta': 2}},
cmax_animals={'Herbivore': 200, 'Carnivore': 50},
img_dir='results',
img_base='sample', log_file=f'data/simulation_hc_{1:05d}')
sim.simulate(400)
sim.make_movie()
# Analyze logs:
data = []
for logfile in Path(f"{sys.path[0]}/data").glob('simulation_hc_*.csv'):
d = pd.read_csv(logfile,
skiprows=1,
index_col=0,
names=['Year', 'Herbivores', 'Carnivores'])
d['Seed'] = int(re.match(r'.*_(\d+)\.csv', str(logfile))[1])
data.append(d)
hc = pd.concat(data).pivot(columns='Seed')
print(hc.tail())
plt.plot(hc.Herbivores, 'b', alpha=0.4)
plt.plot(hc.Carnivores, 'r', alpha=0.4)
plt.show()
print(sum(hc.loc[399, 'Carnivores'] == 0))
print(sum(hc.loc[399, 'Herbivores'] == 0))
with_c = (hc.loc[399, 'Herbivores'] > 0) & (hc.loc[399, 'Carnivores'] > 0)
hc_eq = hc.loc[hc.index >= 175, np.hstack((with_c.values, with_c.values))]
print(hc_eq.Herbivores.unstack().mean(), hc_eq.Herbivores.unstack().std())
print(hc_eq.Carnivores.unstack().mean(), hc_eq.Carnivores.unstack().std())
bins = np.arange(0, 140, 2)
plt.hist(hc_eq.Herbivores.unstack(), bins=bins, fc='b', histtype='step', alpha=1, lw=3,
label='Herbivores')
plt.hist(hc_eq.Carnivores.unstack(), bins=bins, fc='r', histtype='step', alpha=1, lw=3,
label='Carnivores')
plt.legend()
plt.show()