View Javadoc

1   /* 
2    * This is part of the yawn project.
3    * Created on 28-nov-2003 at 1:52:19.
4    */
5   package yawn.envs.plaintext;
6   
7   import java.io.BufferedReader;
8   import java.io.BufferedWriter;
9   import java.io.File;
10  import java.io.FileNotFoundException;
11  import java.io.FileReader;
12  import java.io.FileWriter;
13  import java.io.IOException;
14  import java.util.StringTokenizer;
15  import java.util.Vector;
16  
17  import yawn.config.ValidationException;
18  import yawn.envs.Environment;
19  import yawn.envs.EnvironmentException;
20  import yawn.util.InputOutputPattern;
21  import yawn.util.Pattern;
22  
23  /***
24   * This is part of the yawn project. Created on 28-nov-2003 at 1:52:19.
25   * 
26   * <p>$Id: PlainTextEnvironment.java,v 1.7 2005/05/09 11:04:57 supermarti Exp $</p>
27   * 
28   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
29   * @version $Revision: 1.7 $
30   */
31  public class PlainTextEnvironment extends Environment {
32  
33      protected static final String ELEMENT_SEPARATOR_SPACE = " ";
34  
35  	/***
36  	 * @return Returns the overwriteResults.
37  	 * 
38  	 * @uml.property name="overwriteResults"
39  	 */
40  	public boolean isOverwriteResults() {
41  		return this.overwriteResults;
42  	}
43  
44  	/***
45  	 * @param overwriteResults
46  	 *            The overwriteResults to set.
47  	 * 
48  	 * @uml.property name="overwriteResults"
49  	 */
50  	public void setOverwriteResults(boolean overwriteResults) {
51  		this.overwriteResults = overwriteResults;
52  	}
53  
54  	/***
55  	 * @return Returns the resultsFileName.
56  	 * 
57  	 * @uml.property name="resultsFileName"
58  	 */
59  	public String getResultsFileName() {
60  		return this.resultsFileName;
61  	}
62  
63  	/***
64  	 * @param resultsFileName
65  	 *            The resultsFileName to set.
66  	 * 
67  	 * @uml.property name="resultsFileName"
68  	 */
69  	public void setResultsFileName(String resultsFileName) {
70  		this.resultsFileName = resultsFileName;
71  	}
72  
73  	/***
74  	 * 
75  	 * @uml.property name="dataFileName" 
76  	 */
77  	protected String dataFileName;
78  
79  	/***
80  	 * 
81  	 * @uml.property name="resultsFileName" 
82  	 */
83  	protected String resultsFileName;
84  
85  	/***
86  	 * 
87  	 * @uml.property name="overwriteResults" 
88  	 */
89  	protected boolean overwriteResults = true;
90  
91  	/***
92  	 * 
93  	 * @uml.property name="elementSeparator" 
94  	 */
95  	protected String elementSeparator = ELEMENT_SEPARATOR_SPACE;
96  
97  	/***
98  	 * 
99  	 * @uml.property name="inputSize" 
100 	 */
101 	protected int inputSize;
102 
103 	/***
104 	 * 
105 	 * @uml.property name="numberOfSystemRuns" 
106 	 */
107 	protected int numberOfSystemRuns;
108 
109 	/***
110 	 * 
111 	 * @uml.property name="outputSize" 
112 	 */
113 	protected int outputSize;
114 
115 
116     protected InputOutputPattern[][] testSet = null;
117 
118     protected InputOutputPattern[][] trainSet = null;
119 
120 	/***
121 	 * 
122 	 * @uml.property name="trainToTestRatio" 
123 	 */
124 	protected double trainToTestRatio;
125 
126 	/***
127 	 * @return Returns the dataFileName.
128 	 * 
129 	 * @uml.property name="dataFileName"
130 	 */
131 	public String getDataFileName() {
132 		return dataFileName;
133 	}
134 
135 	/***
136 	 * 
137 	 * @uml.property name="elementSeparator"
138 	 */
139 	public String getElementSeparator() {
140 		return this.elementSeparator;
141 	}
142 
143 	/***
144 	 * 
145 	 * @uml.property name="inputSize"
146 	 */
147 	public int getInputSize() {
148 		return this.inputSize;
149 	}
150 
151 	/***
152 	 * @return Returns the numberOfSystemRuns.
153 	 * 
154 	 * @uml.property name="numberOfSystemRuns"
155 	 */
156 	public int getNumberOfSystemRuns() {
157 		return numberOfSystemRuns;
158 	}
159 
160 	/***
161 	 * 
162 	 * @uml.property name="outputSize"
163 	 */
164 	public int getOutputSize() {
165 		return this.outputSize;
166 	}
167 
168 
169     /***
170      * 
171      * @see yawn.envs.Environment#getTestDatasetInputs(int)
172      */
173     public Pattern[] getTestDatasetInputs(int runNumber) throws EnvironmentException {
174         if ((trainSet == null) || (testSet == null)) {
175             init();
176         }
177         Pattern[] atest = new Pattern[testSet[runNumber].length];
178 
179         for (int i = 0; i < testSet[runNumber].length; i++) {
180             atest[i] = testSet[runNumber][i].input;
181         }
182 
183         return atest;
184     }
185 
186     /***
187      * 
188      * @see yawn.envs.Environment#getTrainingDataset(int)
189      */
190     public InputOutputPattern[] getTrainingDataset(int runNumber) throws EnvironmentException {
191         if ((trainSet == null) || (testSet == null)) {
192             init();
193         }
194 
195         return trainSet[runNumber];
196     }
197 
198 	/***
199 	 * @return Returns the trainToTestRatio.
200 	 * 
201 	 * @uml.property name="trainToTestRatio"
202 	 */
203 	public double getTrainToTestRatio() {
204 		return trainToTestRatio;
205 	}
206 
207 
208     /*
209      * (non-Javadoc)
210      * 
211      * @see yawn.envs.Environment#inputSize()
212      */
213     public int inputSize() {
214         return inputSize;
215     }
216 
217     /*
218      * (non-Javadoc)
219      * 
220      * @see yawn.envs.Environment#outputSize()
221      */
222     public int outputSize() {
223         return outputSize;
224     }
225 
226 	/***
227 	 * @param dataFileName
228 	 *            The dataFileName to set.
229 	 * 
230 	 * @uml.property name="dataFileName"
231 	 */
232 	public void setDataFileName(String dataFileName) {
233 		this.dataFileName = dataFileName;
234 	}
235 
236 	/***
237 	 * 
238 	 * @uml.property name="elementSeparator"
239 	 */
240 	public void setElementSeparator(String elementSeparator) {
241 		this.elementSeparator = elementSeparator;
242 	}
243 
244 	/***
245 	 * 
246 	 * @uml.property name="inputSize"
247 	 */
248 	public void setInputSize(int inputSize) {
249 		this.inputSize = inputSize;
250 	}
251 
252 	/***
253 	 * @param numberOfSystemRuns
254 	 *            The numberOfSystemRuns to set.
255 	 * 
256 	 * @uml.property name="numberOfSystemRuns"
257 	 */
258 	public void setNumberOfSystemRuns(int numberOfSystemRuns) {
259 		this.numberOfSystemRuns = numberOfSystemRuns;
260 	}
261 
262 	/***
263 	 * 
264 	 * @uml.property name="outputSize"
265 	 */
266 	public void setOutputSize(int outputSize) {
267 		this.outputSize = outputSize;
268 	}
269 
270 	/***
271 	 * @param trainToTestRatio
272 	 *            The trainToTestRatio to set.
273 	 * 
274 	 * @uml.property name="trainToTestRatio"
275 	 */
276 	public void setTrainToTestRatio(double trainToTestRatio) {
277 		this.trainToTestRatio = trainToTestRatio;
278 	}
279 
280     /*
281      * (non-Javadoc)
282      * 
283      * @see yawn.envs.Environment#validate()
284      */
285     public void validate() throws ValidationException {
286         if (inputSize <= 0) {
287             throw new ValidationException("Input size must be greater than zero.");
288         }
289         if (outputSize <= 0) {
290             throw new ValidationException("Input size must be greater than zero.");
291         }
292 
293         if (overwriteResults) {
294             if ((new File(getResultsFileName())).exists()) {
295                 throw new ValidationException(
296                         "Results file already exists and configuration requieres not to overwrite it.");
297             }
298         } else {
299             if (!(new File(getResultsFileName())).canWrite()) {
300                 throw new ValidationException("Results file can not be written.");
301             }
302         }
303 
304         if (!(new File(getDataFileName())).exists()) {
305             throw new ValidationException("Data file not found.");
306         }
307 
308         if (!(new File(getDataFileName())).canRead()) {
309             throw new ValidationException("Data file can not be read.");
310         }
311     }
312 
313     /***
314      * Writes the results to a an ascii file.
315      * 
316      * @see PlainTextEnvironment#getElementSeparator()
317      */
318     public void writeResults(Pattern[] results, int runNumber) throws EnvironmentException {
319         try {
320             BufferedWriter bw = new BufferedWriter(new FileWriter(getResultsFileName()));
321 
322             for (int i = 0; i < results.length; i++) {
323                 StringBuffer sb = new StringBuffer();
324 
325                 for (int j = 0; j < results[i].size(); j++) {
326                     if (j != 0) { // if its not the first, add an element
327                                     // separator space
328                         sb.append(getElementSeparator());
329                     }
330                     sb.append(results[i].getComponent(j));
331                 }
332 
333                 bw.write(sb.toString());
334                 bw.newLine();
335             }
336 
337             bw.close();
338         } catch (IOException e) {
339             throw new EnvironmentException(e);
340         }
341 
342     }
343 
344     protected void init() throws EnvironmentException {
345         InputOutputPattern[] set = loadInputOutputFile(getDataFileName());
346 
347         int trainSetSize = (int) Math.round(getTrainToTestRatio() * set.length);
348 
349         trainSet = new InputOutputPattern[getNumberOfSystemRuns()][trainSetSize];
350         testSet = new InputOutputPattern[getNumberOfSystemRuns()][set.length - trainSetSize];
351 
352         for (int i = 0; i < getNumberOfSystemRuns(); i++) {
353             InputOutputPattern[] aset = InputOutputPattern.randomOrderList(set);
354             System.arraycopy(aset, 0, trainSet[i], 0, trainSetSize);
355             System.arraycopy(aset, trainSetSize + 1, testSet[i], 0, aset.length - trainSetSize);
356         }
357     }
358 
359     protected InputOutputPattern[] loadInputOutputFile(String fileName) throws EnvironmentException {
360         try {
361             BufferedReader in = new BufferedReader(new FileReader(fileName));
362             Vector holder = new Vector();
363 
364             while (in.ready()) {
365                 String str = in.readLine().trim();
366 
367                 if (str.length() == 0)
368                     break;
369 
370                 StringTokenizer st = new StringTokenizer(str, elementSeparator);
371 
372                 int patternSize = st.countTokens();
373                 if (patternSize != inputSize + outputSize) {
374                     throw new EnvironmentException("The data file is not properly formated.");
375                 }
376                 InputOutputPattern iop = new InputOutputPattern(inputSize, outputSize);
377 
378                 for (int i = 0; i < inputSize; i++) {
379                     iop.input.setComponent(java.lang.Double.valueOf(st.nextToken()).doubleValue(),
380                             i);
381                 }
382 
383                 for (int i = 0; i < outputSize; i++) {
384                     iop.output.setComponent(java.lang.Double.valueOf(st.nextToken()).doubleValue(),
385                             i);
386                 }
387 
388                 holder.add(iop);
389             }
390             in.close();
391             return (InputOutputPattern[]) holder.toArray(new InputOutputPattern[1]);
392         } catch (FileNotFoundException e) {
393             throw new EnvironmentException(e);
394         } catch (IOException e) {
395             throw new EnvironmentException(e);
396         }
397     }
398 
399 }