<jupyter_start><jupyter_text>Interactive simulations The exploration of agent-based models can often be guided through an interactive simulation interface that allows users to visualize the models dynamics and adjust parameter values while a simulation is running. Examples are the traditional interface of [NetLogo](https://ccl.northwestern.edu/netlogo/), or the browser-based visualization module of [Mesa](https://mesa.readthedocs.io/). This guide shows how to create such interactive interfaces for agentpy models within a Jupyter Notebook by using the libraries [IPySimulate](https://github.com/JoelForamitti/ipysimulate), [ipywidgets](https://ipywidgets.readthedocs.io/) and [d3.js](https://d3js.org/). This approach is still in an early stage of development, and more features will follow in the future. Contributions are very welcome :)<jupyter_code>import agentpy as ap
import ipysimulate as ips

from ipywidgets import AppLayout
from agentpy.examples import WealthModel, SegregationModel<jupyter_output><empty_output><jupyter_text>Lineplot To begin we create an instance of the [wealth transfer model](https://agentpy.readthedocs.io/en/stable/agentpy_wealth_transfer.html) (without parameters).<jupyter_code>model = WealthModel()<jupyter_output><empty_output><jupyter_text>Parameters that are given as ranges will appear as interactive slider widgets. The parameter `fps` (frames per second) will be used automatically to indicate the speed of the simulation. The third value in the range defines the default position of the slider.<jupyter_code>parameters = {
    'agents': 1000,
    'steps': 100,
    'fps': ap.IntRange(1, 20, 5),
}<jupyter_output><empty_output><jupyter_text>We then create an ipysimulate control panel with the model and our set of parameters. We further pass two variables `t` (time-steps) and `gini` to be displayed live during the simulation.<jupyter_code>control = ips.Control(model, parameters, variables=('t', 'gini'))<jupyter_output><empty_output><jupyter_text>Next, we create a lineplot of the variable `gini`:<jupyter_code>lineplot = ips.Lineplot(control, 'gini')<jupyter_output><empty_output><jupyter_text>Finally, we want to display our two widgets `control` and `lineplot` next to each other. For this, we can use the [layout templates](https://ipywidgets.readthedocs.io/en/stable/examples/Layout%20Templates.html) from ipywidgets.<jupyter_code>AppLayout(
    left_sidebar=control,
    center=lineplot,
    pane_widths=['125px', 1, 1], 
    height='400px'
)<jupyter_output><empty_output><jupyter_text>Note that this widget is not displayed interactively if viewed in the docs. To view the widget, please download the Jupyter Notebook at the top of this page or launch this notebook as a binder. Here is a screenshot of an interactive simulation: ![Interactive simulation interface with a lineplot](graphics/ips_wealth.png)  Scatterplot In this second demonstration, we create an instance of the [segregation model](https://agentpy.readthedocs.io/en/stable/agentpy_segregation.html):<jupyter_code>model = SegregationModel()
parameters = {
    'fps': ap.IntRange(1, 10, 5),
    'want_similar': ap.Range(0, 1, 0.3),
    'n_groups': ap.Values(2, 3, 4),
    'density': ap.Range(0, 1, 0.95),
    'size': 50,
}
control = ips.Control(model, parameters, ('t'))
scatterplot = ips.Scatterplot(
    control, 
    xy=lambda m: m.grid.positions.values(),
    c=lambda m: m.agents.group
)
AppLayout(left_sidebar=control,
          center=scatterplot,
          pane_widths=['125px', 1, 1], 
          height='400px')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10075566750629723, 'fraction_numerical': 0.01371396585502379, 'mean_word_length': 5.58195211786372, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '232354', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1063, 'n_words': 349, 'path': 'docs/guide_interactive.ipynb', 'repo_name': 'isabeaups/agentpy', 'size': '3573'}