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(); }
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)); } }