Skip to content

Commit

Permalink
Merge pull request #37 from mhinsch/accessors
Browse files Browse the repository at this point in the history
Accessors
  • Loading branch information
AtiyahElsheikh authored Jul 14, 2022
2 parents 89d7d17 + 7ae160d commit 822f79d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/abms/Population.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This file is included with MultiABMs module. This file is subject to removal or
using MultiAgents: ABM
using MultiAgents: kill_agent!

import XAgents: agestep!, agestepAlive!
import XAgents: agestep!, agestepAlive!, alive

export population_step!, agestepAlive!, removeDead!

Expand All @@ -21,7 +21,7 @@ end

"remove dead persons"
function removeDead!(person::PersonType, population::ABM{PersonType}) where {PersonType}
person.info.alive ? nothing : kill_agent!(person, population)
alive(person) ? nothing : kill_agent!(person, population)
nothing
end

Expand Down
4 changes: 4 additions & 0 deletions src/agents/BasicInfoM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ end

age(person::BasicInfo) = person.age

alive(person::BasicInfo) = person.alive

setDead!(person::BasicInfo) = person.alive = false

"increment an age for a person to be used in typical stepping functions"
agestep!(person::BasicInfo; dt=1//12) = person.age += dt

Expand Down
10 changes: 5 additions & 5 deletions src/agents/Person.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ push!(LOAD_PATH, @__DIR__)

import KinshipM: Kinship,
isSingle, partner, father, mother, setParent!, addChild!, setPartner!
import BasicInfoM: BasicInfo, isFemale, isMale, age, agestep!, agestepAlive!
import BasicInfoM: BasicInfo, isFemale, isMale, age, agestep!, agestepAlive!, alive, setDead!

export Person
export PersonHouse, undefinedHouse
export isSingle, setHouse!, resolvePartnership!

#export Kinship
export isMale, isFemale
export getHomeTown, getHomeTownName, agestep!, agestepAlive!
export setFather!, setMother!, setParent!, setPartner!, setAsPartners!
export isMale, isFemale, age
export getHomeTown, getHomeTownName, agestep!, agestepAlive!, alive, setDead!
export setFather!, setMother!, setParent!, setPartner!, setAsPartners!, partner



Expand Down Expand Up @@ -53,7 +53,7 @@ end

# delegate functions to components

@delegate_onefield Person info [isFemale, isMale, age, agestep!, agestepAlive!]
@delegate_onefield Person info [isFemale, isMale, age, agestep!, agestepAlive!, alive, setDead!]
@delegate_onefield Person kinship [isSingle, partner, father, mother, setParent!, addChild!, setPartner!]


Expand Down
6 changes: 3 additions & 3 deletions src/lpm/Initialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Initialize

using Random: shuffle
using XAgents: Town, PersonHouse, Person
using XAgents: undefined, isFemale, setPartner!
using XAgents: undefined, isFemale, partner, setPartner!

using MultiAgents: ABM, MultiABM
using MultiAgents: add_agent!, allagents, nagents
Expand Down Expand Up @@ -56,9 +56,9 @@ function initial_connect!(abmpopulation::ABM{Person},abmhouses::ABM{PersonHouse}
isFemale(man) ? continue : nothing

house = pop!(randomhouses)
man.pos = man.kinship.partner.pos = house
man.pos = partner(man).pos = house

append!(house.occupants, [man, man.kinship.partner])
append!(house.occupants, [man, partner(man)])

end # for person

Expand Down
45 changes: 23 additions & 22 deletions src/lpm/Simulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
module Simulate

using XAgents: Person
using XAgents: isMale, isFemale, isSingle
using XAgents: removeOccupant!, resolvePartnership!
using XAgents: isMale, isFemale, isSingle, age, alive, setDead!
using XAgents: removeOccupant!, resolvePartnership!, partner

using MultiAgents: ABM, allagents

Expand Down Expand Up @@ -82,37 +82,38 @@ function doDeaths!(population::ABM{Person};
people = allagents(population)

livingPeople = typeof(population.properties[:example]) == LPMUKDemographyOpt ?
people : [person for person in people if person.info.alive]
people : [person for person in people if alive(person)]

@assert length(livingPeople) > 0 ?
typeof(livingPeople[1].info.age) == Rational{Int64} :
typeof(age(livingPeople[1])) == Rational{Int64} :
true # Assumption

for person in livingPeople

@assert isMale(person) || isFemale(person) # Assumption
age = person.info.age
curAge = age(person)
dieProb = 0
lifeExpectancy = 0 # From the code but does not play and rule?

if curryear >= 1950

age = age > 109 ? Rational(109) : person.info.age
ageindex = trunc(Int,age)
rawRate = isMale(person) ? population.data[:death_male][ageindex+1,curryear-1950+1] :
population.data[:death_female][ageindex+1,curryear-1950+1]
curAge = curAge > 109 ? Rational(109) : curAge
ageindex = trunc(Int,curAge)
rawRate = isMale(person) ?
population.data[:death_male][ageindex+1,curryear-1950+1] :
population.data[:death_female][ageindex+1,curryear-1950+1]

lifeExpectancy = max(90 - age, 3 // 1) ## ???
lifeExpectancy = max(90 - curAge, 3 // 1) ## ???

else # curryear < 1950 / made-up probabilities

babyDieProb = age < 1 ? parameters[:babyDieProb] : 0.0
babyDieProb = curAge < 1 ? parameters[:babyDieProb] : 0.0
ageDieProb = isMale(person) ?
exp(age / parameters[:maleAgeScaling]) * parameters[:maleAgeDieProb] :
exp(age / parameters[:femaleAgeScaling]) * parameters[:femaleAgeDieProb]
exp(curAge / parameters[:maleAgeScaling]) * parameters[:maleAgeDieProb] :
exp(curAge / parameters[:femaleAgeScaling]) * parameters[:femaleAgeDieProb]
rawRate = parameters[:baseDieProb] + babyDieProb + ageDieProb

lifeExpectancy = max(90 - age, 5 // 1) ## ???
lifeExpectancy = max(90 - curAge, 5 // 1) ## ???

end # currYear < 1950

Expand All @@ -131,21 +132,21 @@ function doDeaths!(population::ABM{Person};
# dieProb = self.deathProb_UCN(rawRate, person.parentsClassRank, person.careNeedLevel, person.averageShareUnmetNeed, classPop)
=#

if rand() < dieProb && rand(1:12) == currmonth && person.info.alive
if rand() < dieProb && rand(1:12) == currmonth && alive(person)
if verbose
y, m = date2yearsmonths(age)
y, m = date2yearsmonths(curAge)
println("person $(person.id) died year $(curryear) with age of $y")
sleep(sleeptime)
end
person.info.alive = false
setDead!(person)
# person.deadYear = self.year # to be moved to agent_step!
# deaths[person.classRank] += 1
false ? population.variables[:numberDeaths] += 1 : nothing # Temporarily this way till realized
removeOccupant!(person.pos,person)
isSingle(person) ?
nothing :
resolvePartnership!(person.kinship.partner,person)
end # rand
if ! isSingle(person)
resolvePartnership!(person, partner(person))
end
end # rand

end # for livingPeople

Expand All @@ -158,4 +159,4 @@ end # function doDeaths!



end # Simulate
end # Simulate

0 comments on commit 822f79d

Please sign in to comment.