views:

48

answers:

1

I'm using ECJ with Java. I have an army of individuals who I all want to have the same brain.

Basically, I'd like to evolve the brains using GP. I want things like "if-on-enemy-territory" and "if-sense-target" for if statements and "go-home" or "move-randomly" or "shoot" for terminals.

However, these statements need to be full executable Java code. How can I do this with ECJ?

FOR EXAMPLE:

I wish to have a terminal named "moveRandom". If I were to code this within my soldier class, it would look like:

private void moveRandomly(SoldierWorld world)
 {
  //..snip.

  int newX = (int)(this.getLocation().x + speed * Math.cos(this.getDirection() * Math.PI / 180.0));
  int newY = (int)(this.getLocation().y - speed * Math.sin(this.getDirection() * Math.PI / 180.0));

  Point newPoint = new Point(newX, newY);
  this.setLocation(newPoint); 
 }

Now how can I make a terminal that will perform this code?

+1  A: 

I would enumerate all the functions that you have and then make a function set class that executes a function associated with the enum:

public class FunctionSet
{
    public enum FuncName
    {
        MOVE_RANDOM,
        SHOOT,
        GO_HOME,
        ...
    }

    public FunctionSet()
    {

    }

    public void Execute(FuncName funcName, Soldier soldier, SoldierWorld world)
    {
        switch(funcName)
        {
            case FuncName.MOVE_RANDOM:
                soldier.moveRandom(world);
                break;
            case FuncName.SHOOT:
                soldier.shoot(...);
                break;
            case FuncName.GO_HOME:
                soldier.goHome(...);
                break;
            default:
                break;
        }
    }
}

So the nodes in your expression tree are not going to contain an actual function now, but only FuncName enums... you might have to do some extra work, like keep track of how many parameters are associated with each function and place that in a hash map.

Alternately you can use reflection to get all the applicable function names from the Soldier class and then place them in a map with the corresponding number of parameters.

Lirik