public AverageCounter RunAlgorythmWithCounterCondition()
        {
            int counter = 0;

            AverageCounter averageCounter = new AverageCounter();

            while (GENRATION_NUMBERS_STOP_CONDITION > counter)
            {
                CreateNextPopulationCircle();

                averageCounter.SaveData(counter++, BestIndividual.Cost, CountAverageCost(), FindWorstCost());
            }

            return(averageCounter);
        }
Beispiel #2
0
        private void buttonGA_Click(object sender, EventArgs e)
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();
            #region Create and add name of colums to excel
            ExcelWorker excel = new ExcelWorker("GA alg");
            excel.AddStringCellToWorksheetIntoColumnsABCD("Generation number", "Best individual cost", "Average cost", "Worst cost");
            #endregion

            List <AverageCounter> populationDataList = new List <AverageCounter>();

            for (int i = 0; i < POPULATIONS_NUMBER; i++)
            {
                Population population = new Population(StaticMatrixObject.Dimension);
                populationDataList.Add(population.RunAlgorythmWithCounterCondition());
            }

            AverageCounter allPopulationsAverage = new AverageCounter();
            int            populationSize        = populationDataList[0].generationCounterList.Count;
            for (int i = 0; i < populationSize; i++)
            {
                double generationCounterList  = SumElementsAndCountAverageOfAverageGenerationCounterList(populationDataList, i);
                double bestIndividualCostList = SumElementsAndCountAverageOfBest(populationDataList, i);
                double averageCostList        = SumElementsAndCountAverageOfAverage(populationDataList, i);
                double worstCostList          = SumElementsAndCountAverageOfAverageWorst(populationDataList, i);

                allPopulationsAverage.SaveData(generationCounterList, bestIndividualCostList, averageCostList, worstCostList);
            }

            for (int i = 0; i < populationSize; i++)
            {
                excel.AddCellToWorksheetIntoColumnsABCD <double>(
                    allPopulationsAverage.generationCounterList[i],
                    allPopulationsAverage.bestIndividualCostList[i],
                    allPopulationsAverage.averageCostList[i],
                    allPopulationsAverage.worstCostList[i]
                    );
            }

            #region Standard count
            ExcelWorker excelStandardDeviations = new ExcelWorker("GA alg standard deviations");
            excelStandardDeviations.AddStringCellToWorksheetIntoColumnsABCD("Generation number", "Best individual cost", "Average cost", "Worst cost");

            double best, average, worst;

            for (int q = 0; q < POPULATIONS_NUMBER; q++)
            {
                double[] allPopulationVariablesBest = new double[POPULATIONS_NUMBER];
                for (int w = 0; w < populationDataList.Count; w++)
                {
                    allPopulationVariablesBest[w] = populationDataList[w].bestIndividualCostList[q];
                }
                best = StandardDeviationCounter.CountStandardDeviation(allPopulationsAverage.bestIndividualCostList[q], allPopulationVariablesBest);

                double[] allPopulationVariablesAverage = new double[POPULATIONS_NUMBER];
                for (int w = 0; w < populationDataList.Count; w++)
                {
                    allPopulationVariablesAverage[w] = populationDataList[w].averageCostList[q];
                }
                average = StandardDeviationCounter.CountStandardDeviation(allPopulationsAverage.bestIndividualCostList[q], allPopulationVariablesAverage);

                double[] allPopulationVariablesWorst = new double[POPULATIONS_NUMBER];
                for (int w = 0; w < populationDataList.Count; w++)
                {
                    allPopulationVariablesWorst[w] = populationDataList[w].worstCostList[q];
                }
                worst = StandardDeviationCounter.CountStandardDeviation(allPopulationsAverage.bestIndividualCostList[q], allPopulationVariablesWorst);

                excelStandardDeviations.AddCellToWorksheetIntoColumnsABCD(q, best, average, worst);
            }

            #endregion
            timer.Stop();
            textBoxTime.Text = $"Czas = {timer.ElapsedMilliseconds}ms.";
        }