将遗传编程与ECJ 21结合使用,我正在寻找有关如何序列化ec.Individual
(成功(?)进化后)的建议。
诀窍是,我需要在稍后阶段对此GP进行反序列化,并执行它...最好在ECJ框架之外(在执行工作时似乎有很多脚手架参与“执行”GP ECJ框架,因为那实际上是为了使事物发展,而不是“运行”它。)
我几乎就有了这样的东西:
public static void main (String [] args) {
File f = new File("./my.params");
if (!f.exists() ) {
throw new InvalidParameterException(f.getName() + " does NOT exist");
}
ParameterDatabase pd = new ParameterDatabase(f, new String []{f.getCanonicalPath()});
Output output = ec.Evolve.buildOutput();
EvolutionState evs = ec.Evolve.initialize(pd, 0,output);
evs.run(EvolutionState.C_STARTED_FRESH);
Individual [] individuals = ((SimpleStatistics)evs.statistics).getBestSoFar();
String bestIndividStr = "";
for (Individual individual : individuals) {
bestIndividStr = printToLog(evs, individual);
}
Species s = individuals[0].species;
s = new GPSpecies();
Individual gpInd = s.newIndividual(evs, new LineNumberReader(new StringReader(bestIndividStr)));
}
private static String printToLog(EvolutionState evs, Individual individual) {
String bestIndividStr;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
individual.printIndividual(evs, pw);
pw.append(System.lineSeparator());
pw.close();
bestIndividStr = baos.toString();
LoggerFactory.getLogger(Evolve.class).info("Best Dude: \n{}",bestIndividStr);
return bestIndividStr;
}
好的,有些问题:
gpInd
?为printIndividual(..)
生成的文本输出编写自己的解析器和评估堆栈应该不会那么难,但如果有更简单的(内置)方式,我宁愿不这样做。
PS:到目前为止我所基于的是this tutorial
答案 0 :(得分:2)
我有类似的问题。
我的解决方案是通过继承GPNode
来创建个人的XML表示。以下来源显示了相应的实现。
public abstract class XMLGPNode extends GPNode {
/**
*
*/
private static final long serialVersionUID = 2732707537997825895L;
public StringBuilder makeXMLTree() {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
StringBuilder sb = new StringBuilder();
try {
docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
makeXMLTree(doc, null);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
sb.append(new String(sw.getBuffer()));
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb;
}
private void makeXMLTree(Document doc, Element parent) {
Element child = doc.createElement(toString());
if (parent==null) {
doc.appendChild(child);
} else {
parent.appendChild(child);
}
if (children.length > 0) {
for(int x=0; x<children.length; x++) {
((XMLGPNode)children[x]).makeXMLTree(doc, child);
}
}
}
然后可以在问题中对最终个人进行序列化,以便将其保存或转移到另一个应用程序或加载它以进行新的评估。
以下代码段显示了makeXMLTree
- [{1}} - evaluate
实施方法中根节点的GPProblem
- 方法调用。
public void evaluate(final EvolutionState state,
final Individual ind,
final int subpopulation,
final int threadnum) {
if (!ind.evaluated) { // don't bother reevaluating
int hits = 0;
...
...
transformationType = new TransformationType(TransformationType.TYPE.values()[state.random[threadnum].nextInt(TransformationType.MAX_INPUT_TYPES)]);
((GPIndividual)ind).trees[0].child.eval(state,threadnum,input,stack,((GPIndividual)ind),this);
XMLGPNode child = (XMLGPNode)((GPIndividual)ind).trees[0].child;
**String individual = child.makeXMLTree().toString();**
... ...