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

Fixes use of magic numbers in hedonic calculus #78

Closed nkremerh closed 2 months ago

nkremerh commented 2 months ago

Modifies hedonic calculus to use agent properties rather than magic numbers Adds agent finding their new cells in range when catching disease Adds agent lookahead discount when considering future rewards in hedonic calculus Fixes GUI getting proper vision, movement, and metabolism values for agent printouts

nkremerh commented 2 months ago

@colinhanrahan please double-check my work.

Nearly all changes (once bug fixes included are also applied) match exactly with the outcomes before this PR.

Adding a separate agent lookahead discount property causes a deviation. The way initial agents are provided endowments and how children are provided endowments is set up such that the random number generator is shielded from changes when we add new agent properties. I'm likely overlooking something small.

Please also feel free to suggest optimizations.

colinhanrahan commented 2 months ago
nkremerh commented 2 months ago

Breaks determinism but outcomes are practically the same. The seed I was testing led to deviation at timestep 20, which is odd since that is after agents reproduce. So, the determinism change isn't caused by children being born. I'll take another dive into it.

Adding a note to the README.

Future intensity calculation verified.

colinhanrahan commented 2 months ago

For reference, my seed deviated from current determinism on timestep 93.

nkremerh commented 2 months ago

Fixed. I placed it in the paired endowments (could be temporary, but it makes sense to put it there permanently).

colinhanrahan commented 2 months ago

@nkremerh Alright, I found the issue by comparing the parentEndowments and pairedEndowments loops:

        for endowment in parentEndowments:
-           index = random.randrange(2)
            random.seed(self.childEndowmentHashes[endowment] + self.timestep)
+           index = random.randrange(2)
            endowmentValue = parentEndowments[endowment][index]
            childEndowment[endowment] = endowmentValue

The random.randrange() should be called after the seed is set, not before. Otherwise, for the config option directly after the new one, the random.randrange() will be seeded with the hash from the previous option, which is now a different hash than it previously was. In practice, this caused the random index for agentLookaheadFactor (the config option after agentLookaheadDiscount) to change, which is what led to the deviation.

This fixes determinism on the seeds I tested. We should add this so we don't run into the same issue when adding new agent config options in the future.