View Javadoc

1   package yawn.nn;
2   
3   import java.io.Serializable;
4   
5   import org.apache.commons.logging.Log;
6   import org.apache.commons.logging.LogFactory;
7   
8   import yawn.YawnRuntimeException;
9   import yawn.config.ConfigurationException;
10  import yawn.config.NeuralNetworkConfig;
11  import yawn.util.InputOutputPattern;
12  import yawn.util.Pattern;
13  import yawn.util.StatisticsFacility;
14  
15  /***
16   * An abstract neural network
17   * 
18   * $Id: NeuralNetwork.java,v 1.10 2005/05/09 11:04:58 supermarti Exp $</p>
19   * 
20   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
21   * @version $Revision: 1.10 $
22   */
23  
24  public abstract class NeuralNetwork implements Serializable {
25  
26      private static final String DEFAULT_STATISTICS_FACILITY_CLASS_NAME = "yawn.util.DefaultStatisticsFacility";
27  
28      private static final Log log = LogFactory.getLog(NeuralNetwork.class);
29  
30  	/***
31  	 * 
32  	 * @uml.property name="statisticsFacility"
33  	 * @uml.associationEnd multiplicity="(0 1)"
34  	 */
35  	private StatisticsFacility statisticsFacility = null;
36  
37  	/***
38  	 * 
39  	 * @uml.property name="adapting" 
40  	 */
41  	private boolean adapting;
42  
43  
44      /***
45       * 
46       */
47      public NeuralNetwork() {
48          adapting = false;
49      }
50  
51      /***
52       * Performs one learning iteration.
53       * 
54       * @param input
55       *            The network input.
56       * @param output
57       *            The desired output.
58       */
59      public abstract void oneLearningStep(Pattern input, Pattern output);
60  
61      /***
62       * Computes a network prediction
63       * 
64       * @param input
65       *            The input to propagate.
66       * @return the network output.
67       */
68      public abstract Pattern predict(Pattern input);
69  
70      /***
71       * Trains the network until the stop criteria is met.
72       * 
73       * @param iop
74       *            The training set to be learned.
75       */
76      public abstract void train(InputOutputPattern[] iop);
77  
78      public abstract int getInputSize();
79  
80      public abstract int getOutputSize();
81  
82      public abstract void setup(NeuralNetworkConfig config) throws ConfigurationException;
83  
84      public abstract NeuralNetworkConfig yieldConfiguration();
85  
86  	/***
87  	 * 
88  	 * @uml.property name="statisticsFacility"
89  	 */
90  	public StatisticsFacility getStatisticsFacility() {
91  		if (statisticsFacility == null) {
92  			try {
93  				statisticsFacility = (StatisticsFacility) Class.forName(
94  					DEFAULT_STATISTICS_FACILITY_CLASS_NAME).newInstance();
95  			} catch (InstantiationException e) {
96  				log.debug(e);
97  				throw new YawnRuntimeException(e);
98  			} catch (IllegalAccessException e) {
99  				log.debug(e);
100 				throw new YawnRuntimeException(e);
101 			} catch (ClassNotFoundException e) {
102 				log.debug(e);
103 				throw new YawnRuntimeException(e);
104 			}
105 		}
106 		return statisticsFacility;
107 	}
108 
109 	/***
110 	 * Returns a human readable
111 	 * 
112 	 * @return The name
113 	 * 
114 	 * @uml.property name="neuralNetworkName" 
115 	 */
116 	public abstract String getNeuralNetworkName();
117 
118 
119     /***
120      * @return <code>true</code> if the network is adapting itself (training)
121      *         or is frozen
122      */
123     protected boolean isAdapting() {
124         return adapting;
125     }
126 
127 	/***
128 	 * @return <code>true</code> if the network is adapting itself (training)
129 	 *         or is frozen
130 	 * 
131 	 * @uml.property name="adapting"
132 	 */
133 	protected boolean getAdapting() {
134 		return adapting;
135 	}
136 
137 	/***
138 	 * If set to <code>true</code> puts the network in adapting (training)
139 	 * mode; <code>false</code> puts the network in static or frozen (testing)
140 	 * mode.
141 	 * 
142 	 * @param adapting
143 	 * 
144 	 * @uml.property name="adapting"
145 	 */
146 	protected void setAdapting(boolean adapting) {
147 		this.adapting = adapting;
148 	}
149 
150 	/***
151 	 * @param statisticsFacility
152 	 *            The statisticsFacility to set.
153 	 * 
154 	 * @uml.property name="statisticsFacility"
155 	 */
156 	public void setStatisticsFacility(StatisticsFacility statisticsFacility) {
157 		this.statisticsFacility = statisticsFacility;
158 	}
159 
160 }