onKill() question 

Occasionally I call Populations.createFromGenotype() inside the onKill() function - and I've noticed that sometimes the newly generated creature is automatically killed off as well. Any idea how to avoid this? (it happens about half the time).

To clarify - Creature1 runs out of energy and the onKill function is called. In that function Creature4 is generated with the createFromGenotype() function. The system kills off Creature1, and then for some reason calls onKill for Creature4 before it has even stabilized.

Currently i have an ugly little function that checks for unstabilized creatures in onKill and clones them. The onKill function still kills the creature, but at least this way a copy of the creature stays alive.

Thanks for your help!

Forums: 
Maciej Komosinski's picture

Hard to say... could you post a minimal example? an expdef with just the onKill() function, plus other functions if they are required to reproduce the problem.

Sure, here is a very minimal example:

//assumes that the creature getting deleted is the currently selected genotype
function onKill()
{
        var currIndex = GenePools.genotype; //save the index to the current genotype

    	GenePools.mutateSelected(); //make a mutated copy of the creature
    	GenePools.copySelected(currGroup); //add it to the genepool
	Populations.createFromGenotype(); //and to the population

        GenePools.genotype = currIndex; //index back to the original creature's genotype
	GenePools.deleteSelected(); //delete the old genotype from the genepool
        //the creature will automatically get deleted by the onKill function
}

So this all works fine, except once in a while the onKill() function is called for the new (mutated) creature as well.

Szymon Ulatowski's picture

Congratulations, you have found a bug!

From my investigation, it seems that the bug manifests under the following conditions:
#1 there are 2 or more creatures in a population before onKill() is triggered
#2 creature is added in onKill()
#3 uninitialized variable inside framsticks does not equal zero (this depends on luck...)

Workaround:
Since #3 is beyond your control (will be fixed in the next update), for now you can change either #1 or #2 in your experiment: only simulate one creature at a time OR add new creatures outside of onKill (most of the existing expdefs generate new creatures in onStep(), that's why the problem does not appear).

One more thing could cause problems in your expdef:

var currIndex = GenePools.genotype;

The genotype index is not necessarily the genotype of the creature being killed - this depends on what genotype was previously selected. If you want to maintain a reliable creature-genotype association, then you could save the Genotype.uid in the user field of the creature, and use it later to find the correct genotype. Or look for the genotype using the function GenePools.findGenotypeForCreature(), like the standard.expdef does (in this case, the creature-genotype relation is unambiguous even without uid, because the standard.expdef never creates duplicate genotypes).