使用NetLogo API获取海龟坐标

发布于 2024-09-10 10:29:24 字数 616 浏览 17 评论 0原文

我正在尝试使用 Java API 获取 NetLogo 中海龟的坐标。我已成功加载工作区,并一直在使用我编写的以下代码:

public static int getX(HeadlessWorkspace workspace, String playerName, int agentNum)
{

    Double doubleX = null;
    int xVal = 0;
    try
    {
        xVal = doubleX.valueOf((workspace.report("[xcor] of "+playerName+" "+agentNum).toString()).trim()).intValue();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    return xVal;
}

但是,有一个小问题。当乌龟数量超过 5 只时,速度极其缓慢。当我运行 200 只海龟的 Flocking 代码时,在没有获取坐标的情况下,我会在 10 秒内获得大约 300 个刻度。当我使用坐标运行代码时,每个刻度大约需要 3 秒。有没有更有效的方法来实现这一目标?

谢谢,

纳迪姆

I am trying to get coordinates for turtles in NetLogo by using the Java API. I have managed to get the workspace loaded and have been using the following code that I made:

public static int getX(HeadlessWorkspace workspace, String playerName, int agentNum)
{

    Double doubleX = null;
    int xVal = 0;
    try
    {
        xVal = doubleX.valueOf((workspace.report("[xcor] of "+playerName+" "+agentNum).toString()).trim()).intValue();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    return xVal;
}

However, there is one small problem. It is extremely slow when there are more than 5 turtles. When I run the Flocking code with 200 turtles, without getting the coordinates, then I get about 300 ticks in 10 seconds. When I run the code with the coordinates, then each tick takes about 3 seconds. Is there a more efficient way of achieving this?

Thanks,

Nadim

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

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

发布评论

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

评论(2

感情废物 2024-09-17 10:29:24

我设法找出正确的方法应该是什么。这是 Seth Tisue 给出的 NetLogo 邮件列表中的代码。

import org.nlogo.headless.*;
import org.nlogo.api.*;
class J {
   public static void main(String[] args) {
      try {
         HeadlessWorkspace ws = HeadlessWorkspace.newInstance();
         ws.openString(org.nlogo.util.Utils.url2String("/system/empty.nlogo"));
         ws.command("cro 8 [ fd 5 ]");
         org.nlogo.api.Turtle turtle =(org.nlogo.api.Turtle) ws.world().turtles().agent(3);
         System.out.println("[xcor] of turtle 3 = " + turtle.xcor());
         ws.dispose();
      }
      catch(Exception ex) {
         ex.printStackTrace();
      }
   }
}

我在这里复制了代码,以便对其他人有所帮助。要查看可以从 Turtle 获取哪些信息的列表,请查看 NetLogo API 文档。

纳迪姆

I managed to find out what the proper way should be. This is the code in the NetLogo mailing list as given by Seth Tisue.

import org.nlogo.headless.*;
import org.nlogo.api.*;
class J {
   public static void main(String[] args) {
      try {
         HeadlessWorkspace ws = HeadlessWorkspace.newInstance();
         ws.openString(org.nlogo.util.Utils.url2String("/system/empty.nlogo"));
         ws.command("cro 8 [ fd 5 ]");
         org.nlogo.api.Turtle turtle =(org.nlogo.api.Turtle) ws.world().turtles().agent(3);
         System.out.println("[xcor] of turtle 3 = " + turtle.xcor());
         ws.dispose();
      }
      catch(Exception ex) {
         ex.printStackTrace();
      }
   }
}

I have reproduced the code here so it may benefit others. To see a list of what information you can get from Turtle, look at the NetLogo API documentation.

Nadim

街角迷惘 2024-09-17 10:29:24

所以,你使用 Java API 只是为了让你得到

[xcor] of "bob" 10

我很困惑的信息。

我可以告诉您,上面的workspace.report() 调用非常昂贵,因为您要求netlogo 解析然后评估您创建的表达式,然后将其解析为整数以传递回netlogo。

将所有玩家存储在列表或地图中并通过列表中的索引引用它们似乎会容易得多。也就是说,我认为您不需要使用 API 来完成您看起来正在做的事情。

So, you are using the Java API just so you can get the

[xcor] of "bob" 10

I am very confused.

I can tell you that your workspace.report() call above is very expensive as you are asking netlogo to parse then evaluate the expression you create, then you parse it into an integer to pass back to netlogo.

It seems it would be much easier to just store all the players in a list or map and refer to them by their index in the list. That is, I don't think you need to use the API to do what you seem to be doing.

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