Reputation: 117
I need to create a simulation code in NetLogo where agents from nest spread out in world to find food. There will be 10 agents in the nest.
The problem I'm facing now is I need to arrange these agents' position in the nest so that they won't overlap each other. Thus, I plan to sort their position using an array.
But I'm having problem to specify their location using array since I'm still struggling to undertsand NetLogo.
The code below is my attempt to write an array but to no avail.
to setup
ca
create-turtles 10
[
set size 2
]
setup-patches
sort-agent
end
to sort-agent
let n turtles
foreach sort turtles [ setup-nest
ask turtles
[
set plabel n
set n n + 1
]
]
end
to setup-patches
ask patches
[ setup-nest]
end
to setup-nest
set nest? (distancexy 0 0) < 6
end
Can someone help me? Thank you so much.
Upvotes: 1
Views: 199
Reputation: 17678
If you want to create turtles that are spread out, select random patches and use sprout
(assuming there are fewer turtles required than patches available). Based on your comments in response to Seth's answer, I think this is more what you want. I would like to reiterate his comment that you are trying to do too much at once. An obvious break here would be to create the nest (and check that works) before even thinking about creating the turtles within the nest.
globals [nest]
to setup
clear-all
setup-nest
setup-turtles
end
to setup-nest
set nest patches with [distancexy 0 0 < 6]
ask nest [ set pcolor red ]
end
to setup-turtles
ask n-of 10 nest
[ sprout 1
[ set label who
]
]
end
Upvotes: 2
Reputation: 30453
By let n turtles
, I think you probably meant let n count turtles
. By set plabel n
, I think you probably mean set label n
(you want to label the turtles, not the patch they're standing on, right?).
It's unclear why you call setup-nest
inside sort-agent
. I think you simply want to remove that?
It's also unclear why you put a call to ask turtles
inside your foreach
loop. ask turtles
always asks all turtles; I assume your intent was to ask some particular turtle, here...?
I doubt you need to use a list at all here. (NetLogo calls them lists, not arrays.) If I understood the problem you were trying to solve better, perhaps I'd decide involving lists make sense, but for now, I'm skeptical.
If you simply want to label the turtles according to the order they were created, you can just do:
ask turtles [ set label who ]
Or even if label at the time you create them:
create-turtles 10 [ set label who ]
But if this isn't suitable for some reason, a way to accomplish the same thing, with similar code to what you provided, is:
let n 0
foreach sort turtles [
ask ? [
set label n
set n n + 1
]
]
You don't say if you're using NetLogo 5 or 6. I've used NetLogo 5 syntax, but I can change it to 6 if you want.
As a general remark, it appears to me you are trying to write too much code all at once. As a result, the code you're writing has so many things wrong with it that you're going to have a lot of trouble trying to fix all of them at the same time. I'd suggest starting with working code and then try to make very small improvements to it, one at a time, getting each improvement working as you go. Anytime you're having to write a bunch of code at all once, that means you're tackling too big a problem and you should find a smaller version of it to tackle first.
Upvotes: 2