-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
harmonization of simulation solver method imports
- Loading branch information
1 parent
0b6a0e6
commit d021afd
Showing
7 changed files
with
94 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
from causal_inference.config import LV_PARAMS | ||
|
||
class LotkaVolterra(): | ||
''' | ||
Class simulates predator-prey dynamics and solves it with 4th order Runge-Kutta method. | ||
Base Lotka-Volterra Class that defines a predator-prey system. | ||
''' | ||
def __init__(self): | ||
self.A = LV_PARAMS['A'] | ||
self.B = LV_PARAMS['B'] | ||
self.C = LV_PARAMS['C'] | ||
self.D = LV_PARAMS['D'] | ||
self.time = LV_PARAMS['INITIAL_TIME'] | ||
self.step_size = LV_PARAMS['STEP_SIZE'] | ||
self.max_iterations = LV_PARAMS['MAX_ITERATIONS'] | ||
def __init__(self, | ||
A=LV_PARAMS['A'], B=LV_PARAMS['B'], C=LV_PARAMS['C'], D=LV_PARAMS['D'], | ||
prey_population=LV_PARAMS['INITIAL_PREY_POPULATION'], | ||
pred_population=LV_PARAMS['INITIAL_PREDATOR_POPULATION'], | ||
total_time=LV_PARAMS['TOTAL_TIME'], step_size=LV_PARAMS['STEP_SIZE'], | ||
max_iter=LV_PARAMS['MAX_ITERATIONS']): | ||
# Lotka-Volterra parameters | ||
self.A = A | ||
self.B = B | ||
self.C = C | ||
self.D = D | ||
|
||
self.prey_population = LV_PARAMS['INITIAL_PREY_POPULATION'] | ||
self.predator_population = LV_PARAMS['INITIAL_PREDATOR_POPULATION'] | ||
self.prey_population = prey_population # Initial prey population | ||
self.predator_population = pred_population # Initial predator population | ||
|
||
self.init_time = 0 # initial time | ||
self.total_time = total_time # total time in units | ||
self.step_size = step_size # increment for each time step | ||
self.max_iterations = max_iter # tolerance parameter | ||
|
||
self.time_stamps = np.arange(self.init_time, self.total_time, self.step_size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
from os.path import join | ||
import matplotlib.pyplot as plt | ||
|
||
def plot_population_over_time(prey_list, predator_list, time_stamps, results_dir, save=True, filename='predator_prey'): | ||
fig = plt.figure(figsize=(15, 5)) | ||
ax = fig.add_subplot(2, 1, 1) | ||
PreyLine, = plt.plot(time_stamps, prey_list, color='g') | ||
PredatorsLine, = plt.plot(time_stamps, predator_list, color='r') | ||
ax.set_xscale('log') | ||
|
||
plt.legend([PreyLine, PredatorsLine], ['Prey', 'Predators']) | ||
plt.ylabel('Population') | ||
plt.xlabel('Time') | ||
if save: | ||
plt.savefig(join(results_dir, f"{filename}.svg"), | ||
format='svg', transparent=False, bbox_inches='tight') | ||
else: | ||
plt.show() | ||
plt.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
from os.path import join | ||
import h5py | ||
|
||
def _save_population(prey_list, predator_list, time_stamps, results_dir): | ||
filename = join(results_dir, 'populations.h5') | ||
hf = h5py.File(filename, 'w') | ||
hf.create_dataset('time_stamp', data=time_stamps) | ||
hf.create_dataset('prey_pop', data=prey_list) | ||
hf.create_dataset('pred_pop', data=predator_list) | ||
hf.close() |