final commit
This commit is contained in:
@@ -68,7 +68,7 @@ public class Main {
|
||||
else { // Ultra-hard sudoku
|
||||
POPULATIONSIZE = 100000;
|
||||
TOURNAMENTSIZE = 10;
|
||||
MUTATIONRATE = 0.1;
|
||||
MUTATIONRATE = 0.0;
|
||||
}
|
||||
|
||||
// Generate initial population of chromosomes
|
||||
@@ -78,6 +78,17 @@ public class Main {
|
||||
List<Integer> fitnessValues = new ArrayList<>();
|
||||
|
||||
int generation = 0; // Track the number of generations
|
||||
int stuckCounter = 0;
|
||||
int tryUnstuckCount = 0;
|
||||
boolean unstucking = false;
|
||||
|
||||
int INITIAL_POPULATIONSIZE = POPULATIONSIZE;
|
||||
int INITIAL_TOURNAMENTSIZE = TOURNAMENTSIZE;
|
||||
double INITIAL_MUTATIONRATE = MUTATIONRATE;
|
||||
|
||||
|
||||
|
||||
|
||||
while (true) {
|
||||
// Evaluate the fitness of each chromosome in the population
|
||||
mainInstance.evaluatePopulation();
|
||||
@@ -112,13 +123,46 @@ public class Main {
|
||||
if (generation % 10 == 0) {
|
||||
mainInstance.plotFitness(fitnessValues);
|
||||
}
|
||||
|
||||
|
||||
// If the best solution found has a fitness of 0, print it and end the program
|
||||
if (bestSolution.getFitness() == 0) {
|
||||
bestSolution.printChromosome(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/*
|
||||
// Check if algorithm is stuck
|
||||
if (fitnessValues.size() > 2) {
|
||||
if (fitnessValues.get(fitnessValues.size()-1) == fitnessValues.get(fitnessValues.size()-2)){
|
||||
stuckCounter++;
|
||||
System.out.println("StuckCount: " + stuckCounter);
|
||||
if (stuckCounter > 20){
|
||||
if (MUTATIONRATE < 1){
|
||||
unstucking = true;
|
||||
System.out.println(MUTATIONRATE);
|
||||
System.out.println(POPULATIONSIZE);
|
||||
MUTATIONRATE = MUTATIONRATE * 1.1 + 0.01;
|
||||
POPULATIONSIZE /= 2;
|
||||
TOURNAMENTSIZE /= 2;
|
||||
}
|
||||
stuckCounter = 0;
|
||||
}
|
||||
} else {
|
||||
stuckCounter = 0;
|
||||
}
|
||||
}
|
||||
if (unstucking) {
|
||||
System.out.println("Unstucking");
|
||||
tryUnstuckCount++;
|
||||
if (tryUnstuckCount > 100){
|
||||
unstucking = false;
|
||||
tryUnstuckCount = 0;
|
||||
POPULATIONSIZE = INITIAL_POPULATIONSIZE;
|
||||
TOURNAMENTSIZE = INITIAL_TOURNAMENTSIZE;
|
||||
MUTATIONRATE = INITIAL_MUTATIONRATE;
|
||||
}
|
||||
}
|
||||
System.out.println("UnstuckCount: " + tryUnstuckCount);
|
||||
}*/
|
||||
}
|
||||
|
||||
// Generate initial population of chromosomes
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user