예제 #1
0
    public void RandomBreed()
    {
        // spawning new child object
        var child          = GameObject.Instantiate(animalPrefab, transform);
        var childTransform = child.GetComponent <RectTransform> ();
        var randomPos      = Random.insideUnitCircle.normalized * m_animalSpawnRadius;

        childTransform.localPosition = new Vector3(randomPos.x, randomPos.y, 0);

        // choosing parents
        var allAnimals = GetComponentsInChildren <AnimalTest> ();
        int parent1Index = Random.Range(0, allAnimals.Length - 1), parent2Index = 0;

        do
        {
            parent2Index = Random.Range(0, allAnimals.Length - 1);
        }while (parent2Index == parent1Index);        // second parent index must be different from first parent index

        AnimalTest parent1 = allAnimals[parent1Index], parent2 = allAnimals[parent2Index];

        // making new genome
        var childGenome = Genome.Breed(parent1.Genome, parent2.Genome);

        child.GetComponent <AnimalTest> ().Init(childGenome.Genes);
    }
예제 #2
0
        /// <summary>
        /// Breeds new population within species
        /// </summary>
        /// <param name="popSize">Size for new population</param>
        internal void BreedPopulation(int popSize, float mutationChance)
        {
            if (Count < 2)
            {
                throw new InvalidOperationException("Not enough parents to breed");
            }
            List <Genome> parents = new List <Genome>(genomes);

            genomes.Clear();
            for (int i = 0; i < popSize; i++)
            {
                int p1 = Functions.GetRandomNumber(0, parents.Count); // Find Random Parents
                int p2 = Functions.GetRandomNumber(0, parents.Count);
                if (p1 == p2)
                {
                    i--;
                    continue;
                }
                Genome child = Genome.Breed(parents[p1], parents[p2]); // Breed
                genomes.Add(child);                                    // Add to population
                if (Functions.GetRandomNumber(0f, 100f) < mutationChance)
                {
                    child.Mutate(); // Mutate
                }
            }
        }
예제 #3
0
    public void CreateGeneration()
    {
        int xAmount   = 10;
        int yAmount   = 12;
        int countRate = 4;

        if (!start)
        {
            start = true;
            for (float i = -xAmount; i < xAmount; i += countRate)
            {
                for (float j = -yAmount; j < yAmount; j += countRate)
                {
                    CreateFreshPerson(i, j);
                    populationCount++;
                }
            }
        }
        else
        {
            Debug.Log("Generation " + generationCount++ + " Data\nAverage : " + (deathsTotal > 0 ? scoreTotal / deathsTotal : 0) + ", TotalScore : " + scoreTotal);
            deathsTotal = 0;
            scoreTotal  = 0;
            int                    count  = 0;
            List <Genome[]>        newGen = new List <Genome[]>();
            Pair <int, Genome[]>[] oldGen = genomes.OrderByDescending(x => x.first).ToArray();
            for (int i = 0; i < oldGen.Length / 2; i++)
            {
                newGen.Add(oldGen[i].second);
            }

            var amount = newGen.Count;
            for (int i = 0; i < amount; i += 2)
            {
                var childOne = new Genome[AmountOfWhiskers];
                var childTwo = new Genome[AmountOfWhiskers];

                for (int j = 0; j < AmountOfWhiskers; j++)
                {
                    newGen.ElementAt(i)[j].Mutate();
                    newGen.ElementAt(i + 1)[j].Mutate();
                    var children = Genome.Breed(newGen.ElementAt(i)[j], newGen.ElementAt(i + 1)[j]);
                    childOne[j] = children.first;
                    childTwo[j] = children.second;
                    newGen.ElementAt(i)[j].Mutate();
                    newGen.ElementAt(i + 1)[j].Mutate();
                }

                newGen.Add(childOne);
                newGen.Add(childTwo);
            }


            for (float i = -xAmount; i < xAmount; i += countRate)
            {
                for (float j = -yAmount; j < yAmount; j += countRate)
                {
                    CreatePerson(i, j, newGen.ElementAt(count++));
                }
            }

            genomes = new List <Pair <int, Genome[]> >();
        }
    }
예제 #4
0
 public void Inherit(Animal parent1, Animal parent2)
 {
     m_genome = Genome.Breed(parent1.GetGenome(), parent2.GetGenome());
     Invoke("InitAnimal", 0.1f);
 }