# The ideal free distribution
# https://tidyecology.com/posts/the-ideal-free-distribution/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-10-15

## ---- setup ----
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE,
                      dev = "png", dpi = 120, fig.path = "figures/",
                      fig.width = 7, fig.height = 4.4)
library(ggplot2)
theme_te <- theme_minimal(base_size = 12) +
  theme(panel.background = element_rect(fill = "#f5f4ee", colour = NA),
        plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
        panel.grid.minor = element_blank(),
        plot.title = element_text(face = "bold", size = 12))
theme_set(theme_te)
col_for <- "#3a6b52"; col_res <- "#b5651d"; col_ref <- "#555555"

Q <- c(30, 20, 10); N <- 60
n <- c(N, 0, 0)
for (s in 1:3000) {
  intake <- Q / pmax(n, 0.5)
  from <- which(n > 0)[which.min(intake[n > 0])]; to <- which.max(intake)
  if (from != to) { n[from] <- n[from] - 1; n[to] <- n[to] + 1 }
}
intake_eq <- Q / n

## ---- settle ----
Q <- c(30, 20, 10); N <- 60          # input rates of three patches; number of foragers
n <- c(N, 0, 0)                       # start all foragers in patch 1
for (s in 1:3000) {
  intake <- Q / pmax(n, 0.5)          # per-capita intake; an empty patch scores twice its input
  from <- which(n > 0)[which.min(intake[n > 0])]
  to   <- which.max(intake)
  if (from != to) { n[from] <- n[from] - 1; n[to] <- n[to] + 1 }  # move toward the best
}
rbind(foragers = n, intake = round(Q / n, 2))

## ---- matching ----
c(forager_fractions = round(n / N, 3), resource_fractions = round(Q / sum(Q), 3))

## ---- fig-matching ----
d <- rbind(data.frame(patch = factor(1:3), frac = n/N,      what = "foragers"),
           data.frame(patch = factor(1:3), frac = Q/sum(Q), what = "resources"))
ggplot(d, aes(patch, frac, fill = what)) +
  geom_col(position = "dodge", width = 0.7) +
  scale_fill_manual(values = c("foragers" = col_for, "resources" = col_res)) +
  labs(x = "Patch", y = "Fraction of the total", fill = NULL,
       title = "Foragers match the distribution of resources")

## ---- fig-intake ----
crowd <- c(Q[1]/N, Q[2]/1, Q[3]/1)          # everyone in patch 1; empty patches: a first arrival's intake
di <- rbind(data.frame(patch = factor(1:3), intake = crowd, arrangement = "all crowd the richest"),
            data.frame(patch = factor(1:3), intake = Q/n,             arrangement = "ideal free distribution"))
ggplot(di, aes(patch, intake, fill = arrangement)) +
  geom_col(position = "dodge", width = 0.7) +
  scale_fill_manual(values = c("all crowd the richest" = col_res, "ideal free distribution" = col_for)) +
  labs(x = "Patch", y = "Per-capita intake", fill = NULL,
       title = "Only the matching distribution equalises intake")
