java - Serializing ECJ GP Individual for later execution -



java - Serializing ECJ GP Individual for later execution -

using genetic programming ecj 21, i'm looking suggestions on how serialize ec.individual (after successful(?) evolution).

the trick is, need deserialize gp @ later stage, , execute it...preferably outside of ecj framework (there seems lot of scaffolding involved in "executing" gp while working in ecj framework, since aimed @ evolving thing, not "running" it).

i have sorta there this:

public static void main (string [] args) { file f = new file("./my.params"); if (!f.exists() ) { throw new invalidparameterexception(f.getname() + " 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 = ""; (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); homecoming bestindividstr; }

ok, of problems this:

how feed in variables , evaluate gpind? in sample situation, have original individual available, can species reference (which used create new individual output of original). in real situation, won't have that.

writing own parser , evaluation stack text output produced printindividual(..) shouldn't hard, prefer not if there easier (built-in) way.

ps: have far built based on this tutorial

i had similar problem. solution create xml representation of individual subclassing gpnode. next source shows appropriate implementation.

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(); seek { 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 console testing // streamresult result = new streamresult(system.out); transformer.transform(source, result); sb.append(new string(sw.getbuffer())); } grab (transformerconfigurationexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (parserconfigurationexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (transformerexception e) { // todo auto-generated grab block e.printstacktrace(); } homecoming sb; } private void makexmltree(document doc, element parent) { element kid = 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); } } }

the final individual can serializied within problem in order save or transfer application or load new evaluation.

following code snippet shows makexmltree-method phone call of root node within evaluate-method of gpproblem implementation.

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 kid = (xmlgpnode)((gpindividual)ind).trees[0].child; **string individual = child.makexmltree().tostring();**

... ...

java serialization genetic-programming

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -