GithubHelp home page GithubHelp logo

Comments (6)

EwoutH avatar EwoutH commented on September 21, 2024

Thanks for opening this issue and writing it up!

@rht I looked a bit into this issue and I can reproduce it. I think it has to do with either the model clock or the datacollector. Model._steps isn't updated for example with each step. It might be related to how #1942 was implemented. Do you have the opportunity to investigate this further?

from mesa.

rht avatar rht commented on September 21, 2024

This is expected in the new API. Because you are not using model.schedule.step(), the model._steps doesn't get updated automatically. You need to manually advance the clock. See https://github.com/projectmesa/mesa-examples/blob/1e0d6b855c853c1dfa749348445a31000b4daca4/examples/schelling_experimental/model.py#L65-L77

from mesa.

dylan-munson avatar dylan-munson commented on September 21, 2024

Thanks. I tried adding a call self_advance_time() just before data collection in the toy model and the problem persists. I should also note than in the model I was working with where this problem first arose, which is a version of the sugarscape_g1mt model from the mesa examples catalog, there is a call self._steps +=1 just before data collection, which I assume does essentially same thing. Thus, neither one seems to solve the problem.

from mesa.

EwoutH avatar EwoutH commented on September 21, 2024

I can reproduce this bug using the AgentSet API and other modern Mesa best practices.

import mesa

class testAgent(mesa.Agent):
    def __init__(self, unique_id, model):
        super().__init__(unique_id, model)
        self.pos: tuple
    def pollute(self):
        self.model.grid.properties["pollution"].modify_cell(self.pos, lambda p: p+1)

class testModel(mesa.Model):
    def __init__(self, width = 20, height = 20, initial_population=200):
        super().__init__()
        self.width = width
        self.height = height
        self.initial_population = initial_population
        
        # initiate mesa grid class
        self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False)
        #add pollution property layer
        pollution_layer = mesa.space.PropertyLayer(name = "pollution", width = self.width, height = self.height, default_value = 0.0, dtype=float)
        self.grid.add_property_layer(pollution_layer)
        
        self.datacollector = mesa.DataCollector(model_reporters = {
            "Pollution": lambda m: m.grid.properties["pollution"].data,
            "Step": "_steps"
        })
        
        agent_id = 0
        for i in range(self.initial_population):
            # get agent position
            agent = testAgent(agent_id, self)
            self.grid.place_agent(agent, pos=mesa.model.random.choice(list(self.grid.empties)))
            agent_id += 1
            
    
    def step(self):
        self.agents.shuffle().do("pollute")

        print(f"Step {self._steps}: {self.grid.properties["pollution"].data[:1]}")
        self.datacollector.collect(self)
        self._advance_time()

# Run the model
model = testModel()
for i in range(20):
    model.step()

model_results = model.datacollector.get_model_vars_dataframe()
model_results

It prints the correct values each step, so it seems the bug is really in the datacollector itself.

from mesa.

EwoutH avatar EwoutH commented on September 21, 2024

I suspect for some reason it doesn't save a copy of the values at that timestep to model_vars, but a reference to the values that gets continuously updated with each step. @rht do you have an idea where that could happen?

from mesa.

EwoutH avatar EwoutH commented on September 21, 2024

Found the issue and implemented a fix in #2129.

from mesa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.