Пример #1
0
        public List<Organism> readOrganismsFromFile()
        {
            StreamReader reader = new StreamReader(genDirectory + "//" + "Organisms.txt");

            List<Organism> output = new List<Organism>();

            string line = "";
            while ((line = reader.ReadLine()) != null)
            {
                try
                {
                    Guid guid = Guid.Parse(line);
                    line = reader.ReadLine();
                    double speed = double.Parse(line);
                    line = reader.ReadLine();
                    int historicalSum = int.Parse(line);
                    line = reader.ReadLine();
                    int generation = int.Parse(line);
                    List<String> behaviors = new List<String>();
                    List<int> constants = new List<int>();
                    if (reader.ReadLine() == behaviorsInitiate)
                    {
                        while ((line = reader.ReadLine()) != constantsInitiate)
                        {
                            behaviors.Add(line);
                        }
                        while ((line = reader.ReadLine()) != organismTerminate)
                        {
                            constants.Add(int.Parse(line));
                        }
                    }

                    GeneticCode genes = new GeneticCode(behaviors, speed, constants);

                    output.Add(new Organism(genes, guid, world.randomPointInWorld(), this.behaviors, historicalSum, generation));
                }
                catch (FormatException)
                {
                }
            }

            reader.Close();

            return output;
        }
Пример #2
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);
        }
Пример #3
0
        //This method will be used to create a completely new randomly generated organism,
        //with absolutely no inherited.
        public Organism newRandomOrganism()
        {
            List<string> code = new List<string>();

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

            for(int i = 0; i < 3; i++)
            {
                int index = ranGen.Next(behaviors.getBehaviorsLength());
                string behavior = behaviors.getBehavior(index);
                code.Add(behavior);
                if(behavior == "constantValue")
                {
                    constants.Add(ranGen.Next(30));
                }
            }

            GeneticCode genes = new GeneticCode(code, ranGen.NextDouble()*maxSpeed, constants);

            return new Organism(genes, behaviors, world.randomPointInWorld());
        }