Mesa 3.0.0__py3-none-any.whl → 3.0.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of Mesa might be problematic. Click here for more details.

Files changed (104) hide show
  1. mesa/__init__.py +3 -3
  2. mesa/agent.py +114 -406
  3. mesa/batchrunner.py +27 -54
  4. mesa/cookiecutter-mesa/cookiecutter.json +8 -0
  5. mesa/cookiecutter-mesa/hooks/post_gen_project.py +11 -0
  6. mesa/cookiecutter-mesa/{{cookiecutter.snake}}/README.md +4 -0
  7. mesa/cookiecutter-mesa/{{cookiecutter.snake}}/app.pytemplate +27 -0
  8. mesa/cookiecutter-mesa/{{cookiecutter.snake}}/setup.pytemplate +11 -0
  9. mesa/cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}/model.pytemplate +60 -0
  10. mesa/datacollection.py +29 -140
  11. mesa/experimental/__init__.py +1 -11
  12. mesa/experimental/cell_space/__init__.py +1 -16
  13. mesa/experimental/cell_space/cell.py +23 -93
  14. mesa/experimental/cell_space/cell_agent.py +21 -117
  15. mesa/experimental/cell_space/cell_collection.py +17 -54
  16. mesa/experimental/cell_space/discrete_space.py +8 -92
  17. mesa/experimental/cell_space/grid.py +8 -32
  18. mesa/experimental/cell_space/network.py +7 -12
  19. mesa/experimental/devs/__init__.py +0 -2
  20. mesa/experimental/devs/eventlist.py +14 -52
  21. mesa/experimental/devs/examples/epstein_civil_violence.py +39 -71
  22. mesa/experimental/devs/examples/wolf_sheep.py +45 -45
  23. mesa/experimental/devs/simulator.py +15 -55
  24. mesa/main.py +63 -0
  25. mesa/model.py +83 -211
  26. mesa/space.py +149 -215
  27. mesa/time.py +77 -62
  28. mesa/{experimental → visualization}/UserParam.py +6 -17
  29. mesa/visualization/__init__.py +2 -25
  30. mesa/{experimental → visualization}/components/altair.py +0 -10
  31. mesa/visualization/components/matplotlib.py +134 -0
  32. mesa/visualization/solara_viz.py +266 -267
  33. {mesa-3.0.0.dist-info → mesa-3.0.0a1.dist-info}/METADATA +13 -65
  34. mesa-3.0.0a1.dist-info/RECORD +38 -0
  35. mesa-3.0.0.dist-info/licenses/NOTICE → mesa-3.0.0a1.dist-info/licenses/LICENSE +2 -2
  36. mesa/examples/README.md +0 -37
  37. mesa/examples/__init__.py +0 -21
  38. mesa/examples/advanced/epstein_civil_violence/Epstein Civil Violence.ipynb +0 -116
  39. mesa/examples/advanced/epstein_civil_violence/Readme.md +0 -34
  40. mesa/examples/advanced/epstein_civil_violence/__init__.py +0 -0
  41. mesa/examples/advanced/epstein_civil_violence/agents.py +0 -164
  42. mesa/examples/advanced/epstein_civil_violence/app.py +0 -73
  43. mesa/examples/advanced/epstein_civil_violence/model.py +0 -114
  44. mesa/examples/advanced/pd_grid/Readme.md +0 -43
  45. mesa/examples/advanced/pd_grid/__init__.py +0 -0
  46. mesa/examples/advanced/pd_grid/agents.py +0 -50
  47. mesa/examples/advanced/pd_grid/analysis.ipynb +0 -228
  48. mesa/examples/advanced/pd_grid/app.py +0 -54
  49. mesa/examples/advanced/pd_grid/model.py +0 -71
  50. mesa/examples/advanced/sugarscape_g1mt/Readme.md +0 -64
  51. mesa/examples/advanced/sugarscape_g1mt/__init__.py +0 -0
  52. mesa/examples/advanced/sugarscape_g1mt/agents.py +0 -344
  53. mesa/examples/advanced/sugarscape_g1mt/app.py +0 -62
  54. mesa/examples/advanced/sugarscape_g1mt/model.py +0 -180
  55. mesa/examples/advanced/sugarscape_g1mt/sugar-map.txt +0 -50
  56. mesa/examples/advanced/sugarscape_g1mt/tests.py +0 -69
  57. mesa/examples/advanced/wolf_sheep/Readme.md +0 -57
  58. mesa/examples/advanced/wolf_sheep/__init__.py +0 -0
  59. mesa/examples/advanced/wolf_sheep/agents.py +0 -102
  60. mesa/examples/advanced/wolf_sheep/app.py +0 -84
  61. mesa/examples/advanced/wolf_sheep/model.py +0 -137
  62. mesa/examples/basic/__init__.py +0 -0
  63. mesa/examples/basic/boid_flockers/Readme.md +0 -22
  64. mesa/examples/basic/boid_flockers/__init__.py +0 -0
  65. mesa/examples/basic/boid_flockers/agents.py +0 -71
  66. mesa/examples/basic/boid_flockers/app.py +0 -58
  67. mesa/examples/basic/boid_flockers/model.py +0 -69
  68. mesa/examples/basic/boltzmann_wealth_model/Readme.md +0 -56
  69. mesa/examples/basic/boltzmann_wealth_model/__init__.py +0 -0
  70. mesa/examples/basic/boltzmann_wealth_model/agents.py +0 -31
  71. mesa/examples/basic/boltzmann_wealth_model/app.py +0 -74
  72. mesa/examples/basic/boltzmann_wealth_model/model.py +0 -43
  73. mesa/examples/basic/boltzmann_wealth_model/st_app.py +0 -115
  74. mesa/examples/basic/conways_game_of_life/Readme.md +0 -39
  75. mesa/examples/basic/conways_game_of_life/__init__.py +0 -0
  76. mesa/examples/basic/conways_game_of_life/agents.py +0 -47
  77. mesa/examples/basic/conways_game_of_life/app.py +0 -51
  78. mesa/examples/basic/conways_game_of_life/model.py +0 -31
  79. mesa/examples/basic/conways_game_of_life/st_app.py +0 -72
  80. mesa/examples/basic/schelling/Readme.md +0 -40
  81. mesa/examples/basic/schelling/__init__.py +0 -0
  82. mesa/examples/basic/schelling/agents.py +0 -26
  83. mesa/examples/basic/schelling/analysis.ipynb +0 -205
  84. mesa/examples/basic/schelling/app.py +0 -42
  85. mesa/examples/basic/schelling/model.py +0 -59
  86. mesa/examples/basic/virus_on_network/Readme.md +0 -61
  87. mesa/examples/basic/virus_on_network/__init__.py +0 -0
  88. mesa/examples/basic/virus_on_network/agents.py +0 -69
  89. mesa/examples/basic/virus_on_network/app.py +0 -114
  90. mesa/examples/basic/virus_on_network/model.py +0 -96
  91. mesa/experimental/cell_space/voronoi.py +0 -257
  92. mesa/experimental/components/matplotlib.py +0 -242
  93. mesa/experimental/solara_viz.py +0 -453
  94. mesa/visualization/components/__init__.py +0 -83
  95. mesa/visualization/components/altair_components.py +0 -188
  96. mesa/visualization/components/matplotlib_components.py +0 -175
  97. mesa/visualization/mpl_space_drawing.py +0 -593
  98. mesa/visualization/user_param.py +0 -69
  99. mesa/visualization/utils.py +0 -9
  100. mesa-3.0.0.dist-info/RECORD +0 -95
  101. mesa-3.0.0.dist-info/licenses/LICENSE +0 -202
  102. /mesa/{examples/advanced → cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}}/__init__.py +0 -0
  103. {mesa-3.0.0.dist-info → mesa-3.0.0a1.dist-info}/WHEEL +0 -0
  104. {mesa-3.0.0.dist-info → mesa-3.0.0a1.dist-info}/entry_points.txt +0 -0
@@ -1,73 +0,0 @@
1
- from mesa.examples.advanced.epstein_civil_violence.agents import (
2
- Citizen,
3
- CitizenState,
4
- Cop,
5
- )
6
- from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence
7
- from mesa.visualization import (
8
- Slider,
9
- SolaraViz,
10
- make_plot_component,
11
- make_space_component,
12
- )
13
-
14
- COP_COLOR = "#000000"
15
-
16
- agent_colors = {
17
- CitizenState.ACTIVE: "#FE6100",
18
- CitizenState.QUIET: "#648FFF",
19
- CitizenState.ARRESTED: "#808080",
20
- }
21
-
22
-
23
- def citizen_cop_portrayal(agent):
24
- if agent is None:
25
- return
26
-
27
- portrayal = {
28
- "size": 50,
29
- }
30
-
31
- if isinstance(agent, Citizen):
32
- portrayal["color"] = agent_colors[agent.state]
33
- elif isinstance(agent, Cop):
34
- portrayal["color"] = COP_COLOR
35
-
36
- return portrayal
37
-
38
-
39
- def post_process(ax):
40
- ax.set_aspect("equal")
41
- ax.set_xticks([])
42
- ax.set_yticks([])
43
- ax.get_figure().set_size_inches(10, 10)
44
-
45
-
46
- model_params = {
47
- "height": 40,
48
- "width": 40,
49
- "citizen_density": Slider("Initial Agent Density", 0.7, 0.0, 0.9, 0.1),
50
- "cop_density": Slider("Initial Cop Density", 0.04, 0.0, 0.1, 0.01),
51
- "citizen_vision": Slider("Citizen Vision", 7, 1, 10, 1),
52
- "cop_vision": Slider("Cop Vision", 7, 1, 10, 1),
53
- "legitimacy": Slider("Government Legitimacy", 0.82, 0.0, 1, 0.01),
54
- "max_jail_term": Slider("Max Jail Term", 30, 0, 50, 1),
55
- }
56
-
57
- space_component = make_space_component(
58
- citizen_cop_portrayal, post_process=post_process, draw_grid=False
59
- )
60
-
61
- chart_component = make_plot_component(
62
- {state.name.lower(): agent_colors[state] for state in CitizenState}
63
- )
64
-
65
- epstein_model = EpsteinCivilViolence()
66
-
67
- page = SolaraViz(
68
- epstein_model,
69
- components=[space_component, chart_component],
70
- model_params=model_params,
71
- name="Epstein Civil Violence",
72
- )
73
- page # noqa
@@ -1,114 +0,0 @@
1
- import mesa
2
- from mesa.examples.advanced.epstein_civil_violence.agents import (
3
- Citizen,
4
- CitizenState,
5
- Cop,
6
- )
7
-
8
-
9
- class EpsteinCivilViolence(mesa.Model):
10
- """
11
- Model 1 from "Modeling civil violence: An agent-based computational
12
- approach," by Joshua Epstein.
13
- http://www.pnas.org/content/99/suppl_3/7243.full
14
-
15
- Args:
16
- height: grid height
17
- width: grid width
18
- citizen_density: approximate % of cells occupied by citizens.
19
- cop_density: approximate % of cells occupied by cops.
20
- citizen_vision: number of cells in each direction (N, S, E and W) that
21
- citizen can inspect
22
- cop_vision: number of cells in each direction (N, S, E and W) that cop
23
- can inspect
24
- legitimacy: (L) citizens' perception of regime legitimacy, equal
25
- across all citizens
26
- max_jail_term: (J_max)
27
- active_threshold: if (grievance - (risk_aversion * arrest_probability))
28
- > threshold, citizen rebels
29
- arrest_prob_constant: set to ensure agents make plausible arrest
30
- probability estimates
31
- movement: binary, whether agents try to move at step end
32
- max_iters: model may not have a natural stopping point, so we set a
33
- max.
34
- """
35
-
36
- def __init__(
37
- self,
38
- width=40,
39
- height=40,
40
- citizen_density=0.7,
41
- cop_density=0.074,
42
- citizen_vision=7,
43
- cop_vision=7,
44
- legitimacy=0.8,
45
- max_jail_term=1000,
46
- active_threshold=0.1,
47
- arrest_prob_constant=2.3,
48
- movement=True,
49
- max_iters=1000,
50
- seed=None,
51
- ):
52
- super().__init__(seed=seed)
53
- self.movement = movement
54
- self.max_iters = max_iters
55
-
56
- self.grid = mesa.experimental.cell_space.OrthogonalVonNeumannGrid(
57
- (width, height), capacity=1, torus=True, random=self.random
58
- )
59
-
60
- model_reporters = {
61
- "active": CitizenState.ACTIVE.name,
62
- "quiet": CitizenState.QUIET.name,
63
- "arrested": CitizenState.ARRESTED.name,
64
- }
65
- agent_reporters = {
66
- "jail_sentence": lambda a: getattr(a, "jail_sentence", None),
67
- "arrest_probability": lambda a: getattr(a, "arrest_probability", None),
68
- }
69
- self.datacollector = mesa.DataCollector(
70
- model_reporters=model_reporters, agent_reporters=agent_reporters
71
- )
72
- if cop_density + citizen_density > 1:
73
- raise ValueError("Cop density + citizen density must be less than 1")
74
-
75
- for cell in self.grid.all_cells:
76
- klass = self.random.choices(
77
- [Citizen, Cop, None],
78
- cum_weights=[citizen_density, citizen_density + cop_density, 1],
79
- )[0]
80
-
81
- if klass == Cop:
82
- cop = Cop(self, vision=cop_vision, max_jail_term=max_jail_term)
83
- cop.move_to(cell)
84
- elif klass == Citizen:
85
- citizen = Citizen(
86
- self,
87
- regime_legitimacy=legitimacy,
88
- threshold=active_threshold,
89
- vision=citizen_vision,
90
- arrest_prob_constant=arrest_prob_constant,
91
- )
92
- citizen.move_to(cell)
93
-
94
- self.running = True
95
- self._update_counts()
96
- self.datacollector.collect(self)
97
-
98
- def step(self):
99
- """
100
- Advance the model by one step and collect data.
101
- """
102
- self.agents.shuffle_do("step")
103
- self._update_counts()
104
- self.datacollector.collect(self)
105
-
106
- if self.steps > self.max_iters:
107
- self.running = False
108
-
109
- def _update_counts(self):
110
- """Helper function for counting nr. of citizens in given state."""
111
- counts = self.agents_by_type[Citizen].groupby("state").count()
112
-
113
- for state in CitizenState:
114
- setattr(self, state.name, counts.get(state, 0))
@@ -1,43 +0,0 @@
1
- # Demographic Prisoner's Dilemma on a Grid
2
-
3
- ## Summary
4
-
5
- The Demographic Prisoner's Dilemma is a family of variants on the classic two-player [Prisoner's Dilemma]. The model consists of agents, each with a strategy of either Cooperate or Defect. Each agent's payoff is based on its strategy and the strategies of its spatial neighbors. After each step of the model, the agents adopt the strategy of their neighbor with the highest total score.
6
-
7
- The model payoff table is:
8
-
9
- | | Cooperate | Defect|
10
- |:-------------:|:---------:|:-----:|
11
- | **Cooperate** | 1, 1 | 0, D |
12
- | **Defect** | D, 0 | 0, 0 |
13
-
14
- Where *D* is the defection bonus, generally set higher than 1. In these runs, the defection bonus is set to $D=1.6$.
15
-
16
- The Demographic Prisoner's Dilemma demonstrates how simple rules can lead to the emergence of widespread cooperation, despite the Defection strategy dominating each individual interaction game. However, it is also interesting for another reason: it is known to be sensitive to the activation regime employed in it.
17
-
18
- ## How to Run
19
-
20
- ##### Web based model simulation
21
-
22
- To run the model interactively, run ``solara run app.py`` in this directory.
23
-
24
- ##### Jupyter Notebook
25
-
26
- Launch the ``Demographic Prisoner's Dilemma Activation Schedule.ipynb`` notebook and run the code.
27
-
28
- ## Files
29
-
30
- * ``agents.py``: contains the agent class.
31
- * ``model.py``: contains the model class; the model takes a ``activation_order`` string as an argument, which determines in which order agents are activated: Sequential, Random or Simultaneous.
32
- * ``app.py``: contains the interactive visualization server.
33
- * ``Demographic Prisoner's Dilemma Activation Schedule.ipynb``: Jupyter Notebook for running the scheduling experiment. This runs the model three times, one for each activation type, and demonstrates how the activation regime drives the model to different outcomes.
34
-
35
- ## Further Reading
36
-
37
- This model is adapted from:
38
-
39
- Wilensky, U. (2002). NetLogo PD Basic Evolutionary model. http://ccl.northwestern.edu/netlogo/models/PDBasicEvolutionary. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
40
-
41
- The Demographic Prisoner's Dilemma originates from:
42
-
43
- [Epstein, J. Zones of Cooperation in Demographic Prisoner's Dilemma. 1998.](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.8.8629&rep=rep1&type=pdf)
File without changes
@@ -1,50 +0,0 @@
1
- from mesa.experimental.cell_space import CellAgent
2
-
3
-
4
- class PDAgent(CellAgent):
5
- """Agent member of the iterated, spatial prisoner's dilemma model."""
6
-
7
- def __init__(self, model, starting_move=None):
8
- """
9
- Create a new Prisoner's Dilemma agent.
10
-
11
- Args:
12
- model: model instance
13
- starting_move: If provided, determines the agent's initial state:
14
- C(ooperating) or D(efecting). Otherwise, random.
15
- """
16
- super().__init__(model)
17
- self.score = 0
18
- if starting_move:
19
- self.move = starting_move
20
- else:
21
- self.move = self.random.choice(["C", "D"])
22
- self.next_move = None
23
-
24
- @property
25
- def is_cooroperating(self):
26
- return self.move == "C"
27
-
28
- def step(self):
29
- """Get the best neighbor's move, and change own move accordingly
30
- if better than own score."""
31
-
32
- # neighbors = self.model.grid.get_neighbors(self.pos, True, include_center=True)
33
- neighbors = [*list(self.cell.neighborhood.agents), self]
34
- best_neighbor = max(neighbors, key=lambda a: a.score)
35
- self.next_move = best_neighbor.move
36
-
37
- if self.model.activation_order != "Simultaneous":
38
- self.advance()
39
-
40
- def advance(self):
41
- self.move = self.next_move
42
- self.score += self.increment_score()
43
-
44
- def increment_score(self):
45
- neighbors = self.cell.neighborhood.agents
46
- if self.model.activation_order == "Simultaneous":
47
- moves = [neighbor.next_move for neighbor in neighbors]
48
- else:
49
- moves = [neighbor.move for neighbor in neighbors]
50
- return sum(self.model.payoff[(self.move, move)] for move in moves)