예제 #1
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        if (rand == null)
        {
            rand = new Random();
        }

        // Filters the last THRESHOLD_PERCENT bad individuals out
        int   badThresholdIndex = (int)(parentGeneration.Individuals.Count * THRESHOLD_PERCENT);
        float threshouldValue   = parentGeneration.Individuals[parentGeneration.Individuals.Count - badThresholdIndex].Fitness;

        var selectedIndividums = parentGeneration.Individuals.FindAll(x => x.fitnessValue > threshouldValue);

        int diff = parentGeneration.Individuals.Count - selectedIndividums.Count;

        for (int i = 0; i < diff; ++i)
        {
            int index = rand.Next(0, selectedIndividums.Count);
            selectedIndividums.Add(selectedIndividums[index]);
        }

        List <string> newGeneration = new List <string>();

        selectedIndividums.ForEach(x => newGeneration.Add(x.GeneSequence));
        selectedIndividums.ForEach(x => newGeneration.Add(x.GeneSequence));

        return(newGeneration);
    }
예제 #2
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        if (r == null)
        {
            r = new Random();
        }
        int           sizeOfParentGeneration = r.Next(0, parentGeneration.Individuals.Count);
        int           sizeOfIndividual       = parentGeneration.Individuals.Count;
        List <string> c = new List <string>();

        while (sizeOfParentGeneration-- != 0)
        {
            var firstIndividual  = parentGeneration.Individuals[r.Next(0, sizeOfIndividual)];
            var secondIndividual = parentGeneration.Individuals[r.Next(0, sizeOfIndividual)];
            c.Add(firstIndividual.GeneSequence);
            c.Add(secondIndividual.GeneSequence);
        }
        int diff = 2 * sizeOfIndividual - c.Count;

        for (int i = 0; i < diff; ++i)
        {
            c.Add(c[r.Next(0, c.Count)]);
            c.Add(c[r.Next(0, c.Count)]);
        }
        return(c);
    }
예제 #3
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        List <Individual> potentialIndividuals = parentGeneration.Individuals; // all individuals
        List <Individual> selectedIndividuals  = new List <Individual>();      // individuals chosen according to fitness
        List <string>     newGeneration        = new List <string>();          // Gene Sequence of choosen individuals in order of recombination

        float fitnessSum          = .0f;
        int   timesAdded          = 0;
        float weightedProbability = .0f;

        // Only add positive Fitness to FitnessSum
        for (int i = 0; i < potentialIndividuals.Count; i++)
        {
            if (potentialIndividuals[i].Fitness > 0)
            {
                fitnessSum += potentialIndividuals[i].Fitness;
            }
        }

        // avoid divide by 0 error
        if (fitnessSum == 0)
        {
            fitnessSum++;
        }

        // calculate weighted probability of each individual to determine how often an individual will be selected
        // individuals with negative fitness value dont get added at all
        for (int i = 0; i < potentialIndividuals.Count; i++)
        {
            if (potentialIndividuals [i].Fitness > 0)
            {
                weightedProbability = potentialIndividuals [i].Fitness / fitnessSum;
            }
            else
            {
                weightedProbability = .0f;
            }

            timesAdded = (int)Math.Floor(weightedProbability * potentialIndividuals.Count * 2);
            for (int j = 0; j < timesAdded; j++)
            {
                selectedIndividuals.Add(potentialIndividuals [i]);
            }
        }

        // Fill up free spots with random individuals to reach two times the size of the original generation
        for (int i = selectedIndividuals.Count; i < potentialIndividuals.Count * 2; i++)
        {
            int randomIndividual = UnityEngine.Random.Range(0, potentialIndividuals.Count);
            selectedIndividuals.Add(potentialIndividuals[randomIndividual]);
        }

        AssemblyCSharp.MyFunctions.Shuffle(selectedIndividuals);

        for (int i = 0; i < selectedIndividuals.Count; i++)
        {
            newGeneration.Add(selectedIndividuals [i].GeneSequence);
        }
        return(newGeneration);
    }
예제 #4
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        List <string> superAwesomeNewGen = new List <string>();

        foreach (Individual ind in parentGeneration.Individuals)
        {
            superAwesomeNewGen.Add(ind.GeneSequence);
            superAwesomeNewGen.Add(ind.GeneSequence);
        }
        return(superAwesomeNewGen);
    }
예제 #5
0
 public bool JudgementDay(GenerationDB.Generation generation)
 {
     if (generation.Fittest.fitnessValue > 0.97)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #6
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        System.Random rand = new System.Random();
        List <string> ret  = new List <string>();

        for (int i = 0; i < parentGeneration.Individuals.Count * 2; i++)
        {
            ret.Add(parentGeneration.Individuals[rand.Next(parentGeneration.Individuals.Count)].GeneSequence);
        }
        return(ret);
    }
예제 #7
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        List <Individual> potentialIndividuals = parentGeneration.Individuals;        // all individuals
        List <string>     newGeneration        = new List <string>();

        for (int i = 0; i < potentialIndividuals.Count * 2; i++)
        {
            int randomIndividual = UnityEngine.Random.Range(0, potentialIndividuals.Count);
            newGeneration.Add(potentialIndividuals[randomIndividual].GeneSequence);
        }
        return(newGeneration);
    }
예제 #8
0
 public bool JudgementDay(GenerationDB.Generation generation)
 {
     if (generation.Fittest.Fitness >= threshold)
     {
         Debug.Log(generation.Fittest.Fitness.ToString());
         return(true);
     }
     else
     {
         Debug.Log(generation.Fittest.Fitness.ToString());
         return(false);
     }
 }
 public bool JudgementDay(GenerationDB.Generation generation)
 {
     if (UnityEngine.Random.value < chucksWrath)
     {
         Debug.Log("The end is now.");
         return(true);
     }
     else
     {
         Debug.Log("The end is nigh.");
         return(false);
     }
 }
예제 #10
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        System.Random r      = new System.Random();
        List <string> newGen = new List <string>();

        for (int i = 0; i <= parentGeneration.Individuals.Count; i++)
        {
            newGen.Add(parentGeneration.Individuals[r.Next(0, parentGeneration.Individuals.Count)].GeneSequence);
            newGen.Add(parentGeneration.Individuals[r.Next(0, parentGeneration.Individuals.Count)].GeneSequence);
        }

        return(newGen);
    }
예제 #11
0
 public bool JudgementDay(GenerationDB.Generation generation)
 {
     if (generation.Fittest.fitnessValue > 99.0 || generationCount == 30)
     {
         generationCount = 1;
         return(true);
     }
     else
     {
         generationCount++;
         return(false);
     }
 }
예제 #12
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        System.Random     r      = new System.Random();
        List <string>     newGen = new List <string>();
        List <Individual> top20  = parentGeneration.Top20;

        for (int i = 0; i < parentGeneration.Individuals.Count; i++)
        {
            newGen.Add(parentGeneration.Individuals[i].GeneSequence);
            int top20Index = i % 19;
            //Debug.Log("Top 20 index: " + top20Index);
            newGen.Add(top20[top20Index].geneSequence);
        }
        Debug.Log("Size new Gen: " + newGen.Count);
        return(newGen);
    }
예제 #13
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        System.Random r      = new System.Random();
        List <string> newGen = new List <string>();
        Individual    best   = new Individual();

        best = parentGeneration.Fittest;

        for (int i = 0; i <= parentGeneration.Individuals.Count; i++)
        {
            newGen.Add(best.GeneSequence);
            newGen.Add(parentGeneration.Individuals[r.Next(0, parentGeneration.Individuals.Count)].GeneSequence);
        }
        Debug.Log("Size new Gen: " + newGen.Count);
        return(newGen);
    }
예제 #14
0
        public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
        {
            var selectedGeneration = new GenerationDB.Generation();
            var container          = new List <String>();

            if (rand == null)
            {
                rand = new Random();
            }

            for (var i = 0; i < parentGeneration.Individuals.Count; i++)
            {
                container.Add(parentGeneration.Individuals[rand.Next(parentGeneration.Individuals.Count)].GeneSequence);
            }
            selectedGeneration.Add(container);

            return(container);
        }
예제 #15
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        List <string> best50 = new List <string>();

        parentGeneration.Individuals.Sort((i1, i2) => (int)(i1.Fitness * 100 - i2.Fitness * 100));
        foreach (Individual iva in parentGeneration.Individuals)
        {
            best50.Add(iva.GeneSequence);
        }
        best50.RemoveRange(best50.Count / 2, best50.Count / 2);
        System.Random rand = new System.Random();
        List <string> ret  = new List <string>();

        for (int i = 0; i < parentGeneration.Individuals.Count * 2; i++)
        {
            ret.Add(best50[rand.Next(best50.Count)]);
        }
        return(ret);
    }
예제 #16
0
        public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
        {
            var selectedGeneration = new GenerationDB.Generation();
            //  var container = new List<String>(selectedGeneration.Individuals.Count * 2);
            var container = new List <String>();


            var car = new CarState();

            foreach (var parent in parentGeneration.Individuals)
            {
                if (parent.Fitness >= fitness.DetermineFitness(car))
                {
                    container.Add(parent.GeneSequence);
                }
                selectedGeneration.Add(container);
            }
            return(container);
        }
예제 #17
0
        /// <summary>
        /// Ist das hier nur die Liste, die an den Recombinder übergeben wird, oder ist das die komplette neue generation
        /// Add
        /// </summary>
        /// <param name="parentGeneration"></param>
        /// <returns></returns>
        public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
        {
            if (rand == null)
            {
                rand = new Random();
            }
            if (genes == null)
            {
                genes = new List <char>();
                genes.Add('A');
                genes.Add('B');
                genes.Add('C');
                genes.Add('D');
                genes.Add('E');
            }

            parentGeneration.Sort();
            List <string> superAwesomeNewGen = new List <string>();
            int           max   = parentGeneration.Individuals.Count;
            int           cut   = (int)(max * 0.60);
            int           delta = max - cut;

            int count = 0;

            for (int i = 0; i < cut; i++)
            {
                Individual a = parentGeneration.individuals[i];
                superAwesomeNewGen.Add(a.GeneSequence);
                Individual b = parentGeneration.Individuals[(i + 1) % parentGeneration.Individuals.Count];
                superAwesomeNewGen.Add(b.GeneSequence);
            }
            for (int i = 0; i < delta; i++)
            {
                int        len = parentGeneration.Individuals[0].geneSequence.Length;
                Individual a   = GenRandInd(len);
                Individual b   = GenRandInd(len);
                superAwesomeNewGen.Add(a.GeneSequence);
                superAwesomeNewGen.Add(b.GeneSequence);
            }
            return(superAwesomeNewGen);
        }
예제 #18
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        System.Random r      = new System.Random();
        List <string> newGen = new List <string>();

        for (int i = 0; i < parentGeneration.Individuals.Count * 2; i++)
        {
            Individual individual1 = parentGeneration.individuals[r.Next(0, 199)];
            Individual individual2 = parentGeneration.individuals[r.Next(0, 199)];

            if (individual1.Fitness > individual2.Fitness)
            {
                newGen.Add(individual1.geneSequence);
            }
            else
            {
                newGen.Add((individual2.geneSequence));
            }
        }
        Debug.Log("Size new Gen: " + newGen.Count);
        return(newGen);
    }
예제 #19
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        if (rand == null)
        {
            rand = new Random();
        }

        int           count         = parentGeneration.Individuals.Count / 2;
        List <string> newGeneration = new List <string>();

        while (count != 0)
        {
            int lastIndex        = parentGeneration.Individuals.Count;
            var firstIndividual  = parentGeneration.Individuals[rand.Next(0, lastIndex)];
            var secondIndividual = parentGeneration.Individuals[rand.Next(0, lastIndex)];

            if (firstIndividual.Fitness > secondIndividual.Fitness)
            {
                parentGeneration.Individuals.Remove(secondIndividual);
            }
            else
            {
                parentGeneration.Individuals.Remove(firstIndividual);
            }

            count--;
        }



        parentGeneration.Individuals.ForEach(x => newGeneration.Add(x.GeneSequence));
        parentGeneration.Individuals.ForEach(x => newGeneration.Add(x.GeneSequence));
        parentGeneration.Individuals.ForEach(x => newGeneration.Add(x.GeneSequence));
        parentGeneration.Individuals.ForEach(x => newGeneration.Add(x.GeneSequence));

        return(newGeneration);
    }
예제 #20
0
    public List <string> SelectFromGeneration(GenerationDB.Generation parentGeneration)
    {
        System.Random     r      = new System.Random();
        List <string>     newGen = new List <string>();
        List <Individual> top30  = parentGeneration.Top30;
        var individualsCount     = parentGeneration.Individuals.Count;

        for (int i = 0; i < individualsCount * 2; i++)
        {
            int top30Index = i % 30;
            if (top30Index < 25)
            {
                newGen.Add(top30[top30Index].geneSequence);
            }
            else
            {
//                Debug.Log("top30Index: " + (30 - top30Index));
                newGen.Add(parentGeneration.Individuals[individualsCount - (30 - top30Index)].geneSequence);
            }
        }

        Debug.Log("Size new Gen: " + newGen.Count);
        return(newGen);
    }
예제 #21
0
 public bool JudgementDay(GenerationDB.Generation generation)
 {
     return(Math.Abs(generation.Fittest.Fitness) <= 0.2);
 }
예제 #22
0
    public bool JudgementDay(GenerationDB.Generation generation)
    {
        return(true);
//        throw new NotImplementedException();
    }