Homework 2: Predator-Prey Agents
In this lab you will continue working on your agent simulation. If you did not manage to finish the homework, do not worry, you can use this script which contains all the functionality we developed in the lab.
How to submit?
Put all your code (including your or the provided solution of lab 2) in a script named hw.jl
. Zip only this file (not its parent folder) and upload it to BRUTE. Your file cannot contain any package dependencies. For example, having a using Plots
in your code will cause the automatic evaluation to fail.
Counting Agents
To monitor the different populations in our world we need a function that counts each type of agent. For Animal
s we simply have to count how many of each type are currently in our World
. In the case of Plant
s we will use the fraction of size(plant)/max_size(plant)
as a measurement quantity.
Implement a function
agent_count
that can be called on a singleAgent
and returns a number between $(0,1)$ (i.e. always1
for animals; andsize(plant)/max_size(plant)
for plants).Add a method for a vector of agents
Vector{<:Agent}
which sums all agent counts.Add a method for a
World
which returns a dictionary that contains pairs ofSymbol
s and the agent count like below:
julia> grass1 = Grass(1,5,5);
julia> agent_count(grass1)
1.0
julia> grass2 = Grass(2,1,5);
julia> agent_count([grass1,grass2]) # one grass is fully grown; the other only 20% => 1.2
1.2
julia> sheep = Sheep(3,10.0,5.0,1.0,1.0);
julia> wolf = Wolf(4,20.0,10.0,1.0,1.0);
julia> world = World([grass1, grass2, sheep, wolf]);
julia> agent_count(world)
Dict{Symbol, Real} with 3 entries: :Wolf => 1 :Grass => 1.2 :Sheep => 1
Hint: You can get the name of a type by using the nameof
function:
julia> nameof(Grass)
:Grass
Use as much dispatch as you can! ;)