Selected some arguments for new method

This commit is contained in:
emil
2024-12-01 18:04:27 +03:00
parent 39736e90fd
commit 26d454c976
6 changed files with 438 additions and 31 deletions
@@ -57,18 +57,18 @@ public class Main {
// Choosing variables for different sudoku difficulties
if (mutablePositions.size() < EASYTHRESHOLD) { // Easy sudoku
POPULATIONSIZE = 75;
TOURNAMENTSIZE = 4;
MUTATIONRATE = 0.04;
POPULATIONSIZE = 500000;
TOURNAMENTSIZE = 3;
MUTATIONRATE = 0.1;
} else if (mutablePositions.size() < HARDTHRESHOLD) { // Hard sudoku
POPULATIONSIZE = 150;
TOURNAMENTSIZE = 5;
MUTATIONRATE = 0.055;
POPULATIONSIZE = 100000;
TOURNAMENTSIZE = 8;
MUTATIONRATE = 0.34;
}
else { // Ultra-hard sudoku
POPULATIONSIZE = 20000;
TOURNAMENTSIZE = 4;
MUTATIONRATE = 0.032;
POPULATIONSIZE = 500000;
TOURNAMENTSIZE = 3;
MUTATIONRATE = 0.1;
}
// Generate initial population of chromosomes
@@ -109,7 +109,7 @@ public class Main {
generation++;
// Plot the fitness graph every 10 generations
if (generation % 100 == 0) {
if (generation % 1 == 0) {
mainInstance.plotFitness(fitnessValues);
}
@@ -126,11 +126,32 @@ public class Main {
for (int i = 0; i < numberOfChromosomes; i++) {
// Create a copy of the base Sudoku
int[][] sudoku = copyMatrix(baseSudoku);
// Randomly fill mutable positions
for (int[] pos : mutablePositions) {
int row = pos[0];
int col = pos[1];
sudoku[row][col] = random.nextInt(9) + 1;
// Randomly fill mutable positions while ensuring no duplicates in subgrids
for (int gridRow = 0; gridRow < 3; gridRow++) {
for (int gridCol = 0; gridCol < 3; gridCol++) {
boolean[] present = new boolean[10];
List<int[]> subgridPositions = new ArrayList<>();
// Collect all positions in the current 3x3 subgrid
for (int row = gridRow * 3; row < gridRow * 3 + 3; row++) {
for (int col = gridCol * 3; col < gridCol * 3 + 3; col++) {
int value = sudoku[row][col];
if (value != 0) {
present[value] = true;
} else {
subgridPositions.add(new int[]{row, col});
}
}
}
// Randomly fill the subgrid ensuring no duplicates
for (int[] pos : subgridPositions) {
int newValue;
do {
newValue = random.nextInt(9) + 1;
} while (present[newValue]);
sudoku[pos[0]][pos[1]] = newValue;
present[newValue] = true;
}
}
}
// Create a new chromosome with the generated Sudoku and mutable positions
Chromosome chromosome = new Chromosome(sudoku, new ArrayList<>(mutablePositions));
@@ -220,15 +241,29 @@ public class Main {
public void mutateChromosome(Chromosome chromosome, double mutationRate) {
int[][] sudoku = chromosome.getSudoku();
List<int[]> mutablePositions = chromosome.getMutablePositions();
// Mutate each mutable position with a probability defined by mutationRate
for (int[] pos : mutablePositions) {
if (random.nextDouble() < mutationRate) {
int row = pos[0];
int col = pos[1];
int newValue = random.nextInt(9) + 1; // Assign a new value between 1 and 9
sudoku[row][col] = newValue;
// With a probability defined by mutationRate, perform a mutation by swapping subgrids
if (random.nextDouble() < mutationRate) {
// Randomly select two different subgrids to swap
int subgrid1, subgrid2;
do {
subgrid1 = random.nextInt(9);
subgrid2 = random.nextInt(9);
} while (subgrid1 == subgrid2);
// Get the starting coordinates for both subgrids
int rowStart1 = (subgrid1 / 3) * 3;
int colStart1 = (subgrid1 % 3) * 3;
int rowStart2 = (subgrid2 / 3) * 3;
int colStart2 = (subgrid2 % 3) * 3;
// Swap the values in the two selected subgrids
for (int rowOffset = 0; rowOffset < 3; rowOffset++) {
for (int colOffset = 0; colOffset < 3; colOffset++) {
int temp = sudoku[rowStart1 + rowOffset][colStart1 + colOffset];
sudoku[rowStart1 + rowOffset][colStart1 + colOffset] = sudoku[rowStart2 + rowOffset][colStart2 + colOffset];
sudoku[rowStart2 + rowOffset][colStart2 + colOffset] = temp;
}
}
}
@@ -236,6 +271,7 @@ public class Main {
chromosome.setSudoku(sudoku);
chromosome.evaluateFitness();
}
public void evaluatePopulation() {
// Evaluate the fitness of each chromosome in the population