Понимание завершения моделирования

Это часть моделирования, которая описывает завершение программы. Может кто-нибудь, пожалуйста, помогите мне понять, как эта часть работы моделирования я полностью потерял. Код выглядит следующим образом:

while time_elapsed < end_time :
    event=birth () +death () +infection()
    choice=random.random()*event
    choice -= birth()
    time_elapsed += random.expovariate(event)

    if choice < 0 :
        do_birth()
        continue
    choice -= death()
    if choice < 0:
        do_death()
        continue

person user3281911    schedule 06.04.2014    source источник
comment
как работает эта часть симуляции, она работает так, как должна работать. в чем именно ваш вопрос?   -  person laike9m    schedule 06.04.2014
comment
birth() звучит как плохое название функции. Полагаю, это какой-то базовый шанс сущности? Я ожидал get_birth_chance() или что-то в этом роде, теперь, кажется, что-то создает.   -  person Caramiriel    schedule 06.04.2014


Ответы (1)


Я переставил его, чтобы, надеюсь, сделать более очевидным, что происходит:

# at each time-step until sim ends
while time_elapsed < end_time :

    #
    # decide which random event will occur
    #

    # cumulative probability = (chance of birth) + (chance of death) + (chance of infection)
    event = birth() + death() + infection()
    # choose a value in [0..cum_prob)
    choice = random.random() * event

    # interpret the value:
    # if 0 <= value < (chance of birth)  =>  birth
    # if (chance of birth) <= value < (chance of birth) + (chance of death)   => death
    # if (chance of birth) + (chance of death) <= value < (chance of birth) + (chance of death) + (chance of infection)   => infection

    choice -= birth()
    if choice < 0 :
        do_birth()
        continue

    choice -= death()
    if choice < 0:
        do_death()
        continue

    # "what to do in case of infection" is missing;
    # I assume you chopped it off while cut-and-pasting.

    # forward to next time-step;
    #   this uses an exponential step-size, giving
    #   a normal distribution of event intervals
    #   rather than a uniform step size
    time_elapsed += random.expovariate(event)

Я бы написал немного иначе:

while now < stop_at:
    birth_point =               prob_birth()
    death_point = birth_point + prob_death()
    all_events  = death_point + prob_infection()

    event = random.random() * all_events

    if event < birth_point:
        do_birth()
    elif event < death_point:
        do_death()
    else:
        do_infection()

    now += random.expovariate(all_events)
person Hugh Bothwell    schedule 06.04.2014