question about initializing agents

16 views
Skip to first unread message

Jake Saunders

unread,
Jul 3, 2019, 8:09:14 AM7/3/19
to netlogo-devel
Hi

I am trying to initialise and add agents to a netlogo model through the extension api.

This is causing me some issues mostly down to creating the actual agent adding it directly onto the model. i cannot set an agent variable as i cannot initialise it (probably due to my own lack of knowledge).

Can someone please advise me

Kind regards
Jake Saunders 

Michael Tamillow

unread,
Jul 3, 2019, 1:07:06 PM7/3/19
to Jake Saunders, netlogo-devel
Hi Jake,

I'll try to help you if I can, but I'm little rusty.

I am thinking you could use the 'world' object, it is not in the extension API.

For some applications, you may want more power than the controlling API can provide. In these cases, you need to import from the actual Netlogo packages, rather then org.nlogo.api.

My imports in the Auction module look like this: Auction Github Link

import org.nlogo.agent.World
import org.nlogo.api.ExtensionException
import org.nlogo.{agent, api, core}
import core.Syntax._
import api.ScalaConversions._ // implicits
import scala.collection.mutable.HashMap

As you can see, I am importing from org.nlogo.agent.World


When you look at the documentation for World, you will see a method called createTurtle. I believe this is the method you need. After you create the turtle you can modify the attributes associated with each agent so that the agent is in the proper location with the proper internal states. You can even getOrCreateTurtle if you have clear expectations of what you want.

You can see that even though the nlogo.api has a lot of power, if you want unlimited power (and I, for one, do!) you can drill down to actual netlogo code. At one point I needed this line:

val turt = turtle.asInstanceOf[agent.Turtle]

because there is a method in the nlogo.agent.Turtle object that is not in the  nlogo.api.Turtle. Try that, let me know how it works.

Hope that helps,
Mike


--
You received this message because you are subscribed to the Google Groups "netlogo-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-devel/fb976ca0-d1b2-4279-be8d-4647c11d8b8b%40googlegroups.com.

Jake Saunders

unread,
Jul 3, 2019, 1:30:03 PM7/3/19
to netlogo-devel
Hi

Thank you for your help 


I have since been able to create the turtles using this method the real problem is:

I cant create them to be different breeds without getting a invalid breed error. the breed is already initialised within the netlogo code but for some reason doesn't work. Any insights ?


Here is the code:


Java 

World world = (World) cntx.getAgent().world();

AgentSet breed = world.getBreed("males");

Turtle t = world.createTurtle(breed);


Netlogo 

extensions [epiInput];
Breed [males male];
Breed [females female ];

males-own [
  age


]



Error 

java.lang.IllegalArgumentException: invalid breed
 at org.nlogo.agent.TurtleManagement.getVariablesArraySize(TurtleManagement.scala:122)
 at org.nlogo.agent.TurtleManagement.getVariablesArraySize$(TurtleManagement.scala:118)
 at org.nlogo.agent.World.getVariablesArraySize(World.scala:109)
 at org.nlogo.agent.Turtle.<init>(Turtle.java:87)
 at org.nlogo.agent.Turtle2D.<init>(Turtle2D.java:12)
 at org.nlogo.agent.World2D.createTurtle(World.scala:225)
 at demographicInput.createAgents(demographicInput.java:182)
 at demographicInput.report(demographicInput.java:67)
 at org.nlogo.prim._externreport.report(_externreport.java:31)
 at org.nlogo.prim._asm_procedurego_setprocedurevariable_5.perform()
 at org.nlogo.nvm.Context.stepConcurrent(Context.java:107)
 at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.scala:65)
 at org.nlogo.job.JobThread.runPrimaryJobs(JobThread.scala:133)
 at org.nlogo.job.JobThread.$anonfun$run$1(JobThread.scala:68)
 at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
 at scala.util.control.Exception$Catch.apply(Exception.scala:224)
 at org.nlogo.api.Exceptions$.handling(Exceptions.scala:41)
 at org.nlogo.job.JobThread.run(JobThread.scala:66)

Kind regards
Jake 
To unsubscribe from this group and stop receiving emails from it, send an email to netlog...@googlegroups.com.

Michael Tamillow

unread,
Jul 3, 2019, 4:22:51 PM7/3/19
to Jake Saunders, netlogo-devel
Line 118 of TurtleManagement.scala:

  def getVariablesArraySize(turtle: org.nlogo.api.Turtle, breed: org.nlogo.api.AgentSet): Int = {
    if (breed == _turtles) {
      program.turtlesOwn.size
    } else {
      if (breed == null) throw new IllegalArgumentException("invalid breed")
      val breedOwns = program.breeds(breed.printName).owns
      program.turtlesOwn.size + breedOwns.size
    }
  }

}

So, that means your line 

AgentSet breed = world.getBreed("males");

is returning null.

Perhaps the lines in your Java program are being executed before the Netlogo program even starts, which would mean the environment in Netlogo is not initialized with the breeds.

My advice would be localize the Java code in such a way that you can guarantee that it will never execute before the breeds are initialized.

Let me know what you find,
Mike

To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-devel/d336d431-64a9-49b0-b778-c50beca3241f%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages