1   /*
2    * Part of the yawn project
3    * Created on 13-sep-2004 by marti.
4    *
5    */
6   package yawn.io.wrappers;
7   
8   import java.io.FileInputStream;
9   import java.io.FileNotFoundException;
10  import java.io.FileOutputStream;
11  import java.io.IOException;
12  import java.io.InputStreamReader;
13  import java.io.OutputStreamWriter;
14  
15  import junit.framework.TestCase;
16  import yawn.envs.plaintext.PlainTextEnvironment;
17  import yawn.io.Experiment;
18  import yawn.io.serialization.SerializationException;
19  import yawn.io.serialization.xml.CastorXmlSerializer;
20  import yawn.nn.mlp.LinearNode;
21  import yawn.nn.mlp.MLPLayer;
22  import yawn.nn.mlp.MultiLayerPerceptron;
23  import yawn.nn.mlp.SigmoidNode;
24  
25  /***
26   * This is yawn.io.wrappers.ExperimentTesta, part of the yawn project.
27   * 
28   * <p>$Id: ExperimentTest.java,v 1.8 2005/04/20 18:55:19 supermarti Exp $</p>
29   * 
30   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
31   * @version $Revision: 1.8 $
32   */
33  public class ExperimentTest extends TestCase {
34      public static final String EXPERIMENT_FILENAME = "experiment.xml";
35  
36      public void testPersistExperiment() {
37          Experiment exp = new Experiment();
38  
39          PlainTextEnvironment env = new PlainTextEnvironment();
40          env.setDataFileName("test.file");
41          env.setNumberOfSystemRuns(5);
42          env.setTrainToTestRatio(0.5);
43  
44          MultiLayerPerceptron mlp = new MultiLayerPerceptron();
45  
46          for (int i = 0; i < 4; i++) {
47              MLPLayer layer = new MLPLayer(null, 50, 2, SigmoidNode.class);
48              mlp.addLayer(layer);
49          }
50          mlp.addLayer(new MLPLayer(null, 50, 30, LinearNode.class));
51  
52          mlp.setLearningRate(3.14);
53          mlp.setMaxEpochs(2001);
54  
55          exp.setEnvironment(env);
56          exp.setNeuralNetwork(mlp.yieldConfiguration());
57  
58          try {
59              FileOutputStream fos = new FileOutputStream(EXPERIMENT_FILENAME);
60              OutputStreamWriter writer = new OutputStreamWriter(fos, "ISO-8859-1");
61  
62              // (new BetwixtXmlSerializer()).serialize(exp, writer);
63              (new CastorXmlSerializer()).serialize(exp, writer);
64              writer.close();
65          } catch (IOException e) {
66              e.printStackTrace();
67              fail();
68          } catch (SerializationException e) {
69              e.printStackTrace();
70              fail();
71          }
72  
73          Experiment exp2 = null;
74  
75          try {
76              InputStreamReader isr = new InputStreamReader(new FileInputStream(EXPERIMENT_FILENAME));
77              Object rest = (new CastorXmlSerializer()).deSerialize(Experiment.class, isr);
78              exp2 = (Experiment) rest;
79          } catch (FileNotFoundException e1) {
80              e1.printStackTrace();
81              fail();
82          } catch (SerializationException e) {
83              e.printStackTrace();
84              fail();
85          }
86  
87          assertTrue("Success", true);
88          // Same enviroment:", exp.getEnvironment()
89          // .getEnvironmentName(), exp2.getEnvironment()
90          // .getEnvironmentName());
91          // assertEquals("Same network:", exp.getNeuralNetwork()
92          // .getNeuralNetworkName(), exp2.getNeuralNetwork()
93          // .getNeuralNetworkName());
94      }
95  }