# Mountain Runners

Let's consider a race to the top of a mountain. Runners have been scattered about a map in some low lying areas and need to find the best path up to the peak.

We'll use Pathfinding.AStar and a Pathfinding.PenaltyMap to simulate this.

## Setup

using Agents, Agents.Pathfinding
using Random
using FileIO # To load images you also need ImageMagick available to your project

@agent Runner GridAgent{2} begin end

Our agent, as you can see, is very simple. Just an id and position provided by @agent. The rest of the dynamics of this example will be provided by the model.

function initialize(map_url; goal = (128, 409), seed = 88)
# Load an image file and convert it do a simple representation of height
# The space of the model can be obtained directly from the image.
# Our example file is (400, 500).
space = GridSpace(size(heightmap); periodic = false)
# The pathfinder. We use the MaxDistance metric since we want the runners
# to look for the easiest path to run, not just the most direct.
pathfinder = AStar(space; cost_metric = PenaltyMap(heightmap, MaxDistance{2}()))
model = ABM(
Runner,
space;
rng = MersenneTwister(seed),
properties = Dict(:goal => goal, :pathfinder => pathfinder)
)
for _ in 1:10
# Place runners in the low-lying space in the map.
runner = add_agent!((rand(model.rng, 100:350), rand(model.rng, 50:200)), model)
# Everyone wants to get to the same place.
plan_route!(runner, goal, model.pathfinder)
end
return model
end
initialize (generic function with 1 method)

The example heightmap we use here is a small region of countryside in Sweden, obtained with the Tangram heightmapper.

## Dynamics

With the pathfinder in place, and all our runners having a goal position set, stepping is now trivial.

agent_step!(agent, model) = move_along_route!(agent, model, model.pathfinder)
agent_step! (generic function with 1 method)

## Let's Race

Plotting is simple enough. We just need to use the InteractiveDynamics.abmplot for our runners, and display the heightmap for our reference. A better interface to do this is currently a work in progress.

using InteractiveDynamics
using CairoMakie

map_url =
model = initialize(map_url)
AgentBasedModel with 10 agents of type Runner
space: GridSpace with size (400, 500), metric=chebyshev, periodic=false
scheduler: fastest
properties: pathfinder, goal

and plot

static_preplot!(ax, model) = scatter!(ax, model.goal; color = (:red, 50), marker = 'x')

abmvideo(
"runners.mp4",
model,
agent_step!;
figurekwargs = (resolution = (700, 700),),
frames = 200,
framerate = 45,
ac = :black,
as = 8,
scatterkwargs = (strokecolor = :white, strokewidth = 2),
heatarray = model -> penaltymap(model.pathfinder),
heatkwargs = (colormap = :terrain,),
static_preplot!
)