Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Observables to MESA #2291

Merged
merged 127 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
127 commits
Select commit Hold shift + click to select a range
d90b0f7
further updates
quaquel Jan 21, 2024
9586490
Update benchmarks/WolfSheep/__init__.py
quaquel Jan 21, 2024
4aaa35d
Merge remote-tracking branch 'upstream/main'
quaquel Feb 21, 2024
d31478c
Merge remote-tracking branch 'upstream/main'
quaquel Apr 22, 2024
6e4c72e
Merge remote-tracking branch 'upstream/main'
quaquel Aug 14, 2024
70fbaf5
Merge remote-tracking branch 'upstream/main'
quaquel Aug 18, 2024
724c8db
Merge remote-tracking branch 'upstream/main'
quaquel Aug 21, 2024
45184a4
Merge remote-tracking branch 'upstream/main'
quaquel Aug 22, 2024
3d75d30
Merge remote-tracking branch 'upstream/main'
quaquel Aug 27, 2024
2759244
Update __init__.py
quaquel Aug 27, 2024
fc8aaea
Merge remote-tracking branch 'upstream/main'
quaquel Aug 28, 2024
1ba465d
Merge remote-tracking branch 'upstream/main'
quaquel Aug 30, 2024
2b5e822
Merge remote-tracking branch 'upstream/main'
quaquel Sep 2, 2024
3847799
Merge remote-tracking branch 'upstream/main'
quaquel Sep 4, 2024
301d80e
Merge remote-tracking branch 'upstream/main'
quaquel Sep 4, 2024
fe3d655
Merge remote-tracking branch 'upstream/main'
quaquel Sep 4, 2024
7d18880
Merge remote-tracking branch 'upstream/main'
quaquel Sep 5, 2024
6b49a3b
Merge remote-tracking branch 'upstream/main'
quaquel Sep 5, 2024
b9909e6
Merge remote-tracking branch 'upstream/main'
quaquel Sep 9, 2024
8ce3d83
Merge remote-tracking branch 'upstream/main'
quaquel Sep 10, 2024
88fbf74
Merge remote-tracking branch 'upstream/main'
quaquel Sep 13, 2024
b1965fc
wip
quaquel Sep 6, 2024
7cf2384
ongoing
quaquel Sep 8, 2024
ba2affb
draft observables and signals
quaquel Sep 11, 2024
21e185d
Update pyproject.toml
quaquel Sep 11, 2024
75aa6d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 11, 2024
d594052
add observables to class, and signals to instances
quaquel Sep 12, 2024
5d77796
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 12, 2024
e3ac177
add HasObservables mixin and move signals back to experimental
quaquel Sep 13, 2024
8fc657b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
f58acd9
raise valueerror if owner does not have the observables attribute
quaquel Sep 13, 2024
8300d5d
stash
quaquel Sep 21, 2024
4499389
Update boltzmann_wealth.py
quaquel Sep 21, 2024
15d74aa
ongoing work
quaquel Sep 21, 2024
0f58754
Merge remote-tracking branch 'upstream/main' into signal
quaquel Sep 21, 2024
718ab09
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 21, 2024
1acca63
remove dependencies
quaquel Sep 21, 2024
9537c3b
ruff fixes
quaquel Sep 21, 2024
62d27ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 21, 2024
a0102e9
make All() work for signal_type
quaquel Sep 22, 2024
c7cedde
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2024
e4b9593
change how fallback_value is used
quaquel Sep 22, 2024
99483f2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2024
9d0fc75
Update observable_collections.py
quaquel Sep 22, 2024
dcf9564
add a BaseObservable class
quaquel Sep 22, 2024
0e67153
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2024
2a3f108
make BaseObserver an ABC and ruff fixes
quaquel Sep 22, 2024
69d42db
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2024
79f99a1
start of adding Computed and Computable
quaquel Sep 22, 2024
65d4974
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2024
2064c73
Update signal.py
quaquel Sep 22, 2024
72b5445
Merge remote-tracking branch 'upstream/main' into signal
quaquel Sep 22, 2024
5222518
remove wrongfully commited code
quaquel Sep 22, 2024
29974b2
typing fix
quaquel Sep 22, 2024
1588dbf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2024
51a6d0e
first testing of computable
quaquel Sep 23, 2024
dd17009
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 23, 2024
e000097
basic version of computed works
quaquel Sep 23, 2024
43b2a7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 23, 2024
cba2eec
Update test.py
quaquel Sep 23, 2024
5e0e9d5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 23, 2024
6505de0
Update signal.py
quaquel Sep 23, 2024
ca85bcd
fix for cycles
quaquel Sep 24, 2024
4805252
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2024
ed3c5ba
Update signal.py
quaquel Sep 25, 2024
1e91ee5
Merge remote-tracking branch 'upstream/main' into signal
quaquel Sep 25, 2024
304c63a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2024
820c176
update 1 benchmark model
quaquel Sep 25, 2024
04ef6f1
some testing with timings
quaquel Sep 25, 2024
b3d8f9a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2024
74a641a
Merge remote-tracking branch 'upstream/main' into signal
quaquel Sep 25, 2024
27313ec
Update boltzmann_wealth.py
quaquel Sep 25, 2024
5ccf584
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2024
2d90025
Update boltzmann_wealth.py
quaquel Sep 25, 2024
2702bce
Merge branch 'main' into signal
quaquel Sep 26, 2024
b7c405c
perfcounter
quaquel Sep 27, 2024
291c721
various cleanups
quaquel Sep 27, 2024
658bf5e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2024
85cd317
Update test.py
quaquel Sep 27, 2024
f0c600b
Update __init__.py
quaquel Sep 27, 2024
f0c918a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2024
8afea8c
cleaning
quaquel Sep 27, 2024
ee56159
Merge remote-tracking branch 'upstream/main' into signal
quaquel Sep 27, 2024
95b647c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2024
9169d20
cleaning and debugging code
quaquel Sep 27, 2024
728b0d2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2024
1482f07
Merge branch 'main' into signal
quaquel Sep 30, 2024
fab12d4
Update boltzmann_wealth.py
quaquel Sep 30, 2024
34fd9ff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 30, 2024
5f58aaa
Update mesa_signal.py
quaquel Sep 30, 2024
aa0b940
ongoing testing and cleaning
quaquel Oct 1, 2024
d539aa8
Merge remote-tracking branch 'upstream/main' into signal
quaquel Oct 1, 2024
ca9def1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2024
6d96541
Merge remote-tracking branch 'upstream/main' into signal
quaquel Oct 4, 2024
255e91c
switch from named tuple to attribute dict
quaquel Oct 7, 2024
5f96bb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
555621c
Merge remote-tracking branch 'upstream/main' into signal
quaquel Oct 16, 2024
0dc29b8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
05e6d76
Merge remote-tracking branch 'upstream/main' into signal
quaquel Nov 4, 2024
9c4ec7c
cleanup and merging with upstream
quaquel Nov 4, 2024
3b2d240
Merge branch 'main' into signal
quaquel Nov 5, 2024
ac892f0
Merge branch 'main' into signal
quaquel Nov 9, 2024
b3320fb
first set of tests for Observable and HasObservable
quaquel Nov 9, 2024
cb1161b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 9, 2024
d947b16
some ruff stuff
quaquel Nov 9, 2024
7215073
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 9, 2024
da4c876
Update signals_util.py
quaquel Nov 9, 2024
5486de9
Update test.py
quaquel Nov 9, 2024
bad310a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 9, 2024
272f512
Merge branch 'main' into signal
quaquel Nov 9, 2024
b6bbd97
Merge remote-tracking branch 'upstream/main' into signal
quaquel Nov 14, 2024
e50dd82
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 14, 2024
28c186c
initial unit tests for observable list
quaquel Nov 14, 2024
9b17932
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 14, 2024
3e1f578
additional tests
quaquel Nov 14, 2024
9ea74d6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 14, 2024
5e9e15f
include experimental in code coverage
quaquel Nov 15, 2024
4b2ec6a
updates to tests
quaquel Nov 15, 2024
eeb87b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 15, 2024
9761abe
minor fix
quaquel Nov 15, 2024
6460551
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 15, 2024
fd33efe
Update mesa_signal.py
quaquel Nov 15, 2024
03bbf09
Merge branch 'signal_tests' into signal
quaquel Nov 16, 2024
38d5462
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 16, 2024
7ebde11
Merge branch 'main' into signal
quaquel Nov 16, 2024
6640a1d
cleanup
quaquel Nov 16, 2024
2a6ddb4
Merge branch 'main' into signal
quaquel Nov 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion benchmarks/BoltzmannWealth/boltzmann_wealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import mesa
from mesa.experimental.signals import HasObservables, Observable


def compute_gini(model):
Expand Down Expand Up @@ -74,9 +75,11 @@ def run_model(self, n):
self.step()


class MoneyAgent(mesa.Agent):
class MoneyAgent(mesa.Agent, HasObservables):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How expensive would it be for mesa.Agent to automatically inherit HasObservables? The less boilerplate, the better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would not be expensive at all. The same applies to Model, and possible in the future the spaces. However, I have atm designed everything to be in experimental so as not to interfere with the stable code.

"""An agent with fixed initial wealth."""

wealth = Observable()

def __init__(self, model):
"""Instantiate an agent.

Expand Down
5 changes: 3 additions & 2 deletions mesa/experimental/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Experimental init."""

import mesa.experimental.signals as signals
from mesa.experimental import cell_space

from .solara_viz import JupyterViz, Slider, SolaraViz, make_text
# from .solara_viz import JupyterViz, Slider, SolaraViz, make_text

__all__ = ["cell_space", "JupyterViz", "SolaraViz", "make_text", "Slider"]
__all__ = ["cell_space", "signals"]
3 changes: 2 additions & 1 deletion mesa/experimental/cell_space/cell_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

from typing import TYPE_CHECKING

from mesa import Agent, Model
from mesa import Agent

if TYPE_CHECKING:
from mesa import Model

Check warning on line 10 in mesa/experimental/cell_space/cell_agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/cell_space/cell_agent.py#L10

Added line #L10 was not covered by tests
from mesa.experimental.cell_space.cell import Cell


Expand Down
13 changes: 13 additions & 0 deletions mesa/experimental/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Functionality for Observables."""

from .observable_collections import ObservableList
from .signal import All, Computable, Computed, HasObservables, Observable

__all__ = [
"Observable",
"ObservableList",
"HasObservables",
"All",
"Computable",
"Computed",
]
120 changes: 120 additions & 0 deletions mesa/experimental/signals/observable_collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""This module defines observable collections classes.

Observable collections behave like Observable but then for collections.


"""

from collections.abc import Iterable, MutableSequence
from typing import Any

from .signal import BaseObservable, HasObservables

__all__ = [
"ObservableList",
]


class ObservableList(BaseObservable):
"""An ObservableList that emits signals on changes to the underlying list."""

def __init__(self):
"""Initialize the ObservableList."""
super().__init__()
self.signal_types: set = {

Check warning on line 24 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L23-L24

Added lines #L23 - L24 were not covered by tests
"added",
"removed",
"replaced",
"on_change",
}
self.fallback_value = []

Check warning on line 30 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L30

Added line #L30 was not covered by tests

def __set__(self, instance: "HasObservables", value: Iterable):
"""Set the value of the descriptor attribute.

Args:
instance: The instance on which to set the attribute.
value: The value to set the attribute to.

"""
super().__set__(instance, value)
setattr(

Check warning on line 41 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L40-L41

Added lines #L40 - L41 were not covered by tests
instance,
self.private_name,
SignalingList(value, instance, self.public_name),
)


class SignalingList(MutableSequence[Any]):
"""A basic lists that emits signals on changes."""

__slots__ = ["owner", "name", "data"]

def __init__(self, iterable: Iterable, owner: HasObservables, name: str):
"""Initialize a SignalingList.

Args:
iterable: initial values in the list
owner: the HasObservables instance on which this list is defined
name: the attribute name to which this list is assigned

"""
self.owner: HasObservables = owner
self.name: str = name
self.data = list(iterable)

Check warning on line 64 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L62-L64

Added lines #L62 - L64 were not covered by tests

def __setitem__(self, index: int, value: Any) -> None:
"""Set item to index.

Args:
index: the index to set item to
value: the item to set

"""
old_value = self.data[index]
self.data[index] = value
self.owner.notify(self.name, old_value, value, "replaced")

Check warning on line 76 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L74-L76

Added lines #L74 - L76 were not covered by tests

def __delitem__(self, index: int) -> None:
"""Delete item at index.

Args:
index: The index of the item to remove

"""
old_value = self.data
del self.data[index]
self.owner.notify(self.name, old_value, None, "removed")

Check warning on line 87 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L85-L87

Added lines #L85 - L87 were not covered by tests

def __getitem__(self, index) -> Any:
"""Get item at index.

Args:
index: The index of the item to retrieve

Returns:
the item at index
"""
return self.data[index]

Check warning on line 98 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L98

Added line #L98 was not covered by tests

def __len__(self) -> int:
"""Return the length of the list."""
return len(self.data)

Check warning on line 102 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L102

Added line #L102 was not covered by tests

def insert(self, index, value):
"""Insert value at index.

Args:
index: the index to insert value into
value: the value to insert

"""
old_value = self.data[index]
self.data.insert(index, value)
self.owner.notify(self.name, old_value, value, "added")

Check warning on line 114 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L112-L114

Added lines #L112 - L114 were not covered by tests

def __str__(self):
return self.data.__str__()

Check warning on line 117 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L117

Added line #L117 was not covered by tests

def __repr__(self):
return self.data.__repr__()

Check warning on line 120 in mesa/experimental/signals/observable_collections.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/signals/observable_collections.py#L120

Added line #L120 was not covered by tests
Loading
Loading