コード例 #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);
        }
コード例 #3
0
        static void TestAlgorithm()//First proof of concept
        {
            int itemsAmount = 500, dimensions = 6;

            double[] restrictions = new double[] { 100, 600, 1200, 2400, 500, 2000 }, costs = new double[itemsAmount];
            for (int i = 0; i < itemsAmount; i++)
            {
                costs[i] = rand.NextDouble() * 30;
            }
            double[,] itemsSet = new double[itemsAmount, dimensions];
            for (int i = 0; i < itemsAmount; i++)
            {
                for (int j = 0; j < dimensions; j++)
                {
                    itemsSet[i, j] = rand.NextDouble() * 50;
                }
            }
            int ConfigsAmount     = 6;
            GeneticalAlgorithm ga = new GeneticalAlgorithm(itemsAmount, dimensions, restrictions, costs, itemsSet, ConfigsAmount, GeneticalAlgorithm.FixedSinglePointCrossover, GeneticalAlgorithm.SinglePointMutation, 0.75);

            int iterationNumber = 0;

            while (true)
            {
                var watch = new Stopwatch();
                watch.Start();

                while (watch.ElapsedMilliseconds < 200)
                {
                    ga.MakeIteration();
                    iterationNumber++;
                    averageValuations.Enqueue(ga.GetNormaizedAveragePoolCost());
                    maxValuations.Enqueue(ga.GetNormalizedMaximalKnapsackCost());
                }
                watch.Stop();
            }
        }
コード例 #4
0
ファイル: SetPartition.cs プロジェクト: phoenixEkb/Diploma
        static List <string> algorithmWithRestart(int itemsAmount, int dimensions, double maxCost, double[] restrictions, double[] costs, double[,] itemsSet)
        {
            int                ConfigsAmount = 10, restartTime = 2 * 100000, currentMaxValueLiveLength = 0;
            double             PrevCost        = 0;
            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, endIteration = 2 * 1000000;
            List <double>      resetPoints = new List <double>();
            var                workTime    = new Stopwatch();

            workTime.Start();

            // string logFileName = "plotwithreset.txt";
            // List<string> values = new List<string>();
            while (ga.GetAbsoluteMaximalCostAllTime() != maxCost && iterationNumber < endIteration)
            {
                var watch = new Stopwatch();
                watch.Start();
                ga.MakeIteration();
                iterationNumber++;
                double tmp = ga.GetBestConfigsCosts()[0];
                //   values.Add(tmp.ToString());
                if (tmp != PrevCost)
                {
                    PrevCost = tmp;
                    currentMaxValueLiveLength = 0;
                }
                else
                {
                    currentMaxValueLiveLength++;
                }

                /*
                 * if (iterationNumber % 10000 == 0)//Отрисовка
                 * {
                 *  Console.Write(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}, {3}, {4}",
                 *      (maxCost - bestCosts[0]),
                 *      (maxCost - bestCosts[1]),
                 *      (maxCost - bestCosts[2]),
                 *      (maxCost - bestCosts[3]),
                 *      (maxCost - bestCosts[4]));
                 * }
                 */
                if (currentMaxValueLiveLength == restartTime)
                {
                    var restartPercent = 0.4;
                    resetPoints.Add(maxCost - ga.GetBestConfigsCosts()[0]);
                    ga.RestartAlgorithm(restartPercent);
                    PrevCost = 0;
                    currentMaxValueLiveLength = 0;
                    // Console.WriteLine("Restart");
                }
                watch.Stop();
            }
            workTime.Stop();
            Console.WriteLine(workTime.Elapsed.TotalSeconds.ToString());
            //Can use resetPoints for Something.
            //File.WriteAllLines(logFileName, values);
            if (ga.GetAbsoluteMaximalCostAllTime() == maxCost)//problem solved
            {
                return(transformResults(iterationNumber, workTime));
            }

            else
            {
                return(transformResults(maxCost - ga.GetAbsoluteMaximalCostAllTime(), workTime));
            }
        }