Skip to content

Commit

Permalink
Merge pull request #42 from AtiyahElsheikh/issues38_41
Browse files Browse the repository at this point in the history
closes #29 #38 #39 #40 #41
  • Loading branch information
mhinsch authored Jul 27, 2022
2 parents 822f79d + 397ffde commit d813a6b
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 124 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ docs/site/
# committed for packages, but should be committed for applications that require a static
# environment.
Manifest.toml

.vscode/
5 changes: 3 additions & 2 deletions MainDummy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ julia> push!(LOAD_PATH,"/path/to/LoneParentsModel.jl/src")
julia> include("this_script.jl")
"""

include("./loadLibsPath.jl")

using MultiAgents: ABM, dummystep
using MultiABMs: population_step!

using MultiAgents.Util: AbstractExample, DummyExample
using SomeUtil: AbstractExample, DummyExample
using MultiAgents: ABMSimulation
using MultiAgents: run!, attach_pre_model_step!, attach_post_model_step!, attach_agent_step!
import MultiAgents: setup!
using Dummy: createPopulation
using Dummy: createPopulation


function setup!(simulation::ABMSimulation,
Expand Down
5 changes: 3 additions & 2 deletions MainLPM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
Main simulation of the lone parent model
under construction
from REPL execute it using > include("MainLPM.jl")
"""

push!(LOAD_PATH, "../MultiAgents.jl")
push!(LOAD_PATH, "src")
include("./loadLibsPath.jl")

using MultiAgents: MultiABM

Expand Down
2 changes: 0 additions & 2 deletions data/test23.csv

This file was deleted.

14 changes: 14 additions & 0 deletions loadLibsPath.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Base.occursin

"if a substr occurs as a substring in any element of string vector"
function occursin(substr::String,strvec::Vector{String})
for str in strvec
!occursin(substr,str) ? nothing : return true
end
false
end

# Temporary solution
!occursin("MultiAgents.jl",LOAD_PATH) ? push!(LOAD_PATH, "../MultiAgents.jl") : nothing
!occursin("SomeUtil.jl",LOAD_PATH) ? push!(LOAD_PATH, "../SomeUtil.jl") : nothing
!occursin("LoneParentsModel.jl",LOAD_PATH) ? push!(LOAD_PATH, "../LoneParentsModel.jl/src") : nothing
18 changes: 9 additions & 9 deletions src/Dummy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module Dummy

using Utilities: Gender, unknown, female, male
using XAgents: Town, House, Person
using XAgents: setParent!, setMother!, setPartner!
using XAgents: setAsParentChild!, setAsPartners!
using MultiAgents: ABM, add_agent!

export createPopulation

"Establish a dummy population"
function initPopulation(houses::Array{House,1})
function initPopulation(houses::Array{House{Person},1})

population = ABM{Person}()

Expand All @@ -23,12 +23,12 @@ module Dummy
son = Person(house,rand(1:15) + rand(0:11) // 12 , gender=male)
daughter = Person(house,rand(1:15) + rand(0:11) // 12 , gender=female)

setPartner!(mother,father)
setAsPartners!(mother,father)

setParent!(son,father)
setParent!(daughter,father)
setMother!(son,mother)
setParent!(daughter,mother)
setAsParentChild!(son,father)
setAsParentChild!(daughter,father)
setAsParentChild!(son,mother)
setAsParentChild!(daughter,mother)

add_agent!(mother,population)
add_agent!(father,population)
Expand All @@ -52,12 +52,12 @@ module Dummy
numberOfHouses = 100
# sizes = ["small","medium","big"]

houses = House[]
houses = House{Person}[]
for index in range(1,numberOfHouses)
town = rand(towns)
# sz = rand(sizes)
x,y = rand(1:10),rand(1:10)
push!(houses,House(town,(x,y)))#,size=sz))
push!(houses,House{Person}(town,(x,y)))#,size=sz))
end

print("sample houses: \n ")
Expand Down
6 changes: 3 additions & 3 deletions src/XAgents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module XAgents

using MultiAgents: AbstractAgent, AbstractXAgent, getIDCOUNTER

include("./agents/Town.jl")
include("./agents/House.jl")
include("./agents/Person.jl")
include("./agents/town.jl")
include("./agents/house.jl")
include("./agents/person.jl")

end # XAgents

4 changes: 2 additions & 2 deletions src/abms/Population.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export population_step!, agestepAlive!, removeDead!
"Step function for the population"
function population_step!(population::ABM{PersonType};dt=1//12) where {PersonType}
for agent in population.agentsList
agestep!(agent,dt=dt)
agestep!(agent,dt)
end
end

Expand All @@ -26,7 +26,7 @@ function removeDead!(person::PersonType, population::ABM{PersonType}) where {Per
end

"increment age with the simulation step size"
agestep!(person::PersonType,population::ABM{PersonType}) where {PersonType} = agestep!(person,dt=population.properties[:dt])
agestep!(person::PersonType,population::ABM{PersonType}) where {PersonType} = agestep!(person,population.properties[:dt])

"increment age with the simulation step size"
agestepAlivePerson!(person::PersonType,population::ABM{PersonType}) where {PersonType} = agestepAlive!(person, population.properties[:dt])
Expand Down
26 changes: 13 additions & 13 deletions src/agents/BasicInfoM.jl → src/agents/agents_modules/BasicInfo.jl
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
module BasicInfoM
module BasicInfo

using MultiAgents.Util: date2yearsmonths
using SomeUtil: date2yearsmonths
using Utilities: Gender, unknown, female, male

export BasicInfo
export BasicInfoBlock
export isFemale, isMale, agestep!, agestepAlive!, age


# TODO think about whether to make this immutable
mutable struct BasicInfo
mutable struct BasicInfoBlock
age::Rational
# (birthyear, birthmonth)
gender::Gender
alive::Bool
end

"Default constructor"
BasicInfo(;age=0//1, gender=unknown, alive = true) = BasicInfo(age,gender,alive)
BasicInfoBlock(;age=0//1, gender=unknown, alive = true) = BasicInfoBlock(age,gender,alive)

isFemale(person::BasicInfo) = person.gender == female
isMale(person::BasicInfo) = person.gender == male
isFemale(person::BasicInfoBlock) = person.gender == female
isMale(person::BasicInfoBlock) = person.gender == male


"costum @show method for Agent person"
function Base.show(io::IO, info::BasicInfo)
function Base.show(io::IO, info::BasicInfoBlock)
year, month = date2yearsmonths(info.age)
print(" $(year) years & $(month) months, $(info.gender) ")
info.alive ? print(" alive ") : print(" dead ")
end

age(person::BasicInfo) = person.age
age(person::BasicInfoBlock) = person.age

alive(person::BasicInfo) = person.alive
alive(person::BasicInfoBlock) = person.alive

setDead!(person::BasicInfo) = person.alive = false
setDead!(person::BasicInfoBlock) = 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
agestep!(person::BasicInfoBlock, dt=1//12) = person.age += dt

"increment an age for a person to be used in typical stepping functions"
function agestepAlive!(person::BasicInfo, dt=1//12)
function agestepAlive!(person::BasicInfoBlock, dt=1//12)
person.age += person.alive ? dt : 0
end

Expand Down
34 changes: 18 additions & 16 deletions src/agents/KinshipM.jl → src/agents/agents_modules/Kinship.jl
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
module KinshipM
module Kinship

using MultiAgents.Util: date2yearsmonths
using SomeUtil: date2yearsmonths
using BasicInfo: isFemale, isMale # Otherwise, unit test for setAsParentChild! fails

export Kinship

export KinshipBlock
export father, mother, setFather!, setMother!, setParent!, addChild!
export partner, isSingle, setPartner!


mutable struct Kinship{P}
mutable struct KinshipBlock{P}
father::Union{P,Nothing}
mother::Union{P,Nothing}
partner::Union{P,Nothing}
children::Vector{P}
end

"Default Constructor"
Kinship{P}(;father=nothing,mother=nothing,partner=nothing,children=P[]) where {P} =
Kinship(father,mother,partner,children)
KinshipBlock{P}(;father=nothing,mother=nothing,partner=nothing,children=P[]) where {P} =
KinshipBlock(father,mother,partner,children)

father(child::Kinship) = child.father
mother(child::Kinship) = child.mother
father(child::KinshipBlock) = child.father
mother(child::KinshipBlock) = child.mother

"set the father of child"
setFather!(child::Kinship{P},father::P) where {P} = child.father = father
setFather!(child::KinshipBlock{P},father::P) where {P} = child.father = father
"set the mother of child"
setMother!(child::Kinship{P},mother::P) where {P} = child.mother = mother
setMother!(child::KinshipBlock{P},mother::P) where {P} = child.mother = mother

"set child of a parent"
function setParent!(child::Kinship{P},parent::P) where {P}
function setParent!(child::KinshipBlock{P},parent::P) where {P}
if isFemale(parent)
setMother!(child,parent)
elseif isMale(parent)
Expand All @@ -37,21 +39,21 @@ function setParent!(child::Kinship{P},parent::P) where {P}
end
end

addChild!(parent::Kinship{P}, child::P) where{P} = push!(parent.children, child)
addChild!(parent::KinshipBlock{P}, child::P) where{P} = push!(parent.children, child)

isSingle(person::Kinship) = person.partner == nothing
isSingle(person::KinshipBlock) = person.partner == nothing

partner(person::Kinship) = person.partner
partner(person::KinshipBlock) = person.partner

"set a partnership"
function setPartner!(person1::Kinship{P}, person2) where {P}
function setPartner!(person1::KinshipBlock{P}, person2) where {P}
person1.partner = person2
end



"costum @show method for Agent person"
function Base.show(io::IO, kinship::Kinship)
function Base.show(io::IO, kinship::KinshipBlock)
father = kinship.father; mother = kinship.mother; partner = kinship.partner; children = kinship.children;
father == nothing ? nothing : print(" , father : $(father.id)")
mother == nothing ? nothing : print(" , mother : $(mother.id)")
Expand Down
6 changes: 3 additions & 3 deletions src/agents/House.jl → src/agents/house.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export House

export getHomeTown, getHouseLocation, setHouse!, removeOccupant!, undefined
export getHomeTown, getHouseLocation, setHouse!, undefined

using Utilities: HouseLocation
using MultiAgents.Util: removefirst!
using SomeUtil: removefirst!

"""
Specification of a House Agent Type.
Expand Down Expand Up @@ -43,7 +43,7 @@ end

"add an occupant to a house"
function addOccupant!(house::House{P}, person::P) where {P}
push!(house.occupants, person)
push!(house.occupants, person)
nothing
end

Expand Down
File renamed without changes.
33 changes: 18 additions & 15 deletions src/agents/Person.jl → src/agents/person.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using TypedDelegation

# enable using/import from local directory
push!(LOAD_PATH, @__DIR__)
push!(LOAD_PATH, "$(@__DIR__)/agents_modules")

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

export Person
export PersonHouse, undefinedHouse
Expand All @@ -14,7 +14,8 @@ export isSingle, setHouse!, resolvePartnership!
#export Kinship
export isMale, isFemale, age
export getHomeTown, getHomeTownName, agestep!, agestepAlive!, alive, setDead!
export setFather!, setMother!, setParent!, setPartner!, setAsPartners!, partner
export setAsParentChild!, setPartner!, setAsPartners!, partner
export isFemale, isMale



Expand All @@ -37,8 +38,8 @@ mutable struct Person <: AbstractXAgent
- (town::Town, x-y location in the map)
"""
pos::House{Person}
info::BasicInfo
kinship::Kinship{Person}
info::BasicInfoBlock
kinship::KinshipBlock{Person}

# Person(id,pos,age) = new(id,pos,age)
"Internal constructor"
Expand Down Expand Up @@ -71,17 +72,17 @@ end
Person(pos,age; gender=unknown,
father=nothing,mother=nothing,
partner=nothing,children=Person[]) =
Person(pos,BasicInfo(;age, gender),
Kinship(father,mother,partner,children))
Person(pos,BasicInfoBlock(;age, gender),
KinshipBlock(father,mother,partner,children))


"Constructor with default values"
Person(;pos=undefinedHouse,age=0,
gender=unknown,
father=nothing,mother=nothing,
partner=nothing,children=Person[]) =
Person(pos,BasicInfo(;age,gender),
Kinship(father,mother,partner,children))
Person(pos,BasicInfoBlock(;age,gender),
KinshipBlock(father,mother,partner,children))

const PersonHouse = House{Person}
const undefinedHouse = PersonHouse((undefinedTown, (-1, -1)))
Expand All @@ -97,7 +98,7 @@ end
"associate a house to a person"
function setHouse!(person::Person,house)
if ! undefined(person.pos)
removeOccupant!(house, person)
removeOccupant!(person.pos, person)
end

person.pos = house
Expand All @@ -107,11 +108,13 @@ end

"set the father of a child"
function setAsParentChild!(child::Person,parent::Person)
@assert age(child) < age(parent)
@assert (isMale(parent) && father(child) == nothing) ||
(isFemale(parent) && mother(child) == nothing)
isMale(parent) || isFemale(parent) ? nothing : throw(InvalidStateException("$(parent) has unknown gender",:undefined))
age(child) < age(parent) ? nothing : throw(ArgumentError("child's age $(age(child)) >= parent's age $(age(parent))"))
(isMale(parent) && father(child) == nothing) ||
(isFemale(parent) && mother(child) == nothing) ? nothing :
throw(ArgumentError("$(child) has a parent"))
addChild!(parent, child)
setParent!(child, father)
setParent!(child, parent)
nothing
end

Expand Down
2 changes: 1 addition & 1 deletion src/agents/Town.jl → src/agents/town.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end
Town(pos;name="",density=0.0) = Town(pos,name,density)

const undefinedTown = Town((-1,-1),"",0.0)
1




Expand Down
4 changes: 2 additions & 2 deletions src/lpm/Create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Create
using Utilities: Gender, unknown, female, male
using XAgents: Town, PersonHouse, Person, undefinedHouse, setAsPartners!
using MultiAgents: ABM, attach2DData!
using MultiAgents.Util:(-)
using SomeUtil:(-)

import MultiAgents.Util: AbstractExample
import SomeUtil: AbstractExample

export Demography, LPMUKDemography, LPMUKDemographyOpt
export createUKDemography
Expand Down
Loading

0 comments on commit d813a6b

Please sign in to comment.