ABMEv_runworld.jl 3.03 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

"""
Victor's avatar
Victor committed
14
    function runWorld_store_WF(p,world0;mode="std")
15
16
Wright Fisher process. Returns an array worldall with every agents.
"""
Victor's avatar
Victor committed
17
function runWorld_store_WF(p,world0;mode="std")
Victor's avatar
Victor committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    # worldall = repeat(world0,inner = (1,length(1:Int(p["tend"]))))
    # N=length(world0);
    # newworld = copy.(world0)
    # if mode == "std"
    #     update_rates! = update_rates_std!
    # elseif mode == "2D"
    #     update_rates! = update_rates_2D!
    # elseif mode == "grad2D"
    #     update_rates! = update_rates_grad2D!
    # elseif mode == "mountain"
    #     update_rates! = update_rates_mountain!
    # elseif mode == "split"
    #     update_rates! = update_rates_std_split!
    # elseif mode == "graph"
    #     update_rates! = update_rates_graph!
    # else
    #     error("Mode $mode is not recognized")
    # end
    # for i in 1:(Int(p["tend"])-1)
    #     # we have to take views, otherwise it does not affect worldall
    #     world = @view worldall[:,i];newworld = @view worldall[:,i+1];
    #     updateWorld_WF!(world,newworld,p,update_rates!,Float64(i))
    # end
    # return worldall,collect(0:Int(p["tend"]-1))
42
43
end
"""
Victor's avatar
Victor committed
44
    function runWorld_store_G(p,world0)
Victor's avatar
Victor committed
45
46
47
48
49
50
Gillepsie process.
- returns a tuple 'worldall, tspanarray'
- `worldall` stores the world every `p["dt_saving"]` time steps.
If `p["dt_saving"]` not specified, it returns an array with two columns,
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.
51
"""
52
# function run(w::World{AbstractAgent{A,R},S,T},g::G;dt_saving=nothing,callbacks=nothing) where {G<:Gillepsie,A,R,S,T}
53
function run!(w::World{A,S,T},alg::L,tend::Number;
54
                dt_saving=nothing,
55
                cb=(names = String[],agg =nothing)) where {A,S,T,L<:AbstractAlg}
Victor's avatar
Victor committed
56
    n=size(w);
Victor's avatar
Victor committed
57
    NMax = maxsize(w)
58
    t = .0
Victor's avatar
Victor committed
59
60
61
62
    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}
63
        update_rates!(w,alg)
Victor's avatar
Victor committed
64
65
    end
    while t<tend
66
67
        if dt < 0
            throw("We obtained negative time step dt = $dt at event $i")
Victor's avatar
Victor committed
68
        elseif size(w) == NMax
69
70
            @info "All individuals have died :("
            break
Victor's avatar
Victor committed
71
        elseif size(w) == 0
72
73
74
            @info "We have reached the maximum number of individuals allowed"
            break
        end
Victor's avatar
Victor committed
75
76
        if  t - get_tend(sim) >= dt_saving
            @info "saving world @ t = $(t)/ $(tend)"
77
            add_entry!(sim,w)
78
        end
79
        dt = updateWorld!(w,alg)
Victor's avatar
Victor committed
80
        t +=  dt
81
82
        i += 1
    end
Victor's avatar
Victor committed
83
    # Saving last time step
Victor's avatar
Victor committed
84
    add_entry!(sim,w)
85
    clean!(sim)
Victor's avatar
Victor committed
86
    @info "simulation stopped at t=$(t), after $(i) generations"
Victor's avatar
Victor committed
87
    return sim
88
end