1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-25 04:12:29 +00:00
soil/examples/tutorial/soil_tutorial.ipynb

2238 lines
163 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T12:41:48.007238Z",
"start_time": "2017-10-19T14:41:47.980725+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"# Soil Tutorial"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T16:44:14.120953Z",
"start_time": "2017-07-02T18:44:14.117152+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
2023-04-20 15:56:44 +00:00
"collapsed": true,
"hideCode": false,
"hidePrompt": false
},
"source": [
"This notebook is an introduction to the soil agent-based social network simulation framework.\n",
"In particular, we will focus on a specific use case: studying the propagation of news in a social network.\n",
"\n",
"The steps we will follow are:\n",
"\n",
"* Modelling the behavior of agents\n",
"* Running the simulation using different configurations\n",
"* Analysing the results of each simulation"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T13:38:48.052876Z",
"start_time": "2017-07-03T15:38:48.044762+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"But before that, let's import the soil module and networkx."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:13.451481Z",
"start_time": "2017-11-03T11:58:12.643469+01:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
"import soil\n",
"import networkx as nx\n",
" \n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
2023-04-20 15:56:44 +00:00
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T13:41:19.788717Z",
"start_time": "2017-07-03T15:41:19.785448+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"## Basic concepts"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"There are three main elements in a soil simulation:\n",
" \n",
2023-04-20 15:56:44 +00:00
"* The environment or model. It assigns agents to nodes in the network, and stores the environment parameters (shared state for all agents).\n",
"* The network topology. A simulation may use an existing NetworkX topology, or generate one on the fly.\n",
"* Agents. There are several types of agents, depending on their behavior and their capabilities. Some examples of built-in types of agents are:\n",
" - Network agents, which are linked to a node in the topology. They have additional methods to access their neighbors.\n",
" - FSM (Finite state machine) agents. Their behavior is defined in terms of states, and an agent will move from one state to another.\n",
" - Evented agents, an actor-based model of agents, which can communicate with one another through message passing.\n",
" - For convenience, a general `soil.Agent` class is provided, which inherits from Network, FSM and Evented at the same time.\n",
"\n",
"Soil provides several abstractions over events to make developing agents easier.\n",
2023-04-20 15:56:44 +00:00
"This means you can use events (timeouts, delays) in soil, but for the most part we will assume your models will be step-based o.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T15:55:12.933978Z",
"start_time": "2017-07-02T17:55:12.930860+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"## Modeling behaviour"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T13:49:31.269687Z",
"start_time": "2017-07-03T15:49:31.257850+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"Our first step will be to model how every person in the social network reacts to hearing a piece of disinformation (news).\n",
"We will follow a very simple model based on a finite state machine.\n",
"\n",
2023-04-20 15:56:44 +00:00
"A person may be in one of two states: **neutral** (the default state) and **infected**.\n",
"A neutral person may hear about a piece of disinformation either on the TV (with probability **prob_tv_spread**) or through their friends.\n",
"Once a person has heard the news, they will spread it to their friends (with a probability **prob_neighbor_spread**).\n",
2023-04-20 15:56:44 +00:00
"Some users do not have a TV, so they will only be infected by their friends.\n",
"\n",
"The spreading probabilities will change over time due to different factors.\n",
2023-04-20 15:56:44 +00:00
"We will represent this variance using an additional agent which will not be a part of the social network."
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"### Modelling Agents\n",
"\n",
"The following sections will cover the basics of developing agents in SOIL.\n",
"\n",
"For more advanced patterns, please check the **examples** folder in the repository."
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"#### Basic agents"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T14:03:07.171127Z",
"start_time": "2017-07-03T16:03:07.165779+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"The most basic agent in Soil is ``soil.BaseAgent``.\n",
"These agents implement their behavior by overriding the `step` method, which will be run in every simulation step.\n",
"Only one agent will be running at any given time, and it will be doing so until the `step` function returns.\n",
"\n",
2023-04-20 15:56:44 +00:00
"Agents can access their environment through their ``self.model`` attribute.\n",
"This is most commonly used to get access to the environment parameters and methods.\n",
"Here is a simple example of an agent:\n",
"\n",
"\n",
"```python\n",
"class ExampleAgent(BaseAgent):\n",
" def init(self):\n",
" self.is_infected = False\n",
" self.steps_neutral = 0\n",
" \n",
" def step(self):\n",
" # Implement agent logic\n",
" if self.is_infected:\n",
" ... # Do something, like infecting other agents\n",
" return self.die(\"No need to do anything else\") # Stop forever\n",
" else:\n",
" ... # Do something\n",
" self.steps_neutral += 1\n",
" if self.steps_neutral > self.model.max_steps_neutral:\n",
" self.is_infected = True\n",
"```\n",
"\n",
"\n",
"\n",
2023-04-20 15:56:44 +00:00
"Any kind of agent behavior can be implemented with this `step` function.\n",
"However, it has two main drawbacks: 1) complex behaviors can get difficult both write and understand; 2) these behaviors are not composable."
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T14:03:07.171127Z",
"start_time": "2017-07-03T16:03:07.165779+02:00"
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"#### FSM agents\n",
"\n",
"One way to solve both issues is to model agents as **[Finite-state Machines](https://en.wikipedia.org/wiki/Finite-state_machine)** (FSM, for short).\n",
"FSM define a series of possible states for the agent, and changes between these states.\n",
"These states can be modelled and extended independently.\n",
"\n",
2023-04-20 15:56:44 +00:00
"This is modelled in Soil through the `soil.FSM` class.\n",
"Agents that inherit from ``soil.FSM`` do not need to specify a ``step`` method.\n",
"Instead, we describe each finite state with a function.\n",
"To change to another state, a function may return the new state, or the ``id`` of a state.\n",
"If no state is returned, the state remains unchanged.\n",
"\n",
2023-04-20 15:56:44 +00:00
"The current state of the agent can be checked with ``agent.state_id``.\n",
"That state id can be used to look for other agents in that specific state.\n",
"\n",
2023-04-20 15:56:44 +00:00
"Our previous example could be expressed like this:\n",
"\n",
2023-04-20 15:56:44 +00:00
"```python\n",
"class FSMExample(FSM):\n",
"\n",
2023-04-20 15:56:44 +00:00
" def init(self):\n",
" self.steps_neutral = 0\n",
" \n",
" @state(default=True)\n",
" def neutral(self):\n",
" ... # Do something\n",
" self.steps_neutral += 1\n",
" if self.steps_neutral > self.model.max_steps_neutral:\n",
" return self.infected # Change state\n",
"\n",
" @state\n",
" def infected(self):\n",
" ... # Do something\n",
" return self.die(\"No need to do anything else\")\n",
"```"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"#### Generator-based agents\n",
"\n",
2023-04-20 15:56:44 +00:00
"Another design pattern that can be very useful in some cases is to model each step (or a specific state) using generators (the `yield` keyword).\n",
"\n",
"\n",
"\n",
"```python\n",
"class GenExample(BaseAgent):\n",
" def step(self):\n",
" for i in range(self.model.max_steps_neutral):\n",
" ... # Do something\n",
" yield # Signal the scheduler that this step is done for now\n",
" ... # Do something\n",
" return self.die(\"No need to do anything else\") \n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"#### Telling the scheduler when to wake up an agent\n",
"\n",
"By default, every agent will be called in every simulation step, and the time elapsed between two steps is controlled by the `interval` attribute in the environment.\n",
"\n",
"But agents may signal the scheduler when they expect to be called again.\n",
"This is especially useful when an agent is going to be dormant for a long time.\n",
"To do so, an agent can return (or `yield`) from a `step` or a `state` a value of type `soil.When` (absolute time), `soil.Delta` (relative time) or `soil.Cond`, telling the scheduler when the agent will be ready to run again.\n",
"If it returns nothing (i.e., `None`), the agent will be ready to run at the next simulation step."
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T12:22:53.931963Z",
"start_time": "2017-07-02T14:22:53.928340+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"### Environment agents"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"Environment agents allow us to control the state of the environment.\n",
"In this case, we will use an environment agent to simulate a very viral event.\n",
"\n",
"When the event happens, the agent will modify the probability of spreading the rumor."
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 111,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:17.653736Z",
"start_time": "2017-11-03T11:58:17.612944+01:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"outputs": [],
"source": [
2023-04-20 15:56:44 +00:00
"import logging\n",
"\n",
2023-04-20 15:56:44 +00:00
"class EventGenerator(soil.BaseAgent):\n",
" level = logging.INFO\n",
" \n",
" def step(self):\n",
2023-04-20 15:56:44 +00:00
" # Do nothing until the time of the event\n",
" yield soil.When(self.model.event_time)\n",
" self.info(\"TV event happened\")\n",
" self.model.prob_tv_spread = 0.5\n",
" self.model.prob_neighbor_spread *= 2\n",
" self.model.prob_neighbor_spread = min(self.model.prob_neighbor_spread, 1)\n",
" yield\n",
" self.model.prob_tv_spread = 0\n",
"\n",
" while self.alive:\n",
" self.model.prob_neighbor_spread = self.model.prob_neighbor_spread * self.model.neighbor_factor\n",
" if self.model.prob_neighbor_spread < 0.01:\n",
" return self.die(\"neighbors can no longer spread the rumour\")\n",
" yield"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"### Environment (Model)\n",
"\n",
"Let's define a environment model to test our event generator agent.\n",
"This environment will have a single agent (the event generator).\n",
"We will also tell the environment to save the value of `prob_tv_spread` after every step:"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "code",
"execution_count": 112,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
2023-04-20 15:56:44 +00:00
"class NewsEnv(soil.NetworkEnvironment):\n",
" \n",
" prob_tv_spread = 0.1\n",
" prob_neighbor_spread = 0.1\n",
" event_time = 10\n",
" tv_factor = 0.5\n",
" neighbor_factor = 0.9\n",
"\n",
" \n",
" def init(self):\n",
" self.add_model_reporter(\"prob_tv_spread\")\n",
" self.add_agent(EventGenerator)"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"Once the environment has been defined, we can run a simulation "
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 102,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
"model_id": "1d73b3b3155f4132b863b3fb996ed1f1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-20 15:56:44 +00:00
"HBox(children=(IntProgress(value=0, description='NewsEnv', max=1, style=ProgressStyle(description_width='initi…"
]
},
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "display_data"
},
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-20 15:56:44 +00:00
"HBox(children=(IntProgress(value=0, max=1), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
2023-04-20 15:56:44 +00:00
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>step</th>\n",
" <th>agent_count</th>\n",
" <th>prob_tv_spread</th>\n",
" </tr>\n",
" <tr>\n",
" <th>time</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>3</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-04-20 15:56:44 +00:00
" step agent_count prob_tv_spread\n",
"time \n",
"0 0 1 0.1\n",
"10 1 1 0.1\n",
"11 2 1 0.5\n",
"12 3 1 0.0\n",
"13 4 1 0.0\n",
"14 5 1 0.0"
]
},
2023-04-20 15:56:44 +00:00
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"it = NewsEnv.run(iterations=1, dump=False, max_time=14)\n",
"\n",
"it[0].model_df()"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"As we can see, the event occurred right after `t=10`, so by `t=11` the value of `prob_tv_spread` was already set to `1.0`.\n",
"\n",
2023-04-20 15:56:44 +00:00
"You may notice nothing happened between `t=0` and `t=1`.\n",
"That is because there aren't any other agents in the simulation, and our event generator explicitly waited until `t=10`."
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"### Network agents"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-07-03T14:03:07.171127Z",
"start_time": "2017-07-03T16:03:07.165779+02:00"
},
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"In our disinformation scenario, we will model our agents as a FSM with two states: ``neutral`` (default) and ``infected``.\n",
"\n",
2023-04-20 15:56:44 +00:00
"Here's the code:"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "code",
"execution_count": 103,
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-11-03T10:58:16.051690Z",
"start_time": "2017-11-03T11:58:16.006044+01:00"
},
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
2023-04-20 15:56:44 +00:00
"class NewsSpread(soil.Agent):\n",
" has_tv = False\n",
" infected_by_friends = False\n",
" \n",
" @soil.state(default=True)\n",
" def neutral(self):\n",
" if self.infected_by_friends:\n",
" return self.infected\n",
" if self.has_tv:\n",
" if self.prob(self.model.prob_tv_spread):\n",
" return self.infected\n",
" \n",
" @soil.state\n",
" def infected(self):\n",
" for neighbor in self.iter_neighbors(state_id=self.neutral.id):\n",
" if self.prob(self.model.prob_neighbor_spread):\n",
" neighbor.infected_by_friends = True"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"We can check that our states are well defined, here:"
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 104,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [
{
"data": {
"text/plain": [
"['dead', 'neutral', 'infected']"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"NewsSpread.states()"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"### Environment (Model)"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split",
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"Let's modify our simple simulation.\n",
"We will add a network of agents of type NewsSpread.\n",
"\n",
2023-04-20 15:56:44 +00:00
"Only one agent (0) will have a TV (in blue)."
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 105,
"metadata": {
"cell_style": "split",
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
2023-04-20 15:56:44 +00:00
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAAsTAAALEwEAmpwYAAASZ0lEQVR4nO3dcWzX9Z3H8ecPBfurzIYd7Z2YQz3MbTjbzFwxuXNzVZecMM2Cd7cCE1cSFilTj21llwoLGedWc5btsmSrOLP9YC5qIoSRCDEIStTtZuvOKyDLbQozBk3hVuvi71cr9Ht/fKlC+/sVCr/f9/v79ft8JE3b7+fz/X7f4Y8X73y+n9+3qSAIkCRFY0rcBUhSkhi6khQhQ1eSImToSlKEDF1JitCF4w3OnDkzuOKKKyIqRZImh5dffvlYEAS1+cbGDd0rrriCnp6e0lQlSZNUKpX6Y6ExlxckKUKGriRFyNCVpAgZupIUIUNXkiJk6EpShMbdMqYI9fVBJgO9vTAwADU10NAAy5ZBbd7tfpIqkKEbt+5u6OiAnTvD3wcHPxrbuhXWrYP586G9HebNi6dGSUXj8kKcurqgqQm2bQvD9tTABcjlwmPbtoXzurqir1FSUdnpxqWrC9raIJs989wgCOe1tYW/t7aWtjZJJWOnG4fu7rMP3FONBK8fzZYqlqEbh46OcOlglH8BLgZSwFWFzs3lwvMlVSRDN2p9feFDszx/m+5K4F7g6vHODwLYsQOOHi1RgZJKydCNWiZTcOg/gA5gxpmukUqNex1J5cvQjVpv79hdChOVy8G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axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"def generate_simple():\n",
" G = nx.Graph()\n",
" G.add_edge(0, 1)\n",
" G.add_edge(0, 2)\n",
" G.add_edge(2, 3)\n",
" G.add_node(4)\n",
" return G\n",
"\n",
2023-04-20 15:56:44 +00:00
"G = generate_simple()\n",
"pos = nx.spring_layout(G)\n",
"nx.draw_networkx(G, pos, node_color='red')\n",
"nx.draw_networkx(G, pos, nodelist=[0], node_color='blue')"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "code",
"execution_count": 106,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
2023-04-20 15:56:44 +00:00
"class NewsEnv(soil.NetworkEnvironment):\n",
" \n",
" prob_tv_spread = 0\n",
" prob_neighbor_spread = 0.1\n",
" event_time = 10\n",
" tv_factor = 0.5\n",
" neighbor_factor = 0.9\n",
"\n",
2023-04-20 15:56:44 +00:00
" \n",
" def init(self):\n",
" self.add_agent(EventGenerator)\n",
" self.G = generate_simple()\n",
" self.populate_network(NewsSpread)\n",
" self.agent(node_id=0).has_tv = True\n",
" self.add_model_reporter('prob_tv_spread')\n",
" self.add_model_reporter('prob_neighbor_spread')"
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 107,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
2023-04-20 15:56:44 +00:00
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "dc1e3d6242e24e009601774769b9525b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, description='NewsEnv', max=1, style=ProgressStyle(description_width='initi…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=1), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-04-20 15:56:44 +00:00
"\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
2023-04-20 15:56:44 +00:00
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
2023-04-20 15:56:44 +00:00
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>step</th>\n",
" <th>agent_count</th>\n",
" <th>prob_tv_spread</th>\n",
2023-04-20 15:56:44 +00:00
" <th>prob_neighbor_spread</th>\n",
" </tr>\n",
" <tr>\n",
2023-04-20 15:56:44 +00:00
" <th>time</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>0</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
2023-04-20 15:56:44 +00:00
" <td>1</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
2023-04-20 15:56:44 +00:00
" <td>2</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
2023-04-20 15:56:44 +00:00
" <td>3</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
2023-04-20 15:56:44 +00:00
" <td>4</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
2023-04-20 15:56:44 +00:00
" <td>5</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
2023-04-20 15:56:44 +00:00
" <td>6</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
2023-04-20 15:56:44 +00:00
" <td>7</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
2023-04-20 15:56:44 +00:00
" <td>8</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
2023-04-20 15:56:44 +00:00
" <td>9</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>10</td>\n",
2023-04-20 15:56:44 +00:00
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
2023-04-20 15:56:44 +00:00
" <td>11</td>\n",
" <td>6</td>\n",
" <td>0.5</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.200000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
2023-04-20 15:56:44 +00:00
" <td>12</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.180000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
2023-04-20 15:56:44 +00:00
" <td>13</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.162000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
2023-04-20 15:56:44 +00:00
" <td>14</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.145800</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
2023-04-20 15:56:44 +00:00
" <td>15</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.131220</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
2023-04-20 15:56:44 +00:00
" <td>16</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.118098</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
2023-04-20 15:56:44 +00:00
" <td>17</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.106288</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
2023-04-20 15:56:44 +00:00
" <td>18</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.095659</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
2023-04-20 15:56:44 +00:00
" <td>19</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.086093</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>20</td>\n",
2023-04-20 15:56:44 +00:00
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.077484</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-04-20 15:56:44 +00:00
" step agent_count prob_tv_spread prob_neighbor_spread\n",
"time \n",
"0 0 6 0.0 0.100000\n",
"1 1 6 0.0 0.100000\n",
"2 2 6 0.0 0.100000\n",
"3 3 6 0.0 0.100000\n",
"4 4 6 0.0 0.100000\n",
"5 5 6 0.0 0.100000\n",
"6 6 6 0.0 0.100000\n",
"7 7 6 0.0 0.100000\n",
"8 8 6 0.0 0.100000\n",
"9 9 6 0.0 0.100000\n",
"10 10 6 0.0 0.100000\n",
"11 11 6 0.5 0.200000\n",
"12 12 6 0.0 0.180000\n",
"13 13 6 0.0 0.162000\n",
"14 14 6 0.0 0.145800\n",
"15 15 6 0.0 0.131220\n",
"16 16 6 0.0 0.118098\n",
"17 17 6 0.0 0.106288\n",
"18 18 6 0.0 0.095659\n",
"19 19 6 0.0 0.086093\n",
"20 20 6 0.0 0.077484"
]
},
2023-04-20 15:56:44 +00:00
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"it = NewsEnv.run(max_time=20)\n",
"it[0].model_df()"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"In this case, notice that the inclusion of other agents (which run every step) means that the simulation did not skip to `t=10`.\n",
"\n",
"Now, let's look at the state of our agents in every step:"
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 108,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEGCAYAAAB1iW6ZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAAsTAAALEwEAmpwYAAAs4UlEQVR4nO3deXhU5d3/8fc3ITEkIiRgEQXEBZEdJIItirghKnVvwR1xw5Xq06fa6s+6tE+19XGrqJUHiloVFDdErEtdKFaFgOyIoCKCG0LCkhCTwP3748wZhjCTTJKZM5Pk87quuZiZc+acm5Phy5373OdzzDmHiIg0fhmpboCIiCSGCrqISBOhgi4i0kSooIuINBEq6CIiTUSLVO24Xbt2rkuXLqnavYhIozR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447uP3223nooYd228fZZ5/NQw89xD333ENhYSFVVVVcccUVlJaWkpeXx9SpUxk1alTU9i1YsIB169axZMkSAEpKSsLLysrKWLBgAbNmzWLMmDHhdZYtW8bs2bNp2bIl5557Ltdffz1HHnkka9as4cQTT2T58uUceuih/Pvf/6ZFixa89dZb/O53v+P555/nkUceITc3l+XLl7No0SIOO+ywBB/xpsHvmeukqKRKXBcWOedmAjOrvXdrxPPfAr9NZMNq60knU6dOnRg8eDAA559/Pg8++CAAI0eOBGDu3LkMHTo03PM+77zzmDVrFqeffjoZGRnh9c4//3zOPPPMuPbZokULhg8fziuvvMLZZ5/Nq6++yp///Oeo6x544IF8/vnnXHvttZxyyikMGzYsvOycc84BYMiQIWzevDlc7E899VRatmwJwFtvvcWyZTtnnW7evJmtW7eyadMmLrroIlauXImZUVnpDSHMmjWL6667DoA+ffrQp0+fuP5OzY0/5FIS+lMkaGl1pWi6qD6P2X+dl5fX4G3VZNSoUTz00EMUFBRQWFhIq1atoq6Xn5/PwoULef3113n00Ud59tlnmTRpUtxt37FjBx9++CE5OTm7rHvNNddwzDHH8OKLL7J69WqGDh0ad9tl51DLRg25SIoonCuKNWvW8MEHHwDw9NNPc+SRR+6yfODAgbz33nv88MMPbN++nWeeeYajjz4a8IrltGnTYn42UqtWrdiyZUv49dFHH838+fOZMGFCzOEWgB9++IEdO3Zw1lln8Yc//IH58+eHl02dOhWA2bNn07p1a1q3br3b54cNG8Zf//rX8OsFCxYAsGnTJvbbz5uROnny5PDyIUOG8PTTTwOwZMkSFi1aFLNtzZk/1LJpWyXbd7gUt0aaIxX0KLp168b48ePp3r07xcXFXHnllbss79ChA3fddRfHHHMMffv2ZcCAAZx2mnetVV5eHnPmzKFXr168/fbb3HrrrdF2AcDo0aMZO3Ys/fr1Y9u2bWRmZjJixAhee+01RowYEfNz69atY+jQofTr14/zzz+fP/3pT+FlOTk59O/fn7FjxzJx4sSon3/wwQcpKiqiT58+9OjRg0cffRSA3/zmN/z2t7+lf//+u8yGufLKK9m6dSvdu3fn1ltvZcCAJj2Jqd5KQj1z57yiLhI0cy41PYnCwkJXfS7z8uXL6d69e0ra41u9ejUjRowIn0xsTIYOHRo+ydrcpeK7dObD7zN/TQkAb91wNAf/JP5rEkTiZWbznHNR/5Grhy6SIMVllbTJzQo91zi6BE8nRavp0qVLwnvnV199Ne+///4u740bN46LL7641s8OGjSIH3/8cZf3nnzySXr37r3buu+++26D2ikNs7G0goN/sifzvizW1EVJCRX0AIwfP77en/3oo48S2BJJlqrtO9hcXsmB7fKY92VxeDxdJEgachFJgE3bKnEODtzbGzffWKqTohI8FXSRBPDHzPdtk8MeLTI0hi4poYIukgD+VaIFednk52YroEtSQgVdJAH8k6D5udnk52Wrhy4poYKeYHXJQ6+rr7/+mrPPPrvebRg9enT4KtamIpnHuy78HnlBXjYFeVma5SIpoYJeD4nKQ6+rfffdN2UFORk56k0pm93Pb8nPDQ25KKBLUiB9py2+dhN8uzix29ynN5x0V42rBJWHDt6VnYMGDeKdd96hpKSEiRMnctRRR7F9+3Zuuukm3n33XX788Ueuvvpqrrjiil2uYi0rK2P06NEsWbKEbt268fXXXzN+/PjwVaI333wzM2bMoGXLlrz88su0b98e8JIW77rrLjZv3sy9997LiBEjKC8v58orr6SoqIgWLVpw7733cswxxzB58mReeOEFtm7dyvbt23nvvfd2+zt88803jBw5ks2bN1NVVcUjjzzCUUcdxZ577slll12223HwIwtmz57NOeecw9ChQ7nhhhvYunUr7dq1Y/LkyXTo0IEJEybw2GOPUVFRwcEHH8yTTz5Jbm4uX3zxBeeeey5bt24Nxy2kg5KySnKyMmiZnRkq6OqhS/DUQ49ixYoVXHXVVSxfvpy99tprtzz0IUOGcOONN/L222+zYMEC5s6dy0svvQQQzkNfunQpRx99NLfffnuN+6qqqmLOnDncf//94XUnTpxI69atmTt3LnPnzmXChAl88cUXu3zu4YcfJj8/n2XLlnHnnXcyb9688LLS0lKOOOIIFi5cyJAhQ5gwYUJ42erVq5kzZw6vvvoqY8eOpby8nPHjx2NmLF68mGeeeYaLLrqI8vJyAObPn8+0adOiFnPwAshOPPFEFixYwMKFC+nXr1+tx6GiooKioiKuu+46rr32WqZNm8a8efMYM2YMN998MwBnnnkmc+fOZeHChXTv3j2cSzNu3DiuvPJKFi9eTIcOHWo8tkHaWFpBQW42APl52WzaVknV9h0pbpU0N+nbQ6+lJ51MQeah+8sHDBjA6tWrAXjjjTdYtGhReHhl06ZNrFy5kkMOOST8udmzZzNu3DgAevXqtUtGeXZ2djjca8CAAbz55pvhZb/85S/JyMiga9euHHjggXzyySf
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"soil.analysis.plot(it[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"hideCode": false,
"hidePrompt": false,
"run_control": {
"frozen": true
}
},
"source": [
2023-04-20 15:56:44 +00:00
"## Running in more scenarios\n",
"\n",
"In real life, you probably want to run several simulations, varying some of the parameters so that you can compare and answer your research questions.\n",
"\n",
"For instance:\n",
" \n",
"* Does the outcome depend on the structure of our network? We will use different generation algorithms to compare them (Barabasi-Albert and Erdos-Renyi)\n",
"* How does neighbor spreading probability affect my simulation? We will try probability values in the range of [0, 0.4], in intervals of 0.1."
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 109,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [],
"source": [
"class NewsEnvComplete(soil.Environment):\n",
" prob_tv = 0.05\n",
" prob_tv_spread = 0\n",
" prob_neighbor_spread = 0\n",
" event_time = 10\n",
" tv_factor = 0\n",
" neighbor_factor = 0.5\n",
" generator = \"erdos_renyi_graph\"\n",
" n = 100\n",
"\n",
" def init(self):\n",
" self.add_agent(EventGenerator)\n",
" if not self.G:\n",
" opts = {\"n\": self.n}\n",
" if self.generator == \"erdos_renyi_graph\":\n",
" opts[\"p\"] = 0.5\n",
" elif self.generator == \"barabasi_albert_graph\":\n",
" opts[\"m\"] = 4\n",
" self.create_network(generator=self.generator, **opts)\n",
"\n",
" self.populate_network([NewsSpread,\n",
" NewsSpread.w(has_tv=True)],\n",
" [1-self.prob_tv, self.prob_tv])\n",
" self.add_model_reporter('prob_tv_spread')\n",
" self.add_model_reporter('prob_neighbor_spread')\n",
" self.add_agent_reporter('state_id')"
]
},
{
"cell_type": "markdown",
"metadata": {},
2023-04-20 15:56:44 +00:00
"source": [
"Since we do not care about previous results, we will set`overwrite=True`."
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
2023-04-20 15:56:44 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"[INFO ][17:29:24] Output directory: /mnt/data/home/j/git/lab.gsi/soil/soil/examples/tutorial/soil_output\n"
]
},
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
"model_id": "5528388b3491489caecc160a2d19ee7a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, description='newspread', max=10, style=ProgressStyle(description_width='in…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = erdos_renyi_graph\n",
"\r",
"prob_neighbor_spread = 0\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = erdos_renyi_graph\n",
"\r",
"prob_neighbor_spread = 0.25\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = erdos_renyi_graph\n",
"\r",
"prob_neighbor_spread = 0.5\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = erdos_renyi_graph\n",
"\r",
"prob_neighbor_spread = 0.75\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = erdos_renyi_graph\n",
"\r",
"prob_neighbor_spread = 1.0\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = barabasi_albert_graph\n",
"\r",
"prob_neighbor_spread = 0\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = barabasi_albert_graph\n",
"\r",
"prob_neighbor_spread = 0.25\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = barabasi_albert_graph\n",
"\r",
"prob_neighbor_spread = 0.5\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-20 15:56:44 +00:00
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = barabasi_albert_graph\n",
"\r",
"prob_neighbor_spread = 0.75\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"n = 100\n",
"\r",
"generator = barabasi_albert_graph\n",
"\r",
"prob_neighbor_spread = 1.0\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
2023-04-20 15:56:44 +00:00
"s = soil.Simulation(model=NewsEnvComplete, iterations=5, max_time=30, dump=True, overwrite=True)\n",
"N = 100\n",
"probabilities = [0, 0.25, 0.5, 0.75, 1.0]\n",
"generators = [\"erdos_renyi_graph\", \"barabasi_albert_graph\"]\n",
"\n",
"\n",
"it = s.run(name=f\"newspread\", matrix=dict(n=[N], generator=generators, prob_neighbor_spread=probabilities))"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [],
"source": [
"assert len(it) == len(probabilities) * len(generators) * s.iterations"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T11:05:18.043194Z",
"start_time": "2017-07-03T13:05:18.034699+02:00"
},
"cell_style": "center",
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"The results are conveniently stored in sqlite (history of agent and environment state) and the configuration is saved in a YAML file.\n",
"\n",
"You can also export the results to GEXF format (dynamic network) and CSV using .`run(dump=['gexf', 'csv'])` or the command line flags `--graph --csv`."
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 122,
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-11-01T14:05:56.404540Z",
"start_time": "2017-11-01T15:05:56.122876+01:00"
},
"cell_style": "split",
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
2023-04-20 15:56:44 +00:00
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[01;34msoil_output\u001b[00m\n",
"└── \u001b[01;34mnewspread\u001b[00m\n",
" ├── newspread_1681989837.124865.dumped.yml\n",
" ├── newspread_1681990513.1584163.dumped.yml\n",
" ├── newspread_1681990524.5204282.dumped.yml\n",
" ├── newspread_1681990796.858183.dumped.yml\n",
" ├── newspread_1682002299.544348.dumped.yml\n",
" ├── newspread_1682003721.597205.dumped.yml\n",
" ├── newspread_1682003784.1948986.dumped.yml\n",
" ├── newspread_1682003812.4626257.dumped.yml\n",
" ├── newspread_1682004020.182087.dumped.yml\n",
" ├── newspread_1682004044.6837814.dumped.yml\n",
" ├── newspread_1682004398.267355.dumped.yml\n",
" ├── newspread_1682004564.1052232.dumped.yml\n",
" └── newspread.sqlite\n",
"\n",
"1 directory, 13 files\n",
"21M\tsoil_output/newspread\n"
]
}
],
"source": [
2023-04-20 15:56:44 +00:00
"!tree soil_output\n",
"!du -xh soil_output/*"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-07-02T10:40:14.384177Z",
"start_time": "2017-07-02T12:40:14.381885+02:00"
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"### Analysing the results"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"#### Loading data"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"Once the simulations are over, we can use soil to analyse the results.\n",
"\n",
"There are two main ways: directly using the iterations returned by the `run` method, or loading up data from the results database.\n",
"This is particularly useful to store data between sessions, and to accumulate results over multiple runs.\n",
"\n",
"The mainThe main method to load data from the database is `read_sql`, which can be used in two ways:\n",
"\n",
"* `analysis.read_sql(<sqlite_file>)` to load all the results from a sqlite database . e.g. `read_sql('my_simulation/file.db.sqlite')`\n",
"* `analysis.read_sql(name=<simulation name>)` will look for the default path for a simulation named `<simulation name>`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The result in both cases is a named tuple with four dataframes:\n",
"\n",
"* `configuration`, which contains configuration parameters per simulation\n",
"* `parameters`, which shows the parameters used **in every iteration** of every simulation\n",
"* `env`, with the data collected from the model in each iteration (as specified in `model_reporters`)\n",
"* `agents`, like `env`, but for `agent_reporters`"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T14:44:30.978223Z",
"start_time": "2017-07-03T16:44:30.971952+02:00"
},
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"Let's see it in action by loading the stored results into a pandas dataframe:"
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 123,
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-10-19T15:57:44.101253Z",
"start_time": "2017-10-19T17:57:44.039710+02:00"
},
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
"res = soil.read_sql(name=\"newspread\", include_agents=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting data\n",
"\n",
"Once we have loaded the results from the file, we can use them just like any other dataframe.\n",
"\n",
"Here is an example of plotting the ratio of infected users in each of our simulations:"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEWCAYAAAB2X2wCAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAAsTAAALEwEAmpwYAABn5UlEQVR4nO2dd3xUVfr/32dKekgPBEIJNRBSkBKKBYiKisqqSLEBLhYUXFlFXX+iwOLqqt91FXFVVFBBQVEkoogFsCBSAoQqIBAgENJ7m3Z+f8xkSJlMQjJJSDjv1ys69z7PPfe5c4dnnjn3nM8RUkoUCoVC0frRtHQACoVCoXANKqErFApFG0EldIVCoWgjqISuUCgUbQSV0BUKhaKNoBK6QqFQtBFUQlc4RQixTAixsIHHzhNCLHdiPyCEGFndVwjRRQhRJITQNuS8Ds4zQghx1NbmX1zRpqsRQowUQqReBHEsFEJkCSHOufo+NBYhxBVCiMMtHcfFjErojUQIkSKEMAghgqvt3y2EkEKIbi0U2kWPlDJKSrnZwf5TUkofKaUZQAixWQgxvRGnWgC8YWvzy0a006YRQnQBHgP6SSk7VL8PLRCPFEL0rNiWUv4ipezTErG0FlRCdw0ngMkVG0KIaMCr5cKpPxdL9dXEdAUONORAIYTOxbFczHQBsqWUGU19okvsfW02VEJ3DR8B91TangJ8WNlBCOEuhHhFCHFKCJEuhHhLCOFpswUIIdYJITKFELm21+GVjt0shPinEGKLEKJQCPFd9V8ElXxHCiFShRBP2346pwgh7qxkXyaE+J8Q4hshRDEwSgjR13aOPFs3yM3Vmg0WQnxvO/dPQoiuldp7TQhxWghRIIRIEkJcUe1YDyHEKtux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
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"for (g, group) in res.env.dropna().groupby(\"params_id\"):\n",
" params = res.parameters.query(f'params_id == \"{g}\"').iloc[0]\n",
" title = f\"{params.generator.rstrip('_graph')} {params.prob_neighbor_spread}\"\n",
" prob = group.groupby(by=[\"step\"]).prob_neighbor_spread.mean()\n",
" line = \"-\"\n",
" if \"barabasi\" in params.generator:\n",
" line = \"--\"\n",
" prob.rename(title).fillna(0).plot(linestyle=line)\n",
"plt.title(\"Mean probability for each configuration\")\n",
"plt.legend();"
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 130,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false,
"scrolled": true
},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEWCAYAAAB2X2wCAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAAsTAAALEwEAmpwYAAB65UlEQVR4nO2dd3hUVfrHP2dqMukV0oDQWyDUUCxARGzA2iiiggi6qLiyiu1nQRZXV911F8SuYEFBEaSIYgNUeu9C6CQESO+Taef3xx2GBNKANML5PM88mbnn3HPfe+fmnXPfc97vEVJKFAqFQnH5o6trAxQKhUJRPSiHrlAoFA0E5dAVCoWigaAcukKhUDQQlENXKBSKBoJy6AqFQtFAUA5dUWMIId4VQjxfA+22EUJsE0LkCSEeLaP8eyHE6Cq21UgI8Zu7rX9Xt61VRQjRTwiRXFfHVzQMDHVtgKL+IIQ4AjQCnEA+8APwiJQyvwr7jgHGSSmvOrNNSvnXmrGUJ4EVUsr4sgqllDdeQFsPAOmAv7yEpAwhxGwgWUr53MW2oVBcKqqHrjiXwVJKXyAe6AI8U7fmlElTYHc1trXnUpx5Q0EIoa9rGxSXh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LOdQ4lr7N8sjYuZnwth247v4JhDZpdtFt/pHyB/9Y+w9OFJxgWOthPNbtMfxMftVntEKhqDOUQ6+nWA8ehJyTpLaOwTvtJDc8NIn21wy46PBKpjWTf234F8sOLyM2IJZPbviEro2U7I5C0ZBQDr2ecnD+YpKD/RF6I2P+8w4W/4CLakdKyZJDS3h94+vk2/P5a+e/Mj5uPCa9qZotVig
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"for (g, group) in res.agents.dropna().groupby(\"params_id\"):\n",
" params = res.parameters.query(f'params_id == \"{g}\"').iloc[0]\n",
" title = f\"{params.generator.rstrip('_graph')} {params.prob_neighbor_spread}\"\n",
" counts = group.groupby(by=[\"step\", \"state_id\"]).value_counts().unstack()\n",
" line = \"-\"\n",
" if \"barabasi\" in params.generator:\n",
" line = \"--\"\n",
" (counts.infected/counts.sum(axis=1)).rename(title).fillna(0).plot(linestyle=line)\n",
"plt.legend()\n",
"plt.xlim([9, None]);\n",
"plt.title(\"Ratio of infected users\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"## Data format"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"### Parameters\n",
"\n",
"The `parameters` dataframe has three keys:\n",
"\n",
"* The identifier of the simulation. This will be shared by all iterations launched in the same run\n",
"* The identifier of the parameters used in the simulation. This will be shared by all iterations that have the exact same set of parameters.\n",
"* The identifier of the iteration. Each row should have a different iteration identifier\n",
"\n",
"There will be a column per each parameter passed to the environment. In this case, that's three: **generator**, **n** and **prob_neighbor_spread**."
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 18,
"metadata": {
2023-04-20 15:56:44 +00:00
"scrolled": true
},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>key</th>\n",
" <th>generator</th>\n",
" <th>n</th>\n",
" <th>prob_neighbor_spread</th>\n",
" </tr>\n",
" <tr>\n",
" <th>iteration_id</th>\n",
" <th>params_id</th>\n",
" <th>simulation_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th>39063f8</th>\n",
" <th>newspread_1682002299.544348</th>\n",
" <td>erdos_renyi_graph</td>\n",
" <td>100</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5db645d</th>\n",
" <th>newspread_1682002299.544348</th>\n",
" <td>barabasi_albert_graph</td>\n",
" <td>100</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8f26adb</th>\n",
" <th>newspread_1682002299.544348</th>\n",
" <td>barabasi_albert_graph</td>\n",
" <td>100</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>cb3dbca</th>\n",
" <th>newspread_1682002299.544348</th>\n",
" <td>erdos_renyi_graph</td>\n",
" <td>100</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d1fe9c1</th>\n",
" <th>newspread_1682002299.544348</th>\n",
" <td>barabasi_albert_graph</td>\n",
" <td>100</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-04-20 15:56:44 +00:00
"key generator \\\n",
"iteration_id params_id simulation_id \n",
"0 39063f8 newspread_1682002299.544348 erdos_renyi_graph \n",
" 5db645d newspread_1682002299.544348 barabasi_albert_graph \n",
" 8f26adb newspread_1682002299.544348 barabasi_albert_graph \n",
" cb3dbca newspread_1682002299.544348 erdos_renyi_graph \n",
" d1fe9c1 newspread_1682002299.544348 barabasi_albert_graph \n",
"\n",
"key n prob_neighbor_spread \n",
"iteration_id params_id simulation_id \n",
"0 39063f8 newspread_1682002299.544348 100 1.0 \n",
" 5db645d newspread_1682002299.544348 100 0.0 \n",
" 8f26adb newspread_1682002299.544348 100 0.5 \n",
" cb3dbca newspread_1682002299.544348 100 0.5 \n",
" d1fe9c1 newspread_1682002299.544348 100 1.0 "
]
},
2023-04-20 15:56:44 +00:00
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"res.parameters.head()"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"### Configuration\n",
"\n",
"This dataset is indexed by the identifier of the simulation, and there will be a column per each attribute of the simulation.\n",
"For instance, there is one for the number of processes used, another one for the path where the results were stored, etc."
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>index</th>\n",
" <th>version</th>\n",
" <th>source_file</th>\n",
" <th>name</th>\n",
" <th>description</th>\n",
" <th>group</th>\n",
" <th>backup</th>\n",
" <th>overwrite</th>\n",
" <th>dry_run</th>\n",
" <th>dump</th>\n",
" <th>...</th>\n",
" <th>num_processes</th>\n",
" <th>exporters</th>\n",
" <th>model_reporters</th>\n",
" <th>agent_reporters</th>\n",
" <th>tables</th>\n",
" <th>outdir</th>\n",
" <th>exporter_params</th>\n",
" <th>level</th>\n",
" <th>skip_test</th>\n",
" <th>debug</th>\n",
" </tr>\n",
" <tr>\n",
" <th>simulation_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>newspread_1682002299.544348</th>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>None</td>\n",
" <td>newspread</td>\n",
" <td></td>\n",
" <td>None</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>...</td>\n",
" <td>1</td>\n",
" <td>[&lt;class 'soil.exporters.default'&gt;]</td>\n",
" <td>{}</td>\n",
" <td>{}</td>\n",
" <td>{}</td>\n",
" <td>/mnt/data/home/j/git/lab.gsi/soil/soil/example...</td>\n",
" <td>{}</td>\n",
" <td>20</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1 rows × 29 columns</p>\n",
"</div>"
],
"text/plain": [
2023-04-20 15:56:44 +00:00
" index version source_file name description \\\n",
"simulation_id \n",
"newspread_1682002299.544348 0 2 None newspread \n",
"\n",
" group backup overwrite dry_run dump ... \\\n",
"simulation_id ... \n",
"newspread_1682002299.544348 None False True False True ... \n",
"\n",
" num_processes exporters \\\n",
"simulation_id \n",
"newspread_1682002299.544348 1 [<class 'soil.exporters.default'>] \n",
"\n",
" model_reporters agent_reporters tables \\\n",
"simulation_id \n",
"newspread_1682002299.544348 {} {} {} \n",
"\n",
" outdir \\\n",
"simulation_id \n",
"newspread_1682002299.544348 /mnt/data/home/j/git/lab.gsi/soil/soil/example... \n",
"\n",
" exporter_params level skip_test debug \n",
"simulation_id \n",
"newspread_1682002299.544348 {} 20 False False \n",
"\n",
"[1 rows x 29 columns]"
]
},
2023-04-20 15:56:44 +00:00
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"res.config.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Model reporters\n",
"\n",
"The `env` dataframe includes the data collected from the model.\n",
"The keys in this case are the same as `parameters`, and an additional one: **step**."
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th>agent_count</th>\n",
" <th>time</th>\n",
" <th>prob_tv_spread</th>\n",
" <th>prob_neighbor_spread</th>\n",
" </tr>\n",
" <tr>\n",
" <th>simulation_id</th>\n",
" <th>params_id</th>\n",
" <th>iteration_id</th>\n",
" <th>step</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"5\" valign=\"top\">newspread_1682002299.544348</th>\n",
" <th rowspan=\"5\" valign=\"top\">fcfc955</th>\n",
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th>0</th>\n",
" <td>101</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>101</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>101</td>\n",
" <td>2</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>101</td>\n",
" <td>3</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>101</td>\n",
" <td>4</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-04-20 15:56:44 +00:00
" agent_count time \\\n",
"simulation_id params_id iteration_id step \n",
"newspread_1682002299.544348 fcfc955 0 0 101 0 \n",
" 1 101 1 \n",
" 2 101 2 \n",
" 3 101 3 \n",
" 4 101 4 \n",
"\n",
" prob_tv_spread \\\n",
"simulation_id params_id iteration_id step \n",
"newspread_1682002299.544348 fcfc955 0 0 0.0 \n",
" 1 0.0 \n",
" 2 0.0 \n",
" 3 0.0 \n",
" 4 0.0 \n",
"\n",
" prob_neighbor_spread \n",
"simulation_id params_id iteration_id step \n",
"newspread_1682002299.544348 fcfc955 0 0 0.0 \n",
" 1 0.0 \n",
" 2 0.0 \n",
" 3 0.0 \n",
" 4 0.0 "
]
},
2023-04-20 15:56:44 +00:00
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"res.env.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Agent reporters\n",
"\n",
"This dataframe reflects the data collected for all the agents in the simulation, in every step where data collection was invoked.\n",
"\n",
"The key in this dataframe is similar to the one in the `parameters` dataframe, but there will be two more keys: the `step` and the `agent_id`.\n",
"There will be a column per each agent reporter added to the model. In our case, there is only one: `state_id`."
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th>state_id</th>\n",
" </tr>\n",
" <tr>\n",
" <th>simulation_id</th>\n",
" <th>params_id</th>\n",
" <th>iteration_id</th>\n",
" <th>step</th>\n",
" <th>agent_id</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"5\" valign=\"top\">newspread_1682002299.544348</th>\n",
" <th rowspan=\"5\" valign=\"top\">fcfc955</th>\n",
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th>0</th>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>neutral</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-04-20 15:56:44 +00:00
" state_id\n",
"simulation_id params_id iteration_id step agent_id \n",
"newspread_1682002299.544348 fcfc955 0 0 0 None\n",
" 1 neutral\n",
" 2 neutral\n",
" 3 neutral\n",
" 4 neutral"
]
},
2023-04-20 15:56:44 +00:00
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"res.agents.head()"
]
}
],
"metadata": {
2023-04-20 15:56:44 +00:00
"hide_code_all_hidden": false,
"kernelspec": {
2023-04-20 15:56:44 +00:00
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"navigate_num": "#000000",
"navigate_text": "#333333",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700",
"sidebar_border": "#EEEEEE",
"wrapper_background": "#FFFFFF"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "31px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_position": {
"height": "867px",
"left": "0px",
"right": "1670px",
"top": "106px",
"width": "250px"
},
"toc_section_display": "block",
"toc_window_display": false,
"widenNotebook": false
},
"vscode": {
"interpreter": {
"hash": "3581132406f7320837865a422f37590c78ed7dabfbcb5bc7771b9d116b13a5cf"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}