CovidNetworkContagion

To show how the Melodie.Network module can be used, we provide this CovidNetworkContagion model, which is based the CovidContagion model. So, if you haven’t, we will strongly suggest to read the Tutorial section first.

The differences are:

  • Agents are connected within a network randomly.

  • The network is constructed with edges, representing the connections between the agents. An infected agent can pass the virus through the edge to another uninfected agent.

Project Structure

The project structure is as follows.

CovidNetworkContagion
├── data
│   ├── input
│   │   ├── SimulatorScenarios.xlsx
│   │   ├── ID_HealthState.xlsx
│   │   ├── ID_AgeGroup.xlsx
│   │   └── Parameter_AgeGroup_TransitionProb.xlsx
│   └── output
│       ├── CovidGridContagion.sqlite
│       ├── PopulationInfection_S0R0.png
│       └── PopulationInfection_S1R0.png
├── source
│   ├── agent.py
│   ├── environment.py
│   ├── data_collector.py
│   ├── data_info.py
│   ├── data_loader.py
│   ├── scenario.py
│   ├── model.py
│   └── analyzer.py
├── config.py
├── run_simulator.py
├── run_analyzer.py
└── readme.md

Model

As shown in the project structure, unlike CovidGridContagion, it is same with the structure of the CovidContagion model. Because the edges in the network do not store model-specific parameters like spots in the grid. The network only provide common functions.

So, by calling the create_network function (Line 22) without passing any parameters, the network component is created for the model with the default classes in Melodie.

model.py
 1from typing import TYPE_CHECKING
 2
 3from Melodie import Model
 4
 5from source import data_info
 6from source.agent import CovidAgent
 7from source.data_collector import CovidDataCollector
 8from source.environment import CovidEnvironment
 9from source.scenario import CovidScenario
10
11if TYPE_CHECKING:
12    from Melodie import AgentList
13
14
15class CovidModel(Model):
16    scenario: "CovidScenario"
17
18    def create(self):
19        self.agents: "AgentList[CovidAgent]" = self.create_agent_list(CovidAgent)
20        self.environment = self.create_environment(CovidEnvironment)
21        self.data_collector = self.create_data_collector(CovidDataCollector)
22        self.network = self.create_network()
23
24    def setup(self):
25        self.agents.setup_agents(
26            agents_num=self.scenario.agent_num,
27            params_df=self.scenario.get_dataframe(data_info.agent_params),
28        )
29        self.network.setup_agent_connections(
30            agent_lists=[self.agents],
31            network_type=self.scenario.network_type,
32            network_params=self.scenario.get_network_params(),
33        )

Then, the network needs to be setup based on

  • agent_lists - a list of agents, i.e., multiple groups of agents can be connected in the same network. They are distinguished by the category attribute as in the CovidGridContagion model.

  • network_type and network_params - follows the tradition of NetworkX package, because the Melodie.Network module is developed highly based on it.

Scenario

The parameters to setup the network could be inserted into the model through scenario.

scenario.py
 1from Melodie import Scenario
 2from source import data_info
 3
 4
 5class CovidScenario(Scenario):
 6
 7    def setup(self):
 8        self.period_num: int = 0
 9        self.agent_num: int = 0
10        self.network_type: str = ""
11        self.network_param_k: int = 0
12        self.network_param_p: float = 0.0
13        self.network_param_m: int = 0
14        self.initial_infected_percentage: float = 0.0
15        self.young_percentage: float = 0.0
16        self.infection_prob: float = 0.0
17        self.setup_transition_probs()
18
19    def get_network_params(self):
20        if self.network_type == "barabasi_albert_graph":
21            network_params = {"m": self.network_param_m}
22        elif self.network_type == "watts_strogatz_graph":
23            network_params = {"k": self.network_param_k, "p": self.network_param_p}
24        else:
25            raise NotImplementedError
26        return network_params

For more details of the Network module, please refer to the API Reference section.