Skip to content
Snippets Groups Projects
Commit 74048431 authored by Lennart Eichhorn's avatar Lennart Eichhorn
Browse files

Changed Evaluator interface.

parent 75cd94d9
Branches
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ public class EvaluatorNESW implements Evaluator { ...@@ -16,7 +16,7 @@ public class EvaluatorNESW implements Evaluator {
} }
@Override @Override
public double evaluateFitness(Candidate candidate) { public Candidate evaluateFitness(Candidate candidate) {
//TODO Is it a good idea to "cache" fitness this way? Maybe //TODO Is it a good idea to "cache" fitness this way? Maybe
//If fitness was not calculated yet, calculate it //If fitness was not calculated yet, calculate it
if (candidate.getFitness() <= -1) { // Not calculated before if (candidate.getFitness() <= -1) { // Not calculated before
...@@ -30,7 +30,7 @@ public class EvaluatorNESW implements Evaluator { ...@@ -30,7 +30,7 @@ public class EvaluatorNESW implements Evaluator {
candidate.setFitness(fitness); candidate.setFitness(fitness);
} }
return candidate.getFitness(); return candidate;
} }
public int evaluateBonds(Candidate candidate) { public int evaluateBonds(Candidate candidate) {
......
...@@ -4,5 +4,5 @@ import MainClasses.Candidate; ...@@ -4,5 +4,5 @@ import MainClasses.Candidate;
public interface Evaluator { public interface Evaluator {
double evaluateFitness(Candidate candidate); Candidate evaluateFitness(Candidate candidate);
} }
...@@ -153,7 +153,8 @@ public class GeneticAlgorithm { ...@@ -153,7 +153,8 @@ public class GeneticAlgorithm {
int bestIndex = 0; int bestIndex = 0;
this.totalFitness = 0; this.totalFitness = 0;
for (int i = 0; i < config.getPopulationSize(); i++) { for (int i = 0; i < config.getPopulationSize(); i++) {
this.fitness[i] = this.evaluator.evaluateFitness(this.population[i]); this.population[i] = this.evaluator.evaluateFitness(this.population[i]);
this.fitness[i] = this.population[i].getFitness();
this.totalFitness += this.fitness[i]; this.totalFitness += this.fitness[i];
if (this.fitness[i] > bestFitness) { if (this.fitness[i] > bestFitness) {
...@@ -182,7 +183,7 @@ public class GeneticAlgorithm { ...@@ -182,7 +183,7 @@ public class GeneticAlgorithm {
double averageFitness = this.totalFitness / config.getPopulationSize(); double averageFitness = this.totalFitness / config.getPopulationSize();
String log = String.format("%d\t%.4f\t%.4f\t%.4f\t %d\t%d\n", String log = String.format("%d\t%.4f\t%.4f\t%.4f\t %d\t%d\n",
gen, averageFitness, bestFitness, gen, averageFitness, bestFitness,
this.evaluator.evaluateFitness(overallBest), this.overallBest.getFitness(),
-1, -1,
-1); -1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment