blob: 42592853082be1cd76b7eba711e68c234a09bf95 (
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package jcgp.backend.modules.fitness;
import java.util.ArrayList;
import java.util.List;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Population;
import jcgp.backend.resources.Resources;
import jcgp.backend.resources.parameters.IntegerParameter;
import jcgp.backend.resources.parameters.Parameter;
/**
*
* This fitness function module implements a simple test case evaluator.
*
* A TestCase object is a
*
*
* @author Eduardo Pedroni
*
*/
public abstract class TestCaseProblem<U> extends Problem {
public static class TestCase<T> {
private T[] inputs;
private T[] outputs;
public TestCase(T[] inputs, T[] outputs) {
this.inputs = inputs;
this.outputs = outputs;
}
public T getInput(int index) {
return inputs[index];
}
public T getOutput(int index) {
return outputs[index];
}
public T[] getInputs() {
return inputs;
}
public T[] getOutputs() {
return outputs;
}
}
private ArrayList<TestCase<U>> testCases;
private IntegerParameter maxFitness;
public TestCaseProblem(Resources resources) {
super();
maxFitness = new IntegerParameter(0, "Max fitness", true, false) {
@Override
public void validate(int newValue) {
// blank
}
};
testCases = new ArrayList<TestCase<U>>();
}
@Override
public void evaluate(Population population, Resources resources) {
// for every chromosome in the population
for (int i = 0; i < resources.getInt("popSize"); i++) {
// assume an initial fitness of 0
int fitness = 0;
// for each test case
for (int t = 0; t < testCases.size(); t++) {
population.getChromosome(i).setInputs(testCases.get(t).getInputs());
// check each output
for (int o = 0; o < resources.getInt("outputs"); o++) {
if (population.getChromosome(i).getOutput(o).calculate() == testCases.get(t).getOutput(o)) {
fitness++;
}
}
}
// assign the resulting fitness to the respective individual
population.getChromosome(i).setFitness(fitness);
}
}
@Override
public Parameter[] getLocalParameters() {
return new Parameter[]{maxFitness};
}
private int getMaxFitness() {
int fitness = 0;
for (TestCase<U> tc : testCases) {
fitness += tc.getOutputs().length;
}
return fitness;
}
public void setTestCases(List<TestCase<U>> testCases) {
this.testCases.clear();
this.testCases.addAll(testCases);
maxFitness.set(getMaxFitness());
}
@Override
public boolean isPerfectSolution(Chromosome fittest) {
return fittest.getFitness() >= maxFitness.get();
}
}
|