Skip to content

Commit 2f533eb

Browse files
Added line to rabbit_fox_hawk.jl to make animation run and fix #1118 (#1126)
* Added line to rabbit_fox_hawk.jl to make animation run and fix #1118 * Update Project.toml --------- Co-authored-by: George Datseris <[email protected]>
1 parent 626e605 commit 2f533eb

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Agents"
22
uuid = "46ada45e-f475-11e8-01d0-f70cc89e6671"
33
authors = ["George Datseris", "Tim DuBois", "Aayush Sabharwal", "Ali Vahdati", "Adriano Meligrana"]
4-
version = "6.2.1"
4+
version = "6.2.2"
55

66
[deps]
77
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"

examples/rabbit_fox_hawk.jl

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,29 @@ const v0 = (0.0, 0.0, 0.0) # we don't use the velocity field here
6161
# with the specified heightmap and containing the specified number of rabbits, foxes and hawks.
6262

6363
function initialize_model(
64-
heightmap_url =
64+
heightmap_url=
6565
"https://raw.githubusercontent.com/JuliaDynamics/" *
6666
"JuliaDynamics/master/videos/agents/rabbit_fox_hawk_heightmap.png",
67-
water_level = 8,
68-
grass_level = 20,
69-
mountain_level = 35;
70-
n_rabbits = 160, ## initial number of rabbits
71-
n_foxes = 30, ## initial number of foxes
72-
n_hawks = 30, ## initial number of hawks
73-
Δe_grass = 25, ## energy gained from eating grass
74-
Δe_rabbit = 30, ## energy gained from eating one rabbit
75-
rabbit_repr = 0.06, ## probability for a rabbit to (asexually) reproduce at any step
76-
fox_repr = 0.03, ## probability for a fox to (asexually) reproduce at any step
77-
hawk_repr = 0.02, ## probability for a hawk to (asexually) reproduce at any step
78-
rabbit_vision = 6, ## how far rabbits can see grass and spot predators
79-
fox_vision = 10, ## how far foxes can see rabbits to hunt
80-
hawk_vision = 15, ## how far hawks can see rabbits to hunt
81-
rabbit_speed = 1.3, ## movement speed of rabbits
82-
fox_speed = 1.1, ## movement speed of foxes
83-
hawk_speed = 1.2, ## movement speed of hawks
84-
regrowth_chance = 0.03, ## probability that a patch of grass regrows at any step
85-
dt = 0.1, ## discrete timestep each iteration of the model
86-
seed = 42, ## seed for random number generator
67+
water_level=8,
68+
grass_level=20,
69+
mountain_level=35;
70+
n_rabbits=160, ## initial number of rabbits
71+
n_foxes=30, ## initial number of foxes
72+
n_hawks=30, ## initial number of hawks
73+
Δe_grass=25, ## energy gained from eating grass
74+
Δe_rabbit=30, ## energy gained from eating one rabbit
75+
rabbit_repr=0.06, ## probability for a rabbit to (asexually) reproduce at any step
76+
fox_repr=0.03, ## probability for a fox to (asexually) reproduce at any step
77+
hawk_repr=0.02, ## probability for a hawk to (asexually) reproduce at any step
78+
rabbit_vision=6, ## how far rabbits can see grass and spot predators
79+
fox_vision=10, ## how far foxes can see rabbits to hunt
80+
hawk_vision=15, ## how far hawks can see rabbits to hunt
81+
rabbit_speed=1.3, ## movement speed of rabbits
82+
fox_speed=1.1, ## movement speed of foxes
83+
hawk_speed=1.2, ## movement speed of hawks
84+
regrowth_chance=0.03, ## probability that a patch of grass regrows at any step
85+
dt=0.1, ## discrete timestep each iteration of the model
86+
seed=42, ## seed for random number generator
8787
)
8888

8989
## Download and load the heightmap. The grayscale value is converted to `Float64` and
@@ -111,7 +111,7 @@ function initialize_model(
111111

112112
## Note that the dimensions of the space do not have to correspond to the dimensions
113113
## of the pathfinder. Discretisation is handled by the pathfinding methods
114-
space = ContinuousSpace((100., 100., 50.); periodic = false)
114+
space = ContinuousSpace((100.0, 100.0, 50.0); periodic=false)
115115

116116
## Generate an array of random numbers, and threshold it by the probability of grass growing
117117
## at that location. Although this causes grass to grow below `water_level`, it is
@@ -121,30 +121,30 @@ function initialize_model(
121121
)
122122
properties = (
123123
## The pathfinder for rabbits and foxes
124-
landfinder = AStar(space; walkmap = land_walkmap),
124+
landfinder=AStar(space; walkmap=land_walkmap),
125125
## The pathfinder for hawks
126-
airfinder = AStar(space; walkmap = air_walkmap, cost_metric = MaxDistance{3}()),
127-
Δe_grass = Δe_grass,
128-
Δe_rabbit = Δe_rabbit,
129-
rabbit_repr = rabbit_repr,
130-
fox_repr = fox_repr,
131-
hawk_repr = hawk_repr,
132-
rabbit_vision = rabbit_vision,
133-
fox_vision = fox_vision,
134-
hawk_vision = hawk_vision,
135-
rabbit_speed = rabbit_speed,
136-
fox_speed = fox_speed,
137-
hawk_speed = hawk_speed,
138-
heightmap = heightmap,
139-
grass = grass,
140-
regrowth_chance = regrowth_chance,
141-
water_level = water_level,
142-
grass_level = grass_level,
143-
dt = dt,
126+
airfinder=AStar(space; walkmap=air_walkmap, cost_metric=MaxDistance{3}()),
127+
Δe_grass=Δe_grass,
128+
Δe_rabbit=Δe_rabbit,
129+
rabbit_repr=rabbit_repr,
130+
fox_repr=fox_repr,
131+
hawk_repr=hawk_repr,
132+
rabbit_vision=rabbit_vision,
133+
fox_vision=fox_vision,
134+
hawk_vision=hawk_vision,
135+
rabbit_speed=rabbit_speed,
136+
fox_speed=fox_speed,
137+
hawk_speed=hawk_speed,
138+
heightmap=heightmap,
139+
grass=grass,
140+
regrowth_chance=regrowth_chance,
141+
water_level=water_level,
142+
grass_level=grass_level,
143+
dt=dt,
144144
)
145145

146-
model = StandardABM(Animal, space; agent_step! = animal_step!,
147-
model_step! = model_step!, rng, properties)
146+
model = StandardABM(Animal, space; (agent_step!)=animal_step!,
147+
(model_step!)=model_step!, rng, properties)
148148

149149
## spawn each animal at a random walkable position according to its pathfinder
150150
for _ in 1:n_rabbits
@@ -199,32 +199,32 @@ function animal_step!(rabbit, model, ::Rabbit)
199199
## Get a list of positions of all nearby predators
200200
predators = [
201201
x.pos for x in nearby_agents(rabbit, model, model.rabbit_vision) if
202-
variant(x) isa Fox || variant(x) isa Hawk
203-
]
202+
variant(x) isa Fox || variant(x) isa Hawk
203+
]
204204
## If the rabbit sees a predator and isn't already moving somewhere
205205
if !isempty(predators) && is_stationary(rabbit, model.landfinder)
206206
## Try and get an ideal direction away from predators
207-
direction = (0., 0., 0.)
207+
direction = (0.0, 0.0, 0.0)
208208
for predator in predators
209209
## Get the direction away from the predator
210210
away_direction = (rabbit.pos .- predator)
211211
## In case there is already a predator at our location, moving anywhere is
212212
## moving away from it, so it doesn't contribute to `direction`
213-
all(away_direction .≈ 0.) && continue
213+
all(away_direction .≈ 0.0) && continue
214214
## Add this to the overall direction, scaling inversely with distance.
215215
## As a result, closer predators contribute more to the direction to move in
216-
direction = direction .+ away_direction ./ eunorm(away_direction) ^ 2
216+
direction = direction .+ away_direction ./ eunorm(away_direction)^2
217217
end
218218
## If the only predator is right on top of the rabbit
219-
if all(direction .≈ 0.)
219+
if all(direction .≈ 0.0)
220220
## Move anywhere
221221
chosen_position = random_walkable(rabbit.pos, model, model.landfinder, model.rabbit_vision)
222222
else
223223
## Normalize the resultant direction, and get the ideal position to move it
224224
direction = direction ./ eunorm(direction)
225225
## Move to a random position in the general direction of away from predators
226-
position = rabbit.pos .+ direction .* (model.rabbit_vision / 2.)
227-
chosen_position = random_walkable(position, model, model.landfinder, model.rabbit_vision / 2.)
226+
position = rabbit.pos .+ direction .* (model.rabbit_vision / 2.0)
227+
chosen_position = random_walkable(position, model, model.landfinder, model.rabbit_vision / 2.0)
228228
end
229229
plan_route!(rabbit, chosen_position, model.landfinder)
230230
end
@@ -300,7 +300,7 @@ function animal_step!(hawk, model, ::Hawk)
300300
remove_agent!(rand(abmrng(model), food), model, model.airfinder)
301301
hawk.energy += model.Δe_rabbit
302302
## Fly back up
303-
plan_route!(hawk, hawk.pos .+ (0., 0., 7.), model.airfinder)
303+
plan_route!(hawk, hawk.pos .+ (0.0, 0.0, 7.0), model.airfinder)
304304
end
305305

306306
## The rest of the stepping function is similar to that of foxes, except hawks use a
@@ -366,7 +366,7 @@ model = initialize_model()
366366
#
367367
# using GLMakie # CairoMakie doesn't do 3D plots well
368368
# ```
369-
369+
animalcolor(animal) = animalcolor(animal, variant(animal))
370370
animalcolor(a::Rabbit) = :brown
371371
animalcolor(a::Fox) = :orange
372372
animalcolor(a::Hawk) = :blue
@@ -381,7 +381,7 @@ function Agents.static_preplot!(ax::Axis3, p::ABMPlot)
381381
(100/205):(100/205):100,
382382
(100/205):(100/205):100,
383383
p.abmobs[].model[].heightmap;
384-
colormap = :terrain
384+
colormap=:terrain
385385
)
386386
end
387387

0 commit comments

Comments
 (0)