Пример #1
0
 public MainWindow()
 {
     InitializeComponent();
     RandomNumberMain = new RandomNumber();
     ListaOsobnikow   = new List <DataVariablesToList>();
     ParyOsobnikow    = new List <Crossover>();
     Krzyzowanie      = new Crossover();
     header           = new DataVariablesToList(0.75);
     LoadList();
 }
Пример #2
0
 public MainAlgorithm(double crossoverProbability, double mutationProbability, int populationSize,
                      int generationCount, FirstGeneration firstGeneration, FitnessValue fitnessValue, Crossover crossover,
                      Mutation mutation)
 {
     CrossoverProbability = crossoverProbability;
     MutationProbability  = mutationProbability;
     PopulationSize       = populationSize;
     GenerationCount      = generationCount;
     _firstGeneration     = firstGeneration;
     _fitnessValue        = fitnessValue;
     _crossover           = crossover;
     _mutation            = mutation;
 }
        public SimpleGeneticAlgorithm(
            ISelectionAlgorithm selection,
            ICrossoverAlgorithm crossover,
            IMutationAlgorithm mutation,
            IFitnessFunction fitnessFunction)
        {
            Selection       = selection;
            Crossover       = crossover;
            Mutation        = mutation;
            FitnessFunction = fitnessFunction;

            _crossover = new Crossover(crossover);
            _mutation  = new Mutation(mutation);
        }
Пример #4
0
        public Genome(DNA[] dnaArray, Mutant mutant, Crossover crossover)
        {
            this.mutant    = mutant;
            this.crossover = crossover;

            this.dnas = new DNA[0];
            Array.Resize <DNA>(ref this.dnas, dnaArray.Length);

            int i = 0;

            foreach (var dnaOrg in dnaArray)
            {
                this.dnas[i] = dnaOrg.Clone();
                ++i;
            }
        }
Пример #5
0
        public void NewGeneration()
        {
            int finalCount = Settings.PopulationSize + Settings.NumNewDNA;
            List <Chromosome> newPopulation = new List <Chromosome>();

            if (finalCount <= 0)
            {
                return;
            }

            if (Population.Count > 0)
            {
                CalculateAllFitness();
                Population = Population.OrderBy(chrmsm => chrmsm.Fitness).ToList();
            }

            for (int i = 0; i < finalCount; i++)
            {
                // Keep only top individuals of the previous generation
                if (i < Elitism && i < Settings.PopulationSize)
                {
                    newPopulation.Add(Population[i]);
                    //Console.WriteLine("Generation: " + Generation);
                    //Console.WriteLine("/n");
                    //Console.WriteLine("genes: [{0}]", string.Join(",\t", Population[i].genes));
                    //Console.WriteLine(Population[i].fitness);
                }
                else if (i < Settings.PopulationSize)
                {
                    Chromosome parent1 = Selection.PerformSelection(Population);
                    Chromosome parent2 = Selection.PerformSelection(Population);
                    Chromosome child   = Crossover.PerformCrossover(parent1, parent2);
                    child = Mutation.PerformMutation(child);
                    newPopulation.Add(child);
                }
                else if (Settings.EnableCrossoverNewDNA)
                {
                    newPopulation.Add(new Chromosome(shouldInitGenes: true));
                }
            }

            Population = newPopulation;
            Generation++;
        }
Пример #6
0
        public void Epoch()
        {
            CalcFitness();

            var newpopulations = new List <IGenome>();

            Population = Population.OrderBy(o => o.Fitness).ToList();

            for (int j = 0; j < GASettings.BestSolutionToPick; j++)
            {
                Population[j].Fitness = Fitness.Calc(Population[j]);
                Population[j].Score   = 0;
                newpopulations.Add(Population[j]);
            }

            int ran = Rand.Next(1, Population.Count);

            while (newpopulations.Count < Population.Count)
            {
                // Selection
                var nodemom = Selection.Select(Population);
                var nodedad = Selection.Select(Population);
                // CrossOver
                var cross = Crossover.Calc(new CrossoverOperation(nodemom, nodedad));
                //// Mutation
                nodemom = Mutate.Calc(cross.Mom);
                nodedad = Mutate.Calc(cross.Dad);
                // Adaptation
                nodemom = Adaptation.Calc(nodemom);
                nodedad = Adaptation.Calc(nodedad);

                nodemom.Fitness = Fitness.Calc(nodemom);
                nodedad.Fitness = Fitness.Calc(nodedad);

                // Add in new population
                newpopulations.Add(nodemom);
                newpopulations.Add(nodedad);
            }
            Population = null;
            Population = newpopulations.ToList();

            Generation++;
        }
Пример #7
0
        public virtual void PerformIteration()
        {
            foreach (var chromosome in Population)
            {
                chromosome.FittnessValue = FittnessFunction.Calculate(chromosome);
            }

            var selection = Selection.Select(Population);
            var crossover = Crossover.Perform(selection);

            foreach (var chromosome in crossover)
            {
                Mutation.Mutate(chromosome);
            }

            Population.Remove(Population.FirstOrDefault(
                                  x => x.FittnessValue == Population.Min(y => y.FittnessValue)));
            Population.Remove(Population.FirstOrDefault(
                                  x => x.FittnessValue == Population.Min(y => y.FittnessValue)));

            Population.AddRange(crossover);
        }
Пример #8
0
        public GeneticAlgorithm(Selection <T> selectionMethod, Crossover <T> crossoverMethod, Mutation <T> mutationMethod, FitnessFunction <T> fitnessFunction, CreateNewSolution <T> createNewSolution, AcceptResult <T> acceptResult, int population, double mutationProbability, int selectionCount, int eliteCount = 0)
        {
            if (population < 2)
            {
                throw new Exception("Population must be greater or equal 2.");
            }

            if (eliteCount >= population)
            {
                throw new Exception("EliteCount must be less than population.");
            }

            selection      = selectionMethod;
            crossover      = crossoverMethod;
            mutation       = mutationMethod;
            fitness        = fitnessFunction;
            createSolution = createNewSolution;
            accept         = acceptResult;

            maxPopulation            = population;
            this.mutationProbability = mutationProbability;
            this.selectionCount      = selectionCount;
            this.eliteCount          = eliteCount;
        }
Пример #9
0
 public void NextGeneration()
 {
     Selection.SelectPopulation();
     Crossover.CrossPopulation();
     Mutation.MutatePopulation();
 }