如何访问netlogo的agentset中的代理内的属性?

时间:2019-07-25 08:31:17

标签: netlogo agent agentset

问题出在这里

我需要将turtles-own内的值best-food与同一座席集中的其他座席进行比较,但是我不知道如何让座席向队友询问该值,并且我需要他们在整个模拟过程中不断地相互比较彼此的最佳食物价值。 (以便他们可以获得最高价值的食物)

如果是Java语言,则类似于这样=

if agent1.best-food > agent2.best-food then agent1 go to agent2

但是在netlogo中,我不能这样做。有人可以帮我吗?

代码如下:

food_quality =在世界上找到的食品代理商

food_quality_found = food_quality的价值由乌龟拥有

best-food =每个代理商都将自己的食品质量保存在最佳食品中;然后所有座席比较哪个值最高,并且此比较发生在座席集中。

ask myteamset [ set food-quality-found food-quality
set best-food food-quality-found set location patch-here set loca-best-food location]

ask myteamset[

            if best-food != 0 ; I need them to compare constantly so "!=0" need to be substitute with his teammates' best-food, but I don't know how to ask other teammates in same agentset about this value
            [

if (best-food < food-quality-found) ;to make sure best-food always have the highest value. [set best-food food-quality-found set location patch-here set loca-best-food location]

          if best-food > best-food ;

从逻辑上讲这是不正确的,因为它们所有人都比较自己的最佳食物价值,但是我不知道如何问其他海龟其最佳食物并将其与我的比较。。               [

              set g random 100
              if (g >= probability_teammates_to_go) 
             [ move-to loca-best-food]]  

if (patch-here is loca-best-food) ;how to write if with patches? [set i random 100 if (i >= probability_to_ignore) ;after agent arrives at best-food, they still need to choose whether or not they want to stay there. [ fd 0.25 ]

我尝试了let best-food-turtle (turtles with-max [food-quality-found]) 但这并不能将其价值与他的所有队友进行比较,而只是与一位特定的经纪人进行比较,这是错误的……

谢谢。

1 个答案:

答案 0 :(得分:0)

这里是您的问题的简化版本的解决方案。我已经忽略了所有不同类型的食物变量,只是为每只乌龟分配了一个随机变量,称为食物变量。每只乌龟还是红色或蓝色团队的成员(分别称为R和B)。他们找到了食物价值最高的团队成员,与那只乌龟建立了联系,并面对那只乌龟。

这里的重要原语是max-one-of,它标识指定代理集中具有指定变量(在这种情况下为食物)的最大值的代理(在这种情况下,同一团队中的所有海龟) )。

turtles-own
[ food
  team
]

to testme
  clear-all
  create-turtles 10
  [ setxy random-xcor random-ycor
    set food random 100
    set team one-of [ "B" "R" ]
    ifelse team = "B"
    [ set color blue ]
    [ set color red]
  ]
  ask turtles
  [ let best-teammate max-one-of turtles with [team = [team] of myself] [food]
    if self != best-teammate
    [ create-link-to best-teammate
      face best-teammate
    ]
  ]
end

您发现一只乌龟的价值最高,可以向ask询问那只乌龟的补丁(使用[patch-here] of ...)。如果只需要该值,则需要max而不是max-one-of