aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/fitness/TruthTableEvaluator.java
blob: 4adf43568a37e14b48ba1cebaf6f42c7f567d78d (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.fitness;

import jcgp.Parameters;
import jcgp.TruthTable;
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());
			}
		}
	}
}