On Nov 27, 2010, at 7:35 AM, Lucian Ionita wrote: > Hello, > > I'm researching ECJ for use in a course project (together with > MASON, mind you). I've read the tutorials but they only show how to > use it as a stand alone program, when I need it more as a library, > so I can control it from within a java program, just like I do with > MASON. I'm sure that this is possible, but I want to ask if there is > some tutorial or example available or do I have to dig into the > ec.Evolve class to find out how to use it that way? > > Best regards, > Lucian Ionita ECJ can absolutely be used as a library -- it's entirely self- contained -- but it requires a bit of elbow grease. For example, I was recently modifying ECJ in an experimental test on meta-evolution, where one ECJ process would evaluate individuals by using them to define parameters for other ECJ processes. Some information: 0. Be sure to use the version on the current SVN repository, not the snapshot (we'll remedy that soon). 1. You may wish to control Output's dumping of announcements to STDERR. Ordinarily ECJ creates two logs by default. Log 0 goes to STDOUT and log 1 goes to STDERR. Only the STDERR log is set up to receive announcements. So you may need to hush up STDERR, or perhaps redirect it elsewhere: but we've recently gotten rid of verbosity. How would you do it The simplest ECJ top-level loop looks like this public static void main(String[] args) { EvolutionState state = Evolve.possiblyRestoreFromCheckpoint(args); if (state!=null) // loaded from checkpoint state.run(EvolutionState.C_STARTED_FROM_CHECKPOINT); else { ParameterDatabase parameters = Evolve.loadParameterDatabase(args); state = Evolve.initialize(parameters, 0); state.run(EvolutionState.C_STARTED_FRESH); } Evolve.cleanup(state); System.exit(0); } We can simplify this further by eliminating the ability to restore from checkpoint, and preventing an exit(0). Then we get: public static void main(String[] args) { ParameterDatabase parameters = Evolve.loadParameterDatabase(args); state = Evolve.initialize(parameters, 0); state.run(EvolutionState.C_STARTED_FRESH); Evolve.cleanup(state); } The problem here is that the creation of the Output and the STDOUT and STDERR logs appears in Evolve.initialize(...). You can replace that method -- just do some cut and paste -- to delete the STDERR log. Or I just committed to SVN a new version of the initialize method which gives you more control over Output, allowing you to do this: public static void main(String[] args) { ParameterDatabase parameters = Evolve.loadParameterDatabase(args); Output output = new Output(); output.addLog(ec.util.Log.D_STDOUT,false); // output.addLog(ec.util.Log.D_STDERR,true); // no talking out STDERR initialize(parameters, 0, output); state = Evolve.initialize(parameters, 0); state.run(EvolutionState.C_STARTED_FRESH); Evolve.cleanup(state); } All other disk accesses can be controlled in the parameters. 2. If you're repeatedly making new evolutionary runs, may need to load a parameter database and reuse it instead of reloading from disk. To begin you may want to control the parameter database that's loaded rather than just passing in args. You can do that with something like: String myParameterDatabaseFile = ... ; ParameterDatabase parameters = Evolve.loadParameterDatabase( new String[] {"-file", myParameterDatabaseFile}); Second, you may wish to reuse this rather than reloading from disk. You can do this easily like this: String myParameterDatabaseFile = ... ; ParameterDatabase template = Evolve.loadParameterDatabase( new String[] {"-file", myParameterDatabaseFile}); ... now whenever you want to make a parameter database, you just say: ParameterDatabase database = new ParameterDatbase(); database.addParent(template); template is never modified: all modifications are made to just database. Note that I'm planning on releasing a major modification of ParameterDatabase which makes it more easily used in JAR files. But it'll posted to SVN after the upcoming snapshot because I don't know how stable it will be. 3. Which brings us to jar files. You can jar up ECJ but will find that it expects to open parameter database files and other resources as Files, which is a problem. I'm making modifications to the ParameterDatabase system to enable the entire library to exist inside a single jar file if you really need it. But it won't be out until after the snapshot. [Some early preparatory changes have already been made prior to the snapshot]. If you're interested in seeing ECJ running as a subprocess of another ECJ process, get ahold of me and I can throw you some code. Sean