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]); }
static void Main(string[] args) { int[] initial_grid = new int[] { 0, 6, 0, 0, 5, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 9, 0, 7, 0, 0, 6, 0, 0, 0, 1, 0, 0, 0, 6, 0, 3, 0, 4, 0, 0, 0, 0, 4, 0, 7, 0, 1, 0, 0, 0, 0, 5, 0, 9, 0, 8, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 6, 0, 3, 0, 0, 0, 8, 0, 0, 0, 0, 2, 0, 0, 4, 0, 0, 5, 0 }; //declaration du chronometre Stopwatch stopwatch = new Stopwatch(); Noyau.Sudoku s = new Noyau.Sudoku(initial_grid); Noyau.Sudoku s_1 = new Noyau.Sudoku(); var fitness = new SudokuFitness(s); Console.WriteLine("Sudoku initial :"); Console.WriteLine(s.ToString()); Console.WriteLine("\n"); Console.WriteLine("******************************************************"); Console.WriteLine("\n"); Console.WriteLine("Genetic Solver"); GeneticSolver gs = new GeneticSolver(); //chrono start stopwatch.Start(); s_1 = gs.Solve(s); //chrono stop stopwatch.Stop(); Console.WriteLine(s_1.ToString()); //fonction pour evaluer si un sudoku est bon : objectif 0 Console.WriteLine("Fitness : "); Console.WriteLine(fitness.Evaluate(s_1)); //instruction durée d'exe Console.WriteLine("Durée d'exécution: {0} secondes", stopwatch.Elapsed.TotalSeconds); stopwatch.Reset(); }
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]); }