Пример #1
0
        static void Main(string[] args)
        {
            //

            GraphModel graphModel = new GraphModel("data/data_35ver.csv");


            int vezirSayisi = graphModel.vertexes.Count;

            var selection  = new EliteSelection();
            var crossover  = new TwoPointCrossover();
            var mutation   = new ReverseSequenceMutation();
            var fitness    = new MyProblemFitness(graphModel);
            var chromosome = new MyProblemChromosome(vezirSayisi);
            var population = new Population(5000, 5500, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
            {
                //Termination = new FitnessThresholdTermination(vezirSayisi * (vezirSayisi - 1) / 2)
                Termination = new FitnessThresholdTermination(100)
            };

            int index = 0;

            ga.GenerationRan += delegate {
                var bestChromosome = ga.Population.BestChromosome;

                Console.Write("Index: " + index);
                Console.Write(", Fitness: {0}", bestChromosome.Fitness);

                Console.Write(", Genes: {0}", string.Join("-", bestChromosome.GetGenes()));

                bestChromosome.GetGenes();
                Console.Write($", Ilość kolorow: {MyProblemFitness.GetCountOfColors(bestChromosome.GetGenes())}");

                Console.WriteLine();

                index++;
            };

            Console.WriteLine("GA running...");

            ga.Start();

            Console.WriteLine("Best solution found has {0} fitness.", ga.BestChromosome.Fitness);

            Console.Read();
        }
        static void Main(string[] args)
        {
            int vezirSayisi = 8;

            var selection  = new EliteSelection();
            var crossover  = new TwoPointCrossover();
            var mutation   = new ReverseSequenceMutation();
            var fitness    = new MyProblemFitness();
            var chromosome = new MyProblemChromosome(vezirSayisi);
            var population = new Population(5000, 5500, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
            {
                Termination = new FitnessThresholdTermination(vezirSayisi * (vezirSayisi - 1) / 2)
            };

            int index = 0;

            ga.GenerationRan += delegate
            {
                var bestChromosome = ga.Population.BestChromosome;

                Console.Write("Index: " + index);
                Console.Write(", Fitness: {0}", bestChromosome.Fitness);

                Console.Write(", Genes: {0}", string.Join("-", bestChromosome.GetGenes()));

                Console.WriteLine();

                index++;
            };

            Console.WriteLine("GA running...");

            ga.Start();

            Console.WriteLine("Best solution found has {0} fitness.", ga.BestChromosome.Fitness);

            Console.Read();
        }