diff --git a/Main.java b/Main.java index b695b09..8fc9910 100644 --- a/Main.java +++ b/Main.java @@ -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 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 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) { diff --git a/statistical/demo/src/main/java/com/example/Main.java b/statistical/demo/src/main/java/com/example/Main.java index 4258789..40dc4d8 100644 --- a/statistical/demo/src/main/java/com/example/Main.java +++ b/statistical/demo/src/main/java/com/example/Main.java @@ -1,8 +1,11 @@ package com.example; -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; + import org.knowm.xchart.SwingWrapper; import org.knowm.xchart.XYChart; import org.knowm.xchart.XYSeries; @@ -126,7 +129,7 @@ public class Main { // Randomly fill mutable positions for (int[] pos : mutablePositions) { int row = pos[0]; - int col = pos[1]; + int col = pos[1]; sudoku[row][col] = random.nextInt(9) + 1; } // Create a new chromosome with the generated Sudoku and mutable positions diff --git a/statistical/demo/target/classes/Main$Chromosome.class b/statistical/demo/target/classes/Main$Chromosome.class deleted file mode 100644 index 77b54fe..0000000 Binary files a/statistical/demo/target/classes/Main$Chromosome.class and /dev/null differ diff --git a/statistical/demo/target/classes/Main.class b/statistical/demo/target/classes/Main.class deleted file mode 100644 index 257a34b..0000000 Binary files a/statistical/demo/target/classes/Main.class and /dev/null differ diff --git a/statistical/demo/target/classes/com/example/Main$Chromosome.class b/statistical/demo/target/classes/com/example/Main$Chromosome.class index cf9d0a4..ad09172 100644 Binary files a/statistical/demo/target/classes/com/example/Main$Chromosome.class and b/statistical/demo/target/classes/com/example/Main$Chromosome.class differ diff --git a/statistical/demo/target/classes/com/example/Main.class b/statistical/demo/target/classes/com/example/Main.class index 27b60fa..b63c52e 100644 Binary files a/statistical/demo/target/classes/com/example/Main.class and b/statistical/demo/target/classes/com/example/Main.class differ