Gannicus
Gannicus

Reputation: 530

Running a function for every present turtle from each of the turtle's perspectives

From each of the turtle's perspectives, I have to run a function for a turtle to decide which turtle it will assign as it's "buddy".

Right now I have the code below but it doesn't achieve the effect.

foreach sort other turtles [
      ask ? [
        if Smin < Sim myself ? and self != ? [
        ]
      ]
 ]

In C/Java it would've been simple, just a simple for loop and then that's it. Apparently I am having a hard time understanding NetLogo's foreach function and the integration of '?' in looping. How can I do this?

Upvotes: 0

Views: 83

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

It's unclear from the code sample you posted what exactly you are trying to do.

Some things that may help:

  • Unless you want to address your turtles in a specific order, it's usually not necessary to use foreach. Just doing ask other turtles [ ... ] can replace the whole foreach sort other turtles [ ask ? [ ... ] ].
  • Given that you are inside an ask ? block, self != ? will always be false, and thus, so will the and clause of your if. The code inside your inner block is never reached.
  • myself refers the agent from an "outer" ask block (e.g., in ask x [ ask y [ ... ] ], self would be y and myself would be x). Neither myself nor self is affected by foreach, and ? is not affected by ask.

My guess is that maybe you just want:

ask other turtles [
  if Smin < Sim myself self [
  ]
]

But I can't know for sure, especially since I have no idea what Smin and Sim are. If you post more detail, maybe we can help you further.

Finally: NetLogo code usually ends up being much simpler than the equivalent C/Java code, but you must learn to embrace the "NetLogo way". Thinking in Java/C and then trying to translate in NetLogo will usually lead one astray.

Upvotes: 1

Related Questions