SCJ-PredatorPrey/test.ipynb

195 lines
6.3 KiB
Plaintext
Raw Permalink Normal View History

{
"cells": [
{
"cell_type": "code",
2024-06-18 09:06:43 +02:00
"execution_count": 2,
"metadata": {},
2024-06-08 14:02:45 +02:00
"outputs": [],
"source": [
"import Pkg\n",
2024-06-08 14:02:45 +02:00
"Pkg.activate(\"./env\")\n",
"Pkg.instantiate()"
]
},
{
"cell_type": "code",
2024-06-18 09:06:43 +02:00
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(agent_color = acolor, agent_size = 25, agent_marker = ashape, offset = offset, agentsplotkwargs = (strokewidth = 1.0, strokecolor = :black), heatarray = grasscolor, heatkwargs = (colormap = [:brown, :green], colorrange = (0, 1)))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 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",
"offset(a) = a isa Sheep ? (-0.1, -0.1*rand()) : (+0.1, +0.1*rand())\n",
"ashape(a) = a isa Sheep ? :circle : :utriangle\n",
"acolor(a) = a isa Sheep ? RGBAf(1.0, 1.0, 1.0, 0.8) : RGBAf(0.2, 0.2, 0.3, 0.8)\n",
"\n",
"# and instruct [`abmplot`](@ref) how to plot grass as a heatmap:\n",
"grasscolor(model) = model.countdown ./ 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",
"plotkwargs = (;\n",
" agent_color = acolor,\n",
" agent_size = 25,\n",
" agent_marker = ashape,\n",
" offset,\n",
" agentsplotkwargs = (strokewidth = 1.0, strokecolor = :black),\n",
" heatarray = grasscolor,\n",
" heatkwargs = heatkwargs,\n",
")"
]
},
{
"cell_type": "code",
2024-06-18 09:06:43 +02:00
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"include(\"./predator_prey.jl\")\n",
"Pkg.status([\"Agents\",\"GLMakie\"]; mode = Pkg.Types.PKGMODE_MANIFEST, io=stdout)\n",
"using GLMakie\n",
"GLMakie.activate!()\n",
2024-06-18 09:06:43 +02:00
"\n",
"# Event(name, value, pre_value, t_start, t_end, t_cycle, timerstart)\n",
"events = RecurringEvent[]\n",
"#push!(events, RecurringEvent(\"Drought\", 80, 30, 30, 50, 120, 1))\n",
"#push!(events, RecurringEvent(\"Flood\", 50, 30, 70, 80, 120, 1))\n",
"push!(events, RecurringEvent(\"PreyReproduceSeasonal\", 0.5, 0.1, 1, 4, 12, 1))\n",
"push!(events, RecurringEvent(\"PredatorReproduceSeasonal\", 0.1, 0.07, 4, 6, 12, 1))\n",
"\n",
"stable_params = (;\n",
2024-06-18 09:06:43 +02:00
" events = events,\n",
" n_sheep = 30,\n",
" n_wolves = 3,\n",
" dims = (30, 30),\n",
2024-06-18 09:06:43 +02:00
" regrowth_time = 30,\n",
" Δenergy_sheep = 6,\n",
" sheep_reproduce = 0.3,\n",
" sheep_perception = 0,\n",
2024-06-18 09:06:43 +02:00
" wolf_reproduce = 0.07,\n",
" Δenergy_wolf = 20,\n",
" wolf_perception = 0,\n",
" seed = 71758,\n",
")\n",
2024-06-18 09:06:43 +02:00
"\n",
"params = Dict(\n",
" :regrowth_time => 0:1:100,\n",
" :Δenergy_sheep => 0:1:50,\n",
" :sheep_reproduce => 0:0.01:1,\n",
" :sheep_perception => 0:1:8,\n",
2024-06-18 09:06:43 +02:00
" :Δenergy_wolf => 0:1:50, \n",
" :wolf_reproduce => 0:0.01:1, \n",
" :wolf_perception => 0:1:8,\n",
")\n",
2024-06-18 09:06:43 +02:00
"\n",
"sheep(a) = a isa Sheep\n",
"wolf(a) = a isa Wolf\n",
"count_grass(model) = count(model.fully_grown)\n",
"adata = [(sheep, count), (wolf, count)]\n",
"mdata = [count_grass]\n",
"model = initialize_model(;stable_params...)\n",
2024-06-18 09:06:43 +02:00
"fig, abmobs = abmexploration(\n",
" model;\n",
" params,\n",
" plotkwargs...,\n",
" adata,\n",
" alabels = [\"Sheep\", \"Wolf\"],\n",
" mdata, mlabels = [\"Grass\"]\n",
")\n",
"#, step! = (model) -> begin event_handler!(model, \"Dürre\") model.wolf_reproduce = 0.1 Agents.step!() end\n",
"#fig, ax, abmobs = abmplot(model; add_controls=true, plotkwargs...)\n",
2024-06-18 09:06:43 +02:00
"\n",
"fig"
]
},
{
"cell_type": "code",
2024-06-18 09:06:43 +02:00
"execution_count": 143,
"metadata": {},
"outputs": [],
"source": [
"include(\"./predator_prey.jl\")\n",
2024-06-18 09:06:43 +02:00
"\n",
"using CairoMakie\n",
"CairoMakie.activate!() # hide\n",
"sheepwolfgrass = initialize_model(;stable_params...)\n",
"\n",
"abmvideo(\n",
" \"sheepwolf.mp4\",\n",
" sheepwolfgrass;\n",
" frames = 2000,\n",
" framerate = 8,\n",
" title = \"Sheep Wolf Grass\",\n",
" plotkwargs...,\n",
")"
]
},
{
"cell_type": "code",
2024-06-18 09:06:43 +02:00
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"\n",
"sheepwolfgrass = initialize_model()\n",
"\n",
"fig, ax, abmobs = abmplot(sheepwolfgrass; plotkwargs...)\n",
"fig\n",
"\n",
"# Now, lets run the simulation and collect some data. Define datacollection:\n",
"sheep(a) = a isa Sheep\n",
"wolf(a) = a isa Wolf\n",
"count_grass(model) = count(model.fully_grown)\n",
"# Run simulation:\n",
"sheepwolfgrass = initialize_model()\n",
"steps = 1000\n",
"adata = [(sheep, count), (wolf, count)]\n",
"mdata = [count_grass]\n",
"adf, mdf = run!(sheepwolfgrass, steps; adata, mdata)\n",
"\n",
"# The following plot shows the population dynamics over time.\n",
"# Initially, wolves become extinct because they consume the sheep too quickly.\n",
"# The few remaining sheep reproduce and gradually reach an\n",
"# equilibrium that can be supported by the amount of available grass.\n",
"function plot_population_timeseries(adf, mdf)\n",
" figure = Figure(size = (600, 400))\n",
" ax = figure[1, 1] = Axis(figure; xlabel = \"Step\", ylabel = \"Population\")\n",
" sheepl = lines!(ax, adf.time, adf.count_sheep, color = :cornsilk4)\n",
" wolfl = lines!(ax, adf.time, adf.count_wolf, color = RGBAf(0.2, 0.2, 0.3))\n",
" grassl = lines!(ax, mdf.time, mdf.count_grass, color = :green)\n",
" figure[1, 2] = Legend(figure, [sheepl, wolfl, grassl], [\"Sheep\", \"Wolves\", \"Grass\"])\n",
" figure\n",
"end\n",
"\n",
"plot_population_timeseries(adf, mdf)\n"
]
}
],
"metadata": {
"kernelspec": {
2024-06-18 09:06:43 +02:00
"display_name": "Julia 1.10.2",
"language": "julia",
"name": "julia-1.10"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
2024-06-18 09:06:43 +02:00
"version": "1.10.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}