如何使用NetLogo发送参数? [英] How to send parameters using NetLogo?

查看:33
本文介绍了如何使用NetLogo发送参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对NetLogo还是很陌生,这就是我被困在这里数周的时间.

I am quite new to NetLogo and here is what I am stuck here for weeks.

我想要做的是让特工成为2组4组的成员.

What I want to do is to make agents to be in group of 4 for 2 teams.

我的计划是使一个函数容纳4个海龟id,

My plan is to make a function hold 4 turtles id,

to assign-groupmates [a1 a2 a3 a4]

并将其分配给团队1

assign-groupmates [a1 a2 a3 a4] =team1[]
if team1 > 4
assign-groupmates [a1 a2 a3 a4] =team2[]

我所做的是:

    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

如何找到乌龟ID,如何通过参数发送它们? 我使用的turtles-id是随机的.

How can I find the turtles-id and how can i send them through parameter? the turtles-id I used is who random.

谢谢.

推荐答案

有多种方法可以实现所需的功能,但让我从一条常规建议开始:

There are many different ways to accomplish what you want, but let me start with a piece of general advice:

请勿使用 who 可以避免的数字.

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允许您存储对代理的直接引用,例如:

NetLogo lets you store direct references to agents, for example:

ask turtles [ set friend one-of other turtles ]

通常,改用它.

但是,就您而言,您可能不应该存储对代理的单独引用.由于您要处理的是座席组,因此应该改为使用座席组.

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.

这里是一个建议:制作一个名为groups的代理集的全局列表.除此之外,还请每个代理存储对构成其组的代理集的引用.这是完成此任务的一种方法:

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.

另一条一般性建议:如果您发现自己使用了多个变量,例如a1a2a3等,则可能是在做错事.您应该改用列表和/或代理集.

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.

这篇关于如何使用NetLogo发送参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆