View Javadoc

1   package yawn.envs.delve;
2   
3   /***
4    * Interacts with the DELVE test environment
5    * 
6    * <p>$Id: DelveEnvironment.java,v 1.7 2005/05/09 11:04:57 supermarti Exp $</p>
7    * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
8    * @version $Revision: 1.7 $
9    */
10  import java.io.BufferedReader;
11  import java.io.BufferedWriter;
12  import java.io.File;
13  import java.io.FileReader;
14  import java.io.FileWriter;
15  import java.io.IOException;
16  import java.util.StringTokenizer;
17  import java.util.Vector;
18  
19  import yawn.config.ValidationException;
20  import yawn.envs.EnvironmentException;
21  import yawn.util.InputOutputPattern;
22  import yawn.util.Pattern;
23  
24  public class DelveEnvironment extends yawn.envs.Environment {
25  
26      class ProblemTrainFilenameFilter implements java.io.FilenameFilter {
27  
28          public boolean accept(File dir, String name) {
29              return name.startsWith("train");
30          }
31      }
32  
33      protected static final String GUESS_FILE_NAME = "cguess";
34  
35      protected static final String TEST_FILE_NAME = "test";
36  
37      protected static final String TRAIN_FILE_NAME = "train";
38  
39      protected int inputSize;
40  
41      protected int outputSize;
42  
43      protected boolean parsed = false;
44  
45  	/***
46  	 * 
47  	 * @uml.property name="problemDirectory" 
48  	 */
49  	protected String problemDirectory;
50  
51  	/***
52  	 * 
53  	 * @uml.property name="resultsDirectory" 
54  	 */
55  	protected String resultsDirectory;
56  
57  
58      /***
59       * 
60       * @see yawn.envs.Environment#getNumberOfSystemRuns()
61       */
62      public int getNumberOfSystemRuns() {
63          return (new File(problemDirectory)).list(new ProblemTrainFilenameFilter()).length;
64      }
65  
66      public int outputSize() {
67          if (parsed) {
68              return outputSize;
69          }
70          throw new DelveNotInitializedException();
71      }
72  
73      public int inputSize() {
74          if (parsed) {
75              return inputSize;
76          }
77          throw new DelveNotInitializedException();
78      }
79  
80  	/***
81  	 * @return Returns the problemDirectory.
82  	 * 
83  	 * @uml.property name="problemDirectory"
84  	 */
85  	public String getProblemDirectory() {
86  		return problemDirectory;
87  	}
88  
89  	/***
90  	 * @return Returns the resultsDirectory.
91  	 * 
92  	 * @uml.property name="resultsDirectory"
93  	 */
94  	public String getResultsDirectory() {
95  		return resultsDirectory;
96  	}
97  
98  
99      /***
100      * Reads a test data file from a DELVE directory. The test set is a
101      * input-only file.
102      * 
103      * @param runNumber
104      *            the number of the current system run
105      * @return Test set
106      * @throws EnvironmentException
107      * @see yawn.envs.Environment#getTestDatasetInputs(int)
108      */
109     public Pattern[] getTestDatasetInputs(int runNumber) throws EnvironmentException {
110         int trueRunNumber = (runNumber + 1) % getNumberOfSystemRuns();
111 
112         InputOutputPattern[] iop = loadInputOutputFile(getProblemDirectory() + File.separator
113                 + "test." + String.valueOf(trueRunNumber), 0);
114 
115         Pattern[] ret = new Pattern[iop.length];
116 
117         for (int i = 0; i < ret.length; i++) {
118             ret[i] = iop[i].input;
119         }
120         return ret;
121     }
122 
123     /***
124      * Reads a train file from a DELVE directory.
125      * 
126      * @param runNumber
127      *            the number of the current system run
128      * @return the training set as an array of <code>InputOutputPattern</code>
129      * @throws EnvironmentException
130      * @see yawn.envs.Environment#getTrainingDataset(int)
131      */
132     public InputOutputPattern[] getTrainingDataset(int runNumber) throws EnvironmentException {
133         int trueRunNumber = (runNumber + 1) % getNumberOfSystemRuns();
134 
135         return loadInputOutputFile(getProblemDirectory() + File.separator + "train."
136                 + String.valueOf(trueRunNumber), outputSize());
137     }
138 
139     protected void init() throws EnvironmentException {
140         InputOutputPattern[] iops = loadInputOutputFile(TRAIN_FILE_NAME, 0);
141         InputOutputPattern[] spoi = loadInputOutputFile(TEST_FILE_NAME, 0);
142         inputSize = spoi[0].input.size();
143         outputSize = iops[0].input.size() - inputSize;
144         parsed = true;
145     }
146 
147     /***
148      * Reads a file in DELVE format. The last <code>numberOfOutpus</code>
149      * columns are selected as outputs.
150      * 
151      * @return an array of <code>InputOutputPattern</code> loaded from the
152      *         file.
153      * @throws EnvironmentException
154      */
155     protected InputOutputPattern[] loadInputOutputFile(String fileName, int numberOfOutputs)
156             throws EnvironmentException {
157         try {
158             BufferedReader in = new BufferedReader(new FileReader(fileName));
159             Vector holder = new Vector();
160             while (in.ready()) {
161                 String str = in.readLine().trim();
162                 if (str.length() == 0)
163                     break;
164                 StringTokenizer st = new StringTokenizer(str);
165                 int patternSize = st.countTokens();
166                 InputOutputPattern iop = new InputOutputPattern(patternSize - numberOfOutputs,
167                         numberOfOutputs);
168                 for (int i = 0; i < patternSize - numberOfOutputs; i++) {
169                     iop.input.setComponent(java.lang.Double.valueOf(st.nextToken()).doubleValue(),
170                             i);
171                 }
172                 for (int i = 0; i < numberOfOutputs; i++) {
173                     iop.output.setComponent(java.lang.Double.valueOf(st.nextToken()).doubleValue(),
174                             i);
175                 }
176                 holder.add(iop);
177             }
178             in.close();
179             return (InputOutputPattern[]) holder.toArray(new InputOutputPattern[1]);
180         } catch (IOException e) {
181             throw new EnvironmentException(e);
182         }
183     }
184 
185     public void setNumberOfSystemRuns(int numberOfRuns) {
186         throw new UnsupportedOperationException();
187     }
188 
189 	/***
190 	 * @param problemDirectory
191 	 *            The problemDirectory to set.
192 	 * 
193 	 * @uml.property name="problemDirectory"
194 	 */
195 	public void setProblemDirectory(String problemDirectory) {
196 		this.problemDirectory = problemDirectory;
197 	}
198 
199 	/***
200 	 * @param resultsDirectory
201 	 *            The resultsDirectory to set.
202 	 * 
203 	 * @uml.property name="resultsDirectory"
204 	 */
205 	public void setResultsDirectory(String resultsDirectory) {
206 		this.resultsDirectory = resultsDirectory;
207 	}
208 
209     /***
210      * 
211      * @see yawn.envs.Environment#validate()
212      */
213     public void validate() throws ValidationException {
214         try {
215             init();
216             if (!(new File(problemDirectory)).exists()) {
217                 throw new ValidationException("Problem directory not found.");
218             }
219         } catch (EnvironmentException e) {
220             throw new ValidationException(e);
221         }
222     }
223 
224     /***
225      * Writes a DELVE cguess file representing the predictions of a neural
226      * network.
227      * 
228      * @param results
229      *            the results to be written
230      * @param runNumber
231      *            the number of the current system run
232      * @throws EnvironmentException
233      */
234     public void writeResults(Pattern[] results, int runNumber) throws EnvironmentException {
235         try {
236             int trueRunNumber = (runNumber + 1) % getNumberOfSystemRuns();
237             BufferedWriter bw = new BufferedWriter(new FileWriter(getResultsDirectory()
238                     + File.separator + "cguess." + String.valueOf(trueRunNumber)));
239             for (int i = 0; i < results.length; i++) {
240                 String str = "";
241                 if ((results[i] == null) || (results[i].asDoubleArray() == null)) { // if
242                     // pattern == null
243                     // means an "i don't know" answer
244                     int j;
245                     for (j = 0; ((results[j] == null) || (results[j].asDoubleArray() == null))
246                             && (j < results.length); j++) {
247                         // guessing the size of the pattern to be written
248                     }
249                     // System.out.println("Located pattern "+j);
250                     if ((results[j] != null) && (results[j].asDoubleArray() != null)) {
251                         for (int k = 0; k < results[j].size(); k++) {
252                             str = str + "? ";
253                         }
254                     } else {
255                         throw new RuntimeException("The results about to be written are invalid.");
256                     }
257                 } else {
258                     str = results[i].toString();
259                 }
260                 str.trim();
261                 if (str.endsWith(".0")) {
262                     str = str.substring(0, str.length() - 2);
263                 }
264                 bw.write(str.replaceAll("NaN", "?"));
265                 bw.newLine();
266             }
267             bw.close();
268         } catch (IOException e) {
269             throw new EnvironmentException(e);
270         }
271     }
272 }