Some version
This commit is contained in:
@@ -4,6 +4,13 @@ import java.util.List;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class Main {
|
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 to store the population of chromosomes
|
||||||
List<Chromosome> population = new ArrayList<>();
|
List<Chromosome> population = new ArrayList<>();
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
@@ -39,10 +46,26 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Main mainInstance = new Main(); // Create instance of Main class
|
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;
|
Chromosome bestSolution = null;
|
||||||
|
|
||||||
int generation = 0; // Track the number of generations
|
int generation = 0; // Track the number of generations
|
||||||
@@ -53,14 +76,14 @@ public class Main {
|
|||||||
List<Chromosome> newPopulation = new ArrayList<>();
|
List<Chromosome> newPopulation = new ArrayList<>();
|
||||||
for (int i = 0; i < mainInstance.population.size() / 2; i++) {
|
for (int i = 0; i < mainInstance.population.size() / 2; i++) {
|
||||||
// Select parents using tournament selection
|
// Select parents using tournament selection
|
||||||
List<Chromosome> parents = mainInstance.tournamentSelection(5);
|
List<Chromosome> parents = mainInstance.tournamentSelection(TOURNAMENTSIZE);
|
||||||
// Perform crossover to create two children from the selected parents
|
// Perform crossover to create two children from the selected parents
|
||||||
Chromosome child1 = mainInstance.crossoverBySubgrids(parents.get(0), parents.get(1));
|
Chromosome child1 = mainInstance.crossoverBySubgrids(parents.get(0), parents.get(1));
|
||||||
Chromosome child2 = mainInstance.crossoverBySubgrids(parents.get(1), parents.get(0));
|
Chromosome child2 = mainInstance.crossoverBySubgrids(parents.get(1), parents.get(0));
|
||||||
|
|
||||||
// Apply mutation to both children
|
// Apply mutation to both children
|
||||||
mainInstance.mutateChromosome(child1, mutationRate);
|
mainInstance.mutateChromosome(child1, MUTATIONRATE);
|
||||||
mainInstance.mutateChromosome(child2, mutationRate);
|
mainInstance.mutateChromosome(child2, MUTATIONRATE);
|
||||||
|
|
||||||
// Add both children to the new population
|
// Add both children to the new population
|
||||||
newPopulation.add(child1);
|
newPopulation.add(child1);
|
||||||
|
|||||||
@@ -7,12 +7,3 @@
|
|||||||
8 - - 1 - 4 - - 9
|
8 - - 1 - 4 - - 9
|
||||||
- - 1 9 - 3 6 - -
|
- - 1 9 - 3 6 - -
|
||||||
- 4 - - - - - 2 -
|
- 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
|
|
||||||
Reference in New Issue
Block a user