English 中文(简体)
Using the NetLogo API to get turtle coordinates
原标题:
  • 时间:2010-07-19 11:39:20
  •  标签:
  • java
  • netlogo

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

最佳回答

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

问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签