Exemple #1
0
            public static async Task <IConfiguration> TestConfigurations(IConfiguration first_configuration, IConfiguration second_configuration, int number_of_measurements = 30, int evaluation_duration = 400, int seed_limit = 100)
            {
                if (number_of_measurements > seed_limit)
                {
                    throw new Exception("Error: El número de mediciones no puede superar a la semilla límite.");
                }

                var random   = new Random();
                var seeds    = Enumerable.Range(1, seed_limit).OrderBy(num => random.Next()).Take(number_of_measurements).ToList();
                var u_values = new List <U_Value>();

                Table = new DynamicTable(Console.CursorTop, "U de Mann Whitney", new string[] { "Medición", first_configuration.ToString(), second_configuration.ToString() }, seeds.Select((seed, index) => new string[] { (index + 1).ToString(), "-", "-" }).ToArray());
                Table.Display("Nivel de significancia α: 5%");

                var get_measurements_tasks = new List <Task <List <U_Value> > > {
                    Task.Run(() => GetMeasurements(first_configuration, 1, evaluation_duration, seeds)),
                    Task.Run(() => GetMeasurements(second_configuration, 2, evaluation_duration, seeds))
                };
                await Task.WhenAll(get_measurements_tasks);

                get_measurements_tasks.Select(task => task.Result).ToList().ForEach(measurements => u_values.AddRange(measurements));

                u_values = u_values.OrderBy(u_value => u_value.value).ToList();
                for (int position = 0; position < u_values.Count;)
                {
                    u_values[position].range = ++position;
                }

                var n1 = number_of_measurements;
                var n2 = number_of_measurements;
                var R1 = TotalRange(u_values, first_configuration);
                var R2 = TotalRange(u_values, second_configuration);
                var U1 = Get_U_Statistic(n1, n2, R1);
                var U2 = Get_U_Statistic(n2, n1, R2);
                var U  = Math.Min(U1, U2);
                var Z  = Math.Round(Math.Abs((U - (float)(n1 * n2) / 2) / Math.Sqrt((float)(n1 * n2 * (n1 + n2 + 1)) / 12)), 2);

                Table.Display($"Estadístico Z: {Z} {( Z > 1.96 ? ">" : "<=" )} 1.96 | α: 5%\n");
                if (Z > 1.96)
                {
                    return(R1 <= R2 ? first_configuration : second_configuration);
                }
                return(default);
        //Segmenta el hiperespacio alrededor de un punto central de forma hipercúbica para luego procesar concurrentemente cada cluster mediante Black Hole,
        //con lo que se obtiene una configuración candidata por cada hipercubo. Finalmente se realiza un torneo asistido por Stateless Q-learning para determinar
        //cuál de las configuraciones candidatas es mejor considerando los resultados y desviaciones estandar obtenidas tras una serie de evaluaciones por ronda.
        public static async Task <ConfigurationType> Run <ConfigurationType>(ConfigurationType middle_configuration, float hypercube_length, int factor = 1, int depth = 1, int inner_depth = 1, int evaluation_duration = 200, int number_of_epochs = 10, int iterations_by_round = 100, int duration_step_between_rounds = 250, int number_of_performances_to_average = 2, int verification_duration = 1000) where ConfigurationType : IConfiguration
        {
            var candidate_solutions = await Run_Hypercubic_Black_Hole(middle_configuration, hypercube_length, factor, depth, inner_depth, evaluation_duration, number_of_epochs);

            Console.WriteLine($"\nSe han encontrado las siguientes {candidate_solutions.Count} soluciones candidatas luego de aplicar HyperCubic Black Hole:");
            for (int index = 0; index < candidate_solutions.Count; index++)
            {
                Console.WriteLine($"{index + 1}. {candidate_solutions[index]}");
            }
            Console.WriteLine("\nA continuación se determinará cuál de estas soluciones es mejor mediante un torneo asistido por Stateless Q Learning.\n");
            candidate_solutions = candidate_solutions.Distinct().ToList();
            var best_configuration = Run_Stateless_Q_Learning(candidate_solutions.ToArray(), iterations_by_round, duration_step_between_rounds, number_of_performances_to_average);
            var Table = new DynamicTable(Console.CursorTop, "Comprobación de resultado", new string[] { "Configuración", "IGD" }, candidate_solutions.Select(solution => new string[] { solution.ToString(), "─" }).ToArray());

            Table.Display($"Generación final: {verification_duration}");
            for (int index = 0; index < candidate_solutions.Count; index++)
            {
                Table.Update(decimal.Parse(candidate_solutions[index].Evaluate(verification_duration).ToString(), NumberStyles.Any, CultureInfo.InvariantCulture).ToString(), index, 1);
            }
            return(best_configuration);
        }
        //Genera una red hipercúbica alrededor de la configuración vectorizada central, para luego aplicar el black hole concurrentemente en cada hipercubo.
        public static async Task <List <ConfigurationType> > Run <ConfigurationType>(ConfigurationType middle_configuration, float length, int factor, int depth, int inner_depth, int evaluation_duration, int number_of_epochs) where ConfigurationType : IConfiguration
        {
            if (middle_configuration == null)
            {
                throw new ArgumentNullException("<middle_configuration> no puede ser nulo.");
            }

            var path_to_folder_container = @"C:\Users\Trifenix\Desktop\Tesis_Learnheuristics\HyperSpace";

            if (Directory.Exists(path_to_folder_container))
            {
                Directory.Delete(path_to_folder_container, true);
            }

            var HyperSpace = HyperCube.CreateHyperCubeNetwork(middle_configuration.Vectorized_Configuration, length, factor, depth, inner_depth);

            Console.WriteLine($"A continuación se procesarán los {HyperSpace.Count} hipercubos concurrentemente.\n");
            Table = new DynamicTable(Console.CursorTop, "Progreso", new string[] { "HiperCubo", "Época" }, HyperSpace.Select((hypercube, index) => new string[] { (index + 1).ToString(), "0" }).ToArray(), $"Época final: {number_of_epochs}");
            Table.Display();

            ProblemName = middle_configuration.ProblemName;

            var black_holes_hypercubes_task = new List <Task <ConfigurationType> >();

            for (int i = 0; i < HyperSpace.Count; i++)
            {
                var index     = i;
                var hypercube = HyperSpace[index];
                var task      = Task.Run(() => ProcessHyperCube <ConfigurationType>(hypercube, $"HyperCube_{++index}", evaluation_duration, number_of_epochs));
                black_holes_hypercubes_task.Add(task);
            }
            // Esperando por las tareas (Un black hole por hipercubo).
            await Task.WhenAll(black_holes_hypercubes_task);

            var candidate_solutions = new List <ConfigurationType>();

            black_holes_hypercubes_task.Select(task => task.Result).Where(result => result != null).ToList().ForEach(candidate_solution => candidate_solutions.Add(candidate_solution));
            return(candidate_solutions);
        }