1   /*
2    * CircleInTheSquareEnvironmentTest.java
3    * Part of the yawn project
4    * Created on 18-dic-2004 by marti.
5    *
6    */
7   package yawn.envs.synthetic;
8   
9   import junit.framework.TestCase;
10  import yawn.envs.EnvironmentException;
11  import yawn.util.InputOutputPattern;
12  
13  /***
14   * This is yawn.envs.synthetic.CircleInTheSquareEnvironmentTest, part of the
15   * yawn project.
16   * 
17   * <p>$Id: CircleInTheSquareEnvironmentTest.java,v 1.9 2005/05/09 11:04:54 supermarti Exp $</p>
18   * 
19   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
20   * @version $Revision: 1.9 $
21   */
22  public class CircleInTheSquareEnvironmentTest extends TestCase {
23      protected static final int NUMBER_OF_RUNS = 6;
24  
25      protected static final double SQUARE_SIZE = 1;
26  
27      /***
28       * The 0.399 radius produces a cicle with half the area of a 1x1 square
29       */
30      protected static final double CIRCLE_RADIUS = 0.399;
31  
32      protected static final int TRAIN_SET_SIZE = 10000;
33  
34      protected static final int TEST_SET_SIZE = 800;
35  
36      protected static final double RAND_EPSILON = 0.1;
37  
38  	/***
39  	 * 
40  	 * @uml.property name="cis"
41  	 * @uml.associationEnd multiplicity="(0 1)"
42  	 */
43  	protected CircleInTheSquareEnvironment cis;
44  
45      /*
46       * (non-Javadoc)
47       * 
48       * @see junit.framework.TestCase#setUp()
49       */
50      protected void setUp() throws Exception {
51          // TODO Auto-generated method stub
52          super.setUp();
53          cis = new CircleInTheSquareEnvironment();
54          cis.setNumberOfSystemRuns(NUMBER_OF_RUNS);
55          cis.setSquareDimension(SQUARE_SIZE);
56          cis.setInnerCircleRadius(CIRCLE_RADIUS);
57          cis.setTestSetSize(TEST_SET_SIZE);
58          cis.setTrainSetSize(TRAIN_SET_SIZE);
59      }
60  
61      public void testGetTrainingDataset() {
62          double tot = 0;
63  
64          InputOutputPattern[] iop = null;
65  
66          for (int i = 0; i < cis.getNumberOfSystemRuns(); i++) {
67              try {
68                  tot += outputMean(cis.getTrainingDataset(i));
69  
70              } catch (EnvironmentException e) {
71                  e.printStackTrace();
72                  fail();
73              }
74          }
75          assertTrue("Are samples (0's and 1's) equally probable?", Math.abs(tot
76                  / cis.getNumberOfSystemRuns() - 0.5) < RAND_EPSILON);
77      }
78  
79      public void testDatasetStats() {
80          for (int i = 0; i < cis.getNumberOfSystemRuns(); i++) {
81              try {
82                  double trainMean = outputMean(cis.getTrainingDataset(i));
83                  double testMean = outputMean(cis.getFullTestDataset(i));
84  
85                  double trainVar = outputVariance(cis.getTrainingDataset(i), trainMean);
86                  double testVar = outputVariance(cis.getFullTestDataset(i), testMean);
87  
88                  assertTrue("Are the train and test sets output means similar?", Math.abs(trainMean
89                          - testMean) < RAND_EPSILON);
90                  assertTrue("Are the train and test sets output variances similar?", Math
91                          .abs(trainVar - testVar) < RAND_EPSILON);
92              } catch (EnvironmentException e) {
93                  e.printStackTrace();
94                  fail();
95              }
96          }
97  
98      }
99  
100     public double outputMean(InputOutputPattern[] set) {
101         double mean = 0;
102         for (int j = 0; j < set.length; j++) {
103             mean += set[j].output.asDoubleArray()[0];
104         }
105         return mean / set.length;
106     }
107 
108     public double outputVariance(InputOutputPattern[] set, double mean) {
109         double variance = 0;
110         for (int j = 0; j < set.length; j++) {
111             variance += Math.pow(set[j].output.asDoubleArray()[0] - mean, 2.0);
112         }
113         return variance / set.length;
114     }
115 
116 }