NetLogo-计算相邻代理变量的差 [英] NetLogo - calculate the difference of a variable of neighbouring agents

查看:80
本文介绍了NetLogo-计算相邻代理变量的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让每个特工向他们的邻居询问乌龟拥有的变量的值,并根据其所拥有的差异进行设置.

I would like to have each agents ask their neighbours of their value of a turtle-owned variable and set them according to the differences the have.

我知道如何做到这一点:

I know how to do this for distances:

if (any? other turtles-here)
  [
    ask neighbors [ ;; ask 8 neighbors / neighbors4 for 4
      ;if (max-one-of turtles [distance myself]) <= 3
      ;[set opinion opinion - .1] ; no change in opinion
      ;if (distancexy point1-pxcor point1-pycor) > 20 and (distancexy point1-pxcor point1-pycor) <= 50
      ;[set point1-location "middle"]
      ;if (distancexy point1-pxcor point1-pycor) > 50
      ;[set point1-location "far"]
    ]

但是我为实现价值交换而努力实施它.我该如何实现?

however I struggle with implementing it for an exchange of values. How do I achieve this?

这是我的MWE.

请注意,所涉及的代码部分是伪代码.

Note that the code part in question is in pseudo-code.

breed [ turtles ]
turtles-own [ variable ]

to setup
  clear-all
  create-turtles 100
  [
    set variable random-float 10
  ]
  reset-ticks
end

to communicate
  if (any? other turtles-here)
  [
    ask neighbors [
      pseudo-code: if difference of your variable and my variable is bigger then 3, than do nothing
      if differences less then 3, calculate the higher variable minus 0.1 and the lower variable plus 0.1
      if difference less then 2, calculate the higher variable minus 0.3 and the lower plus 0.3
      if difference less then 1, calculate the arithmetical mean
    ]
  ]

end

to go
  ask turtles [
    rt random 360
    fd 1
    communicate
  ]
  tick
end

推荐答案

您的伪代码为解释留下了很大的空间,因此这可能不是您想要的,但是我认为它可以帮助您入门:

Your pseudo-code leaves a lot of room for interpretation, so this might not be exactly what you want, but I think it can get you started:

to communicate
  ask turtles-on neighbors [
    let both-turtles (turtle-set self myself)
    let difference abs (variable - [ variable ] of myself)
    if difference < 1 [ ask both-turtles [ set variable mean [ variable ] of both-turtles ] ]
    if difference < 2 [ bring-closer both-turtles 0.3 ]
    if difference < 3 [ bring-closer both-turtles 0.1 ]
  ]
end

to bring-closer [ both-turtles delta ]
  ask min-one-of both-turtles [ variable ] [ set variable variable + delta ]
  ask max-one-of both-turtles [ variable ] [ set variable variable - delta ]
end

这里发生了很多事情,但是没有什么太复杂了.我认为您在这里必须理解的主要概念是:selfmyself,将代理集存储在局部变量中的想法以及编写带有参数的过程的想法.您可以在编程指南中了解所有内容,并查找相关内容字典中的基元.

There is a lot going on here, but nothing overly complicated. I think the main concepts you will have to understand here are: self and myself, the idea of storing an agentset in a local variable, and the idea of writing a procedure that takes arguments. You can read about all of this in the programming guide and look up the relevant primitives in the dictionnary.

这篇关于NetLogo-计算相邻代理变量的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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