aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/problem/TestCaseProblem.java
blob: 6c4a7dc084da78a585efcf17deff7cdf7f86ba9a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package jcgp.backend.modules.problem;

import java.io.File;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import jcgp.backend.parsers.TestCaseParser;
import jcgp.backend.resources.ModifiableResources;
import jcgp.backend.resources.Resources;
import jcgp.backend.resources.parameters.DoubleParameter;
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 Object> 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;
		}
	}
	
	protected ObservableList<TestCase<U>> testCases;
	protected DoubleParameter maxFitness;
	protected Resources resources;
	
	public TestCaseProblem(Resources resources) {
		super();
		
		this.resources = resources;
		
		maxFitness = new DoubleParameter(0, "Max fitness", true, false) {
			@Override
			public void validate(Number newValue) {
				// blank
			}
		};
		testCases = FXCollections.observableArrayList();
	}
	
	@Override
	public Parameter<?>[] getLocalParameters() {
		return new Parameter[]{maxFitness};
	}
	
	protected double 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());
	}
	
	public ObservableList<TestCase<U>> getTestCases() {
		return testCases;
	}
	
	public abstract void addTestCase(String[] inputs, String[] outputs);
	
	protected final void addTestCase(TestCase<U> testCase) {
		if (testCase.getInputs().length != resources.inputs()) {
			throw new IllegalArgumentException("Received test case with " + testCase.getInputs().length + 
					" inputs but need exactly " + resources.inputs());
		} else if (testCase.getOutputs().length != resources.outputs()) {
			throw new IllegalArgumentException("Received test case with " + testCase.getOutputs().length + 
					" outputs but need exactly " + resources.outputs());
		} else {
			this.testCases.add(testCase);
			maxFitness.set(getMaxFitness());
		}
	}
	
	public int getInputCount() {
		return resources.inputs();
	}
	
	public int getOutputCount() {
		return resources.outputs();
	}

	public void clearTestCases() {
		testCases.clear();
	}

	public void parse(File file, ModifiableResources resources) {
		TestCaseParser.parseParameters(file, resources);
		TestCaseParser.parse(file, this, resources);
	}
}