Skip to content

wolf sheep example crashes when i try to run it with grass regrowth enabled=False #3597

Closed

Description

@disgruntled-penguin
Image

Activity

  1. laxmi2577 commented on Mar 22, 2026

    @laxmi2577
    Contributor

    Hey @disgruntled-penguin, thanks for the clear traceback!

    It looks like the DataCollector stops tracking the 'Grass' metric when grass_regrowth is set to False, but the matplotlib_components.py visualizer is still blindly attempting to plot it, throwing the KeyError.

    I'd love to take this one on. I can put together a PR that adds a safety check in the plotting component to verify the metric exists in the dataframe columns before attempting to plot it. Let me know if I'm good to open a PR for this!

  2. quaquel commented on Mar 22, 2026

    @quaquel
    Member

    I think I have seen this issue before at some point. Is this on main or on 3.5?

  3. codebreaker32 commented on Mar 22, 2026

    @codebreaker32
    Collaborator

    I think I have seen this issue before at some point. Is this on main or on 3.5?

    I checked it, prominent in both.

  4. quaquel commented on Mar 22, 2026

    @quaquel
    Member

    Thanks for checking. @laxmi2577 diagnosis makes sense to me. The main question then becomes what an elegant solution is for the problem.

  5. codebreaker32 commented on Mar 22, 2026

    @codebreaker32
    Collaborator

    I went ahead and tried to fix this, there were two separate issues:

    1. The Frontend KeyError (@laxmi2577 is spot on):
      Added quick if check

    if isinstance(measure, str):
    ax.plot(df.loc[:, measure])
    ax.set_ylabel(measure)
    elif isinstance(measure, dict):
    for m, color in measure.items():
    ax.plot(df.loc[:, m], label=m, color=color)
    ax.legend(loc="best")
    elif isinstance(measure, list | tuple):
    for m in measure:
    ax.plot(df.loc[:, m], label=m)
    ax.legend(loc="best")

    to

    if isinstance(measure, str):
            if measure in df.columns:
                ax.plot(df.loc[:, measure])
                ax.set_ylabel(measure)
        elif isinstance(measure, dict):
            for m, color in measure.items():
                if m in df.columns:
                    ax.plot(df.loc[:, m], label=m, color=color)
            ax.legend(loc="best")
        elif isinstance(measure, list | tuple):
            for m in measure:
                if m in df.columns:
                    ax.plot(df.loc[:, m], label=m)
            ax.legend(loc="best")
    1. If you run the model headlessly (bypassing the UI crash), it dies with a StopIteration error because Sheep.feed() assumes a grass patch exists and calls next() on an empty generator without a default fallback.

    I passed None as a default to next() in Sheep.feed(), and add a quick if grass_patch: check.

    But then after reset, background disppeared

    Image
  6. codebreaker32 commented on Mar 22, 2026

    @codebreaker32
    Collaborator

    This looks somewhat similar to what we encountered in Network(#3323 and #3345)

  7. added a commit that references this issue on Mar 22, 2026
    e2afae8
  8. DipayanDasgupta commented on Mar 22, 2026

    @DipayanDasgupta
    Contributor

    Reproduced both crashes locally and opened a fix PR.
    Bug 1 (StopIteration): Sheep.feed() calls next(generator) with no default. When grass=False no GrassPatch exists in any cell so the generator is always empty. Fix: next((gen), None) + if grass_patch is not None guard.
    Bug 2 (KeyError: 'Grass'): DataCollector only registers the "Grass" reporter when grass=True (model.py line 87). PlotMatplotlib was plotting all measure keys unconditionally. Fix: if m in df.columns guard — generalises to any model with conditionally-collected metrics.
    Both fixes are additive guards only, no change to the grass=True path. Two regression tests added.

    Image
  9. DipayanDasgupta commented on Mar 22, 2026

    @DipayanDasgupta
    Contributor

    Here's the fixed model running interactively with grass=False in the Solara visualizer — Wolves and Sheep population curves shown correctly, no crash, Grass metric absent from both the plot and the sidebar as expected.

    Image
  10. disgruntled-penguin commented on Mar 22, 2026

    @disgruntled-penguin
    ContributorAuthor

    @DipayanDasgupta i assumed setting regrowth to False means we start out with some initial grass and then it doesn’t grow back, is grass just absent from the get go in your solution?

  11. added 3 commits that reference this issue on Mar 23, 2026
    8deefe9
    3429f6c
    406e03a
  12. DipayanDasgupta commented on Mar 23, 2026

    @DipayanDasgupta
    Contributor

    @disgruntled-penguin good point . You're right that semantically grass=False could mean "grass exists but doesn't regrow." In the current Mesa implementation though, grass=False means no GrassPatch agents are created at all (model.py line 122: if self.grass: # Create grass patches). So grass is absent from the start entirely. The parameter name grass controls whether the grass mechanic exists, not just whether it regrows — grass_regrowth_time controls the regrowth rate separately. The crash happens because Sheep.feed() assumed GrassPatch agents always exist in cells, which isn't true when grass=False.

  13. added 5 commits that reference this issue on Mar 23, 2026
    fa2fed0
    0da0eb1
    e0071d9
    e44d7bd
    fb96d62
  14. added a commit that references this issue on Mar 24, 2026
    101b49c
  15. added a commit that references this issue on Mar 26, 2026
  16. added a commit that references this issue on Mar 28, 2026
    b373829
  17. added a commit that references this issue on Apr 1, 2026
    706404c
  18. added a commit that references this issue on Oct 28, 2026
    d8304fe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions