@@ -61,29 +61,29 @@ const v0 = (0.0, 0.0, 0.0) # we don't use the velocity field here
61
61
# with the specified heightmap and containing the specified number of rabbits, foxes and hawks.
62
62
63
63
function initialize_model (
64
- heightmap_url =
64
+ heightmap_url=
65
65
" https://raw.githubusercontent.com/JuliaDynamics/" *
66
66
" 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
87
87
)
88
88
89
89
# # Download and load the heightmap. The grayscale value is converted to `Float64` and
@@ -111,7 +111,7 @@ function initialize_model(
111
111
112
112
# # Note that the dimensions of the space do not have to correspond to the dimensions
113
113
# # 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 )
115
115
116
116
# # Generate an array of random numbers, and threshold it by the probability of grass growing
117
117
# # at that location. Although this causes grass to grow below `water_level`, it is
@@ -121,30 +121,30 @@ function initialize_model(
121
121
)
122
122
properties = (
123
123
# # The pathfinder for rabbits and foxes
124
- landfinder = AStar (space; walkmap = land_walkmap),
124
+ landfinder= AStar (space; walkmap= land_walkmap),
125
125
# # 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,
144
144
)
145
145
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)
148
148
149
149
# # spawn each animal at a random walkable position according to its pathfinder
150
150
for _ in 1 : n_rabbits
@@ -199,32 +199,32 @@ function animal_step!(rabbit, model, ::Rabbit)
199
199
# # Get a list of positions of all nearby predators
200
200
predators = [
201
201
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
+ ]
204
204
# # If the rabbit sees a predator and isn't already moving somewhere
205
205
if ! isempty (predators) && is_stationary (rabbit, model. landfinder)
206
206
# # Try and get an ideal direction away from predators
207
- direction = (0. , 0. , 0. )
207
+ direction = (0.0 , 0.0 , 0.0 )
208
208
for predator in predators
209
209
# # Get the direction away from the predator
210
210
away_direction = (rabbit. pos .- predator)
211
211
# # In case there is already a predator at our location, moving anywhere is
212
212
# # moving away from it, so it doesn't contribute to `direction`
213
- all (away_direction .≈ 0. ) && continue
213
+ all (away_direction .≈ 0.0 ) && continue
214
214
# # Add this to the overall direction, scaling inversely with distance.
215
215
# # 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
217
217
end
218
218
# # If the only predator is right on top of the rabbit
219
- if all (direction .≈ 0. )
219
+ if all (direction .≈ 0.0 )
220
220
# # Move anywhere
221
221
chosen_position = random_walkable (rabbit. pos, model, model. landfinder, model. rabbit_vision)
222
222
else
223
223
# # Normalize the resultant direction, and get the ideal position to move it
224
224
direction = direction ./ eunorm (direction)
225
225
# # 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 )
228
228
end
229
229
plan_route! (rabbit, chosen_position, model. landfinder)
230
230
end
@@ -300,7 +300,7 @@ function animal_step!(hawk, model, ::Hawk)
300
300
remove_agent! (rand (abmrng (model), food), model, model. airfinder)
301
301
hawk. energy += model. Δe_rabbit
302
302
# # 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)
304
304
end
305
305
306
306
# # The rest of the stepping function is similar to that of foxes, except hawks use a
@@ -366,7 +366,7 @@ model = initialize_model()
366
366
#
367
367
# using GLMakie # CairoMakie doesn't do 3D plots well
368
368
# ```
369
-
369
+ animalcolor (animal) = animalcolor (animal, variant (animal))
370
370
animalcolor (a:: Rabbit ) = :brown
371
371
animalcolor (a:: Fox ) = :orange
372
372
animalcolor (a:: Hawk ) = :blue
@@ -381,7 +381,7 @@ function Agents.static_preplot!(ax::Axis3, p::ABMPlot)
381
381
(100 / 205 ): (100 / 205 ): 100 ,
382
382
(100 / 205 ): (100 / 205 ): 100 ,
383
383
p. abmobs[]. model[]. heightmap;
384
- colormap = :terrain
384
+ colormap= :terrain
385
385
)
386
386
end
387
387
0 commit comments