We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
random.sample with a set is removed in Python 3.11
Using [`random.sample`](https://docs.python.org/3/library/random.html#random.sample) with a set has been depreciated in Python 3.9 and was removed in Python 3.11.

> Changed in version 3.11: The population must be a sequence. Automatic conversion of sets to lists is no longer supported.

Two cases of this function being used with a set have been detected by the CI in Mesa, both in example models:

1. https://github.com/projectmesa/mesa/blob/5135be8759d9fdac551c5c6ed13e9ae5205bce3e/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py#L28
2. https://github.com/projectmesa/mesa/blob/5135be8759d9fdac551c5c6ed13e9ae5205bce3e/examples/virus_on_network/virus_on_network/model.py#L84

What would be the most efficient way to get `self.G.nodes()` in a sequence form (so probably list or tuple)? I feel like just putting it inside a `list()` isn't the proper way to do it.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `examples/virus_on_network/virus_on_network/model.py`
Content:
```
1 import math
2 from enum import Enum
3 import networkx as nx
4 
5 from mesa import Agent, Model
6 from mesa.time import RandomActivation
7 from mesa.datacollection import DataCollector
8 from mesa.space import NetworkGrid
9 
10 
11 class State(Enum):
12     SUSCEPTIBLE = 0
13     INFECTED = 1
14     RESISTANT = 2
15 
16 
17 def number_state(model, state):
18     return sum(1 for a in model.grid.get_all_cell_contents() if a.state is state)
19 
20 
21 def number_infected(model):
22     return number_state(model, State.INFECTED)
23 
24 
25 def number_susceptible(model):
26     return number_state(model, State.SUSCEPTIBLE)
27 
28 
29 def number_resistant(model):
30     return number_state(model, State.RESISTANT)
31 
32 
33 class VirusOnNetwork(Model):
34     """A virus model with some number of agents"""
35 
36     def __init__(
37         self,
38         num_nodes=10,
39         avg_node_degree=3,
40         initial_outbreak_size=1,
41         virus_spread_chance=0.4,
42         virus_check_frequency=0.4,
43         recovery_chance=0.3,
44         gain_resistance_chance=0.5,
45     ):
46 
47         self.num_nodes = num_nodes
48         prob = avg_node_degree / self.num_nodes
49         self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)
50         self.grid = NetworkGrid(self.G)
51         self.schedule = RandomActivation(self)
52         self.initial_outbreak_size = (
53             initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes
54         )
55         self.virus_spread_chance = virus_spread_chance
56         self.virus_check_frequency = virus_check_frequency
57         self.recovery_chance = recovery_chance
58         self.gain_resistance_chance = gain_resistance_chance
59 
60         self.datacollector = DataCollector(
61             {
62                 "Infected": number_infected,
63                 "Susceptible": number_susceptible,
64                 "Resistant": number_resistant,
65             }
66         )
67 
68         # Create agents
69         for i, node in enumerate(self.G.nodes()):
70             a = VirusAgent(
71                 i,
72                 self,
73                 State.SUSCEPTIBLE,
74                 self.virus_spread_chance,
75                 self.virus_check_frequency,
76                 self.recovery_chance,
77                 self.gain_resistance_chance,
78             )
79             self.schedule.add(a)
80             # Add the agent to the node
81             self.grid.place_agent(a, node)
82 
83         # Infect some nodes
84         infected_nodes = self.random.sample(self.G.nodes(), self.initial_outbreak_size)
85         for a in self.grid.get_cell_list_contents(infected_nodes):
86             a.state = State.INFECTED
87 
88         self.running = True
89         self.datacollector.collect(self)
90 
91     def resistant_susceptible_ratio(self):
92         try:
93             return number_state(self, State.RESISTANT) / number_state(
94                 self, State.SUSCEPTIBLE
95             )
96         except ZeroDivisionError:
97             return math.inf
98 
99     def step(self):
100         self.schedule.step()
101         # collect data
102         self.datacollector.collect(self)
103 
104     def run_model(self, n):
105         for i in range(n):
106             self.step()
107 
108 
109 class VirusAgent(Agent):
110     def __init__(
111         self,
112         unique_id,
113         model,
114         initial_state,
115         virus_spread_chance,
116         virus_check_frequency,
117         recovery_chance,
118         gain_resistance_chance,
119     ):
120         super().__init__(unique_id, model)
121 
122         self.state = initial_state
123 
124         self.virus_spread_chance = virus_spread_chance
125         self.virus_check_frequency = virus_check_frequency
126         self.recovery_chance = recovery_chance
127         self.gain_resistance_chance = gain_resistance_chance
128 
129     def try_to_infect_neighbors(self):
130         neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
131         susceptible_neighbors = [
132             agent
133             for agent in self.model.grid.get_cell_list_contents(neighbors_nodes)
134             if agent.state is State.SUSCEPTIBLE
135         ]
136         for a in susceptible_neighbors:
137             if self.random.random() < self.virus_spread_chance:
138                 a.state = State.INFECTED
139 
140     def try_gain_resistance(self):
141         if self.random.random() < self.gain_resistance_chance:
142             self.state = State.RESISTANT
143 
144     def try_remove_infection(self):
145         # Try to remove
146         if self.random.random() < self.recovery_chance:
147             # Success
148             self.state = State.SUSCEPTIBLE
149             self.try_gain_resistance()
150         else:
151             # Failed
152             self.state = State.INFECTED
153 
154     def try_check_situation(self):
155         if self.random.random() < self.virus_check_frequency:
156             # Checking...
157             if self.state is State.INFECTED:
158                 self.try_remove_infection()
159 
160     def step(self):
161         if self.state is State.INFECTED:
162             self.try_to_infect_neighbors()
163         self.try_check_situation()
164
```

Path: `examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py`
Content:
```
1 import mesa
2 import networkx as nx
3 
4 
5 def compute_gini(model):
6     agent_wealths = [agent.wealth for agent in model.schedule.agents]
7     x = sorted(agent_wealths)
8     N = model.num_agents
9     B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
10     return 1 + (1 / N) - 2 * B
11 
12 
13 class BoltzmannWealthModelNetwork(mesa.Model):
14     """A model with some number of agents."""
15 
16     def __init__(self, num_agents=7, num_nodes=10):
17 
18         self.num_agents = num_agents
19         self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents
20         self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5)
21         self.grid = mesa.space.NetworkGrid(self.G)
22         self.schedule = mesa.time.RandomActivation(self)
23         self.datacollector = mesa.DataCollector(
24             model_reporters={"Gini": compute_gini},
25             agent_reporters={"Wealth": lambda _: _.wealth},
26         )
27 
28         list_of_random_nodes = self.random.sample(self.G.nodes(), self.num_agents)
29 
30         # Create agents
31         for i in range(self.num_agents):
32             a = MoneyAgent(i, self)
33             self.schedule.add(a)
34             # Add the agent to a random node
35             self.grid.place_agent(a, list_of_random_nodes[i])
36 
37         self.running = True
38         self.datacollector.collect(self)
39 
40     def step(self):
41         self.schedule.step()
42         # collect data
43         self.datacollector.collect(self)
44 
45     def run_model(self, n):
46         for i in range(n):
47             self.step()
48 
49 
50 class MoneyAgent(mesa.Agent):
51     """An agent with fixed initial wealth."""
52 
53     def __init__(self, unique_id, model):
54         super().__init__(unique_id, model)
55         self.wealth = 1
56 
57     def move(self):
58         possible_steps = [
59             node
60             for node in self.model.grid.get_neighbors(self.pos, include_center=False)
61             if self.model.grid.is_cell_empty(node)
62         ]
63         if len(possible_steps) > 0:
64             new_position = self.random.choice(possible_steps)
65             self.model.grid.move_agent(self, new_position)
66 
67     def give_money(self):
68 
69         neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
70         neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)
71         if len(neighbors) > 0:
72             other = self.random.choice(neighbors)
73             other.wealth += 1
74             self.wealth -= 1
75 
76     def step(self):
77         self.move()
78         if self.wealth > 0:
79             self.give_money()
80
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py
--- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py
+++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py
@@ -25,7 +25,7 @@
             agent_reporters={"Wealth": lambda _: _.wealth},
         )
 
-        list_of_random_nodes = self.random.sample(self.G.nodes(), self.num_agents)
+        list_of_random_nodes = self.random.sample(list(self.G), self.num_agents)
 
         # Create agents
         for i in range(self.num_agents):
diff --git a/examples/virus_on_network/virus_on_network/model.py b/examples/virus_on_network/virus_on_network/model.py
--- a/examples/virus_on_network/virus_on_network/model.py
+++ b/examples/virus_on_network/virus_on_network/model.py
@@ -81,7 +81,7 @@
             self.grid.place_agent(a, node)
 
         # Infect some nodes
-        infected_nodes = self.random.sample(self.G.nodes(), self.initial_outbreak_size)
+        infected_nodes = self.random.sample(list(self.G), self.initial_outbreak_size)
         for a in self.grid.get_cell_list_contents(infected_nodes):
             a.state = State.INFECTED

{"golden_diff": "diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py\n--- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py\n+++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py\n@@ -25,7 +25,7 @@\n             agent_reporters={\"Wealth\": lambda _: _.wealth},\n         )\n \n-        list_of_random_nodes = self.random.sample(self.G.nodes(), self.num_agents)\n+        list_of_random_nodes = self.random.sample(list(self.G), self.num_agents)\n \n         # Create agents\n         for i in range(self.num_agents):\ndiff --git a/examples/virus_on_network/virus_on_network/model.py b/examples/virus_on_network/virus_on_network/model.py\n--- a/examples/virus_on_network/virus_on_network/model.py\n+++ b/examples/virus_on_network/virus_on_network/model.py\n@@ -81,7 +81,7 @@\n             self.grid.place_agent(a, node)\n \n         # Infect some nodes\n-        infected_nodes = self.random.sample(self.G.nodes(), self.initial_outbreak_size)\n+        infected_nodes = self.random.sample(list(self.G), self.initial_outbreak_size)\n         for a in self.grid.get_cell_list_contents(infected_nodes):\n             a.state = State.INFECTED\n", "issue": "random.sample with a set is removed in Python 3.11\nUsing [`random.sample`](https://docs.python.org/3/library/random.html#random.sample) with a set has been depreciated in Python 3.9 and was removed in Python 3.11.\r\n\r\n> Changed in version 3.11: The population must be a sequence. Automatic conversion of sets to lists is no longer supported.\r\n\r\nTwo cases of this function being used with a set have been detected by the CI in Mesa, both in example models:\r\n\r\n1. https://github.com/projectmesa/mesa/blob/5135be8759d9fdac551c5c6ed13e9ae5205bce3e/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py#L28\r\n2. https://github.com/projectmesa/mesa/blob/5135be8759d9fdac551c5c6ed13e9ae5205bce3e/examples/virus_on_network/virus_on_network/model.py#L84\r\n\r\nWhat would be the most efficient way to get `self.G.nodes()` in a sequence form (so probably list or tuple)? I feel like just putting it inside a `list()` isn't the proper way to do it.\n", "before_files": [{"content": "import math\nfrom enum import Enum\nimport networkx as nx\n\nfrom mesa import Agent, Model\nfrom mesa.time import RandomActivation\nfrom mesa.datacollection import DataCollector\nfrom mesa.space import NetworkGrid\n\n\nclass State(Enum):\n    SUSCEPTIBLE = 0\n    INFECTED = 1\n    RESISTANT = 2\n\n\ndef number_state(model, state):\n    return sum(1 for a in model.grid.get_all_cell_contents() if a.state is state)\n\n\ndef number_infected(model):\n    return number_state(model, State.INFECTED)\n\n\ndef number_susceptible(model):\n    return number_state(model, State.SUSCEPTIBLE)\n\n\ndef number_resistant(model):\n    return number_state(model, State.RESISTANT)\n\n\nclass VirusOnNetwork(Model):\n    \"\"\"A virus model with some number of agents\"\"\"\n\n    def __init__(\n        self,\n        num_nodes=10,\n        avg_node_degree=3,\n        initial_outbreak_size=1,\n        virus_spread_chance=0.4,\n        virus_check_frequency=0.4,\n        recovery_chance=0.3,\n        gain_resistance_chance=0.5,\n    ):\n\n        self.num_nodes = num_nodes\n        prob = avg_node_degree / self.num_nodes\n        self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)\n        self.grid = NetworkGrid(self.G)\n        self.schedule = RandomActivation(self)\n        self.initial_outbreak_size = (\n            initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes\n        )\n        self.virus_spread_chance = virus_spread_chance\n        self.virus_check_frequency = virus_check_frequency\n        self.recovery_chance = recovery_chance\n        self.gain_resistance_chance = gain_resistance_chance\n\n        self.datacollector = DataCollector(\n            {\n                \"Infected\": number_infected,\n                \"Susceptible\": number_susceptible,\n                \"Resistant\": number_resistant,\n            }\n        )\n\n        # Create agents\n        for i, node in enumerate(self.G.nodes()):\n            a = VirusAgent(\n                i,\n                self,\n                State.SUSCEPTIBLE,\n                self.virus_spread_chance,\n                self.virus_check_frequency,\n                self.recovery_chance,\n                self.gain_resistance_chance,\n            )\n            self.schedule.add(a)\n            # Add the agent to the node\n            self.grid.place_agent(a, node)\n\n        # Infect some nodes\n        infected_nodes = self.random.sample(self.G.nodes(), self.initial_outbreak_size)\n        for a in self.grid.get_cell_list_contents(infected_nodes):\n            a.state = State.INFECTED\n\n        self.running = True\n        self.datacollector.collect(self)\n\n    def resistant_susceptible_ratio(self):\n        try:\n            return number_state(self, State.RESISTANT) / number_state(\n                self, State.SUSCEPTIBLE\n            )\n        except ZeroDivisionError:\n            return math.inf\n\n    def step(self):\n        self.schedule.step()\n        # collect data\n        self.datacollector.collect(self)\n\n    def run_model(self, n):\n        for i in range(n):\n            self.step()\n\n\nclass VirusAgent(Agent):\n    def __init__(\n        self,\n        unique_id,\n        model,\n        initial_state,\n        virus_spread_chance,\n        virus_check_frequency,\n        recovery_chance,\n        gain_resistance_chance,\n    ):\n        super().__init__(unique_id, model)\n\n        self.state = initial_state\n\n        self.virus_spread_chance = virus_spread_chance\n        self.virus_check_frequency = virus_check_frequency\n        self.recovery_chance = recovery_chance\n        self.gain_resistance_chance = gain_resistance_chance\n\n    def try_to_infect_neighbors(self):\n        neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)\n        susceptible_neighbors = [\n            agent\n            for agent in self.model.grid.get_cell_list_contents(neighbors_nodes)\n            if agent.state is State.SUSCEPTIBLE\n        ]\n        for a in susceptible_neighbors:\n            if self.random.random() < self.virus_spread_chance:\n                a.state = State.INFECTED\n\n    def try_gain_resistance(self):\n        if self.random.random() < self.gain_resistance_chance:\n            self.state = State.RESISTANT\n\n    def try_remove_infection(self):\n        # Try to remove\n        if self.random.random() < self.recovery_chance:\n            # Success\n            self.state = State.SUSCEPTIBLE\n            self.try_gain_resistance()\n        else:\n            # Failed\n            self.state = State.INFECTED\n\n    def try_check_situation(self):\n        if self.random.random() < self.virus_check_frequency:\n            # Checking...\n            if self.state is State.INFECTED:\n                self.try_remove_infection()\n\n    def step(self):\n        if self.state is State.INFECTED:\n            self.try_to_infect_neighbors()\n        self.try_check_situation()\n", "path": "examples/virus_on_network/virus_on_network/model.py"}, {"content": "import mesa\nimport networkx as nx\n\n\ndef compute_gini(model):\n    agent_wealths = [agent.wealth for agent in model.schedule.agents]\n    x = sorted(agent_wealths)\n    N = model.num_agents\n    B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))\n    return 1 + (1 / N) - 2 * B\n\n\nclass BoltzmannWealthModelNetwork(mesa.Model):\n    \"\"\"A model with some number of agents.\"\"\"\n\n    def __init__(self, num_agents=7, num_nodes=10):\n\n        self.num_agents = num_agents\n        self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents\n        self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5)\n        self.grid = mesa.space.NetworkGrid(self.G)\n        self.schedule = mesa.time.RandomActivation(self)\n        self.datacollector = mesa.DataCollector(\n            model_reporters={\"Gini\": compute_gini},\n            agent_reporters={\"Wealth\": lambda _: _.wealth},\n        )\n\n        list_of_random_nodes = self.random.sample(self.G.nodes(), self.num_agents)\n\n        # Create agents\n        for i in range(self.num_agents):\n            a = MoneyAgent(i, self)\n            self.schedule.add(a)\n            # Add the agent to a random node\n            self.grid.place_agent(a, list_of_random_nodes[i])\n\n        self.running = True\n        self.datacollector.collect(self)\n\n    def step(self):\n        self.schedule.step()\n        # collect data\n        self.datacollector.collect(self)\n\n    def run_model(self, n):\n        for i in range(n):\n            self.step()\n\n\nclass MoneyAgent(mesa.Agent):\n    \"\"\"An agent with fixed initial wealth.\"\"\"\n\n    def __init__(self, unique_id, model):\n        super().__init__(unique_id, model)\n        self.wealth = 1\n\n    def move(self):\n        possible_steps = [\n            node\n            for node in self.model.grid.get_neighbors(self.pos, include_center=False)\n            if self.model.grid.is_cell_empty(node)\n        ]\n        if len(possible_steps) > 0:\n            new_position = self.random.choice(possible_steps)\n            self.model.grid.move_agent(self, new_position)\n\n    def give_money(self):\n\n        neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)\n        neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)\n        if len(neighbors) > 0:\n            other = self.random.choice(neighbors)\n            other.wealth += 1\n            self.wealth -= 1\n\n    def step(self):\n        self.move()\n        if self.wealth > 0:\n            self.give_money()\n", "path": "examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py"}], "after_files": [{"content": "import math\nfrom enum import Enum\nimport networkx as nx\n\nfrom mesa import Agent, Model\nfrom mesa.time import RandomActivation\nfrom mesa.datacollection import DataCollector\nfrom mesa.space import NetworkGrid\n\n\nclass State(Enum):\n    SUSCEPTIBLE = 0\n    INFECTED = 1\n    RESISTANT = 2\n\n\ndef number_state(model, state):\n    return sum(1 for a in model.grid.get_all_cell_contents() if a.state is state)\n\n\ndef number_infected(model):\n    return number_state(model, State.INFECTED)\n\n\ndef number_susceptible(model):\n    return number_state(model, State.SUSCEPTIBLE)\n\n\ndef number_resistant(model):\n    return number_state(model, State.RESISTANT)\n\n\nclass VirusOnNetwork(Model):\n    \"\"\"A virus model with some number of agents\"\"\"\n\n    def __init__(\n        self,\n        num_nodes=10,\n        avg_node_degree=3,\n        initial_outbreak_size=1,\n        virus_spread_chance=0.4,\n        virus_check_frequency=0.4,\n        recovery_chance=0.3,\n        gain_resistance_chance=0.5,\n    ):\n\n        self.num_nodes = num_nodes\n        prob = avg_node_degree / self.num_nodes\n        self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)\n        self.grid = NetworkGrid(self.G)\n        self.schedule = RandomActivation(self)\n        self.initial_outbreak_size = (\n            initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes\n        )\n        self.virus_spread_chance = virus_spread_chance\n        self.virus_check_frequency = virus_check_frequency\n        self.recovery_chance = recovery_chance\n        self.gain_resistance_chance = gain_resistance_chance\n\n        self.datacollector = DataCollector(\n            {\n                \"Infected\": number_infected,\n                \"Susceptible\": number_susceptible,\n                \"Resistant\": number_resistant,\n            }\n        )\n\n        # Create agents\n        for i, node in enumerate(self.G.nodes()):\n            a = VirusAgent(\n                i,\n                self,\n                State.SUSCEPTIBLE,\n                self.virus_spread_chance,\n                self.virus_check_frequency,\n                self.recovery_chance,\n                self.gain_resistance_chance,\n            )\n            self.schedule.add(a)\n            # Add the agent to the node\n            self.grid.place_agent(a, node)\n\n        # Infect some nodes\n        infected_nodes = self.random.sample(list(self.G), self.initial_outbreak_size)\n        for a in self.grid.get_cell_list_contents(infected_nodes):\n            a.state = State.INFECTED\n\n        self.running = True\n        self.datacollector.collect(self)\n\n    def resistant_susceptible_ratio(self):\n        try:\n            return number_state(self, State.RESISTANT) / number_state(\n                self, State.SUSCEPTIBLE\n            )\n        except ZeroDivisionError:\n            return math.inf\n\n    def step(self):\n        self.schedule.step()\n        # collect data\n        self.datacollector.collect(self)\n\n    def run_model(self, n):\n        for i in range(n):\n            self.step()\n\n\nclass VirusAgent(Agent):\n    def __init__(\n        self,\n        unique_id,\n        model,\n        initial_state,\n        virus_spread_chance,\n        virus_check_frequency,\n        recovery_chance,\n        gain_resistance_chance,\n    ):\n        super().__init__(unique_id, model)\n\n        self.state = initial_state\n\n        self.virus_spread_chance = virus_spread_chance\n        self.virus_check_frequency = virus_check_frequency\n        self.recovery_chance = recovery_chance\n        self.gain_resistance_chance = gain_resistance_chance\n\n    def try_to_infect_neighbors(self):\n        neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)\n        susceptible_neighbors = [\n            agent\n            for agent in self.model.grid.get_cell_list_contents(neighbors_nodes)\n            if agent.state is State.SUSCEPTIBLE\n        ]\n        for a in susceptible_neighbors:\n            if self.random.random() < self.virus_spread_chance:\n                a.state = State.INFECTED\n\n    def try_gain_resistance(self):\n        if self.random.random() < self.gain_resistance_chance:\n            self.state = State.RESISTANT\n\n    def try_remove_infection(self):\n        # Try to remove\n        if self.random.random() < self.recovery_chance:\n            # Success\n            self.state = State.SUSCEPTIBLE\n            self.try_gain_resistance()\n        else:\n            # Failed\n            self.state = State.INFECTED\n\n    def try_check_situation(self):\n        if self.random.random() < self.virus_check_frequency:\n            # Checking...\n            if self.state is State.INFECTED:\n                self.try_remove_infection()\n\n    def step(self):\n        if self.state is State.INFECTED:\n            self.try_to_infect_neighbors()\n        self.try_check_situation()\n", "path": "examples/virus_on_network/virus_on_network/model.py"}, {"content": "import mesa\nimport networkx as nx\n\n\ndef compute_gini(model):\n    agent_wealths = [agent.wealth for agent in model.schedule.agents]\n    x = sorted(agent_wealths)\n    N = model.num_agents\n    B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))\n    return 1 + (1 / N) - 2 * B\n\n\nclass BoltzmannWealthModelNetwork(mesa.Model):\n    \"\"\"A model with some number of agents.\"\"\"\n\n    def __init__(self, num_agents=7, num_nodes=10):\n\n        self.num_agents = num_agents\n        self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents\n        self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5)\n        self.grid = mesa.space.NetworkGrid(self.G)\n        self.schedule = mesa.time.RandomActivation(self)\n        self.datacollector = mesa.DataCollector(\n            model_reporters={\"Gini\": compute_gini},\n            agent_reporters={\"Wealth\": lambda _: _.wealth},\n        )\n\n        list_of_random_nodes = self.random.sample(list(self.G), self.num_agents)\n\n        # Create agents\n        for i in range(self.num_agents):\n            a = MoneyAgent(i, self)\n            self.schedule.add(a)\n            # Add the agent to a random node\n            self.grid.place_agent(a, list_of_random_nodes[i])\n\n        self.running = True\n        self.datacollector.collect(self)\n\n    def step(self):\n        self.schedule.step()\n        # collect data\n        self.datacollector.collect(self)\n\n    def run_model(self, n):\n        for i in range(n):\n            self.step()\n\n\nclass MoneyAgent(mesa.Agent):\n    \"\"\"An agent with fixed initial wealth.\"\"\"\n\n    def __init__(self, unique_id, model):\n        super().__init__(unique_id, model)\n        self.wealth = 1\n\n    def move(self):\n        possible_steps = [\n            node\n            for node in self.model.grid.get_neighbors(self.pos, include_center=False)\n            if self.model.grid.is_cell_empty(node)\n        ]\n        if len(possible_steps) > 0:\n            new_position = self.random.choice(possible_steps)\n            self.model.grid.move_agent(self, new_position)\n\n    def give_money(self):\n\n        neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)\n        neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)\n        if len(neighbors) > 0:\n            other = self.random.choice(neighbors)\n            other.wealth += 1\n            self.wealth -= 1\n\n    def step(self):\n        self.move()\n        if self.wealth > 0:\n            self.give_money()\n", "path": "examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py"}]}