Skip to content
Snippets Groups Projects
Select Git revision
  • bd188dcf86a9282914dec394fd03d863439db414
  • master default protected
2 results

another-file

Blame
  • Forked from an inaccessible project.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    lifehistory.jl 2.06 KiB
    ### Persephone - a socio-economic-ecological model of European agricultural landscapes.
    ###
    ### This file contains a set of life-history related utility functions needed by species.
    ###
    
    """
        initrandompopulation(popsize, asexual=true)
    
    A simplified version of `initpopulation()`. Creates a function that initialises
    `popsize` individuals, spread at random across the landscape. If `popsize` is less
    than 1, it is interpreted as a population density (i.e. 1 animal per `popsize` pixels).
    """
    function initrandompopulation(popsize::Float64, asexual::Bool=true)
        initfunc = function(species::Dict{String,Any}, model::AgentBasedModel)
            if popsize < 1.0
                x, y = size(model.landscape)
                popsize = Int(round(x*y*popsize))
            end
            for i in 1:popsize
                sex = asexual ? hermaphrodite : rand([male, female])
                add_agent!(Animal, model, species, sex, 0)
            end
            @debug "Initialised $(popsize) $(species["name"])s."
        end
        return initfunc
    end
    
    #TODO initpopulation
    
    """
        reproduce!(animal, model, n=1)
    
    Produce one or more offspring for the given animal at its current location.
    """
    function reproduce!(animal::Animal, model::AgentBasedModel, n::Int64=1)
        # XXX at the moment we don't have intra-specific variation, so currently we
        # don't need sexual recombination here
        for i in 1:n
            sex = animal.sex == hermaphrodite ? hermaphrodite : rand([male, female])
            add_agent!(animal.pos, Animal, model, animal.traits, sex, 0)
        end
        @debug "$(animal.traits["name"]) $(animal.id) has reproduced."
    end
    
    """
        kill(animal, model, probability=1.0, cause="")
    
    Kill this animal, optionally with a given percentage probability.
    Returns true if the animal dies, false if not.
    """
    function kill!(animal::Animal, model::AgentBasedModel, probability::Float64=1.0, cause::String="")
        if rand() < probability
            postfix = isempty(cause) ? "." : " from $cause."
            @debug "$(animal.traits["name"]) $(animal.id) has died$(postfix)"
            kill_agent!(animal, model)
            return true
        end
        return false
    end