コード例 #1
0
        public Organism reproduceAsexually(Organism parent)
        {
            List<string> newCode = new List<string>();

            List<int> newConstants = new List<int>();

            int constantCounter = 0;

            double newSpeed;

            foreach (string behavior in parent.getGeneticCode().getBehaviors())
            {
                bool changed = false;
                string newBehavior = copyBehaviorAsexually(behavior, out changed);

                if (insertionRoll())
                {
                    string insertedBehavior = behaviors.getRandomBehavior();
                    newCode.Add(insertedBehavior);
                    if(insertedBehavior == "constantValue")
                    {
                        newConstants.Insert(constantCounter, ranGen.Next(behaviors.getBehaviorsLength()));
                    }

                }

                if (!deletionRoll())
                {
                    newCode.Add(newBehavior);
                    if(changed)
                    {
                        newConstants.Insert(constantCounter, ranGen.Next(behaviors.getBehaviorsLength()));
                    }
                    else
                    {
                        if(newBehavior == "constantValue")
                        {
                            newConstants.Insert(constantCounter, parent.getGeneticCode().getConstants()[constantCounter]);
                            constantCounter++;
                        }
                    }
                }
                else
                {
                }
            }

            if (mutationRoll())
            {
                newSpeed = ranGen.NextDouble() * maxSpeed;
            }
            else
            {
                newSpeed = parent.getGeneticCode().getSpeed();
            }

            //An organism cannot have 0 behaviors
            if(newCode.Count == 0)
            {
                int index = ranGen.Next(behaviors.getBehaviorsLength());
                newCode.Add(behaviors.getBehavior(index));
            }

            GeneticCode newGeneticCode = new GeneticCode(newCode, newSpeed, newConstants);

            return new Organism(newGeneticCode, behaviors, world.randomPointInWorld(), parent.getGeneration() + 1);
        }