Source code for simulai.utilities.lotka_volterra_solver

# (C) Copyright IBM Corp. 2019, 2020, 2021, 2022.

#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at

#           http://www.apache.org/licenses/LICENSE-2.0

#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.

import numpy as np
from scipy.integrate import odeint

[docs]class LotkaVolterra: def __init__(self, alpha=None, beta=None, gamma=None, delta=None): self.alpha = alpha self.beta = beta self.gamma = gamma self.delta = delta
[docs] def eval(self, state: np.ndarray = None, t: float = None) -> np.ndarray: x = state[0] y = state[1] x_residual = self.alpha * x - self.beta * x * y y_residual = self.delta * x * y - self.gamma * y return np.array([x_residual, y_residual])
[docs] def run(self, initial_state, t): solution = odeint(self.eval, initial_state, t) return np.vstack(solution)