How to send parameters using NetLogo?

2019-07-29 03:58发布

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.

标签: netlogo
1条回答
趁早两清
2楼-- · 2019-07-29 04:32

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:

ask turtles [ set friend one-of other turtles ]

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:

globals [ groups ]
turtles-own [ my-group ]

to setup
  clear-all

  let number-of-groups 2
  let turtles-per-group 4  
  create-turtles turtles-per-group * number-of-groups 

  set groups [] ; empty list
  repeat number-of-groups [
    let new-group n-of turtles-per-group turtles with [
      not is-agentset? my-group
    ]
    ask new-group [ set my-group new-group ]
    set groups lput new-group groups
  ]

  print (word "First group:  " sort first groups)
  print (word "Second group: " sort last groups)
  ; each turtle can easily access other members of its group:
  ask turtles [ show sort other my-group ]

end

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.

查看更多
登录 后发表回答