View Javadoc

1   package yawn.nn;
2   
3   import java.util.ArrayList;
4   
5   import yawn.util.Pattern;
6   
7   /***
8    * A layer of neurons
9    * 
10   * <p>$Id: Layer.java,v 1.10 2005/05/09 11:04:58 supermarti Exp $</p>
11   * 
12   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
13   * @version $Revision: 1.10 $
14   */
15  public class Layer {
16  
17  	/***
18  	 * 
19  	 * @uml.property name="adapting" 
20  	 */
21  	protected boolean adapting;
22  
23  	/***
24  	 * 
25  	 * @uml.property name="inputSize" 
26  	 */
27  	protected int inputSize;
28  
29  	/***
30  	 * 
31  	 * @uml.property name="nextLayer"
32  	 * @uml.associationEnd multiplicity="(0 1)"
33  	 */
34  	protected Layer nextLayer;
35  
36  	/***
37  	 * 
38  	 * @uml.property name="units"
39  	 * @uml.associationEnd multiplicity="(0 -1)" elementType="yawn.nn.NeuralNode"
40  	 */
41  	protected ArrayList units;
42  
43  
44      protected Layer() {
45          setAdapting(false);
46          units = new ArrayList();
47          nextLayer = null;
48          inputSize = 0;
49      }
50  
51      public Layer(Layer nextLayer, int inputSize) {
52          this();
53          this.nextLayer = nextLayer;
54          this.inputSize = inputSize;
55      }
56  
57      public void connectWith(Layer next) {
58          nextLayer = next;
59      }
60  
61      protected Pattern getActivations() {
62          Pattern res = new Pattern(size());
63  
64          for (int i = 0; i < size(); i++) {
65              res.setComponent(((NeuralNode) units.get(i)).output(), i);
66          }
67          return res;
68      }
69  
70  	/***
71  	 * 
72  	 * @uml.property name="inputSize"
73  	 */
74  	public int getInputSize() {
75  		return inputSize;
76  	}
77  
78  
79      public NeuralNode[] getNodes() {
80          return (NeuralNode[]) units.toArray(new NeuralNode[1]);
81      }
82  
83  	/***
84  	 * @return true if adaptation is taking place in the neuron
85  	 * 
86  	 * @uml.property name="adapting"
87  	 */
88  	public boolean isAdapting() {
89  		return this.adapting;
90  	}
91  
92  
93      public Pattern output() {
94          return getActivations();
95      }
96  
97      public void propagateToNextLayer() {
98          nextLayer.setInput(getActivations());
99      }
100 
101     public void reset() {
102         int l = size();
103         for (int i = 0; i < l; i++)
104             ((NeuralNode) units.get(i)).reset();
105     }
106 
107 	/***
108 	 * @param adapting
109 	 * 
110 	 * @uml.property name="adapting"
111 	 */
112 	public void setAdapting(boolean adapting) {
113 		this.adapting = adapting;
114 	}
115 
116 
117     public void setInput(Pattern input) {
118         NeuralNode[] nodes = getNodes();
119         for (int i = 0; i < nodes.length; i++) {
120             nodes[i].setInput(input);
121         }
122     }
123 
124 	/***
125 	 * 
126 	 * @uml.property name="inputSize"
127 	 */
128 	protected void setInputSize(int inputSize) {
129 		this.inputSize = inputSize;
130 	}
131 
132     public int size() {
133         return units.size();
134     }
135 }