mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Tutorial MoneyModel is not deterministic with a given random seed

Open SimonMarkWhittle opened this issue 5 years ago • 3 comments

Describe the bug When two copies of the tutorial's MoneyModel are given the same random seed, they are not guaranteed to produce the same behavior. This is because a MultiGrid does not always return the agents in a cell in the same order, as cell contents are stored in sets and two sets with the same contents are not guaranteed to arrange the agents in the same order when cast to lists.

Expected behavior Two models that are given the same random seed should reliably produce the same behavior. Speaking to this particular context, either the contents of a MultiGrid's cell should return in a consistent order, or the tutorial should show how to order that list of cell contents once it is returned (at the very least the tutorial could point out that it is non-deterministic).

To Reproduce Running the following code at the end of the intro_tutorial.ipynb notebook a few times should eventually result in the two identically-seeded models each producing a different end state (shown by their having different gini scores).

model0 = MoneyModel(3, 3, 3)
model0.reset_randomizer(15)

model0.step()
model0.step()
model0.step()
model0.step()

gini0 = compute_gini(model0)
print(f"model0 gini:{gini0}")

model1 = MoneyModel(3, 3, 3)
model1.reset_randomizer(15)

model1.step()
model1.step()
model1.step()
model1.step()

gini1 = compute_gini(model1)
print(f"model1 gini:{gini1}")

Additional context A viable fix is to add the line: cellmates = sorted(cellmates, key=lambda c: c.unique_id) to the method give_money in the MoneyAgent, such that it reads:

    def give_money(self):
        cellmates = self.model.grid.get_cell_list_contents([self.pos])
        cellmates = sorted(cellmates, key=lambda c: c.unique_id)
        if len(cellmates) > 1:
            other = self.random.choice(cellmates)
            other.wealth += 1
            self.wealth -= 1

(First time contributing to an open source project; I've read the guidelines, but please do let me know if I've missed anything!)

SimonMarkWhittle avatar Oct 03 '19 18:10 SimonMarkWhittle

Hi, thank you for your report! Excellent observation and clear description. I haven't checked if I can make the same observation, but it is true that sets are unpredictable in that way and it sounds logical that this could happen. In that case I think it would be better to reconsider the use of sets for the MultiGrid and elsewhere!

Corvince avatar Oct 03 '19 20:10 Corvince

Hi All, How do you manage the reset_randomizer(n) if you have a BatchRun? I can't manage to get the same results with multiple runs. Yet I do if I do a single run of my model.

TabernaA avatar Mar 05 '21 16:03 TabernaA

Hi All, How do you manage the reset_randomizer(n) if you have a BatchRun? I can't manage to get the same results with multiple runs. Yet I do if I do a single run of my model.

I think the easiest way would be to pass a seed to batchRunner for each run (as a fixed parameter). Otherwise each run will use a different non-deterministic seed.

Corvince avatar Mar 15 '21 19:03 Corvince