Пример #1
0
        static void Algorithm(int itemsAmount, int dimensions, double maxCost, double[] restrictions, double[] costs, double[,] itemsSet)//version w/o restarts.
        {
            int                ConfigsAmount   = 10;
            double             mutationPercent = 0.20;//FROM 0 TO 1
            GeneticalAlgorithm ga = new GeneticalAlgorithm(itemsAmount, dimensions, restrictions, costs, itemsSet, ConfigsAmount, GeneticalAlgorithm.TwoPointCrossover, GeneticalAlgorithm.SinglePointMutation, mutationPercent);
            int                iterationNumber = 0;

            while (ga.GetAbsoluteMaximalKnapsackCost() != maxCost)
            {
                //var watch = new Stopwatch();
                //watch.Start();
                ga.MakeIteration();
                iterationNumber++;
                if (iterationNumber % 10000 == 0)
                {
                    Console.WriteLine(iterationNumber + ") delta with avg is " + (maxCost - ga.GetAbsoluteAverageKnapsackCost()) + "\n delta with max is " + (maxCost - ga.GetAbsoluteMaximalKnapsackCost()));
                    var bestCosts = ga.GetBestConfigsCosts();
                    Console.WriteLine("Top 3 of the best configs pool are {0}, {1}, {2}",
                                      (maxCost - bestCosts[0]),
                                      (maxCost - bestCosts[1]),
                                      (maxCost - bestCosts[2]));
                }
                //  watch.Stop();
            }
            Console.WriteLine("Finished in {0}", iterationNumber);
            Console.ReadKey();
        }
Пример #2
0
        static void Algorithm(int itemsAmount, int dimensions, double maxCost, double[] restrictions, double[] costs, double[,] itemsSet, int testNumber)
        {
            int                ConfigsAmount   = 8;
            double             mutationPercent = 0.75;
            GeneticalAlgorithm ga = new GeneticalAlgorithm(itemsAmount, dimensions, restrictions, costs, itemsSet, ConfigsAmount, GeneticalAlgorithm.BitByBitCrossover, GeneticalAlgorithm.MutateHalf, mutationPercent);
            int                iterationNumber = 1;
            var                controlWatch    = new Stopwatch();

            controlWatch.Start();
            while (ga.GetAbsoluteMaximalKnapsackCost() != maxCost)
            {
                ga.MakeIteration();
                iterationNumber++;
            }
            controlWatch.Stop();
            using (StreamWriter file1 = new StreamWriter(@"C:\Users\black_000\Documents\visual studio 2015\Projects\ConsoleKnapsack\ConsoleKnapsack\out.txt", true))
                file1.WriteLine(iterationNumber + " iterations, " + controlWatch.ElapsedMilliseconds + " ms");
            //averageTime[testNumber].Add(controlWatch.ElapsedMilliseconds);
            //averageIterations[testNumber].Add(iterationNumber);
        }