1 package com.delhezi.ga;
2
3 import com.delhezi.ga.crossover.factory.CrossoverOperatorType;
4 import com.delhezi.ga.exception.GeneticAlgorithmException;
5
6 import com.delhezi.ga.fitnessfunction.FitnessFunction;
7 import com.delhezi.ga.fitnessfunction.FitnessFunctionOption;
8 import com.delhezi.ga.fitnessfunction.drivers.IFitnessFunctionDriver;
9 import com.delhezi.ga.fitnessfunction.drivers.factory.FitnessFunctionDriverFactory;
10 import com.delhezi.ga.initialize.data.SampleTsp;
11 import com.delhezi.ga.mutation.factory.MutationOperatorType;
12 import com.delhezi.ga.selection.factory.SelectionMethodType;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.LinkedList;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.traversal.NodeIterator;
21 import org.w3c.dom.traversal.NodeFilter;
22 import org.w3c.dom.Node;
23 import org.xml.sax.InputSource;
24 import org.xml.sax.SAXException;
25 import javax.xml.XMLConstants;
26 import javax.xml.validation.SchemaFactory;
27 import javax.xml.validation.Schema;
28 import javax.xml.parsers.*;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.Source;
32 import javax.xml.transform.stream.StreamSource;
33
34 import org.w3c.dom.traversal.DocumentTraversal;
35
36
37 public class GeneticAlgorithmXmlDomParserFactory {
38
39 private static final Logger LOGGER =
40 Logger.getLogger(GeneticAlgorithmXmlSaxParserFactory.class.getName());
41
42
43
44
45
46 public static enum InitializeDataSource {
47
48 db,
49
50
51 sampleTSP;
52 }
53 private static final String XML_SCHEMA_FILE = "newInstanceParameters.xsd";
54 private GeneticAlgorithm ga;
55
56 private Population population;
57 private PopulationType populationType;
58 private CrossoverOperatorType crossoverOperator;
59 private double crossoverProbability;
60 private MutationOperatorType mutationOperator;
61 private double mutationProbability;
62 private boolean elitism;
63 private String fitnessFunctionScript;
64 private FitnessFunctionOption fitnessFunctionOption;
65 private SelectionMethodType selectionMethod;
66 private int maxLT;
67 private int minLT;
68 private InitializeDataSource initializeDataSource;
69 private String initializePopulationConnectionName;
70 private String initializePopulationSQLQuery;
71 private int populationSize;
72
73 private Document doc = null;
74
75 public GeneticAlgorithm newGeneticAlgorithm(final String fitnassFunctionScriptsPath,
76 final String xmlParams) throws GeneticAlgorithmException {
77 ga = new GeneticAlgorithm();
78 try {
79 doc = parserXML(xmlParams);
80 parse(doc);
81 initGA(fitnassFunctionScriptsPath);
82 } catch (Exception error) {
83 error.printStackTrace();
84 }
85 return ga;
86 }
87
88 private void initGA(final String fitnassFunctionScriptsPath) throws GeneticAlgorithmException {
89 FitnessFunction fitnessFunction = null;
90 IFitnessFunctionDriver fitnessFunctionDriver;
91 try {
92 fitnessFunctionDriver =
93 FitnessFunctionDriverFactory.getFitnessFunctionEngineDriver("javascript",
94 fitnassFunctionScriptsPath,
95 fitnessFunctionScript);
96 fitnessFunction = new FitnessFunction(fitnessFunctionDriver, "fitnessFunction");
97 } catch (GeneticAlgorithmException ex) {
98 LOGGER.log(Level.SEVERE, null, ex);
99 throw new GeneticAlgorithmException(ex.getMessage());
100 }
101 if (fitnessFunctionOption == FitnessFunctionOption.minimisation) {
102 fitnessFunction.setMaximisation(false);
103 } else if (fitnessFunctionOption == FitnessFunctionOption.maximisation) {
104 fitnessFunction.setMaximisation(true);
105 }
106
107 ChromosomeProperties chromosomeProperties =
108 ChromosomeProperties.getInstance();
109 chromosomeProperties.setFitnessFunction(fitnessFunction);
110
111 LinkedList<Chromosome> chromosomes = null;
112 switch (initializeDataSource) {
113 case sampleTSP:
114 chromosomes = SampleTsp.newChromosomes(populationSize, chromosomeProperties);
115 break;
116 case db:
117
118
119
120 chromosomes = null;
121 break;
122 }
123
124 switch (populationType) {
125 case PopulationChangeableSize:
126 try {
127 population = PopulationChangeableSize.newPopulationChangeableSize(
128 maxLT, minLT, chromosomes,
129 crossoverOperator, crossoverProbability,
130 mutationOperator, mutationProbability,
131 chromosomeProperties);
132 } catch (GeneticAlgorithmException ex) {
133 LOGGER.log(Level.SEVERE, null, ex);
134 throw new GeneticAlgorithmException(ex.getMessage());
135 }
136 break;
137 case PopulationConstantSize:
138 try {
139 population = PopulationConstantSize.newPopulationConstantSize(
140 selectionMethod, chromosomes,
141 crossoverOperator, crossoverProbability,
142 mutationOperator, mutationProbability,
143 chromosomeProperties);
144 } catch (GeneticAlgorithmException ex) {
145 LOGGER.log(Level.SEVERE, null, ex);
146 throw new GeneticAlgorithmException(ex.getMessage());
147 }
148 break;
149 }
150 ga.setPopulation(population);
151 ga.setState(GeneticAlgorithmState.INITIALIZED);
152 }
153
154
155
156
157
158 public void parse(Document doc) {
159
160 Element root = doc.getDocumentElement();
161
162
163 NodeIterator i =
164 ((DocumentTraversal)doc).createNodeIterator(root, NodeFilter.SHOW_ALL,
165 null, true);
166
167 Node n;
168 String tmpNodeName = null;
169 while ((n = i.nextNode()) != null) {
170 if (n.getNodeType() == Node.ELEMENT_NODE) {
171
172 tmpNodeName = n.getNodeName();
173
174 if (tmpNodeName.equalsIgnoreCase("changeablePopulationSize")) {
175 populationType = PopulationType.PopulationChangeableSize;
176 } else if (tmpNodeName.equalsIgnoreCase("constantPopulationSize")) {
177 populationType = PopulationType.PopulationConstantSize;
178 } else if (tmpNodeName.equalsIgnoreCase("elitism")) {
179 elitism = true;
180 } else if (tmpNodeName.equalsIgnoreCase("db")) {
181 initializeDataSource =
182 GeneticAlgorithmXmlDomParserFactory.InitializeDataSource.db;
183 } else if (tmpNodeName.equalsIgnoreCase("sampleTSP")) {
184 initializeDataSource =
185 GeneticAlgorithmXmlDomParserFactory.InitializeDataSource.sampleTSP;
186 }
187
188
189 } else if (n.getNodeType() == Node.TEXT_NODE) {
190 if (tmpNodeName.equalsIgnoreCase("maxLT")) {
191 maxLT = Integer.parseInt(n.getNodeValue());
192 } else if (tmpNodeName.equalsIgnoreCase("minLT")) {
193 minLT = Integer.parseInt(n.getNodeValue());
194 } else if (tmpNodeName.equalsIgnoreCase("selectionMethod")) {
195 selectionMethod =
196 SelectionMethodType.valueOf(n.getNodeValue());
197 } else if (tmpNodeName.equalsIgnoreCase("crossoverOperator")) {
198 crossoverOperator =
199 CrossoverOperatorType.valueOf(n.getNodeValue());
200 } else if (tmpNodeName.equalsIgnoreCase("crossoverProbability")) {
201 crossoverProbability =
202 Double.parseDouble(n.getNodeValue());
203 } else if (tmpNodeName.equalsIgnoreCase("mutationOperator")) {
204 mutationOperator =
205 MutationOperatorType.valueOf(n.getNodeValue());
206 } else if (tmpNodeName.equalsIgnoreCase("mutationProbability")) {
207 mutationProbability = Double.parseDouble(n.getNodeValue());
208 } else if (tmpNodeName.equalsIgnoreCase("elitism")) {
209
210 elitism = Boolean.valueOf(n.getNodeValue());
211 } else if (tmpNodeName.equalsIgnoreCase("fitnessFunctionScript")) {
212 fitnessFunctionScript = n.getNodeValue();
213 } else if (tmpNodeName.equalsIgnoreCase("fitnessFunctionOption")) {
214 fitnessFunctionOption =
215 FitnessFunctionOption.valueOf(n.getNodeValue());
216 } else if (tmpNodeName.equalsIgnoreCase("initializePopulationConnectionName")) {
217 this.initializePopulationConnectionName = n.getNodeValue();
218 } else if (tmpNodeName.equalsIgnoreCase("initializePopulationSQLQuery")) {
219 this.initializePopulationSQLQuery = n.getNodeValue();
220 } else if (tmpNodeName.equalsIgnoreCase("populationSize")) {
221 this.populationSize = Integer.parseInt(n.getNodeValue());
222 } else if (tmpNodeName.equalsIgnoreCase("maxGenerationCount")) {
223 ga.setMaxGenerationCount(Integer.parseInt(n.getNodeValue()));
224 } else if (tmpNodeName.equalsIgnoreCase("lastGenerationTopChromosomeFind")) {
225 ga.setLastGenerationTopChromosomeFind(Integer.parseInt(n.getNodeValue()));
226 }
227
228 }
229 }
230 }
231
232 public Document parserXML(String xmlParams) throws SAXException,
233 IOException,
234 ParserConfigurationException {
235 InputStream xmlSchemaInputStream =
236 GeneticAlgorithmXmlSaxParserFactory.class.getClassLoader().getResourceAsStream(XML_SCHEMA_FILE);
237
238
239 SchemaFactory xsdFactory =
240 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
241 Schema schema =
242 xsdFactory.newSchema(new Source[] { new StreamSource(xmlSchemaInputStream) });
243
244
245 DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
246 df.setValidating(false);
247 df.setNamespaceAware(true);
248 df.setSchema(schema);
249 DocumentBuilder builder = df.newDocumentBuilder();
250 InputSource xmlInputSource =
251 new InputSource(new java.io.ByteArrayInputStream(xmlParams.getBytes()));
252 return builder.parse(xmlInputSource);
253 }
254 }