blob: a69de961b742cf9ca5d26605f5435bc4be0f2aea (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package jcgp.modules.fitness;
import jcgp.TruthTable;
import jcgp.parameters.Parameters;
import jcgp.population.Population;
public class TruthTableEvaluator implements FitnessFunction {
@Override
public void evaluate(Population population) {
// for every chromosome in the population
for (int i = 0; i < Parameters.getPopulationSize(); i++) {
int fitness = 0;
// for every test case
for (int t = 0; t < TruthTable.getTestCaseCount(); t++) {
population.getChromosome(i).setInputs(TruthTable.getTestCase(t).getInputs());
// check every output
for (int o = 0; o < Parameters.getOutputs(); o++) {
if (population.getChromosome(i).getOutput(o).calculate() == TruthTable.getTestCase(t).getOutput(o)) {
fitness++;
}
}
}
population.getChromosome(i).setFitness(fitness);
if (Parameters.getDebug()) {
System.out.println("active nodes: " + population.getChromosome(i).getActiveNodes().size());
}
}
}
}
|