From 344680ad89a24b2bda090092d7b1cf04e59b2724 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Fri, 1 Jul 2022 09:40:26 +0100 Subject: [PATCH 01/21] removing unit tests related to MultiAgents.Util --- data/test23.csv | 2 -- tests/RunTests.jl | 26 -------------------------- 2 files changed, 28 deletions(-) delete mode 100644 data/test23.csv diff --git a/data/test23.csv b/data/test23.csv deleted file mode 100644 index 972a7d5..0000000 --- a/data/test23.csv +++ /dev/null @@ -1,2 +0,0 @@ -2.11,3.12,4.1 -0.223,0.3434,2.3 \ No newline at end of file diff --git a/tests/RunTests.jl b/tests/RunTests.jl index 185ee12..19fb756 100644 --- a/tests/RunTests.jl +++ b/tests/RunTests.jl @@ -20,7 +20,6 @@ using XAgents: getHomeTown, getHomeTownName, getHouseLocation using Utilities: HouseLocation -using MultiAgents.Util: read2DArray, date2yearsmonths, removefirst!, subtract! using Utilities: createTimeStampedFolder using Utilities: Gender, male, female, unknown @@ -136,31 +135,6 @@ using Utilities: Gender, male, female, unknown simfolder = createTimeStampedFolder() @test !isempty(simfolder) @test isdir(simfolder) - - # reading matrix from file - T = read2DArray("../data/test23.csv") - @test size(T) == (2,3) - @test 0.3434 - eps() < T[2,2] < .3434 + eps() - - # dictionary subtraction - param = Dict(:a=>1,:b=>[1,2],:c=>3.1,:s=>"msg") - paramab = subtract!([:a,:b],param) - @test issetequal(keys(paramab),[:a,:b]) - @test issetequal(keys(param),[:s,:c]) - @test_throws ArgumentError subtract!([:c,:d],param) - parama = [:a] - paramab - @test issetequal(keys(parama),[:a]) - @test issetequal(keys(paramab),[:b]) - - @test date2yearsmonths(1059 // 12) == (88 , 3) - @test_throws ArgumentError date2yearsmonths(1059 // 5) - @test_throws ArgumentError date2yearsmonths(-1059 // 5) - - arr = [person3, person2, person6] - removefirst!(arr, person2) - @test arr[1] == person3 && arr[2] == person6 - @test_throws ArgumentError removefirst!(arr,person5) - end @testset verbose=true "Lone Parent Model Simulation" begin From 44de3439000cabdbe02f3599e5dd5194fee2d12f Mon Sep 17 00:00:00 2001 From: Atiyah Date: Thu, 7 Jul 2022 13:31:57 +0100 Subject: [PATCH 02/21] standard julia .gitgignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 29126e4..ec897e9 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ docs/site/ # committed for packages, but should be committed for applications that require a static # environment. Manifest.toml + +.vscode/ From 867b4ce1ff57dff374d72270a86f6834658716a2 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Thu, 14 Jul 2022 11:36:33 +0100 Subject: [PATCH 03/21] MultiAgents.Util => SomeUtil --- MainDummy.jl | 2 +- src/agents/BasicInfoM.jl | 2 +- src/agents/House.jl | 2 +- src/agents/KinshipM.jl | 2 +- src/lpm/Create.jl | 4 ++-- src/lpm/Simulate.jl | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/MainDummy.jl b/MainDummy.jl index 88c7ebe..8edb3c0 100644 --- a/MainDummy.jl +++ b/MainDummy.jl @@ -16,7 +16,7 @@ julia> include("this_script.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! diff --git a/src/agents/BasicInfoM.jl b/src/agents/BasicInfoM.jl index 22022c1..75ebfaa 100644 --- a/src/agents/BasicInfoM.jl +++ b/src/agents/BasicInfoM.jl @@ -1,6 +1,6 @@ module BasicInfoM -using MultiAgents.Util: date2yearsmonths +using SomeUtil: date2yearsmonths using Utilities: Gender, unknown, female, male export BasicInfo diff --git a/src/agents/House.jl b/src/agents/House.jl index c5ffeee..5ab73f1 100644 --- a/src/agents/House.jl +++ b/src/agents/House.jl @@ -3,7 +3,7 @@ export House export getHomeTown, getHouseLocation, setHouse!, removeOccupant!, undefined using Utilities: HouseLocation -using MultiAgents.Util: removefirst! +using SomeUtil: removefirst! """ Specification of a House Agent Type. diff --git a/src/agents/KinshipM.jl b/src/agents/KinshipM.jl index 8e48e1e..51d626f 100644 --- a/src/agents/KinshipM.jl +++ b/src/agents/KinshipM.jl @@ -1,6 +1,6 @@ module KinshipM -using MultiAgents.Util: date2yearsmonths +using SomeUtil: date2yearsmonths export Kinship export father, mother, setFather!, setMother!, setParent!, addChild! diff --git a/src/lpm/Create.jl b/src/lpm/Create.jl index 80c68e8..9ddd496 100644 --- a/src/lpm/Create.jl +++ b/src/lpm/Create.jl @@ -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 diff --git a/src/lpm/Simulate.jl b/src/lpm/Simulate.jl index 7520c14..2161ab0 100644 --- a/src/lpm/Simulate.jl +++ b/src/lpm/Simulate.jl @@ -11,7 +11,7 @@ using XAgents: removeOccupant!, resolvePartnership!, partner using MultiAgents: ABM, allagents -using MultiAgents.Util: date2yearsmonths +using SomeUtil: date2yearsmonths using LoneParentsModel.Create: LPMUKDemographyOpt export doDeaths! From 9c214e4aaa3281e19a70896a050ac6d8b31640d0 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Thu, 14 Jul 2022 13:53:08 +0100 Subject: [PATCH 04/21] MultiAgents.Util => SomeUtil --- src/agents/KinshipM.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agents/KinshipM.jl b/src/agents/KinshipM.jl index 51d626f..0b54672 100644 --- a/src/agents/KinshipM.jl +++ b/src/agents/KinshipM.jl @@ -1,6 +1,6 @@ module KinshipM -using SomeUtil: date2yearsmonths +using SomeUtil: date2yearsmonths export Kinship export father, mother, setFather!, setMother!, setParent!, addChild! From ade18ce658a1b39142bd81c64af9a9c10b03ebf4 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Thu, 14 Jul 2022 13:54:29 +0100 Subject: [PATCH 05/21] prevent internal functions from external usage --- src/agents/Person.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agents/Person.jl b/src/agents/Person.jl index ff1a402..7e93c73 100644 --- a/src/agents/Person.jl +++ b/src/agents/Person.jl @@ -14,7 +14,7 @@ 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 From e35164bb9564e99ec1986763cca258390aef49e3 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Thu, 14 Jul 2022 15:44:29 +0100 Subject: [PATCH 06/21] remove internal functions from testings and correct House declaration --- tests/RunTests.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/RunTests.jl b/tests/RunTests.jl index 19fb756..bf56058 100644 --- a/tests/RunTests.jl +++ b/tests/RunTests.jl @@ -14,7 +14,7 @@ using XAgents: Person, House, Town using MultiAgents: verify using XAgents: isFemale, isMale -using XAgents: setFather!, setParent!, setPartner!, setHouse!, setMother! +using XAgents: setAsParentChild!, setAsPartners!, setHouse! using XAgents: resolvePartnership! using XAgents: getHomeTown, getHomeTownName, getHouseLocation @@ -33,9 +33,9 @@ using Utilities: Gender, male, female, unknown aberdeen = Town((20,12),name="Aberdeen") # List of houses - house1 = House(edinbrugh,(1,2)::HouseLocation) - house2 = House(aberdeen,(5,10)::HouseLocation) - house3 = House(glasgow,(2,3)::HouseLocation) + house1 = House{Person}(edinbrugh,(1,2)::HouseLocation) + house2 = House{Person}(aberdeen,(5,10)::HouseLocation) + house3 = House{Person}(glasgow,(2,3)::HouseLocation) # List of persons person1 = Person(house1,25,gender=male) @@ -73,7 +73,7 @@ using Utilities: Gender, male, female, unknown @test isMale(person1) @test !isFemale(person1) - setFather!(person1,person6) + setAsParentChild!(person1,person6) @test person1 in person6.kinship.children @test person1.kinship.father === person6 @@ -87,8 +87,8 @@ using Utilities: Gender, male, female, unknown @test_throws InvalidStateException setPartner!(person3,person4) # same gender @test_throws InvalidStateException setParent!(person4,person5) # unknown gender - @test_throws ArgumentError setFather!(person4,person1) # ages incompatibe / well they are also partners - @test_throws ArgumentError setMother!(person2,person3) # person 2 has a mother + @test_throws ArgumentError setAsParentChild!(person4,person1) # ages incompatibe / well they are also partners + @test_throws ArgumentError setAsParentChild!(person2,person3) # person 2 has a mother resolvePartnership!(person4,person1) @test person1.kinship.partner !== person4 && person4.kinship.partner != person1 From 88239ac87da61920533ab8645deee0b99ba02713 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Thu, 14 Jul 2022 15:49:48 +0100 Subject: [PATCH 07/21] missing exports --- src/agents/Person.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/agents/Person.jl b/src/agents/Person.jl index 7e93c73..075703e 100644 --- a/src/agents/Person.jl +++ b/src/agents/Person.jl @@ -15,6 +15,7 @@ export isSingle, setHouse!, resolvePartnership! export isMale, isFemale, age export getHomeTown, getHomeTownName, agestep!, agestepAlive!, alive, setDead! export setAsParentChild!, setPartner!, setAsPartners!, partner +export isFemale, isMale From b10ac9a9a9bd62082b2154144c30438b966c210f Mon Sep 17 00:00:00 2001 From: Atiyah Date: Mon, 25 Jul 2022 08:35:50 +0100 Subject: [PATCH 08/21] missing using --- src/agents/KinshipM.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/agents/KinshipM.jl b/src/agents/KinshipM.jl index 0b54672..d008d0c 100644 --- a/src/agents/KinshipM.jl +++ b/src/agents/KinshipM.jl @@ -1,6 +1,8 @@ module KinshipM using SomeUtil: date2yearsmonths +using BasicInfoM: isFemale, isMale # Otherwise, unit test for setAsParentChild! fails + export Kinship export father, mother, setFather!, setMother!, setParent!, addChild! From e3a2264a8ce443d3786fb7767f00c25b4ab0aca6 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Mon, 25 Jul 2022 08:36:13 +0100 Subject: [PATCH 09/21] bug correction --- src/agents/Person.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agents/Person.jl b/src/agents/Person.jl index 075703e..2bdcf6d 100644 --- a/src/agents/Person.jl +++ b/src/agents/Person.jl @@ -112,7 +112,7 @@ function setAsParentChild!(child::Person,parent::Person) @assert (isMale(parent) && father(child) == nothing) || (isFemale(parent) && mother(child) == nothing) addChild!(parent, child) - setParent!(child, father) + setParent!(child, parent) nothing end From 4d41255884118e22b87a4292f278108679755d2b Mon Sep 17 00:00:00 2001 From: Atiyah Date: Mon, 25 Jul 2022 08:56:58 +0100 Subject: [PATCH 10/21] ArgumentError exception --- src/agents/Person.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/agents/Person.jl b/src/agents/Person.jl index 2bdcf6d..90ea60e 100644 --- a/src/agents/Person.jl +++ b/src/agents/Person.jl @@ -108,9 +108,10 @@ 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) + 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, parent) nothing From 0f279245bb840382895bf19cfff9deb456434b6f Mon Sep 17 00:00:00 2001 From: Atiyah Date: Mon, 25 Jul 2022 09:36:59 +0100 Subject: [PATCH 11/21] bug correction --- src/agents/Person.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agents/Person.jl b/src/agents/Person.jl index 90ea60e..da9b071 100644 --- a/src/agents/Person.jl +++ b/src/agents/Person.jl @@ -98,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 From efa7794ea36fa60a30bd4c5286b88d67b5da9bc1 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Mon, 25 Jul 2022 09:48:49 +0100 Subject: [PATCH 12/21] missing case: unknown gender of parent --- src/agents/Person.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/agents/Person.jl b/src/agents/Person.jl index da9b071..9cd0b55 100644 --- a/src/agents/Person.jl +++ b/src/agents/Person.jl @@ -108,6 +108,7 @@ end "set the father of a child" function setAsParentChild!(child::Person,parent::Person) + 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 : From 799840ebdbd5a824aaa5251d2beb1ae267b2c438 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Mon, 25 Jul 2022 10:09:33 +0100 Subject: [PATCH 13/21] modify tests according to latest API changes --- tests/RunTests.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/RunTests.jl b/tests/RunTests.jl index bf56058..a146c7d 100644 --- a/tests/RunTests.jl +++ b/tests/RunTests.jl @@ -77,16 +77,16 @@ using Utilities: Gender, male, female, unknown @test person1 in person6.kinship.children @test person1.kinship.father === person6 - setParent!(person2,person4) + setAsParentChild!(person2,person4) @test person2.kinship.mother === person4 @test person2 in person4.kinship.children - setPartner!(person1,person4) + setAsPartners!(person1,person4) @test person1.kinship.partner === person4 && person4.kinship.partner === person1 - @test_throws InvalidStateException setPartner!(person3,person4) # same gender + @test_throws InvalidStateException setAsPartners!(person3,person4) # same gender - @test_throws InvalidStateException setParent!(person4,person5) # unknown gender + @test_throws InvalidStateException setAsParentChild!(person4,person5) # unknown gender @test_throws ArgumentError setAsParentChild!(person4,person1) # ages incompatibe / well they are also partners @test_throws ArgumentError setAsParentChild!(person2,person3) # person 2 has a mother @@ -106,11 +106,11 @@ using Utilities: Gender, male, female, unknown @test getHomeTown(person1) === aberdeen @test person1 in house2.occupants - setHouse!(house2,person4) + setHouse!(person4,house2) @test getHomeTown(person4) === aberdeen person1.pos = house1 - @test_throws InvalidStateException setHouse!(person1,house3) + @test_throws ArgumentError setHouse!(person1,house3) person1.pos = house2 end # House functionalities From 605512586be49cdd64f805bbb626f12f5017c7eb Mon Sep 17 00:00:00 2001 From: Atiyah Date: Wed, 27 Jul 2022 12:25:15 +0100 Subject: [PATCH 14/21] remove internal function from export list --- src/agents/House.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/agents/House.jl b/src/agents/House.jl index 5ab73f1..794237d 100644 --- a/src/agents/House.jl +++ b/src/agents/House.jl @@ -1,6 +1,6 @@ export House -export getHomeTown, getHouseLocation, setHouse!, removeOccupant!, undefined +export getHomeTown, getHouseLocation, setHouse!, undefined using Utilities: HouseLocation using SomeUtil: removefirst! @@ -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 From fc29e493d4643adfbd71cb080f77bcca0cc5a679 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Wed, 27 Jul 2022 12:27:51 +0100 Subject: [PATCH 15/21] modify seperate tests for submodules Kinship & BasicInfo --- tests/RunTests.jl | 65 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/tests/RunTests.jl b/tests/RunTests.jl index a146c7d..6b7a3bf 100644 --- a/tests/RunTests.jl +++ b/tests/RunTests.jl @@ -13,10 +13,17 @@ using Test using XAgents: Person, House, Town using MultiAgents: verify + +# BasicInfo module +using XAgents: alive, setDead!, age, agestep!, agestepAlive! using XAgents: isFemale, isMale -using XAgents: setAsParentChild!, setAsPartners!, setHouse! -using XAgents: resolvePartnership! -using XAgents: getHomeTown, getHomeTownName, getHouseLocation + +# Kinship Module +using XAgents: father, mother, partner, isSingle +using XAgents: setAsParentChild!, setAsPartners!, resolvePartnership!, resetPartner! + +# Person type +using XAgents: setHouse!, getHomeTown, getHomeTownName, getHouseLocation using Utilities: HouseLocation @@ -64,25 +71,38 @@ using Utilities: Gender, male, female, unknown @test person1 === person2 end - @testset verbose=true "Type Person" begin - @test getHomeTown(person1) != nothing - @test getHomeTownName(person1) == "Edinbrugh" - - @test typeof(person1.info.age) == Rational{Int64} - + @testset verbose=true "BasicInfo Module" begin + + @test typeof(age(person1)) == Rational{Int64} @test isMale(person1) @test !isFemale(person1) + @test alive(person1) + + person7 = Person(house1,25,gender=male) + setDead!(person7) + @test !alive(person7) + + agestepAlive!(person7) + @test age(person7) < 25.01 + agestep!(person7) + @test age(person7) > 25 + + end + + @testset verbose=true "Kinship Module" begin setAsParentChild!(person1,person6) @test person1 in person6.kinship.children - @test person1.kinship.father === person6 + @test father(person1) === person6 setAsParentChild!(person2,person4) - @test person2.kinship.mother === person4 + @test mother(person2) === person4 @test person2 in person4.kinship.children - setAsPartners!(person1,person4) - @test person1.kinship.partner === person4 && person4.kinship.partner === person1 + @test isSingle(person1) + setAsPartners!(person1,person4) + @test !isSingle(person4) + @test partner(person1) === person4 && partner(person4) === person1 @test_throws InvalidStateException setAsPartners!(person3,person4) # same gender @@ -91,8 +111,23 @@ using Utilities: Gender, male, female, unknown @test_throws ArgumentError setAsParentChild!(person2,person3) # person 2 has a mother resolvePartnership!(person4,person1) - @test person1.kinship.partner !== person4 && person4.kinship.partner != person1 + @test isSingle(person4) + @test partner(person1) !== person4 && partner(person4) != person1 @test_throws ArgumentError resolvePartnership!(person1,person4) + + end + + @testset verbose=true "Type Person" begin + @test getHomeTown(person1) != nothing + @test getHomeTownName(person1) == "Edinbrugh" + + setAsPartners!(person4,person6) + @test !isSingle(person6) + @test !isSingle(person4) + + resetPartner!(person4) + @test isSingle(person6) + @test isSingle(person4) end @testset verbose=true "Type House" begin @@ -130,7 +165,7 @@ using Utilities: Gender, male, female, unknown # TODO testing ABMs once designed # TODO testing stepping functions once design is fixed - + @testset verbose=true "Utilities" begin simfolder = createTimeStampedFolder() @test !isempty(simfolder) From 54cbf6ee09b2b146a40fe811cf31eb9b44287722 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Wed, 27 Jul 2022 12:31:18 +0100 Subject: [PATCH 16/21] removing extra code --- src/agents/Town.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agents/Town.jl b/src/agents/Town.jl index c697d38..7cafa1c 100644 --- a/src/agents/Town.jl +++ b/src/agents/Town.jl @@ -45,7 +45,7 @@ end Town(pos;name="",density=0.0) = Town(pos,name,density) const undefinedTown = Town((-1,-1),"",0.0) -1 + From ce8cd4fd6f5c6293db1cddac2c93c2ae681540f7 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Wed, 27 Jul 2022 13:13:26 +0100 Subject: [PATCH 17/21] remove keyword argument --- src/abms/Population.jl | 4 ++-- src/agents/BasicInfoM.jl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/abms/Population.jl b/src/abms/Population.jl index 4c25578..6ec9d2b 100644 --- a/src/abms/Population.jl +++ b/src/abms/Population.jl @@ -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 @@ -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]) diff --git a/src/agents/BasicInfoM.jl b/src/agents/BasicInfoM.jl index 75ebfaa..d109345 100644 --- a/src/agents/BasicInfoM.jl +++ b/src/agents/BasicInfoM.jl @@ -36,7 +36,7 @@ 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 +agestep!(person::BasicInfo, 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) From 3c7e01ef23192433bd4053ab31f43c537851630b Mon Sep 17 00:00:00 2001 From: Atiyah Date: Wed, 27 Jul 2022 13:13:38 +0100 Subject: [PATCH 18/21] update dummy simulation --- MainDummy.jl | 2 +- src/Dummy.jl | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/MainDummy.jl b/MainDummy.jl index 8edb3c0..8226224 100644 --- a/MainDummy.jl +++ b/MainDummy.jl @@ -20,7 +20,7 @@ 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, diff --git a/src/Dummy.jl b/src/Dummy.jl index eb59a9f..1e4609c 100644 --- a/src/Dummy.jl +++ b/src/Dummy.jl @@ -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}() @@ -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) @@ -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 ") From 6085b01e243664fe3b82f35d39eb290144bb96ca Mon Sep 17 00:00:00 2001 From: Atiyah Date: Wed, 27 Jul 2022 13:45:44 +0100 Subject: [PATCH 19/21] renaming and relocating BasicInfo & Kinship modules --- src/agents/Person.jl | 18 +++++------ .../BasicInfo.jl} | 24 +++++++------- .../Kinship.jl} | 32 +++++++++---------- 3 files changed, 37 insertions(+), 37 deletions(-) rename src/agents/{BasicInfoM.jl => agents_modules/BasicInfo.jl} (54%) rename src/agents/{KinshipM.jl => agents_modules/Kinship.jl} (55%) diff --git a/src/agents/Person.jl b/src/agents/Person.jl index 9cd0b55..7047e78 100644 --- a/src/agents/Person.jl +++ b/src/agents/Person.jl @@ -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 @@ -38,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" @@ -72,8 +72,8 @@ 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" @@ -81,8 +81,8 @@ 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))) diff --git a/src/agents/BasicInfoM.jl b/src/agents/agents_modules/BasicInfo.jl similarity index 54% rename from src/agents/BasicInfoM.jl rename to src/agents/agents_modules/BasicInfo.jl index d109345..5581baa 100644 --- a/src/agents/BasicInfoM.jl +++ b/src/agents/agents_modules/BasicInfo.jl @@ -1,14 +1,14 @@ -module BasicInfoM +module BasicInfo 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 @@ -16,30 +16,30 @@ mutable struct BasicInfo 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 diff --git a/src/agents/KinshipM.jl b/src/agents/agents_modules/Kinship.jl similarity index 55% rename from src/agents/KinshipM.jl rename to src/agents/agents_modules/Kinship.jl index d008d0c..c94ac69 100644 --- a/src/agents/KinshipM.jl +++ b/src/agents/agents_modules/Kinship.jl @@ -1,15 +1,15 @@ -module KinshipM +module Kinship using SomeUtil: date2yearsmonths -using BasicInfoM: isFemale, isMale # Otherwise, unit test for setAsParentChild! fails +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} @@ -17,19 +17,19 @@ mutable struct Kinship{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) @@ -39,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)") From 9a8df6040ebe7f1dad1aca879f86d27ff2d490ed Mon Sep 17 00:00:00 2001 From: Atiyah Date: Wed, 27 Jul 2022 13:51:14 +0100 Subject: [PATCH 20/21] applying naming conventions for source files and modules --- src/XAgents.jl | 6 +++--- src/agents/{House.jl => house.jl} | 0 src/agents/{Household.jl => household.jl} | 0 src/agents/{Person.jl => person.jl} | 0 src/agents/{Town.jl => town.jl} | 0 5 files changed, 3 insertions(+), 3 deletions(-) rename src/agents/{House.jl => house.jl} (100%) rename src/agents/{Household.jl => household.jl} (100%) rename src/agents/{Person.jl => person.jl} (100%) rename src/agents/{Town.jl => town.jl} (100%) diff --git a/src/XAgents.jl b/src/XAgents.jl index a1547f9..60eff69 100644 --- a/src/XAgents.jl +++ b/src/XAgents.jl @@ -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 diff --git a/src/agents/House.jl b/src/agents/house.jl similarity index 100% rename from src/agents/House.jl rename to src/agents/house.jl diff --git a/src/agents/Household.jl b/src/agents/household.jl similarity index 100% rename from src/agents/Household.jl rename to src/agents/household.jl diff --git a/src/agents/Person.jl b/src/agents/person.jl similarity index 100% rename from src/agents/Person.jl rename to src/agents/person.jl diff --git a/src/agents/Town.jl b/src/agents/town.jl similarity index 100% rename from src/agents/Town.jl rename to src/agents/town.jl From 397ffde2bb52a8a305857c826b818f4533dfb0e6 Mon Sep 17 00:00:00 2001 From: Atiyah Date: Wed, 27 Jul 2022 15:26:57 +0100 Subject: [PATCH 21/21] LOAD Path to libraries via additional script without adding existing path --- MainDummy.jl | 1 + MainLPM.jl | 5 +++-- loadLibsPath.jl | 14 ++++++++++++++ tests/RunTests.jl | 4 +++- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 loadLibsPath.jl diff --git a/MainDummy.jl b/MainDummy.jl index 8226224..239bdef 100644 --- a/MainDummy.jl +++ b/MainDummy.jl @@ -12,6 +12,7 @@ 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! diff --git a/MainLPM.jl b/MainLPM.jl index 085b26e..4188f48 100644 --- a/MainLPM.jl +++ b/MainLPM.jl @@ -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 diff --git a/loadLibsPath.jl b/loadLibsPath.jl new file mode 100644 index 0000000..28790e2 --- /dev/null +++ b/loadLibsPath.jl @@ -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 diff --git a/tests/RunTests.jl b/tests/RunTests.jl index 6b7a3bf..2f83fa0 100644 --- a/tests/RunTests.jl +++ b/tests/RunTests.jl @@ -5,9 +5,11 @@ Run this script from shell as or within REPL julia> push!(LOAD_PATH,"/path/to/LoneParentsModels.jl/src") -julia> include("RunTests.jl") +julia> include("tests/RunTests.jl") """ +include("../loadLibsPath.jl") + using Test using XAgents: Person, House, Town