nkremerh / sugarscape

Development repository for the Digital Terraria Lab implementation of the Sugarscape agent-based societal simulation.
https://github.com/digital-terraria-lab/sugarscape
MIT License
7 stars 11 forks source link

Adds pollution timers and fixes pollution diffusion logic #70

Closed colinhanrahan closed 2 months ago

colinhanrahan commented 2 months ago

Pollution diffusion logic change

Pollution timers

TODO for this PR:

These two features can be split into separate PRs if necessary, but they kind of need to work together. I'm trying to replicate pollution_formation.json and running into some issues, which is why I'm opening this PR now. It seems like agents produce pollution behind themselves which encourages them to move in straight lines (kind of reminiscent of Conway's Game of Life). In the example, most agents also die before they get the chance to move back to the sugar peaks, which makes sense to me because they have to survive 50 timesteps with barely any food. The book's diagram makes less sense to me. Can somebody try this out for themselves and help brainstorm solutions to these discrepancies? See pages 47-49.

nkremerh commented 2 months ago

Both these features should be in this single PR. While the exact behavior of the diagrams from the book might not be entirely reproducible, this gets us closer to what the book intended. That is still a win.

nkremerh commented 2 months ago

I think I found a promising lead: I think I implemented the pollution ratio incorrectly (lines 606-607 of agent.py).

When trying to mesh all the features together (something not really done in the book), I originally interpreted the resources to pollution ratio as being the calculated agent welfare compared to pollution. However, I think the welfare calculation needs to incorporate the resource to pollution ratio for both sugar and spice inside it.

Line 606 becomes: welfare = self.findWelfare(((cell.sugar + welfarePreySugar) / (1 + cell.pollution)), ((cell.spice + welfarePreySpice) / (1 + cell.pollution)))

Line 607 becomes: cellWealth = welfare # This line can be refactored away

With this change, a society using the default decision model (greedy) with no combat or disease manages to treat cells having low pollution as a preference whereas before it seemed to treat it as a necessity. However, the starting population needs to be much higher than our standard (1250 instead of 250) for society to stick around longterm.

Does this change get you closer to a resolution?

colinhanrahan commented 2 months ago

With this change, a society using the default decision model (greedy) with no combat or disease manages to treat cells having low pollution as a preference whereas before it seemed to treat it as a necessity. However, the starting population needs to be much higher than our standard (1250 instead of 250) for society to stick around longterm.

I think this make sense. When pollution was tested long-term in the book (p. 128), there was a delay before it was turned on to allow society to start off on the right foot. If agents start polluting right away, they will scatter, which means they will be much less likely to reproduce.

pollution_formation does seem to match better with the book after this change. In pictures 3-4 on page 49, agents are traveling to cells with no sugar at all, which doesn't make sense considering that sugar / (pollution + 1) = 0 when sugar is 0. Our agents never leave the radius of the sugar peak because they are still primarily looking for sugar.

There are some other small differences. After pollution diffuses everywhere, our agents generally remain on the very edge of sugar peak tiers because higher tiers produce more consumption pollution which is cascading down to the surroundings. We could add some margin of error to try to achieve more "natural" behavior by rounding pollution instead of allowing it to be infinitely precise, but other than that, I think the example is good. I'll finish up the rest of the features on the TODO.

colinhanrahan commented 2 months ago

Take a look at the behavior in trade_pollution.json before you merge.

nkremerh commented 2 months ago

Add in verification that start and stop timesteps are in the right order, if the max is greater than the configured timesteps for the run, if the max is -1 set the value to be equal to timesteps, and if the min is below 0 set it to 0.

colinhanrahan commented 2 months ago

@nkremerh Are the edge cases handled how you want?