View Javadoc

1   /***
2    * 
3    */
4   package yawn.optim.genetic;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.jgap.Chromosome;
9   import org.jgap.FitnessFunction;
10  
11  import yawn.config.NeuralNetworkConfig;
12  import yawn.nn.NeuralNetwork;
13  import yawn.optim.OptimizationAdapter;
14  import yawn.util.InputOutputPattern;
15  
16  /***
17   * This is yawn.optim.JGapAdapter, part of the yawn project.
18   * 
19   * <p>$Id: JGapAdapter.java,v 1.5 2005/05/09 11:04:56 supermarti Exp $</p>
20   * 
21   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
22   * @version $Revision: 1.5 $
23   */
24  
25  public abstract class JGapAdapter extends FitnessFunction implements OptimizationAdapter {
26  
27      private static final Log log = LogFactory.getLog(JGapAdapter.class);
28  
29  	/***
30  	 * 
31  	 * @uml.property name="templateConfig"
32  	 * @uml.associationEnd multiplicity="(1 1)"
33  	 */
34  	protected NeuralNetworkConfig templateConfig;
35  
36  
37      /***
38       * 
39       */
40      protected JGapAdapter(NeuralNetworkConfig config) {
41          this.templateConfig = config;
42      }
43  
44  	/***
45  	 * Returns a valid sample <code>org.jgap.Chromosome</code> to be used for
46  	 * optimization
47  	 * 
48  	 * @return A sample chromosome.
49  	 * 
50  	 * @uml.property name="sampleChromosome"
51  	 * @uml.associationEnd multiplicity="(0 1)"
52  	 */
53  	public abstract Chromosome getSampleChromosome();
54  
55  
56      protected abstract NeuralNetwork buildNeuralNetworkFromChromosome(Chromosome chromosome);
57  
58  	/***
59  	 * 
60  	 * @uml.property name="trainingSet"
61  	 * @uml.associationEnd multiplicity="(0 -1)"
62  	 */
63  	protected InputOutputPattern[] trainingSet;
64  
65  	/***
66  	 * 
67  	 * @uml.property name="evalSet"
68  	 * @uml.associationEnd multiplicity="(0 -1)"
69  	 */
70  	protected InputOutputPattern[] evalSet;
71  
72  
73      /*
74       * (non-Javadoc)
75       * 
76       * @see org.jgap.FitnessFunction#evaluate(org.jgap.Chromosome)
77       */
78      protected double evaluate(Chromosome arg0) {
79          NeuralNetwork net = buildNeuralNetworkFromChromosome(arg0);
80          net.train(trainingSet);
81          double eval = net.getStatisticsFacility().computePredictionError(net, evalSet);
82          log.debug("Eval index: " + eval);
83          return eval;
84      }
85  
86  	/***
87  	 * @return Returns the trainingSet.
88  	 * 
89  	 * @uml.property name="trainingSet"
90  	 */
91  	public InputOutputPattern[] getTrainingSet() {
92  		return trainingSet;
93  	}
94  
95  	/***
96  	 * @param trainingSet
97  	 *            The trainingSet to set.
98  	 * 
99  	 * @uml.property name="trainingSet"
100 	 */
101 	public void setTrainingSet(InputOutputPattern[] trainingSet) {
102 		this.trainingSet = trainingSet;
103 	}
104 
105 	/***
106 	 * @return Returns the templateConfig.
107 	 * 
108 	 * @uml.property name="templateConfig"
109 	 */
110 	public NeuralNetworkConfig getTemplateConfig() {
111 		return templateConfig;
112 	}
113 
114 	/***
115 	 * @param templateConfig
116 	 *            The templateConfig to set.
117 	 * 
118 	 * @uml.property name="templateConfig"
119 	 */
120 	public void setTemplateConfig(NeuralNetworkConfig templateConfig) {
121 		this.templateConfig = templateConfig;
122 	}
123 
124 	/***
125 	 * @return Returns the evalSet.
126 	 * 
127 	 * @uml.property name="evalSet"
128 	 */
129 	public InputOutputPattern[] getEvalSet() {
130 		return evalSet;
131 	}
132 
133 	/***
134 	 * @param evalSet
135 	 *            The evalSet to set.
136 	 * 
137 	 * @uml.property name="evalSet"
138 	 */
139 	public void setEvalSet(InputOutputPattern[] evalSet) {
140 		this.evalSet = evalSet;
141 	}
142 
143 }