private void searchThread(int pID, int max, int seed, INeighborhood <GraphGenome> neighborhood, bool silent = false)
        {
            Console.WriteLine("Thread {0} started...", pID);

            Random random = new Random(seed);

            // TODO: Resultaten (dus CPU computational time) opslaan in bestand.
            LocalSearch <GraphGenome> local_search =
                new LocalSearch <GraphGenome>(500,
                                              neighborhood,
                                              new GraphComparer <GraphGenome>(), random);

            // Dit wil ik eigenlijk static doen, maar dan komen we in de knoei met data_size.
            GraphGenome graph = new GraphGenome(500);

            graph.CreateGraph("Graph500.txt");

            GraphGenome optimum = null;

            for (int i = 0; i < ExperimentAmount; ++i)
            {
                AssignmentTwoResults <GraphGenome> inner_results = new AssignmentTwoResults <GraphGenome>();
                for (int j = 0; j < max; j++)
                {
                    graph = new GraphGenome(500);
                    graph.Generate(ref random);
                    Stopwatch   sw            = Stopwatch.StartNew();
                    GraphGenome inner_optimum = local_search.Search(graph);
                    sw.Stop();
                    inner_results.Add(inner_optimum, sw.ElapsedTicks);

                    if (!silent)
                    {
                        Console.WriteLine("Found {0} in {1} ticks or {2}ms. ({3}/{4}), pID: {5}", inner_optimum.Fitness, sw.ElapsedTicks,
                                          sw.ElapsedMilliseconds, j, max, pID);
                    }

                    if (optimum == null || optimum.Fitness >= inner_optimum.Fitness)
                    {
                        optimum = inner_optimum;
                    }
                }
                lock (solutionsThreads)
                {
                    solutionsThreads[pID] = optimum;
                }
                lock (elapsedMilisecondsThreads)
                {
                    elapsedMilisecondsThreads[pID].Add(inner_results);
                }
            }
        }
        private AssignmentTwoResultList <GraphGenome> IteratedLocalSearch(INeighborhood <GraphGenome> neighborhood, bool silent = false)
        {
            LocalSearch <GraphGenome> local_search =
                new LocalSearch <GraphGenome>(500,
                                              neighborhood,
                                              new GraphComparer <GraphGenome>(), main_random_source);
            IMutation <GraphGenome> pertubation = new ILSPertubation <GraphGenome>(main_random_source);

            GraphGenome graph = new GraphGenome(500);

            graph.CreateGraph("Graph500.txt");

            GraphGenome optimum = null;

            List <long> elapsedMilisecondsList = new List <long>();

            AssignmentTwoResultList <GraphGenome> results = new AssignmentTwoResultList <GraphGenome>("$ILS_{" + neighborhood.Name + "}$");

            for (int i = 0; i < ExperimentAmount; ++i)
            {
                AssignmentTwoResults <GraphGenome> res = new AssignmentTwoResults <GraphGenome>();
                for (int j = 0; j < OptimaAmount; j++)
                {
                    if (optimum == null)
                    {
                        graph = new GraphGenome(500);
                        graph.Generate(ref main_random_source);
                    }
                    else
                    {
                        graph = pertubation.Mutate(optimum);
                    }

                    Stopwatch   sw            = Stopwatch.StartNew();
                    GraphGenome inner_optimum = local_search.Search(graph);
                    sw.Stop();
                    elapsedMilisecondsList.Add(sw.ElapsedMilliseconds);

                    res.Add(inner_optimum, sw.ElapsedTicks);

                    if (!silent)
                    {
                        Console.WriteLine("Found {0} in {1} ticks or {2}ms. ({3}/{4})", inner_optimum.Fitness, sw.ElapsedTicks,
                                          sw.ElapsedMilliseconds, j, OptimaAmount);
                    }

                    if (optimum == null || optimum.Fitness >= inner_optimum.Fitness)
                    {
                        optimum = inner_optimum;
                    }

                    optimum.FunctionEvaluations = 0;
                }
                results.Add(res);
            }
            //Console.WriteLine("Best solution found: {0} in an average of {1} ms", optimum.Fitness, cpu_ticks.Average());
            Console.WriteLine("Best solution found: {0} in an average of {1} ms", optimum.Fitness, elapsedMilisecondsList.Sum() / OptimaAmount);
            //Console.WriteLine("Total time: {0} sec", sw.ElapsedMilliseconds / 1000f);

            optimum.ToImage(String.Format("results/ILS[{0}]-{1}.bmp", neighborhood.Name, optimum.Fitness), 3000, 3000);

            //Console.ReadLine();
            return(results);
        }
        private AssignmentTwoResultList <GraphGenome> GeneticLocalSearch(INeighborhood <GraphGenome> neighborhood)
        {
            AssignmentTwoResultList <GraphGenome> results = new AssignmentTwoResultList <GraphGenome>("$GLS_{" + neighborhood.Name + "}$");

            GraphGenome graph = new GraphGenome(500);

            graph.CreateGraph("Graph500.txt");

            GraphGenome optimum = null;

            for (int i = 0; i < ExperimentAmount; ++i)
            {
                AssignmentTwoResults <GraphGenome> res = new AssignmentTwoResults <GraphGenome>();
                int population_size = 50;
                int data_size       = 500;

                LocalSearch <GraphGenome> local_search =
                    new LocalSearch <GraphGenome>(data_size,
                                                  neighborhood,
                                                  new GraphComparer <GraphGenome>(), main_random_source);

                LocalSearchProcreator <GraphGenome> lsp = new LocalSearchProcreator <GraphGenome>(
                    new UniformSymmetricCrossover <GraphGenome>(main_random_source),
                    local_search,
                    main_random_source,
                    res);

                GeneticAlgorithm <GraphGenome> ga = new GeneticAlgorithm <GraphGenome>(
                    data_size,
                    lsp,
                    new DefaultSelector <GraphGenome>(
                        new GraphComparer <GraphGenome>()),
                    new LocalSearchPopulationGenerator <GraphGenome>(main_random_source, local_search),
                    new Goal(100, 0),
                    main_random_source,
                    "GLS");

                InnerResult ir = ga.start(population_size, OptimaAmount / (population_size / 2));
                // Eventueel nog een keer uitvoeren om tot 2500 optima te komen.
                while (res.Count < OptimaAmount)
                {
                    ir = ga.start(population_size, OptimaAmount / (population_size / 2));
                }

                // Als we er teveel hebben gekregen door de GLS run meerdere keren uit te voeren,
                // pak enkel hoeveel we nodig hebben.
                res = res.TakeFirstN(OptimaAmount);
                results.Add(res);

                if (optimum == null || res.BestResult.Optimum.Fitness < optimum.Fitness)
                {
                    optimum = new GraphGenome(res.BestResult.Optimum.Data, res.BestResult.Optimum.Fitness);
                }
            }



            // We maken iedere keer population_size / 2 optima.
            // We willen OptimaAmount optima. Dus we gaan OptimaAmount / (population_size / 2) generaties uitvoeren.

            //IteratedLocalSearch();

            optimum.ToImage(String.Format("results/GLS[{0}]-{1}.bmp", neighborhood.Name, optimum.Fitness), 3000, 3000);

            return(results);
        }
 public LocalSearchProcreator(ICrossover <T> crossover, LocalSearch <T> _local_search, Random _random, AssignmentTwoResults <T> _results) : base(crossover, "LS")
 {
     random       = _random;
     local_search = _local_search;
     results      = _results;
 }