-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
237 lines (195 loc) · 7.44 KB
/
simulation.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Template for BioSim class.
"""
import csv
import random
import sys
from os import path
from matplotlib import pyplot as plt
from biosim.ecosystem.fauna import Herbivore, Carnivore
from biosim.ecosystem.rossumoya import Rossumoya
from biosim.visualization.visuals import Visuals
# The material in this file is licensed under the BSD 3-clause license
# https://opensource.org/licenses/BSD-3-Clause
# (C) Copyright 2023 Hans Ekkehard Plesser / NMBU
class BioSim:
"""
Top-level interface to BioSim package.
"""
def __init__(self,
island_map,
ini_pop,
seed,
vis_years=1,
ymax_animals=None,
cmax_animals=None,
hist_specs=None,
img_years=None,
img_dir=None,
img_base=None,
img_fmt='png',
log_file=None,
console_output_island=False):
"""
Parameters
----------
island_map : str
Multi-line string specifying island geography
ini_pop : list
List of dictionaries specifying initial population
seed : int
Integer used as random number seed
vis_years : int
Years between visualization updates (if 0, disable graphics)
ymax_animals : int
Number specifying y-axis limit for graph showing animal numbers
cmax_animals : dict
Color-scale limits for animal densities, see below
hist_specs : dict
Specifications for histograms, see below
img_years : int
Years between visualizations saved to files (default: `vis_years`)
img_dir : str
Path to directory for figures
img_base : str
Beginning of file name for figures
img_fmt : str
File type for figures, e.g. 'png' or 'pdf'
log_file : str
If given, write animal counts to this file
Notes
-----
- If `ymax_animals` is None, the y-axis limit should be adjusted automatically.
- If `cmax_animals` is None, sensible, fixed default values should be used.
- `cmax_animals` is a dict mapping species names to numbers, e.g.,
.. code:: python
{'Herbivore': 50, 'Carnivore': 20}
- `hist_specs` is a dictionary with one entry per property for which a histogram
shall be shown. For each property, a dictionary providing the maximum value
and the bin width must be given, e.g.,
.. code:: python
{'weight': {'max': 80, 'delta': 2},
'fitness': {'max': 1.0, 'delta': 0.05}}
Permitted properties are 'weight', 'age', 'fitness'.
- If `img_dir` is None, no figures are written to file.
- Filenames are formed as
.. code:: python
Path(img_dir) / f'{img_base}_{img_number:05d}.{img_fmt}'
where `img_number` are consecutive image numbers starting from 0.
- `img_dir` and `img_base` must either be both None or both strings.
"""
self._island_map = island_map
self._island = Rossumoya(island_map)
self._island.populate_island(ini_pop, initial=True)
random.seed(seed)
self._visuals = Visuals(vis_years,
ymax_animals,
cmax_animals,
hist_specs,
img_years,
img_dir,
img_base,
img_fmt)
self._log_file = log_file
self._current_year = 0
self._console_output_island = console_output_island
def set_animal_parameters(self, species, params):
"""
Set parameters for animal species.
Parameters
----------
species : str
Name of species for which parameters shall be set.
params : dict
New parameter values
Raises
------
ValueError
If invalid parameter values are passed.
"""
Rossumoya.set_animal_params(species, params)
def set_landscape_parameters(self, landscape, params):
"""
Set parameters for landscape type.
Parameters
----------
landscape : str
Code letter for landscape
params : dict
New parameter values
Raises
------
ValueError
If invalid parameter values are passed.
"""
Rossumoya.set_island_params(landscape, params)
def simulate(self, num_years):
"""
Run simulation while visualizing the result.
Parameters
----------
num_years : int
Number of years to simulate
"""
csvfile = None
writer = None
if self._log_file is not None:
file_path = f"{sys.path[0]}/{self._log_file}.csv"
new_file = not path.exists(file_path)
csvfile = open(f"{sys.path[0]}/{self._log_file}.csv", 'a', newline="", encoding="utf-8")
writer = csv.writer(csvfile, delimiter=',')
if new_file:
writer.writerow(["Year", "Herbivore Count", "Carnivore Count"])
self._print_migration_data()
if self._visuals.is_enabled():
if self._current_year == 0:
self._visuals.initialize_figure(num_years, self._island.animal_details())
self._visuals.set_island(self._island_map)
else:
self._visuals.extend_figure(self._current_year, num_years, )
else:
print("Graphics is disabled.")
for _ in range(num_years):
if self._log_file is not None:
writer.writerow([self._current_year, Herbivore.count(), Carnivore.count()])
self._island.go_through_annual_cycle()
self._print_migration_data()
if self._visuals.is_enabled() and \
self._current_year % self._visuals.vis_years == 0:
self._visuals.refresh(self._current_year, self._island.animal_details())
self._current_year += 1
if self._log_file is not None:
csvfile.flush()
csvfile.close()
if self._visuals.is_enabled():
plt.show(block=False)
def add_population(self, population):
"""
Add a population to the island
Parameters
----------
population : List of dictionaries
See BioSim Task Description, Sec 3.3.3 for details.
"""
self._island.populate_island(population)
@property
def year(self):
"""Last year simulated."""
return self._current_year
@property
def num_animals(self):
"""Total number of animals on island."""
return Herbivore.count() + Carnivore.count()
@property
def num_animals_per_species(self):
"""Number of animals per species in island, as dictionary."""
return {'Herbivore': Herbivore.count(), 'Carnivore': Carnivore.count()}
def make_movie(self):
""" Create MPEG4 movie from visualization images saved."""
self._visuals.make_movie()
def _print_migration_data(self):
if self._console_output_island:
Rossumoya.console_output_island(True)
print(f"Y:{self._current_year}")
print(f"H:{Herbivore.count()}.C:{Carnivore.count()}")
print(self._island)