diff --git a/Main.java b/Main.java index 3b0b627..e1c38d8 100644 --- a/Main.java +++ b/Main.java @@ -4,6 +4,13 @@ import java.util.List; import java.util.Random; public class Main { + // Initalizing variables + public static int POPULATIONSIZE = 0; + public static int TOURNAMENTSIZE = 0; + public static double MUTATIONRATE = 0; + // Threshold of number of mutable positions for easy level sudoku + public static int EASYTHRESHOLD = 60; + public static int HARDTHRESHOLD = 70; // List to store the population of chromosomes List population = new ArrayList<>(); Random random = new Random(); @@ -39,10 +46,26 @@ public class Main { } Main mainInstance = new Main(); // Create instance of Main class - // Generate initial population of 100 chromosomes - mainInstance.generateInitialChromosomes(100, baseSudoku, mutablePositions); - double mutationRate = 0.05; // Mutation rate for genetic algorithm + // Choosing variables for different sudoku difficulties + if (mutablePositions.size() < EASYTHRESHOLD) { // Easy sudoku + POPULATIONSIZE = 75; + TOURNAMENTSIZE = 4; + MUTATIONRATE = 0.04; + } else if (mutablePositions.size() < HARDTHRESHOLD) { // Hard sudoku + POPULATIONSIZE = 150; + TOURNAMENTSIZE = 5; + MUTATIONRATE = 0.055; + } + else { // Ultra-hard sudoku + POPULATIONSIZE = 2000; + TOURNAMENTSIZE = 10; + MUTATIONRATE = 0.2; + } + + // Generate initial population of 100 chromosomes + mainInstance.generateInitialChromosomes(POPULATIONSIZE, baseSudoku, mutablePositions); + Chromosome bestSolution = null; int generation = 0; // Track the number of generations @@ -53,14 +76,14 @@ public class Main { List newPopulation = new ArrayList<>(); for (int i = 0; i < mainInstance.population.size() / 2; i++) { // Select parents using tournament selection - List parents = mainInstance.tournamentSelection(5); + List parents = mainInstance.tournamentSelection(TOURNAMENTSIZE); // Perform crossover to create two children from the selected parents Chromosome child1 = mainInstance.crossoverBySubgrids(parents.get(0), parents.get(1)); Chromosome child2 = mainInstance.crossoverBySubgrids(parents.get(1), parents.get(0)); // Apply mutation to both children - mainInstance.mutateChromosome(child1, mutationRate); - mainInstance.mutateChromosome(child2, mutationRate); + mainInstance.mutateChromosome(child1, MUTATIONRATE); + mainInstance.mutateChromosome(child2, MUTATIONRATE); // Add both children to the new population newPopulation.add(child1); @@ -340,4 +363,4 @@ public class Main { } } } -} \ No newline at end of file +} diff --git a/input.txt b/input.txt index 2992fbc..5ff6b13 100644 --- a/input.txt +++ b/input.txt @@ -7,12 +7,3 @@ 8 - - 1 - 4 - - 9 - - 1 9 - 3 6 - - - 4 - - - - - 2 - -1 8 5 4 3 6 2 9 7 -4 3 7 5 9 2 8 1 6 -6 9 2 8 1 7 3 4 5 -3 7 6 2 8 9 4 5 1 -2 1 4 3 7 5 9 6 8 -9 5 8 6 4 1 7 3 2 -8 6 3 1 2 4 5 7 9 -7 2 1 9 5 3 6 8 4 -5 4 9 7 6 8 1 2 3 \ No newline at end of file