Skip to content

Give a duplicated virus its own dna list - #498

Merged
quaquel merged 2 commits into
mesa:mainfrom
lahari936:fix-virus-dna-aliasing
Oct 28, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
lahari936:fix-virus-dna-aliasing

Conversation

@lahari936

Copy link
Copy Markdown
Contributor

Follow-up to #496, with scope agreed with @RudraDudhat2509 there.

Summary

VirusAgent.generate_dna() writes into the list it is handed and returns that same object, and duplicate() calls it with the parent's own genome:

dna=self.generate_dna(self.dna)

So the first time a virus reproduces, it rewrites its own dna in place and hands the child the identical list. Parent and child alias from then on, and any later mutation by either one changes both.

Relationship to #496

Same defect, two places. #496 fixes creation, where one shared dna list was passed for every initial virus. This fixes duplication. They are independent: with only #496 applied to a clean checkout,

initial dna objects distinct:                   True    <- #496 fixes this
parent genome unchanged by spawning a child:    False
child dna is a separate object:                 False

This PR does not depend on #496 and does not touch model.py, so there is no overlap in the diff. The tests go through VirusAgent directly rather than VirusAntibodyModel, which means they run on main today without #496 merged.

Implementation

def generate_dna(self, dna=None):
    if dna is None:
        return [self.random.randint(0, 9) for _ in range(3)]
    # Work on a copy, so a parent keeps its own genome when it spawns a child.
    dna = list(dna)

One line. It makes the behaviour match what duplicate() reads like it intends: the child is a mutated copy of the parent, not another name for it. AntibodyAgent memory already deep-copies what it stores, so it is unaffected.

Testing

Two tests, both failing on main and passing with the fix:

  • generate_dna() leaves the genome it is given alone, and returns a different object
  • duplicate() gives the child its own list, the parent's genome survives, and mutating the child does not change the parent

ruff check and ruff format --check clean on examples/virus_antibody/.

VirusAgent.generate_dna() writes into the list it is handed and returns that
same object, and duplicate() calls it with the parent's own genome:

    dna=self.generate_dna(self.dna)

So the first time a virus reproduces it rewrites its own dna in place and
hands the child the identical list. Parent and child alias from then on, and
any later mutation by either one changes both.

Copying before mutating makes the behaviour match what duplicate() reads
like it intends: the child is a mutated copy of the parent, not another name
for it.

This is the follow-up to mesa#496, which fixes the matching problem at creation
time, where one shared dna list was passed for every initial virus. Scope
agreed with @RudraDudhat2509 there. The two halves are independent: this one
is reproducible on main today without mesa#496, because it goes through
VirusAgent directly rather than VirusAntibodyModel.

Adds two tests, both failing on main:
- generate_dna() leaves the genome it is given alone
- duplicate() gives the child its own list, and mutating the child does not
  change the parent
Copilot AI balanced review requested due to automatic review settings October 14, 2026 00:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@quaquel
quaquel merged commit d6bdc02 into mesa:main Oct 28, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants