ABMEv_runworld.jl 1.9 KB
Newer Older
1
2
3
function  update_rates_graph!(world,C,p::Dict,t::Float64)
    for e in edges(p["g"])
        # agents on e
4
        aidx_e = findall(a -> get_x(a,1)==e,world)
5
6
7
8
9
10
11
12
13
        na = length(aidx_e)
        for i in aidx_e
            world[i].d = na^2
            world[i].b = 1
        end
    end
end

"""
14
15
16
$(SIGNATURES)
Run `w` with algorithm `alg`, until `tend` is reached.
Returns a `Simulation` type.
Victor's avatar
Victor committed
17
- `worldall` stores the world every `p["dt_saving"]` time steps.
18
If `dt_saving` not specified, `sim` contains an array of two elements,
Victor's avatar
Victor committed
19
20
first corresponding to initial conditions and last corresponding to world in the last time step.
>:warning: if you choose `nagents = 1` then nothing is saved until the end of the simulation.
21
"""
22
# function run(w::World{AbstractAgent{A,R},S,T},g::G;dt_saving=nothing,callbacks=nothing) where {G<:Gillepsie,A,R,S,T}
23
function run!(w::World{A,S,T},alg::L,tend::Number;
24
                dt_saving=nothing,
25
                cb=(names = String[],agg =nothing)) where {A,S,T,L<:AbstractAlg}
Victor's avatar
Victor committed
26
    n=size(w);
Victor's avatar
Victor committed
27
    NMax = maxsize(w)
28
    t = .0
Victor's avatar
Victor committed
29
30
31
32
    i = 1;j=1;dt = 0.
    isnothing(dt_saving) ? dt_saving =  tend + 1. : nothing
    sim = Simulation(w,cb=cb)
    if A <: AbstractAgent{AA,Rates{true}} where {AA}
33
        update_rates!(w,alg)
Victor's avatar
Victor committed
34
35
    end
    while t<tend
36
37
        if dt < 0
            throw("We obtained negative time step dt = $dt at event $i")
Victor's avatar
Victor committed
38
        elseif size(w) == NMax
39
40
            @info "All individuals have died :("
            break
Victor's avatar
Victor committed
41
        elseif size(w) == 0
42
43
44
            @info "We have reached the maximum number of individuals allowed"
            break
        end
Victor's avatar
Victor committed
45
46
        if  t - get_tend(sim) >= dt_saving
            @info "saving world @ t = $(t)/ $(tend)"
47
            add_entry!(sim,w)
48
        end
49
        dt = updateWorld!(w,alg)
Victor's avatar
Victor committed
50
        t +=  dt
51
52
        i += 1
    end
Victor's avatar
Victor committed
53
    # Saving last time step
Victor's avatar
Victor committed
54
    add_entry!(sim,w)
Victor's avatar
Victor committed
55
    @info "simulation stopped at t=$(t), after $(i) generations"
Victor's avatar
Victor committed
56
    return sim
57
end