{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Install and use environment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import Pkg\n", "Pkg.activate(\"./env\")\n", "Pkg.instantiate()\n", "include(\"./predator_prey_generic.jl\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Setup for GLMakie" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using GLMakie\n", "GLMakie.activate!()\n", "# To view our starting population, we can build an overview plot using [`abmplot`](@ref).\n", "# We define the plotting details for the wolves and sheep:\n", "ashape(a) = a.def.symbol\n", "acolor(a) = a.def.color\n", "\n", "# and instruct [`abmplot`](@ref) how to plot grass as a heatmap:\n", "grasscolor(model) = model.growth ./ model.regrowth_time\n", "# and finally define a colormap for the grass:\n", "heatkwargs = (colormap = [:brown, :green], colorrange = (0, 1))\n", "\n", "# and put everything together and give it to [`abmplot`](@ref)\n", "return plotkwargs = (;\n", " agent_color = acolor,\n", " agent_size = 25,\n", " agent_marker = ashape,\n", " agentsplotkwargs = (strokewidth = 1.0, strokecolor = :black),\n", " heatarray = grasscolor,\n", " heatkwargs = heatkwargs,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Select scenario by executing the corresponding cell. Then run the model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scenario 1\n", "This has similar functionality to the original model, where animals walk randomly and reproduce by chance" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "events = []\n", "animal_defs = [\n", "AnimalDefinition(30,'●',RGBAf(1.0, 1.0, 1.0, 1),0, 0, 1, 0.3, 20, 0, \"Sheep\", [\"Wolf\",\"Bear\"], [\"Grass\"])\n", "AnimalDefinition(3,'▲',RGBAf(0.2, 0.2, 0.3, 1),0, 0, 1, 0.07, 20, 0, \"Wolf\", [], [\"Sheep\"])\n", "]\n", "stable_params = (;\n", " events = events,\n", " animal_defs = animal_defs,\n", " dims = (30, 30),\n", " regrowth_time = 30,\n", " Δenergy_grass = 6,\n", " seed = 71758,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scenario 2\n", "This uses score based movement logic and animals only reproduce when they have enough energy.\n", "This model is more stable. Less prey starves and a similar oscillation to the Lotka-Voltera equations emerges." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "events = []\n", "animal_defs = [\n", "AnimalDefinition(30,'●',RGBAf(1.0, 1.0, 1.0, 1),20, 20, 1, 0.3, 20, 3, \"Sheep\", [\"Wolf\",\"Bear\"], [\"Grass\"])\n", "AnimalDefinition(3,'▲',RGBAf(0.2, 0.2, 0.3, 1),20, 20, 1, 0.07, 20, 1, \"Wolf\", [], [\"Sheep\"])\n", "]\n", "stable_params = (;\n", " events = events,\n", " animal_defs = animal_defs,\n", " dims = (30, 30),\n", " regrowth_time = 30,\n", " Δenergy_grass = 6,\n", " seed = 71758,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scenario 3\n", "This tries to simulate the effects of droughts and floods" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "events = RecurringEvent[\n", "RecurringEvent(\"Drought\", 30, 40, 100, 124, 240, 0)\n", "RecurringEvent(\"Flood\", 0.4, 0, 20, 20, 120, 0)\n", "] # -> extreme Populationsschwankungen, Räuber stirbt aus (t=433) und System kollabiert\n", "animal_defs = [\n", "AnimalDefinition(30,'●',RGBAf(1.0, 1.0, 1.0, 1),20, 20, 1, 0.3, 20, 3, \"Sheep\", [\"Wolf\",\"Bear\"], [\"Grass\"])\n", "AnimalDefinition(3,'▲',RGBAf(0.2, 0.2, 0.3, 1),20, 20, 1, 0.07, 20, 1, \"Wolf\", [], [\"Sheep\"])\n", "]\n", "stable_params = (;\n", " events = events,\n", " animal_defs = animal_defs,\n", " dims = (30, 30),\n", " regrowth_time = 30,\n", " Δenergy_grass = 6,\n", " seed = 71758,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scenario 4\n", "This tries to simulate winter seasons." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "events = RecurringEvent[\n", "RecurringEvent(\"Winter\", 0, 0, 18, 24, 24, 0)\n", "RecurringEvent(\"PreyReproduceSeasonal\", 0.5, 0.1, 1, 7, 24, 0)\n", "RecurringEvent(\"PredatorReproduceSeasonal\", 0.1, 0.04, 6, 12, 24, 0)\n", "] # -> geringere max. Population, dafür weniger starke Schwankung.\n", "animal_defs = [\n", "AnimalDefinition(30,'●',RGBAf(1.0, 1.0, 1.0, 1),20, 20, 1, 0.3, 20, 3, \"Sheep\", [\"Wolf\",\"Bear\"], [\"Grass\"])\n", "AnimalDefinition(3,'▲',RGBAf(0.2, 0.2, 0.3, 1),20, 20, 1, 0.07, 20, 1, \"Wolf\", [], [\"Sheep\"])\n", "]\n", "stable_params = (;\n", " events = events,\n", " animal_defs = animal_defs,\n", " dims = (30, 30),\n", " regrowth_time = 30,\n", " Δenergy_grass = 6,\n", " seed = 71758,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scenario 5\n", "This tries to simulate droughts and floods with winter seasons" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "events = RecurringEvent[\n", "RecurringEvent(\"Drought\", 30, 40, 100, 124, 240, 0)\n", "RecurringEvent(\"Flood\", 0.4, 0, 25, 25, 120, 0)\n", "RecurringEvent(\"Winter\", 0, 0, 18, 24, 24, 0)\n", "RecurringEvent(\"PreyReproduceSeasonal\", 0.5, 0.1, 1, 7, 24, 0)\n", "RecurringEvent(\"PredatorReproduceSeasonal\", 0.1, 0.05, 6, 12, 24, 0)\n", "] # -> starke Schwankung durch, allerdings bleibt das System stabil\n", "animal_defs = [\n", "AnimalDefinition(30,'●',RGBAf(1.0, 1.0, 1.0, 1),20, 2000, 1, 0.3, 20, 3, \"Sheep\", [\"Wolf\",\"Bear\"], [\"Grass\"])\n", "AnimalDefinition(3,'▲',RGBAf(0.2, 0.2, 0.3, 1),20, 2000, 1, 0.07, 20, 1, \"Wolf\", [], [\"Sheep\"])\n", "]\n", "stable_params = (;\n", " events = events,\n", " animal_defs = animal_defs,\n", " dims = (30, 30),\n", " regrowth_time = 30,\n", " Δenergy_grass = 6,\n", " seed = 71758,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Run the Model interactive" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "# GLMakie Parameters\n", "model_params_ranges = Dict(\n", " :regrowth_time => 0:1:100,\n", " :Δenergy_grass => 0:1:50,\n", ")\n", "animal_params_ranges = generate_animal_parameter_ranges(animal_defs)\n", "params = merge(model_params_ranges,animal_params_ranges)\n", "\n", "# Data Collection\n", "sheep(a) = a.def.type == \"Sheep\"\n", "wolf(a) = a.def.type == \"Wolf\"\n", "eaten(a) = a.def.type == \"Sheep\" && a.death_cause == Predation\n", "starved(a) = a.def.type == \"Sheep\" && a.death_cause == Starvation\n", "count_grass(model) = count(model.fully_grown)\n", "adata = [(sheep, count), (wolf, count), (eaten, count), (starved, count)]\n", "mdata = [count_grass]\n", "\n", "# initialize and run\n", "model = initialize_model(;stable_params...)\n", "fig, abmobs = abmexploration(\n", " model;\n", " params,\n", " plotkwargs...,\n", " adata,\n", " alabels = [\"Sheep\", \"Wolf\", \"Eaten\", \"Starved\"],\n", " mdata, mlabels = [\"Grass\"]\n", ")\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Just run and plot the data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "steps = 2000\n", "\n", "# GLMakie Parameters\n", "model_params_ranges = Dict(\n", " :regrowth_time => 0:1:100,\n", " :Δenergy_grass => 0:1:50,\n", ")\n", "animal_params_ranges = generate_animal_parameter_ranges(animal_defs)\n", "params = merge(model_params_ranges,animal_params_ranges)\n", "\n", "# Data Collection\n", "sheep(a) = a.def.type == \"Sheep\"\n", "wolf(a) = a.def.type == \"Wolf\"\n", "eaten(a) = a.def.type == \"Sheep\" && a.death_cause == Predation\n", "starved(a) = a.def.type == \"Sheep\" && a.death_cause == Starvation\n", "count_grass(model) = count(model.fully_grown)\n", "adata = [(sheep, count), (wolf, count), (eaten, count), (starved, count)]\n", "mdata = [count_grass]\n", "\n", "# initialize and run\n", "model = initialize_model(;stable_params...)\n", "df1,df2 = run!(model, steps; adata, mdata)\n", "\n", "using DataFrames, Plots\n", "\n", "fig = Plots.plot(layout = (2, 1), size = (800, 600), legend = true)\n", "Plots.plot!(fig[1], df1.time, df1.count_sheep, label = \"Sheep\", linewidth = 1)\n", "Plots.plot!(fig[1], df1.time, df1.count_wolf, label = \"Wolf\", linewidth = 1)\n", "Plots.plot!(fig[1], df2.time, df2.count_grass, label = \"Grass\", linewidth = 1)\n", "Plots.plot!(fig[2], df1.time, df1.count_eaten, label = \"Eaten\", linewidth = 1)\n", "Plots.plot!(fig[2], df1.time, df1.count_starved, label = \"Starved\", linewidth = 1)\n", "\n", "Plots.title!(fig[1], \"Population Dynamics\")\n", "Plots.xlabel!(fig[1], \"Time\")\n", "Plots.ylabel!(fig[1], \"Count\")\n", "Plots.title!(fig[2], \"Death Cause\")\n", "Plots.xlabel!(fig[2], \"Time\")\n", "Plots.ylabel!(fig[2], \"Count\")\n", "display(fig)" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.10.3", "language": "julia", "name": "julia-1.10" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.3" } }, "nbformat": 4, "nbformat_minor": 2 }