mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Error when running Hotelling's Law example

Open venzen opened this issue 4 months ago • 5 comments

Describe the bug In Hotelling Law example, running app.py gives the following error:

ERROR:    Component react.component(__main__.SpaceDrawer) raised exception TypeError("'NoneType' object is not subscriptable")
Traceback (most recent call last):
  File "/data/mesa/venv/lib/python3.12/site-packages/reacton/core.py", line 1702, in _render
    root_element = el.component.f(*el.args, **el.kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/mesa/mesa-examples/examples/hotelling_law/app.py", line 141, in SpaceDrawer
    rect_x = pos[0] + (i * width)
             ~~~^^^
TypeError: 'NoneType' object is not subscriptable

Expected behavior According to the PNG file included with the example, agents should be positioned on a grid.

To Reproduce

$ cd mesa-examples/examples/hotelling_law/
$ git branch
* main
$ solara run app.py

Additional context

$ python --version
Python 3.12.11
$ python -c "import mesa; print(mesa.__version__)"
3.2.0
$ uname -a
Linux --- 5.15.0-139-generic #149-Ubuntu SMP Fri Apr 11 22:06:13 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

venzen avatar Aug 06 '25 12:08 venzen

Thanks @venzen, we will have it look into it, more than likely you can fix by installing Mesa < 3.2 (i.e. 3.15). But we will give it a look.

tpike3 avatar Aug 07 '25 09:08 tpike3

I’ve already read the contribution guidelines. Is this issue available, or can I proceed with it?

dhiraj-143r avatar Dec 01 '25 14:12 dhiraj-143r

Go ahead!

Note that that is this example: https://github.com/projectmesa/mesa-examples/tree/main/examples/hotelling_law

I would recommend reproducing the issue first.

EwoutH avatar Dec 01 '25 16:12 EwoutH

@EwoutH Is this issue available? Can I look on to it.

ShreyasN707 avatar Dec 04 '25 18:12 ShreyasN707

I think so, can you try reproducing it?

EwoutH avatar Dec 04 '25 19:12 EwoutH

@EwoutH i have made a pull request for this , could you kindly check it

schrodingers-cat-T10 avatar Dec 18 '25 11:12 schrodingers-cat-T10

Please, as indicated also in a PR, we first need to have an example producing the bug so we can properly diagnose what is going on. Just adding some if statements to avoid the crash seems like patching it without addressing the potential root cause. Because in theory this bug should not be triggered at the solara side in the first place.

quaquel avatar Dec 18 '25 11:12 quaquel

@quaquel alright understood! , first i tried to replicate the bug but it popped up right after running the application

The Hotelling Law visualization crashed because Solara renders the UI before all CellAgents have valid spatial data. I think in Mesa 3.x, agents store their position in agent.cell.coordinate, which can temporarily be None.

The visualization code seems to be assumed this coordinate always existed and tried to index into it, probably causing the TypeError.

I tryna fixed this by adding defensive checks to skip agents whose cell or coordinate is not yet available.

This seems to prevent render-time crashes without changing any model behavior.

schrodingers-cat-T10 avatar Dec 18 '25 17:12 schrodingers-cat-T10

Ok, good to know that the bug is still there. However, I still don't understand how this can happen. All the examples that ship with Mesa itself use Solara, and none of them show this behavior. The proper fix thus seems to update this example to use the modern mesa 3.3 way of creating a UI rather than trying to patch the currently dated example code.

quaquel avatar Dec 18 '25 18:12 quaquel

totally valid , it would be better to fix this by implementing mesa 3.3 rather than the patch up

schrodingers-cat-T10 avatar Dec 18 '25 19:12 schrodingers-cat-T10