我将基因编程与ECJ 21结合使用,正在寻找有关如何序列化ec.Individual(在成功(?)进化后)的建议。

诀窍是,然后我需要在以后的阶段反序列化此GP,然后执行它……最好是在ECJ框架之外(在ECJ框架中工作时似乎有很多脚手架涉及“执行” GP) ,因为这实际上是为了使事物进化,而不是“运行”它)。

我几乎有一些与此相关的东西:

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

最佳答案

我有一个类似的问题。
我的解决方案是通过子类化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实现的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();**


...
...

10-02 05:21