Skip to content

Commit 277705a

Browse files
committed
Lab7: Added eats macro. Refactor of plant/animal macros.
Created a new local version of Ecosystem.
1 parent 739a15a commit 277705a

File tree

6 files changed

+433
-78
lines changed

6 files changed

+433
-78
lines changed
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
[[ArgTools]]
4+
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
5+
6+
[[Artifacts]]
7+
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
8+
9+
[[Base64]]
10+
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
11+
12+
[[ChainRulesCore]]
13+
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
14+
git-tree-sha1 = "f885e7e7c124f8c92650d61b9477b9ac2ee607dd"
15+
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
16+
version = "1.11.1"
17+
18+
[[ChangesOfVariables]]
19+
deps = ["LinearAlgebra", "Test"]
20+
git-tree-sha1 = "9a1d594397670492219635b35a3d830b04730d62"
21+
uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0"
22+
version = "0.1.1"
23+
24+
[[Compat]]
25+
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
26+
git-tree-sha1 = "dce3e3fea680869eaa0b774b2e8343e9ff442313"
27+
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
28+
version = "3.40.0"
29+
30+
[[DataAPI]]
31+
git-tree-sha1 = "cc70b17275652eb47bc9e5f81635981f13cea5c8"
32+
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
33+
version = "1.9.0"
34+
35+
[[DataStructures]]
36+
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
37+
git-tree-sha1 = "7d9d316f04214f7efdbb6398d545446e246eff02"
38+
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
39+
version = "0.18.10"
40+
41+
[[Dates]]
42+
deps = ["Printf"]
43+
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
44+
45+
[[DelimitedFiles]]
46+
deps = ["Mmap"]
47+
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
48+
49+
[[Distributed]]
50+
deps = ["Random", "Serialization", "Sockets"]
51+
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
52+
53+
[[DocStringExtensions]]
54+
deps = ["LibGit2"]
55+
git-tree-sha1 = "b19534d1895d702889b219c382a6e18010797f0b"
56+
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
57+
version = "0.8.6"
58+
59+
[[Downloads]]
60+
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
61+
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
62+
63+
[[EcosystemCore]]
64+
deps = ["StatsBase"]
65+
git-tree-sha1 = "234cff8809f0c32fde3d7ed7c20b66af4286db14"
66+
repo-rev = "onlycore"
67+
repo-url = "https://github.com/JuliaTeachingCTU/EcosystemCore.jl.git"
68+
uuid = "3e0d8730-8ea0-4ee2-afe6-c85384c618a2"
69+
version = "0.1.0"
70+
71+
[[InteractiveUtils]]
72+
deps = ["Markdown"]
73+
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
74+
75+
[[InverseFunctions]]
76+
deps = ["Test"]
77+
git-tree-sha1 = "a7254c0acd8e62f1ac75ad24d5db43f5f19f3c65"
78+
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
79+
version = "0.1.2"
80+
81+
[[IrrationalConstants]]
82+
git-tree-sha1 = "7fd44fd4ff43fc60815f8e764c0f352b83c49151"
83+
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
84+
version = "0.1.1"
85+
86+
[[LibCURL]]
87+
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
88+
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
89+
90+
[[LibCURL_jll]]
91+
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
92+
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
93+
94+
[[LibGit2]]
95+
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
96+
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
97+
98+
[[LibSSH2_jll]]
99+
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
100+
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
101+
102+
[[Libdl]]
103+
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
104+
105+
[[LinearAlgebra]]
106+
deps = ["Libdl"]
107+
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
108+
109+
[[LogExpFunctions]]
110+
deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"]
111+
git-tree-sha1 = "be9eef9f9d78cecb6f262f3c10da151a6c5ab827"
112+
uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
113+
version = "0.3.5"
114+
115+
[[Logging]]
116+
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
117+
118+
[[Markdown]]
119+
deps = ["Base64"]
120+
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
121+
122+
[[MbedTLS_jll]]
123+
deps = ["Artifacts", "Libdl"]
124+
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
125+
126+
[[Missings]]
127+
deps = ["DataAPI"]
128+
git-tree-sha1 = "bf210ce90b6c9eed32d25dbcae1ebc565df2687f"
129+
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
130+
version = "1.0.2"
131+
132+
[[Mmap]]
133+
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
134+
135+
[[MozillaCACerts_jll]]
136+
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
137+
138+
[[NetworkOptions]]
139+
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
140+
141+
[[OrderedCollections]]
142+
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
143+
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
144+
version = "1.4.1"
145+
146+
[[Pkg]]
147+
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
148+
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
149+
150+
[[Printf]]
151+
deps = ["Unicode"]
152+
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
153+
154+
[[REPL]]
155+
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
156+
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
157+
158+
[[Random]]
159+
deps = ["Serialization"]
160+
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
161+
162+
[[SHA]]
163+
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
164+
165+
[[Serialization]]
166+
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
167+
168+
[[SharedArrays]]
169+
deps = ["Distributed", "Mmap", "Random", "Serialization"]
170+
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
171+
172+
[[Sockets]]
173+
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
174+
175+
[[SortingAlgorithms]]
176+
deps = ["DataStructures"]
177+
git-tree-sha1 = "b3363d7460f7d098ca0912c69b082f75625d7508"
178+
uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
179+
version = "1.0.1"
180+
181+
[[SparseArrays]]
182+
deps = ["LinearAlgebra", "Random"]
183+
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
184+
185+
[[Statistics]]
186+
deps = ["LinearAlgebra", "SparseArrays"]
187+
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
188+
189+
[[StatsAPI]]
190+
git-tree-sha1 = "1958272568dc176a1d881acb797beb909c785510"
191+
uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
192+
version = "1.0.0"
193+
194+
[[StatsBase]]
195+
deps = ["DataAPI", "DataStructures", "LinearAlgebra", "LogExpFunctions", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics", "StatsAPI"]
196+
git-tree-sha1 = "eb35dcc66558b2dda84079b9a1be17557d32091a"
197+
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
198+
version = "0.33.12"
199+
200+
[[TOML]]
201+
deps = ["Dates"]
202+
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
203+
204+
[[Tar]]
205+
deps = ["ArgTools", "SHA"]
206+
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
207+
208+
[[Test]]
209+
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
210+
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
211+
212+
[[UUIDs]]
213+
deps = ["Random", "SHA"]
214+
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
215+
216+
[[Unicode]]
217+
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
218+
219+
[[Zlib_jll]]
220+
deps = ["Libdl"]
221+
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
222+
223+
[[nghttp2_jll]]
224+
deps = ["Artifacts", "Libdl"]
225+
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
226+
227+
[[p7zip_jll]]
228+
deps = ["Artifacts", "Libdl"]
229+
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name = "Ecosystem"
2+
uuid = "e629da61-eb5c-4a46-88f0-e08c691183e3"
3+
authors = ["Jan Francu <[email protected]>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
EcosystemCore = "3e0d8730-8ea0-4ee2-afe6-c85384c618a2"
8+
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Ecosystem
2+
3+
using StatsBase
4+
using EcosystemCore
5+
6+
include("./ecosystem_macros.jl")
7+
include("./ecosystem_agents.jl")
8+
9+
export Grass, Sheep, Wolf, World, Mushroom
10+
export agent_step!, agent_count, world_step!, simulate!, every_nth
11+
12+
function simulate!(world::World, iters::Int; cb=()->())
13+
for i in 1:iters
14+
world_step!(world)
15+
cb()
16+
end
17+
end
18+
19+
agent_count(p::Plant) = size(p)/EcosystemCore.max_size(p)
20+
agent_count(::Animal) = 1
21+
agent_count(as::Vector{<:Agent}) = sum(agent_count,as)
22+
23+
function agent_count(w::World)
24+
function op(d::Dict,a::Agent{S}) where S<:Species
25+
n = nameof(S)
26+
if n in keys(d)
27+
d[n] += agent_count(a)
28+
else
29+
d[n] = agent_count(a)
30+
end
31+
return d
32+
end
33+
foldl(op, w.agents |> values |> collect, init=Dict{Symbol,Real}())
34+
end
35+
36+
function every_nth(f::Function, n::Int)
37+
i = 1
38+
function callback(args...)
39+
# display(i) # comment this out to see out the counter increases
40+
if i == n
41+
f(args...)
42+
i = 1
43+
else
44+
i += 1
45+
end
46+
end
47+
end
48+
49+
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
### old definition
2+
3+
#=
4+
# animals
5+
abstract type Sheep <: AnimalSpecies end
6+
abstract type Wolf <: AnimalSpecies end
7+
8+
# plants
9+
abstract type Mushroom <: PlantSpecies end
10+
abstract type Grass <: PlantSpecies end
11+
12+
Base.show(io::IO, ::Type{Sheep}) = print(io,"🐑")
13+
Base.show(io::IO, ::Type{Wolf}) = print(io,"🐺")
14+
15+
Base.show(io::IO,::Type{Mushroom}) = print(io,"🍄")
16+
Base.show(io::IO, ::Type{Grass}) = print(io,"🌿")
17+
18+
19+
function EcosystemCore.eat!(s::Animal{Sheep}, m::Plant{Mushroom}, w::World)
20+
if size(p)>0
21+
incr_energy!(s, -size(m)*Δenergy(s))
22+
m.size = 0
23+
end
24+
end
25+
26+
function EcosystemCore.eat!(a::Animal{Wolf}, b::Animal{Sheep}, w::World)
27+
incr_energy!(a, energy(b)*Δenergy(a))
28+
kill_agent!(b,w)
29+
end
30+
31+
function EcosystemCore.eat!(a::Animal{Sheep}, b::Plant{Grass}, w::World)
32+
incr_energy!(a, size(b)*Δenergy(a))
33+
b.size = 0
34+
end
35+
36+
EcosystemCore.eats(::Animal{Sheep}, ::Plant{Mushroom}) = true
37+
EcosystemCore.eats(::Animal{Sheep}, ::Plant{Grass}) = true
38+
EcosystemCore.eats(::Animal{Wolf},::Animal{Sheep}) = true
39+
40+
EcosystemCore.mates(::Animal{S,Female}, ::Animal{S,Male}) where S<:Species = true
41+
EcosystemCore.mates(::Animal{S,Male}, ::Animal{S,Female}) where S<:Species = true
42+
EcosystemCore.mates(a::Agent, b::Agent) = false
43+
=#
44+
45+
### new definition using macros from `ecosystem_macros.jl`
46+
@plant Grass 🌿
47+
@plant Broccoli 🥦
48+
@plant Mushroom 🍄
49+
@animal Sheep 🐑
50+
@animal Wolf 🐺
51+
@animal Rabbit 🐇
52+
53+
@eats Rabbit [Grass => 0.5, Broccoli => 1.0]
54+
@eats Sheep [Grass => 0.5, Broccoli => 1.0, Mushroom => -1.0]
55+
@eats Wolf [Sheep => 0.9]

0 commit comments

Comments
 (0)