Print

Print



 In last few days i spent a lot of time to figure out if I could use some
GPNodes that were generated during runtime, at least at Setup of evaluation
function class
i.e. public class XXX extends GPProblem implements SimpleProblemForm

i am using javassist jar library for this as given below.

I am generating and initiating a new class myGPNode class and trying to use that in a functionset as follows:

   s = "ec.app.JSwarmGP.func.myGPNode";
   base.param = "gp.fs.0.func.2";
   state.parameters.set(base, s);
   
   s = "nc0";
   base.param = "gp.fs.0.func.2.nc";
   state.parameters.set(base, s);

program is running fine without any problem but it is not using my new class to generate the GPTree.


    public void setup(final EvolutionState state, final Parameter base)
    {
   // very important, remember this
    super.setup(state, base);
    //ISinitilized = false;
   XXX ss = new XXX();

      
   ClassPool pool = ClassPool.getDefault();
   String dirname =  (new File("")).getAbsolutePath();
   File file = new File(dirname);
   recursiveTraversal(file, pool);   //get all the classes path that we could use
   
   // get the superclass from the pool
CtClass exprClass = pool.get("ec.gp.GPNode");
String myclass = "ec.app.JSwarmGP.func.PTerm{"+System.currentTimeMillis()+"}";
   // the class name is 'Eval'
CtClass evalClass = pool.makeClass(myclass); //{"+System.currentTimeMillis()+"}");
evalClass.setSuperclass(exprClass);
// Since we don't really care about the class name, we'll append the current milliseconds as a minimal guard against multi-threading problems
// the interface implemented by the new class
//evalClass.setInterfaces(new CtClass[] { pool.makeClass("XTerm") });
// add the eval method to the class
evalClass.addMethod(CtNewMethod.make("public void eval (EvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, Problem problem){System.out.println(\"PTerm\");}", 
evalClass));

//public String toString() { return "XTerm"; }
evalClass.addMethod(CtNewMethod.make("public String toString() { return \"PTerm\"; }", 
evalClass));
s = "public void checkConstraints( ec.EvolutionState state," + 
       " int tree," +
       " ec.gp.GPIndividual typicalIndividual," + 
       " ec.util.Parameter individualBase)" + 
       "{" + 
       "super.checkConstraints(state,tree,typicalIndividual,individualBase);" + 
       "if (children.length!=0)" + 
       "   state.output.error(\"Incorrect number of children for node \" + " + 
       "        toStringForError() + \" at \" + " +
       "        individualBase);"+
       "}";
evalClass.addMethod(CtNewMethod.make(s, evalClass));
Class clazz = evalClass.toClass();
// instantiate it and cast it to Evaluator
//obj = (XTerm) clazz.newInstance();
///
//gp.fs.0.func.1 = ec.app.JSwarmGP.func.PTerm
//gp.fs.0.func.1.nc = nc0
   s = "ec.app.JSwarmGP.func." + myclass;
   base.param = "gp.fs.0.func.2";
   state.parameters.set(base, s);
   
   s = "nc0";
   base.param = "gp.fs.0.func.2.nc";
   state.parameters.set(base, s);
///gp.fs.0.size = 2
   s = "3";
   base.param = "gp.fs.0.size";
   state.parameters.set(base, s);
}