不是发送数字,而是通过基因编程将代码传递给个人?欧洲法院

发布于 2024-08-27 01:51:27 字数 770 浏览 8 评论 0原文

我将 ECJ 与 Java 结合使用。我有一支由一群人组成的军队,我都希望他们拥有相同的大脑。

基本上,我想利用 GP 来进化大脑。我想要诸如“if-on-enemy-territory”和“if-sense-target”之类的 if 语句和“go-home” ”或“随机移动”或“射击”(对于终端)。

但是,这些语句需要是完整的可执行 Java 代码。我怎样才能通过 ECJ 做到这一点?

例如:

我希望有一个名为“moveRandom”的终端。如果我要在我的士兵类中编写此代码,它会看起来像:

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); 
 }

现在我怎样才能制作一个执行此代码的终端?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福丶如此 2024-09-03 01:51:28

我将枚举您拥有的所有函数,然后创建一个函数集类来执行与枚举关联的函数:

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;
        }
    }
}

因此表达式树中的节点现在不会包含实际的函数,而仅包含 FuncName enums ...您可能需要做一些额外的工作,例如跟踪与每个函数关联的参数数量并将其放置在哈希映射中。

或者,您可以使用反射从 Soldier 类中获取所有适用的函数名称,然后将它们放置在具有相应数量参数的映射中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文