-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from MRC-CSO-SPHSU/develop
Version 0.3
- Loading branch information
Showing
11 changed files
with
266 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
module SimSetup | ||
|
||
using Memoization | ||
|
||
using MALPM.Models: MAModel, allPeople, birthParameters | ||
using MALPM.Examples | ||
using MALPM.Simulate: dodeaths!, dobirths!, | ||
do_age_transitions!, do_work_transitions!, do_social_transitions!, | ||
dodivorces!, domarriages!, do_assign_guardians! | ||
|
||
using MultiAgents: AbstractABMSimulator | ||
using MultiAgents: attach_pre_model_step!, attach_post_model_step!, | ||
attach_agent_step!, currstep | ||
using SocioEconomics.Utilities: setVerbose!, unsetVerbose!, setDelay!, | ||
checkAssumptions!, ignoreAssumptions!, date2yearsmonths | ||
using SocioEconomics.XAgents: age, isMale, isFemale, isSingle, alive, hasDependents | ||
using SocioEconomics.API.Traits: FullPopulation, AlivePopulation | ||
using SocioEconomics.Specification.SimulateNew: death!, birth!, divorce!, marriage!, | ||
assign_guardian!, age_transition!, work_transition!, social_transition! | ||
|
||
import SocioEconomics.API.ModelFunc: share_childless_men, eligible_women | ||
import MultiAgents: setup!, verbose | ||
#export setup! | ||
|
||
function setupCommon!(sim::AbstractABMSimulator) | ||
|
||
verbose(sim) ? setVerbose!() : unsetVerbose!() | ||
setDelay!(sim.parameters.sleeptime) | ||
sim.parameters.checkassumption ? checkAssumptions!() : | ||
ignoreAssumptions!() | ||
|
||
attach_post_model_step!(sim,dodeaths!) | ||
attach_post_model_step!(sim,do_assign_guardians!) | ||
attach_post_model_step!(sim,dobirths!) | ||
attach_post_model_step!(sim,domarriages!) | ||
|
||
nothing | ||
end | ||
|
||
_popfeature(::LPMUKDemography) = FullPopulation() | ||
_popfeature(::LPMUKDemographyOpt) = AlivePopulation() | ||
|
||
deathstep!(person, model, sim, example) = | ||
death!(person, currstep(sim),model,_popfeature(example)) | ||
|
||
birthstep!(person, model, sim, example) = | ||
birth!(person, currstep(sim),model,_popfeature(example)) | ||
|
||
divorcestep!(person, model, sim, example) = | ||
divorce!(person, currstep(sim), model, _popfeature(example)) | ||
|
||
marriagestep!(person, model, sim, example) = | ||
marriage!(person, currstep(sim), model, _popfeature(example)) | ||
|
||
assign_guardian_step!(person, model, sim, example) = | ||
assign_guardian!(person, currstep(sim), model, _popfeature(example)) | ||
|
||
age_transition_step!(person, model, sim, example) = | ||
age_transition!(person, currstep(sim), model, _popfeature(example)) | ||
|
||
work_transition_step!(person, model, sim, example) = | ||
work_transition!(person, currstep(sim), model, _popfeature(example)) | ||
|
||
social_transition_step!(person, model, sim, example) = | ||
social_transition!(person, currstep(sim), model, _popfeature(example)) | ||
|
||
_ageclass(person) = trunc(Int, age(person)/10) | ||
@memoize Dict function share_childless_men(model::MAModel, ageclass :: Int) | ||
nAll = 0 | ||
nNoC = 0 | ||
for p in Iterators.filter(x->alive(x) && isMale(x) && _ageclass(x) == ageclass, allPeople(model)) | ||
nAll += 1 | ||
if !hasDependents(p) | ||
nNoC += 1 | ||
end | ||
end | ||
return nNoC / nAll | ||
end | ||
|
||
@memoize eligible_women(model::MAModel) = | ||
[f for f in allPeople(model) if isFemale(f) && alive(f) && | ||
isSingle(f) && age(f) > birthParameters(model).minPregnancyAge] | ||
|
||
function reset_cache_marriages(model,sim,::LPMUKDemography) | ||
#@info "cache reset at $(date2yearsmonths(currstep(sim)))" | ||
Memoization.empty_cache!(share_childless_men) | ||
Memoization.empty_cache!(eligible_women) | ||
end | ||
|
||
"set up simulation functions where dead people are removed" | ||
function setup!(sim::AbstractABMSimulator, example::LPMUKDemography) | ||
attach_agent_step!(sim,age_transition_step!) | ||
attach_agent_step!(sim,divorcestep!) | ||
attach_agent_step!(sim,work_transition_step!) | ||
attach_agent_step!(sim,social_transition_step!) | ||
# attach_agent_step!(sim,marriagestep!) # does not seem to work properly (may be due to memoization) | ||
setupCommon!(sim) | ||
nothing | ||
end | ||
|
||
function setup!(sim::AbstractABMSimulator,example::LPMUKDemographyOpt) | ||
attach_post_model_step!(sim,do_age_transitions!) | ||
attach_post_model_step!(sim,dodivorces!) | ||
attach_post_model_step!(sim,do_work_transitions!) | ||
attach_post_model_step!(sim,do_social_transitions!) | ||
setupCommon!(sim) | ||
nothing | ||
end | ||
|
||
end # SimSetup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
Main simulation functions for the demographic aspect of LPM. | ||
""" | ||
|
||
module Simulate | ||
|
||
using MultiAgents: AbstractMABM, AbstractABMSimulator | ||
using MultiAgents: currstep | ||
using MALPM.Examples | ||
using SocioEconomics | ||
using SocioEconomics.XAgents: Person | ||
using SocioEconomics.API.Traits: FullPopulation, AlivePopulation, | ||
SimProcess, Death, Birth, Marriage, | ||
AssignGuardian, AgeTransition, | ||
WorkTransition, SocialTransition | ||
using SocioEconomics.Utilities: date2yearsmonths | ||
import SocioEconomics.Specification.SimulateNew: dodeaths!, dobirths!, | ||
dodivorces!, domarriages!, do_assign_guardians!, | ||
do_age_transitions!, do_work_transitions!, do_social_transitions! | ||
|
||
_popfeature(::LPMUKDemography) = FullPopulation() | ||
_popfeature(::LPMUKDemographyOpt) = AlivePopulation() | ||
|
||
_init_return(::LPMUKDemographyOpt,::SimProcess) = 0 | ||
|
||
const retDeath = Person[] | ||
_init_return(::LPMUKDemography,::Death) = retDeath | ||
|
||
function dodeaths!(model, sim, example::DemographyExample) | ||
ret = _init_return(example,Death()) | ||
ret = dodeaths!(model,currstep(sim),_popfeature(example),ret) | ||
#=years, months = date2yearsmonths(currstep(sim)) | ||
@info "# of deaths $(years) $(months+1) : $(length(ret.people))"=# | ||
nothing | ||
end # function doDeaths! | ||
|
||
const retBirth = Person[] | ||
_init_return(::LPMUKDemography,::Birth) = retBirth | ||
|
||
function dobirths!(model, sim, example::DemographyExample) | ||
ret = _init_return(example,Birth()) | ||
ret = dobirths!(model,currstep(sim),_popfeature(example),ret) | ||
nothing | ||
end | ||
|
||
const retBirth = Person[] | ||
_init_return(::LPMUKDemography,::Birth) = retBirth | ||
|
||
function dodivorces!(model, sim, example::DemographyExample) | ||
ret = _init_return(example, Birth()) | ||
ret = dodivorces!(model,currstep(sim),_popfeature(example),ret) | ||
nothing | ||
end | ||
|
||
const retMarriage = Person[] | ||
_init_return(::LPMUKDemography,::Marriage) = retMarriage | ||
|
||
function domarriages!(model, sim, example::DemographyExample) | ||
ret = _init_return(example, Marriage()) | ||
ret = domarriages!(model,currstep(sim),_popfeature(example),ret) | ||
end | ||
|
||
const retAGuarians = Person[] | ||
_init_return(::LPMUKDemography,::AssignGuardian) = retAGuarians | ||
|
||
function do_assign_guardians!(model::AbstractMABM, sim::AbstractABMSimulator, example::DemographyExample) | ||
ret = _init_return(example, AssignGuardian()) | ||
ret = do_assign_guardians!(model,currstep(sim),_popfeature(example),ret) | ||
nothing | ||
end | ||
|
||
_init_return(::LPMUKDemography,pr::AgeTransition) = | ||
_init_return(LPMUKDemographyOpt(),pr) | ||
|
||
function do_age_transitions!(model::AbstractMABM, sim::AbstractABMSimulator, example::DemographyExample) | ||
ret = _init_return(example,AgeTransition()) | ||
ret = do_age_transitions!(model,currstep(sim),_popfeature(example),ret) | ||
nothing | ||
end | ||
|
||
_init_return(::LPMUKDemography,pr::WorkTransition) = | ||
_init_return(LPMUKDemographyOpt(),pr) | ||
|
||
function do_work_transitions!(model::AbstractMABM, sim, example::DemographyExample) | ||
ret = _init_return(example,WorkTransition()) | ||
ret = do_work_transitions!(model,currstep(sim),_popfeature(example),ret) | ||
nothing | ||
end | ||
|
||
_init_return(::LPMUKDemography,pr::SocialTransition) = | ||
_init_return(LPMUKDemographyOpt(),pr) | ||
|
||
function do_social_transitions!(model::AbstractMABM, sim, example::DemographyExample) | ||
ret = _init_return(example,SocialTransition()) | ||
ret = do_social_transitions!(model,currstep(sim),_popfeature(example),ret) | ||
nothing | ||
end | ||
|
||
end # Simulate |
Oops, something went wrong.