Esempio n. 1
0
        public static Sudoku Eval(Sudoku sudoku, int populationSize, double fitnessThreshold, int generationNb)
        {
            //creation du chromosome
            IChromosome chromosome = new SudokuPermutationsChromosome(sudoku);
            var         fitness    = new SudokuFitness(sudoku);

            var selection = new EliteSelection();
            var crossover = new UniformCrossover();
            var mutation  = new UniformMutation();

            var population = new Population(populationSize, populationSize, chromosome);
            var ga         = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
            {
                Termination = new OrTermination(new ITermination[]
                {
                    new FitnessThresholdTermination(fitnessThreshold),
                    new GenerationNumberTermination(generationNb)
                })
            };

            ga.Start();

            var bestIndividual = ((ISudokuChromosome)ga.Population.BestChromosome);
            var solutions      = bestIndividual.GetSudokus();

            return(solutions[0]);
        }
Esempio n. 2
0
        public static Sudoku Eval(Sudoku sudoku, int populationSize, double fitnessThreshold, int generationNb)
        {
            //creation du chromosome
            IChromosome chromosome = new SudokuPermutationsChromosome(sudoku);

            //variable qui indique l'erreure
            var fitness = new SudokuFitness(sudoku);

            //choix de la selection : ici elite
            var selection = new EliteSelection();
            //var selection = new RouletteWheelSelection();
            //var selection = new SelectionException();
            //var selection = new TournamentSelection();

            //Choix du crossover : ici uniform
            var crossover = new UniformCrossover();

            //choix de la mutation : ici uniform
            var mutation = new UniformMutation();

            //creation de la population
            var population = new Population(populationSize, populationSize, chromosome);

            //création de l'algo génétique
            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
            {
                //pour mettre fin a l'exec si on atteint le threshold ou le nombre de rep fixé
                Termination = new OrTermination(new ITermination[]
                {
                    new FitnessThresholdTermination(fitnessThreshold),
                    new GenerationNumberTermination(generationNb)
                })
            };

            ga.Start();

            //recupération de la meilleure solution
            var bestIndividual = ((ISudokuChromosome)ga.Population.BestChromosome);
            var solutions      = bestIndividual.GetSudokus();

            return(solutions[0]);
        }
        public override IChromosome CreateNew()
        {
            var toReturn = new SudokuPermutationsChromosome(TargetSudoku);

            return(toReturn);
        }