예제 #1
0
        //setup condetions for breeding
        //hunger on rotations

        public Creature(int[] dna1, int[] dna2)
        {
            //perform dna splice and mutations
            //selection
            DNA = new int[dna1.Length];
            for (int i = 0; i < dna1.Length; i++)
            {
                if (RNG.Next(1, 1001) > 50)
                {
                    DNA[i] = dna1[i];
                }
                else
                {
                    DNA[i] = dna2[i];
                }
            }
            //mutation
            for (int i = 0; i < DNA.Length; i++)
            {
                if (RNG.Next(1, 101) <= (SingleTon.GetMutationInstance() * 100))//use mutation rate here
                {
                    switch (i)
                    {
                    case 2:
                        DNA[2] = RNG.Next(1, 4);
                        break;

                    case 3:
                        DNA[3] = RNG.Next((int)((float)(DNA[3] - ((float)DNA[3] * SingleTon.GetMutationInstance()))), (int)((float)(DNA[3] + (float)(DNA[3] * (float)SingleTon.GetMutationInstance()))));
                        break;

                    case 4:
                        DNA[4] = RNG.Next((int)((float)(DNA[4] - ((float)DNA[4] * SingleTon.GetMutationInstance()))), (int)((float)(DNA[4] + (float)(DNA[4] * (float)SingleTon.GetMutationInstance()))));
                        break;

                    case 5:
                        DNA[5] = RNG.Next((int)((float)(DNA[5] - ((float)DNA[5] * SingleTon.GetMutationInstance()))), (int)((float)(DNA[5] + (float)(DNA[5] * (float)SingleTon.GetMutationInstance()))));
                        break;

                    case 6:
                        DNA[6] = RNG.Next((int)((float)(DNA[6] - ((float)DNA[6] * SingleTon.GetMutationInstance()))), (int)((float)(DNA[6] + (float)(DNA[6] * (float)SingleTon.GetMutationInstance()))));
                        break;
                    }
                }
            }
            DNA[0]++;
            Generation = DNA[0];
            Species    = (short)DNA[1];
            switch (DNA[2])
            {
            case 1:
                Foodtype = ConsumeType.Carnivore;
                break;

            case 2:
                Foodtype = ConsumeType.Herbivore;
                break;

            case 3:
                Foodtype = ConsumeType.Omnivore;
                break;
            }
            HungerRate    = ((float)DNA[3] / 10);
            StomachSize   = DNA[4];
            Attack        = DNA[5];
            Health        = DNA[6];
            CurrentHealth = Health;
        }
예제 #2
0
 public void Start()
 {
     Console.Title = "GECreatureGeneration";
     Console.WriteLine("Welcome to this demo of genetic evolution");
     Console.WriteLine("there is a few parameters you need to fill out before we begin");
     Console.Write("size of starting pool: ");
     StartingPoolStringInput = Console.ReadLine();
     if (!(int.TryParse(StartingPoolStringInput, out StartingPoolSize)))
     {
         StartingPoolSize = (int)Redo();
     }
     Console.Clear();
     Console.Write("Max population size: ");
     MaxPopulationInputString = Console.ReadLine();
     if (!(long.TryParse(MaxPopulationInputString, out MaxPopulation)))
     {
         MaxPopulation = Redo();
     }
     Console.Clear();
     Console.Write("Mutation rate: ");
     MutationRateInputString = Console.ReadLine();
     if (!(float.TryParse(MutationRateInputString, out MutationRate)))
     {
         MutationRate = RedoFloat();
     }
     Console.Clear();
     Console.Write("what limit to generations: ");
     GenerationToSimulateInputString = Console.ReadLine();
     if (!(int.TryParse(GenerationToSimulateInputString, out GenerationToSimulate)))
     {
         GenerationToSimulate = (int)Redo();
     }
     SingleTon.SetMutationRate(MutationRate);
     Creatures       = new List <Creature>();
     TotalPopulation = Creatures.Count;
     SingleTon.SetMaxPopulation((int)MaxPopulation);
     SingleTon.SetTotalPopulation((int)TotalPopulation);
     CreateInitialCreattures();
     BestCreature   = Creatures[0];
     LowestCreature = Creatures[0];
     while (GenerationToSimulate != CurrentGeneration)
     {
         Console.Clear();
         Years       += 0.1f;
         YearCleaner += 0.1f;
         SimulateLife();
         DrawVisuals();
         if (YearCleaner >= 1)
         {
             if (TotalPopulation > (MaxPopulation * 0.25))
             {
                 if (RNG.Next(1, 101) < 1)
                 {
                     Creatures.Remove(LowestCreature);
                 }
             }
         }
         if (Console.ReadKey().Key == ConsoleKey.R)
         {
             Start();
         }
     }
     Console.Beep();
     Thread.Sleep(3000);
     Console.ReadKey();
 }
예제 #3
0
        public void Live()
        {
            Age += 0.1f;
            if (StomachFuldness < (StomachSize * 0.7))
            {
                //hunt / eat   | the more there is the less plant matter there is, the less herbivore there is the less food for carnivores
                switch (Foodtype)
                {
                case ConsumeType.Carnivore:
                    if (CreatureAttackingToEat != null)
                    {
                        try
                        {
                            CreatureAttackingToEat.GetAttacked(Attack);
                            if (CreatureAttackingToEat.GetHealth() == 0)
                            {
                                CreatureAttackingToEat = null;
                                StomachFuldness       += (StomachFuldness * 0.5f);
                            }
                        }
                        catch
                        {
                            CreatureAttackingToEat = null;
                        }
                    }
                    else
                    {
                        float foodChances;
                        foodChances = RNG.Next(1, 101);
                        if ((foodChances / 100) < ((float)SingleTon.GetHerbivoresAliveData() / (float)SingleTon.GetTotalPopulation()))
                        {
                            Creature PickedCreature;
                            while (CreatureAttackingToEat == null)
                            {
                                PickedCreature = GetCreatureList.GetCreatureList()[RNG.Next(0, GetCreatureList.GetCreatureList().Count)];
                                if (PickedCreature.GetFoodType() == ConsumeType.Herbivore)
                                {
                                    CreatureAttackingToEat = PickedCreature;
                                }
                            }
                            CreatureAttackingToEat.GetAttacked(Attack);
                            if (CreatureAttackingToEat.GetHealth() == 0)
                            {
                                CreatureAttackingToEat = null;
                                StomachFuldness       += (StomachFuldness * 0.5f);
                            }
                        }
                    }
                    break;

                case ConsumeType.Herbivore:
                    //20% chance to find food
                    float ChanceTofindfood = 40;
                    if (((((float)SingleTon.GetHerbivoresAliveData() * 2) + (float)SingleTon.GetOmnivoresAliveData()) / (float)SingleTon.GetMaxPopulation()) > 0.5)
                    {
                        ChanceTofindfood = ChanceTofindfood - (ChanceTofindfood * (((((float)SingleTon.GetHerbivoresAliveData() * 2) + (float)SingleTon.GetOmnivoresAliveData()) / (float)SingleTon.GetMaxPopulation()) - 0.5f));
                    }
                    //ChanceTofindfood

                    if (RNG.Next(1, 101) <= ChanceTofindfood)
                    {
                        //add 2 point to herb pool
                        StomachFuldness += 2;     //temp
                    }
                    break;

                case ConsumeType.Omnivore:
                    //30% chance to find food
                    if (RNG.Next(1, 101) <= 50)
                    {
                        //1 point to herb pool
                        //chance of meat or herb   = 20/80
                        StomachFuldness += 1;    //temp
                    }
                    break;
                }
            }
            StomachFuldness -= HungerRate;
            if (StomachFuldness < 0)
            {
                StomachFuldness = 0;
            }
            if (StomachFuldness == 0)
            {
                CurrentHealth--;
            }

            if (CurrentHealth == 0)
            {
                Dead = true;
            }


            if (StomachFuldness >= (StomachSize * 0.5) && CurrentHealth > (Health * 0.5) && Age > 5)
            {
                CanBreed = true;
            }
            else
            {
                CanBreed = false;
            }
        }
예제 #4
0
        private void SimulateLife()
        {
            #region Stats reset
            TempOmni       = 0;
            TempCarn       = 0;
            TempHerb       = 0;
            TempSpecies0   = 0;
            TempSpecies1   = 0;
            TempSpecies2   = 0;
            TempSpecies3   = 0;
            TempSpecies4   = 0;
            TempSpecies5   = 0;
            TempSpecies6   = 0;
            TempSpecies7   = 0;
            TempSpecies8   = 0;
            TempSpecies9   = 0;
            AverageFitness = 0;
            try
            {
                LowestFitness  = Creatures[0].GetFitness();
                HighestFitness = Creatures[0].GetFitness();
            }
            catch { };
            #endregion

            for (int i = 0; i < Creatures.Count; i++)
            {
                Creatures[i].Live();
                if (Creatures[i].Dead == true)
                {
                    Creatures.Remove(Creatures[i]);
                }
                else
                {
                    #region Stats collecting
                    if (CurrentGeneration < Creatures[i].GetGeneration())
                    {
                        CurrentGeneration = Creatures[i].GetGeneration();
                    }
                    AverageFitness += Creatures[i].GetFitness();
                    if (Creatures[i].GetFitness() > HighestFitness)
                    {
                        HighestFitness = Creatures[i].GetFitness();
                        BestCreature   = Creatures[i];
                    }
                    if (Creatures[i].GetFitness() < LowestFitness)
                    {
                        LowestFitness  = Creatures[i].GetFitness();
                        LowestCreature = Creatures[i];
                    }


                    switch (Creatures[i].GetFoodType())
                    {
                    case ConsumeType.Carnivore:
                        TempCarn++;
                        break;

                    case ConsumeType.Herbivore:
                        TempHerb++;
                        break;

                    case ConsumeType.Omnivore:

                        TempOmni++;
                        break;
                    }

                    switch (Creatures[i].GetSpecies())
                    {
                    case 0:
                        TempSpecies0++;
                        break;

                    case 1:
                        TempSpecies1++;
                        break;

                    case 2:
                        TempSpecies2++;
                        break;

                    case 3:
                        TempSpecies3++;
                        break;

                    case 4:
                        TempSpecies4++;
                        break;

                    case 5:
                        TempSpecies5++;
                        break;

                    case 6:
                        TempSpecies6++;
                        break;

                    case 7:
                        TempSpecies7++;
                        break;

                    case 8:
                        TempSpecies8++;
                        break;

                    case 9:
                        TempSpecies9++;
                        break;
                    }
                    #endregion
                }
            }
            SingleTon.SetCarivoreAliveData(TempCarn);
            SingleTon.SetHerbivoreAliveData(TempHerb);
            SingleTon.SetOmnivoreAliveData(TempOmni);
            TotalPopulation = Creatures.Count;
            AverageFitness /= Creatures.Count;

            //try and breed
            if (TotalPopulation <= MaxPopulation)
            {
                for (int i = 0; i < Creatures.Count; i++)
                {
                    if (Creatures[i].CanBreed)
                    {
                        for (int x = 0; x < Creatures.Count; x++)
                        {
                            if (TotalPopulation <= MaxPopulation)
                            {
                                if (Creatures[x] == Creatures[i] && Creatures[x].CanBreed && Creatures[x].GetSpecies() == Creatures[i].GetSpecies())
                                {
                                    Creatures.Add(new Creature(Creatures[x].GetDNAForBreeding(), Creatures[i].GetDNAForBreeding()));
                                }
                            }
                        }
                    }
                }
            }
        }