Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var puntajealto = 0;
            var index       = 1;
            var acdenamayor = "";

            while (puntajealto < 26)
            {
                for (int x = 0; x < 50; x++)
                {
                    var lista = new List <WeightedItem <string> >();
                    for (int y = 0; y < mutacion.Length; y++)
                    {
                        WeightedItem <string> valoresEntrada = new WeightedItem <string>(y.ToString(), 0.03);
                        lista.Add(valoresEntrada);
                    }
                    var valorElegido   = Convert.ToInt32(WeightedItem <string> .Choose(lista));
                    var insertCaracter = CrearCombinacion(1);
                    index   += 1;
                    mutacion = mutacion.Remove(valorElegido, 1).Insert(valorElegido, insertCaracter);
                    var resulPuntaje = validarDatos(mutacion);
                    if (puntajealto < resulPuntaje)
                    {
                        puntajealto = resulPuntaje;
                        acdenamayor = mutacion;
                    }
                    Console.WriteLine("Generación:" + index + " - Mutación:" + mutacion + " - Puntaje:" + validarDatos(mutacion));
                }
                mutacion = acdenamayor;
            }
            Console.WriteLine("Generación:" + index + " - Mutación:" + mutacion + " - Puntaje:" + validarDatos(mutacion));
            Console.ReadKey();
        }
        public static T Choose(List <WeightedItem <T> > items)
        {
            double cumulSum = 0;
            int    cnt      = items.Count();

            for (int slot = 0; slot < cnt; slot++)
            {
                cumulSum += items[slot].weight;
                items[slot].cumulativeSum = cumulSum;
            }

            double           divSpot = rndInst.NextDouble() * cumulSum;
            WeightedItem <T> chosen  = items.FirstOrDefault(i => i.cumulativeSum >= divSpot);

            if (chosen == null)
            {
                throw new Exception("No item chosen - there seems to be a problem with the probability distribution.");
            }
            return(chosen.value);
        }