I am quite new to NetLogo and here is what I am stuck here for weeks.
What I want to do is to make agents to be in group of 4 for 2 teams.
My plan is to make a function hold 4 turtles id,
to assign-groupmates [a1 a2 a3 a4]
and assign them to team 1
assign-groupmates [a1 a2 a3 a4] =team1[]
if team1 > 4
assign-groupmates [a1 a2 a3 a4] =team2[]
What I done is:
to assign-groupmates [ f g h i]
let A who random f
let B who random g
let C who random h
let D who random i
ifelse f != g, g != h, h != i, i != g
[ set group-id 1
separate
align
cohere ]
[assign-groupmates [W X Y Z]
set group-id 2]
end
How can I find the turtles-id and how can i send them through parameter? the turtles-id I used is who random.
Thank you.
There are many different ways to accomplish what you want, but let me start with a piece of general advice:
Don't use
who
numbers if you can avoid it.They have some legitimate uses, but those are few and far between. They are error prone, they lead to ugly code, and they tend to make you think the wrong way about the problem you are trying to solve.
NetLogo lets you store direct references to agents, for example:
In general, use that instead.
In your case, though, you should probably not be storing individual references to agents. Since you are dealing with groups of agents, you should be working with agentsets instead.
Here is one suggestion: make a global list of agentsets called
groups
. In addition to that, have each agent store a reference to the agentset that constitute its group. Here is one way to accomplish that:This code has the advantage of being very flexible. If you ever want more than two groups, or more than four turtles per group, it's a very easy change.
Another piece of general advice: if you ever find yourself using multiple variables like
a1
,a2
,a3
, etc., you are probably doing something wrong. You should be using lists and/or agentsets instead.