1

I am currently trying to write some code to simulate a Lotka-Volterra model about a population of preyes and predators.

I have written this code:

function PP_gen(N_prey::Int128, N_pred::Int128, max_time::Int128, λ_breed::Float64, λ_interaction::Float64, λ_death::Float64)::Vector{Int128}
    Prey_list = Vector{Int128}([N_prey])
    Pred_list = Vector{Int128}([N_pred])
    
    t = 1
    
    while t < max_time 
        t += 1
        new_Prey = Int128(0)
        new_Pred = Int128(0)
        
        breed = Int128(rand(Binomial(N_prey, λ_breed)));
        new_Prey = breed
        
        interaction = Int128(rand(Binomial(N_pred, λ_interaction)));
        new_Pred = interaction
        new_Prey -= interaction
        
        death = Int128(rand(Binomial(N_pred, λ_death)));
        new_Pred -= death
        
        N_prey += new_Prey
        N_pred += new_Pred
        
        if N_prey <= 0
            push!(Prey_list, 0);
            push!(Pred_list, N_pred)
            return Prey_list, Pred_list
        end
        
        if N_pred <= 0
            push!(Pred_list, 0);
            push!(Prey_list, N_prey)
            return Prey_list, Pred_list
        end
        
        push!(Prey_list, N_prey)
        push!(Pred_list, N_pred)
    end
    return Prey_list, Pred_list
end

I need to try a high number of iteration (for example 1000) but I can only manage to obtain the variables Prey_list and Pred_list as Vector{Int64}, which is a problem since Int64 incurs in stack overflow too soon. How can I manage to obtain an object of the type Vector{Int128}?

Kotatsu
  • 11
  • 2
  • Can you provide an example input, that is, what values are you giving to the function `PP_gen`? It is easier to help if we can run your code. It seems a bit odd that you need `Int128`, though, I must say. – DNF Apr 13 '23 at 16:00
  • Hi, thank you for your reply. I am trying to run the simulation with these parameters: `max_time = 1000 p_breed = 0.32 p_interaction = 0.3 p_death = 0.3 preys, predators=PP_gen(1000, 1000, max_time, p_breed, p_interaction, p_death)` – Kotatsu Apr 15 '23 at 14:10

1 Answers1

1

Changing the top of your method to:

function PP_gen(N_prey::Integer, N_pred::Integer, max_time::Integer, λ_breed::Float64, λ_interaction::Float64, λ_death::Float64)::Vector{Int128}
    Prey_list = Int128[N_prey]
    Pred_list = Int128[N_pred]

should be enough to make your code work.

(if it does not work then as @DFN commented seeing the whole code that is problematic would be useful)

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • Thank you for your reply. I tried running the code after applying your changes. I used these parameters `max_time = 1000 p_breed = 0.32 p_interaction = 0.3 p_death = 0.3 preys, predators=PP_gen(1000, 1000, max_time, p_breed, p_interaction, p_death)` but I get the error `InexactError: trunc(Int64, 10196591051645084449)` – Kotatsu Apr 15 '23 at 14:15
  • But in which line do you get this error? Somewhere in the code you are trying to convert 128 bit integer into 64 bit ingeger, but it is not clear where as the code is long. – Bogumił Kamiński Apr 15 '23 at 14:22