View Javadoc

1   /*
2    * CircleInTheSquareEnvironment.java
3    * Part of the yawn project
4    * Created on 30-nov-2004 by marti.
5    *
6    */
7   package yawn.envs.synthetic;
8   
9   import yawn.config.ValidationException;
10  import yawn.util.Pattern;
11  
12  /***
13   * A synthetic environment for the circle-in-the-square problem. In this problem
14   * inputs are distributed in a two-dimensional square. The objective is to
15   * determine of weather a given point is located inside a circunsference laid on
16   * the center of the square.
17   * 
18   * In this case, if the point belongs to the circle it will have `1' as output
19   * or `0' in the other case.
20   * 
21   * <p>$Id: CircleInTheSquareEnvironment.java,v 1.6 2005/05/09 11:04:56 supermarti Exp $</p>
22   * 
23   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
24   * @version $Revision: 1.6 $
25   */
26  public class CircleInTheSquareEnvironment extends SyntheticDataEnvironment {
27  
28  	/***
29  	 * 
30  	 * @uml.property name="innerCircleRadius" 
31  	 */
32  	protected double innerCircleRadius = 0.399;
33  
34  	/***
35  	 * 
36  	 * @uml.property name="squareDimension" 
37  	 */
38  	protected double squareDimension = 1.0;
39  
40  	/***
41  	 * 
42  	 * @uml.property name="innerCircleRadius"
43  	 */
44  	public double getInnerCircleRadius() {
45  		return this.innerCircleRadius;
46  	}
47  
48  	/***
49  	 * 
50  	 * @uml.property name="innerCircleRadius"
51  	 */
52  	public void setInnerCircleRadius(double innerCircleRadius) {
53  		this.innerCircleRadius = innerCircleRadius;
54  	}
55  
56  	/***
57  	 * 
58  	 * @uml.property name="squareDimension"
59  	 */
60  	public double getSquareDimension() {
61  		return this.squareDimension;
62  	}
63  
64  	/***
65  	 * 
66  	 * @uml.property name="squareDimension"
67  	 */
68  	public void setSquareDimension(double squareDimension) {
69  		this.squareDimension = squareDimension;
70  	}
71  
72  	/***
73  	 * 
74  	 * @uml.property name="center"
75  	 * @uml.associationEnd multiplicity="(0 1)"
76  	 */
77  	protected Pattern center = null;
78  
79      protected static final double[] ONE_PAT = { 1 };
80  
81      protected static final double[] ZERO_PAT = { 0 };
82  
83      protected Pattern synthetizeOutput(Pattern input) {
84          if (center == null) {
85              double[] cent = { squareDimension / 2, squareDimension / 2 };
86              center = new Pattern(cent);
87          }
88  
89          double dist = center.dist(input);
90  
91          if (dist > innerCircleRadius) {
92              return new Pattern(ZERO_PAT);
93          }
94  
95          return new Pattern(ONE_PAT);
96      }
97  
98      protected Pattern generateRandomInput() {
99          Pattern input = new Pattern(2);
100         input.randomize(0, squareDimension);
101         return input;
102     }
103 
104     public void validate() throws ValidationException {
105         super.validate();
106         if ((squareDimension <= 0) || (innerCircleRadius * 2 > squareDimension)) {
107             throw new ValidationException("Wrong parameters");
108         }
109     }
110 
111 }