Updated with guaranteed subrid method. Statistial version update required.

This commit is contained in:
emil
2024-11-28 14:28:17 +03:00
parent b1615d03b1
commit 39736e90fd
6 changed files with 58 additions and 16 deletions
+53 -14
View File
@@ -1,4 +1,6 @@
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@@ -110,11 +112,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));
@@ -123,6 +146,7 @@ public class Main {
}
}
// Create a deep copy of a matrix
private int[][] copyMatrix(int[][] original) {
int[][] copy = new int[original.length][original[0].length];
@@ -205,15 +229,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;
}
}
}
@@ -222,6 +260,7 @@ public class Main {
chromosome.evaluateFitness();
}
public void evaluatePopulation() {
// Evaluate the fitness of each chromosome in the population
for (Chromosome chromosome : population) {