コード例 #1
0
        public CoreText <T> Crossover(CoreText <T> otherParent)
        {
            CoreText <T> child = new CoreText <T>(TxtGen.Length, random, getRandomTextGen, AjusteFuncion, shouldInitGenes: false);

            for (int i = 0; i < TxtGen.Length; i++)
            {
                child.TxtGen[i] = random.NextDouble() < 0.5 ? TxtGen[i] : otherParent.TxtGen[i];
            }

            return(child);
        }
コード例 #2
0
 private int CompareCore(CoreText <T> a, CoreText <T> b)
 {
     if (a.Fitness > b.Fitness)
     {
         return(-1);
     }
     else if (a.Fitness < b.Fitness)
     {
         return(1);
     }
     else
     {
         return(0);
     }
 }
コード例 #3
0
        public void NuevaGeneracion(int numNuevoCore = 0, bool crossoverNewCore = false)
        {
            int finalCount = TextPop.Count + numNuevoCore;

            if (finalCount <= 0)
            {
                return;
            }

            if (TextPop.Count > 0)
            {
                CalcularAjuste();
                TextPop.Sort(CompareCore);
            }
            nuevoTextoPop.Clear();

            for (int i = 0; i < TextPop.Count; i++)
            {
                if (i < Optimizado && i < TextPop.Count)
                {
                    nuevoTextoPop.Add(TextPop[i]);
                }
                else if (i < TextPop.Count || crossoverNewCore)
                {
                    CoreText <T> par1 = SelectPar();
                    CoreText <T> par2 = SelectPar();

                    CoreText <T> child = par1.Crossover(par2);

                    child.Mutacion(MutationRate);

                    nuevoTextoPop.Add(child);
                }
                else
                {
                    nuevoTextoPop.Add(new CoreText <T>(CoreSize, random, getRandomGene, FuncAjuste, shouldInitGenes: true));
                }
            }

            List <CoreText <T> > tmpList = TextPop;

            TextPop       = nuevoTextoPop;
            nuevoTextoPop = tmpList;

            Generacion++;
        }
コード例 #4
0
        private void CalcularAjuste()
        {
            SumaAjustados = 0;
            CoreText <T> best = TextPop[0];

            for (int i = 0; i < TextPop.Count; i++)
            {
                SumaAjustados += TextPop[i].CalcularAjuste(i);

                if (TextPop[i].Fitness > best.Fitness)
                {
                    best = TextPop[i];
                }
            }

            MejorAjuste = best.Fitness;
            best.TxtGen.CopyTo(MejorTextCore, 0);
        }
コード例 #5
0
        static private float FuncAjuste(int index)
        {
            float           score = 0;
            CoreText <char> dna   = aEvolu.TextPop[index];

            for (int i = 0; i < dna.TxtGen.Length; i++)
            {
                if (dna.TxtGen[i] == TextoComparacion[i])
                {
                    score += 1;
                }
            }

            score /= TextoComparacion.Length;

            score = ((float)Math.Pow(2, score) - 1) / (2 - 1);

            return(score);
        }