Beispiel #1
0
        public override int GeneticAlgorithm(MainGraph graph, ProblemData problemData)
        {
            algorithmInfo = new AlgorithmInfo();
            adjacencyList = AdjacencyList.GenerateList(graph);
            cost          = Cost.GanerateCostArray(graph, problemData); problem = problemData;

            var crossover = GeneticMethod.ChosenCrossoverMethod(crossoverMethod, CrossoverProbability, dotCrossover);
            var mutation  = GeneticMethod.ChosenMutationMethod(mutationMethod, MutationProbability, dotMutation);
            var selection = GeneticMethod.ChosenSelectionMethod(selectionMethod, CountTour, CountSelected);
            ReductionForClassicGA reduction = new ReductionForClassicGA();

            Chromosome bestChromosome  = null;
            Population startPopulation = new Population(PopulationSize, cost);
            var        population      = startPopulation;
            int        stepGA          = 0;

            double          MediumFitness   = Solution.MediumFitnessPopulation(population);
            RandomSelection randomSelection = new RandomSelection();

            FitnessCalculation(population.populationList);
            stopwatch = new Stopwatch();
            stopwatch.Start();
            while (stepGA < IterateSize)
            {
                List <Chromosome> midPopulation    = selection.Selection(population);
                List <Chromosome> parentPopulation = randomSelection.Selection(midPopulation, selection.indexSelectChrom);
                List <Chromosome> childList        = crossover.Crossover(parentPopulation);
                if (childList.Count == 0)
                {
                    continue;
                }
                mutation.Mutation(childList);
                FitnessCalculation(childList);


                reduction.Reduction(childList, randomSelection.indexTwoParant, population.populationList);

                double tempMediumFitness = Solution.MediumFitnessPopulation(population);
                double absFitness        = Math.Abs(tempMediumFitness - MediumFitness);
                MediumFitness = tempMediumFitness;
                if (absFitness >= 0 && absFitness <= 1)
                {
                    bestChromosome = population.BestChromosome();
                    var worstChromosome = population.WorstChromosome();
                    if (bestChromosome.fitness - worstChromosome.fitness <= 1 && bestChromosome.fitness - worstChromosome.fitness >= 0)
                    {
                        if (Solution.isAnswerTrue(bestChromosome, cost, problemData))
                        {
                            stopwatch.Stop();

                            algorithmInfo.Time   = stopwatch.Elapsed;
                            algorithmInfo.BestFx = bestChromosome.fitness;
                            algorithmInfo.Steps  = stepGA;
                            break;
                        }
                    }
                }
                stepGA++;
            }
            stopwatch.Stop();
            if (stepGA == IterateSize)
            {
                bool answer = false;
                bestChromosome = population.BestChromosome();
                while (population.populationList.Count != 0)
                {
                    if (Solution.isAnswerTrue(bestChromosome, cost, problemData))
                    {
                        algorithmInfo.Time   = stopwatch.Elapsed;
                        algorithmInfo.BestFx = bestChromosome.fitness;
                        algorithmInfo.Steps  = stepGA;
                        answer = true;
                        break;
                    }
                    else
                    {
                        population.populationList.Remove(bestChromosome);
                        if (population.populationList.Count == 0)
                        {
                            break;
                        }
                        bestChromosome = population.BestChromosome();
                    }
                }
                if (!answer)
                {
                    bestChromosome = null;
                }
            }

            return(Solution.Answer(cost, bestChromosome, problemData, graph));
        }
Beispiel #2
0
        /// <summary>
        /// Реализация генетического алгоритма "CHC".
        /// </summary>
        /// <param name="graph">Граф.</param>
        /// <param name="problemData">Параметры задачи.</param>
        public override int GeneticAlgorithm(MainGraph graph, ProblemData problemData)
        {
            algorithmInfo = new AlgorithmInfo();
            // Инициализация основных структур.
            adjacencyList = AdjacencyList.GenerateList(graph);
            cost          = Cost.GanerateCostArray(graph, problemData);
            problem       = problemData;

            Population   startPopulation = new Population(PopulationSize, cost);
            Population   population      = startPopulation;
            HUXCrossover crossover       = new HUXCrossover(population.SizeChromosome / 4, CrossoverProbability);

            Chromosome bestChromosome = null;
            int        stepGA         = 0;

            stopwatch = new Stopwatch();
            stopwatch.Start();
            while (stepGA < IterateSize)
            {
                FitnessCalculation(population.populationList);

                List <Chromosome> childList = crossover.Crossover(population.populationList);


                if (crossover.Distanse == 0)
                {
                    CatacliysmicMutation(population);
                    crossover.Distanse = population.SizeChromosome / 4;
                    stepGA++;
                    continue;
                }
                if (childList.Count == 0)
                {
                    crossover.Distanse--;
                    stepGA++;
                    continue;
                }

                FitnessCalculation(childList);

                List <Chromosome> newPopulation = EliteReductionForCHC.Reduction(population.populationList, childList, PopulationSize);

                population.populationList = newPopulation;

                stepGA++;
            }
            stopwatch.Stop();
            if (stepGA == IterateSize)
            {
                bool answer = false;
                bestChromosome = population.BestChromosome();
                while (population.populationList.Count != 0)
                {
                    if (population.populationList.Count == 0)
                    {
                        break;
                    }

                    if (Solution.isAnswerTrue(bestChromosome, cost, problemData))
                    {
                        algorithmInfo.Time   = stopwatch.Elapsed;
                        algorithmInfo.BestFx = bestChromosome.fitness;
                        algorithmInfo.Steps  = stepGA;

                        answer = true;
                        break;
                    }
                    else
                    {
                        population.populationList.Remove(bestChromosome);
                        if (population.populationList.Count == 0)
                        {
                            break;
                        }
                        bestChromosome = population.BestChromosome();
                    }
                }
                if (!answer)
                {
                    bestChromosome = null;
                }
            }



            return(Solution.Answer(cost, bestChromosome, problemData, graph));
        }
Beispiel #3
0
        /// <summary>
        /// Реализация генетического алгоритма "Genitor".
        /// </summary>
        /// <param name="graph">Граф.</param>
        /// <param name="problemData">Параметры задачи</param>
        public override int GeneticAlgorithm(MainGraph graph, ProblemData problemData)
        {
            this.algorithmInfo = new AlgorithmInfo();
            // Инициализация основных структур.
            adjacencyList = AdjacencyList.GenerateList(graph);
            cost          = Cost.GanerateCostArray(graph, problemData); ProblemData problem = problemData;

            var        crossover      = GeneticMethod.ChosenCrossoverMethod(crossoverMethod, 100, dotCrossover);
            var        mutation       = GeneticMethod.ChosenMutationMethod(mutationMethod, MutationProbability, dotMutation);
            var        selection      = GeneticMethod.ChosenSelectionMethod(selectionMethod, CountTour, CountSelected);
            Chromosome bestChromosome = null;
            double     MediumFitness  = 0;


            Population startPopulation = new Population(PopulationSize, cost);

            var population = startPopulation;

            int stepGA = 0;

            // вычесление пригодности хромосом.
            for (int i = 0; i < PopulationSize; i++)
            {
                population.populationList[i].fitness = Fitness.FunctionTrue(cost, problemData, population.populationList[i]);
            }
            RandomSelection randomSelection = new RandomSelection();

            MediumFitness = Solution.MediumFitnessPopulation(population);
            stopwatch     = new Stopwatch();
            stopwatch.Start();
            while (stepGA < IterateSize)
            {
                List <Chromosome> midPopulation    = selection.Selection(population);
                List <Chromosome> parentPopulation = randomSelection.Selection(midPopulation);
                Chromosome        child            = crossover.Crossover(parentPopulation[0], parentPopulation[1]);

                if (mutation != null)
                {
                    mutation.Mutation(child);
                }
                // вычесление ранга хромосомы.

                //population.Sort();
                Ranking2(population);
                // пригодность потомка
                child.fitness = Fitness.FunctionTrue(cost, problemData, child);
                // поиск самой худщей хромосомы
                Chromosome bedChrom = null;

                bedChrom = population.populationList.First();


                // замена худшей хромосомы на потомка
                int index = population.populationList.IndexOf(bedChrom);
                population.populationList.Insert(index, child);
                population.populationList.RemoveAt(index + 1);

                double tempMediumFitness = Solution.MediumFitnessPopulation(population);
                double absFitness        = Math.Abs(tempMediumFitness - MediumFitness);
                MediumFitness = tempMediumFitness;
                if (absFitness >= 0 && absFitness <= 1)
                {
                    bestChromosome = population.BestChromosome();
                    var worstChromosome = population.WorstChromosome();
                    if (bestChromosome.fitness - worstChromosome.fitness <= 1 && bestChromosome.fitness - worstChromosome.fitness >= 0)
                    {
                        if (Solution.isAnswerTrue(bestChromosome, cost, problemData))
                        {
                            stopwatch.Stop();

                            algorithmInfo.Time   = stopwatch.Elapsed;
                            algorithmInfo.BestFx = bestChromosome.fitness;
                            algorithmInfo.Steps  = stepGA;

                            break;
                        }
                    }
                }


                stepGA++;
            }
            stopwatch.Stop();

            if (stepGA == IterateSize)
            {
                bool answer = false;
                bestChromosome = population.BestChromosome();
                while (population.populationList.Count != 0)
                {
                    if (Solution.isAnswerTrue(bestChromosome, cost, problemData))
                    {
                        algorithmInfo.Time   = stopwatch.Elapsed;
                        algorithmInfo.BestFx = bestChromosome.fitness;
                        algorithmInfo.Steps  = stepGA;
                        answer = true;
                        break;
                    }
                    else
                    {
                        population.populationList.Remove(bestChromosome);
                        if (population.populationList.Count == 0)
                        {
                            break;
                        }
                        bestChromosome = population.BestChromosome();
                    }
                }
                if (!answer)
                {
                    bestChromosome = null;
                }
            }

            return(Solution.Answer(cost, bestChromosome, problemData, graph));
        }