Mesa 3.0.2__py3-none-any.whl → 3.1.0__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 (50) hide show
  1. mesa/__init__.py +4 -6
  2. mesa/agent.py +48 -25
  3. mesa/batchrunner.py +14 -1
  4. mesa/datacollection.py +1 -6
  5. mesa/examples/__init__.py +2 -2
  6. mesa/examples/advanced/epstein_civil_violence/app.py +5 -0
  7. mesa/examples/advanced/pd_grid/app.py +5 -0
  8. mesa/examples/advanced/sugarscape_g1mt/app.py +7 -2
  9. mesa/examples/basic/boid_flockers/app.py +5 -0
  10. mesa/examples/basic/boltzmann_wealth_model/app.py +8 -5
  11. mesa/examples/basic/boltzmann_wealth_model/st_app.py +1 -1
  12. mesa/examples/basic/conways_game_of_life/app.py +5 -0
  13. mesa/examples/basic/conways_game_of_life/st_app.py +2 -2
  14. mesa/examples/basic/schelling/app.py +5 -0
  15. mesa/examples/basic/virus_on_network/app.py +5 -0
  16. mesa/experimental/__init__.py +17 -10
  17. mesa/experimental/cell_space/__init__.py +19 -7
  18. mesa/experimental/cell_space/cell.py +22 -37
  19. mesa/experimental/cell_space/cell_agent.py +12 -1
  20. mesa/experimental/cell_space/cell_collection.py +18 -3
  21. mesa/experimental/cell_space/discrete_space.py +15 -64
  22. mesa/experimental/cell_space/grid.py +74 -4
  23. mesa/experimental/cell_space/network.py +13 -1
  24. mesa/experimental/cell_space/property_layer.py +444 -0
  25. mesa/experimental/cell_space/voronoi.py +13 -1
  26. mesa/experimental/devs/__init__.py +20 -2
  27. mesa/experimental/devs/eventlist.py +19 -1
  28. mesa/experimental/devs/simulator.py +24 -8
  29. mesa/experimental/mesa_signals/__init__.py +23 -0
  30. mesa/experimental/mesa_signals/mesa_signal.py +485 -0
  31. mesa/experimental/mesa_signals/observable_collections.py +133 -0
  32. mesa/experimental/mesa_signals/signals_util.py +52 -0
  33. mesa/mesa_logging.py +190 -0
  34. mesa/model.py +17 -74
  35. mesa/visualization/__init__.py +2 -2
  36. mesa/visualization/mpl_space_drawing.py +2 -2
  37. mesa/visualization/solara_viz.py +23 -5
  38. {mesa-3.0.2.dist-info → mesa-3.1.0.dist-info}/METADATA +3 -4
  39. {mesa-3.0.2.dist-info → mesa-3.1.0.dist-info}/RECORD +43 -44
  40. {mesa-3.0.2.dist-info → mesa-3.1.0.dist-info}/WHEEL +1 -1
  41. mesa/experimental/UserParam.py +0 -67
  42. mesa/experimental/components/altair.py +0 -81
  43. mesa/experimental/components/matplotlib.py +0 -242
  44. mesa/experimental/devs/examples/epstein_civil_violence.py +0 -305
  45. mesa/experimental/devs/examples/wolf_sheep.py +0 -250
  46. mesa/experimental/solara_viz.py +0 -453
  47. mesa/time.py +0 -391
  48. {mesa-3.0.2.dist-info → mesa-3.1.0.dist-info}/entry_points.txt +0 -0
  49. {mesa-3.0.2.dist-info → mesa-3.1.0.dist-info}/licenses/LICENSE +0 -0
  50. {mesa-3.0.2.dist-info → mesa-3.1.0.dist-info}/licenses/NOTICE +0 -0
@@ -1,453 +0,0 @@
1
- """Mesa visualization module for creating interactive model visualizations.
2
-
3
- This module provides components to create browser- and Jupyter notebook-based visualizations of
4
- Mesa models, allowing users to watch models run step-by-step and interact with model parameters.
5
-
6
- Key features:
7
- - SolaraViz: Main component for creating visualizations, supporting grid displays and plots
8
- - ModelController: Handles model execution controls (step, play, pause, reset)
9
- - UserInputs: Generates UI elements for adjusting model parameters
10
- - Card: Renders individual visualization elements (space, measures)
11
-
12
- The module uses Solara for rendering in Jupyter notebooks or as standalone web applications.
13
- It supports various types of visualizations including matplotlib plots, agent grids, and
14
- custom visualization components.
15
-
16
- Usage:
17
- 1. Define an agent_portrayal function to specify how agents should be displayed
18
- 2. Set up model_params to define adjustable parameters
19
- 3. Create a SolaraViz instance with your model, parameters, and desired measures
20
- 4. Display the visualization in a Jupyter notebook or run as a Solara app
21
-
22
- See the Visualization Tutorial and example models for more details.
23
- """
24
-
25
- import threading
26
-
27
- import reacton.ipywidgets as widgets
28
- import solara
29
- from solara.alias import rv
30
-
31
- import mesa.experimental.components.altair as components_altair
32
- import mesa.experimental.components.matplotlib as components_matplotlib
33
- from mesa.experimental.UserParam import Slider
34
-
35
-
36
- # TODO: Turn this function into a Solara component once the current_step.value
37
- # dependency is passed to measure()
38
- def Card(
39
- model, measures, agent_portrayal, space_drawer, dependencies, color, layout_type
40
- ):
41
- """Create a card component for visualizing model space or measures.
42
-
43
- Args:
44
- model: The Mesa model instance
45
- measures: List of measures to be plotted
46
- agent_portrayal: Function to define agent appearance
47
- space_drawer: Method to render agent space
48
- dependencies: List of dependencies for updating the visualization
49
- color: Background color of the card
50
- layout_type: Type of layout (Space or Measure)
51
-
52
- Returns:
53
- rv.Card: A card component containing the visualization
54
- """
55
- with rv.Card(
56
- style_=f"background-color: {color}; width: 100%; height: 100%"
57
- ) as main:
58
- if "Space" in layout_type:
59
- rv.CardTitle(children=["Space"])
60
- if space_drawer == "default":
61
- # draw with the default implementation
62
- components_matplotlib.SpaceMatplotlib(
63
- model, agent_portrayal, dependencies=dependencies
64
- )
65
- elif space_drawer == "altair":
66
- components_altair.SpaceAltair(
67
- model, agent_portrayal, dependencies=dependencies
68
- )
69
- elif space_drawer:
70
- # if specified, draw agent space with an alternate renderer
71
- space_drawer(model, agent_portrayal, dependencies=dependencies)
72
- elif "Measure" in layout_type:
73
- rv.CardTitle(children=["Measure"])
74
- measure = measures[layout_type["Measure"]]
75
- if callable(measure):
76
- # Is a custom object
77
- measure(model)
78
- else:
79
- components_matplotlib.PlotMatplotlib(
80
- model, measure, dependencies=dependencies
81
- )
82
- return main
83
-
84
-
85
- @solara.component
86
- def SolaraViz(
87
- model_class,
88
- model_params,
89
- measures=None,
90
- name=None,
91
- agent_portrayal=None,
92
- space_drawer="default",
93
- play_interval=150,
94
- seed=None,
95
- ):
96
- """Initialize a component to visualize a model.
97
-
98
- Args:
99
- model_class: Class of the model to instantiate
100
- model_params: Parameters for initializing the model
101
- measures: List of callables or data attributes to plot
102
- name: Name for display
103
- agent_portrayal: Options for rendering agents (dictionary);
104
- Default drawer supports custom `"size"`, `"color"`, and `"shape"`.
105
- space_drawer: Method to render the agent space for
106
- the model; default implementation is the `SpaceMatplotlib` component;
107
- simulations with no space to visualize should
108
- specify `space_drawer=False`
109
- play_interval: Play interval (default: 150)
110
- seed: The random seed used to initialize the model
111
- """
112
- if name is None:
113
- name = model_class.__name__
114
-
115
- current_step = solara.use_reactive(0)
116
-
117
- # 1. Set up model parameters
118
- reactive_seed = solara.use_reactive(0)
119
- user_params, fixed_params = split_model_params(model_params)
120
- model_parameters, set_model_parameters = solara.use_state(
121
- {**fixed_params, **{k: v.get("value") for k, v in user_params.items()}}
122
- )
123
-
124
- # 2. Set up Model
125
- def make_model():
126
- """Create a new model instance with current parameters and seed."""
127
- model = model_class.__new__(
128
- model_class, **model_parameters, seed=reactive_seed.value
129
- )
130
- model.__init__(**model_parameters)
131
- current_step.value = 0
132
- return model
133
-
134
- reset_counter = solara.use_reactive(0)
135
- model = solara.use_memo(
136
- make_model,
137
- dependencies=[
138
- *list(model_parameters.values()),
139
- reset_counter.value,
140
- reactive_seed.value,
141
- ],
142
- )
143
-
144
- def handle_change_model_params(name: str, value: any):
145
- """Update model parameters when user input changes."""
146
- set_model_parameters({**model_parameters, name: value})
147
-
148
- # 3. Set up UI
149
-
150
- with solara.AppBar():
151
- solara.AppBarTitle(name)
152
-
153
- # render layout and plot
154
- def do_reseed():
155
- """Update the random seed for the model."""
156
- reactive_seed.value = model.random.random()
157
-
158
- dependencies = [
159
- *list(model_parameters.values()),
160
- current_step.value,
161
- reactive_seed.value,
162
- ]
163
-
164
- # if space drawer is disabled, do not include it
165
- layout_types = [{"Space": "default"}] if space_drawer else []
166
-
167
- if measures:
168
- layout_types += [{"Measure": elem} for elem in range(len(measures))]
169
-
170
- grid_layout_initial = make_initial_grid_layout(layout_types=layout_types)
171
- grid_layout, set_grid_layout = solara.use_state(grid_layout_initial)
172
-
173
- with solara.Sidebar():
174
- with solara.Card("Controls", margin=1, elevation=2):
175
- solara.InputText(
176
- label="Seed",
177
- value=reactive_seed,
178
- continuous_update=True,
179
- )
180
- UserInputs(user_params, on_change=handle_change_model_params)
181
- ModelController(model, play_interval, current_step, reset_counter)
182
- solara.Button(label="Reseed", color="primary", on_click=do_reseed)
183
- with solara.Card("Information", margin=1, elevation=2):
184
- solara.Markdown(md_text=f"Step - {current_step}")
185
-
186
- items = [
187
- Card(
188
- model,
189
- measures,
190
- agent_portrayal,
191
- space_drawer,
192
- dependencies,
193
- color="white",
194
- layout_type=layout_types[i],
195
- )
196
- for i in range(len(layout_types))
197
- ]
198
- solara.GridDraggable(
199
- items=items,
200
- grid_layout=grid_layout,
201
- resizable=True,
202
- draggable=True,
203
- on_grid_layout=set_grid_layout,
204
- )
205
-
206
-
207
- JupyterViz = SolaraViz
208
-
209
-
210
- @solara.component
211
- def ModelController(model, play_interval, current_step, reset_counter):
212
- """Create controls for model execution (step, play, pause, reset).
213
-
214
- Args:
215
- model: The model being visualized
216
- play_interval: Interval between steps during play
217
- current_step: Reactive value for the current step
218
- reset_counter: Counter to trigger model reset
219
- """
220
- playing = solara.use_reactive(False)
221
- thread = solara.use_reactive(None)
222
- # We track the previous step to detect if user resets the model via
223
- # clicking the reset button or changing the parameters. If previous_step >
224
- # current_step, it means a model reset happens while the simulation is
225
- # still playing.
226
- previous_step = solara.use_reactive(0)
227
-
228
- def on_value_play(change):
229
- """Handle play/pause state changes."""
230
- if previous_step.value > current_step.value and current_step.value == 0:
231
- # We add extra checks for current_step.value == 0, just to be sure.
232
- # We automatically stop the playing if a model is reset.
233
- playing.value = False
234
- elif model.running:
235
- do_step()
236
- else:
237
- playing.value = False
238
-
239
- def do_step():
240
- """Advance the model by one step."""
241
- model.step()
242
- previous_step.value = current_step.value
243
- current_step.value = model.steps
244
-
245
- def do_play():
246
- """Run the model continuously."""
247
- model.running = True
248
- while model.running:
249
- do_step()
250
-
251
- def threaded_do_play():
252
- """Start a new thread for continuous model execution."""
253
- if thread is not None and thread.is_alive():
254
- return
255
- thread.value = threading.Thread(target=do_play)
256
- thread.start()
257
-
258
- def do_pause():
259
- """Pause the model execution."""
260
- if (thread is None) or (not thread.is_alive()):
261
- return
262
- model.running = False
263
- thread.join()
264
-
265
- def do_reset():
266
- """Reset the model."""
267
- reset_counter.value += 1
268
-
269
- def do_set_playing(value):
270
- """Set the playing state."""
271
- if current_step.value == 0:
272
- # This means the model has been recreated, and the step resets to
273
- # 0. We want to avoid triggering the playing.value = False in the
274
- # on_value_play function.
275
- previous_step.value = current_step.value
276
- playing.set(value)
277
-
278
- with solara.Row():
279
- solara.Button(label="Step", color="primary", on_click=do_step)
280
- # This style is necessary so that the play widget has almost the same
281
- # height as typical Solara buttons.
282
- solara.Style(
283
- """
284
- .widget-play {
285
- height: 35px;
286
- }
287
- .widget-play button {
288
- color: white;
289
- background-color: #1976D2; // Solara blue color
290
- }
291
- """
292
- )
293
- widgets.Play(
294
- value=0,
295
- interval=play_interval,
296
- repeat=True,
297
- show_repeat=False,
298
- on_value=on_value_play,
299
- playing=playing.value,
300
- on_playing=do_set_playing,
301
- )
302
- solara.Button(label="Reset", color="primary", on_click=do_reset)
303
- # threaded_do_play is not used for now because it
304
- # doesn't work in Google colab. We use
305
- # ipywidgets.Play until it is fixed. The threading
306
- # version is definite a much better implementation,
307
- # if it works.
308
- # solara.Button(label="▶", color="primary", on_click=viz.threaded_do_play)
309
- # solara.Button(label="⏸︎", color="primary", on_click=viz.do_pause)
310
- # solara.Button(label="Reset", color="primary", on_click=do_reset)
311
-
312
-
313
- def split_model_params(model_params):
314
- """Split model parameters into user-adjustable and fixed parameters.
315
-
316
- Args:
317
- model_params: Dictionary of all model parameters
318
-
319
- Returns:
320
- tuple: (user_adjustable_params, fixed_params)
321
- """
322
- model_params_input = {}
323
- model_params_fixed = {}
324
- for k, v in model_params.items():
325
- if check_param_is_fixed(v):
326
- model_params_fixed[k] = v
327
- else:
328
- model_params_input[k] = v
329
- return model_params_input, model_params_fixed
330
-
331
-
332
- def check_param_is_fixed(param):
333
- """Check if a parameter is fixed (not user-adjustable).
334
-
335
- Args:
336
- param: Parameter to check
337
-
338
- Returns:
339
- bool: True if parameter is fixed, False otherwise
340
- """
341
- if isinstance(param, Slider):
342
- return False
343
- if not isinstance(param, dict):
344
- return True
345
- if "type" not in param:
346
- return True
347
-
348
-
349
- @solara.component
350
- def UserInputs(user_params, on_change=None):
351
- """Initialize user inputs for configurable model parameters.
352
-
353
- Currently supports :class:`solara.SliderInt`, :class:`solara.SliderFloat`,
354
- :class:`solara.Select`, and :class:`solara.Checkbox`.
355
-
356
- Args:
357
- user_params: Dictionary with options for the input, including label,
358
- min and max values, and other fields specific to the input type.
359
- on_change: Function to be called with (name, value) when the value of an input changes.
360
- """
361
- for name, options in user_params.items():
362
-
363
- def change_handler(value, name=name):
364
- on_change(name, value)
365
-
366
- if isinstance(options, Slider):
367
- slider_class = (
368
- solara.SliderFloat if options.is_float_slider else solara.SliderInt
369
- )
370
- slider_class(
371
- options.label,
372
- value=options.value,
373
- on_value=change_handler,
374
- min=options.min,
375
- max=options.max,
376
- step=options.step,
377
- )
378
- continue
379
-
380
- # label for the input is "label" from options or name
381
- label = options.get("label", name)
382
- input_type = options.get("type")
383
- if input_type == "SliderInt":
384
- solara.SliderInt(
385
- label,
386
- value=options.get("value"),
387
- on_value=change_handler,
388
- min=options.get("min"),
389
- max=options.get("max"),
390
- step=options.get("step"),
391
- )
392
- elif input_type == "SliderFloat":
393
- solara.SliderFloat(
394
- label,
395
- value=options.get("value"),
396
- on_value=change_handler,
397
- min=options.get("min"),
398
- max=options.get("max"),
399
- step=options.get("step"),
400
- )
401
- elif input_type == "Select":
402
- solara.Select(
403
- label,
404
- value=options.get("value"),
405
- on_value=change_handler,
406
- values=options.get("values"),
407
- )
408
- elif input_type == "Checkbox":
409
- solara.Checkbox(
410
- label=label,
411
- on_value=change_handler,
412
- value=options.get("value"),
413
- )
414
- else:
415
- raise ValueError(f"{input_type} is not a supported input type")
416
-
417
-
418
- def make_text(renderer):
419
- """Create a function that renders text using Markdown.
420
-
421
- Args:
422
- renderer: Function that takes a model and returns a string
423
-
424
- Returns:
425
- function: A function that renders the text as Markdown
426
- """
427
-
428
- def function(model):
429
- solara.Markdown(renderer(model))
430
-
431
- return function
432
-
433
-
434
- def make_initial_grid_layout(layout_types):
435
- """Create an initial grid layout for visualization components.
436
-
437
- Args:
438
- layout_types: List of layout types (Space or Measure)
439
-
440
- Returns:
441
- list: Initial grid layout configuration
442
- """
443
- return [
444
- {
445
- "i": i,
446
- "w": 6,
447
- "h": 10,
448
- "moved": False,
449
- "x": 6 * (i % 2),
450
- "y": 16 * (i - i % 2),
451
- }
452
- for i in range(len(layout_types))
453
- ]