Netlogo 为海龟群创建渐变

Netlogo 为海龟群创建渐变,netlogo,Netlogo,我用总共1000只海龟创建了一个矩形海龟网格 let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ] ask in-shape [ sprout 1 ] 让[pxcor>=-50和pxcor=-5以及pycor对形状补丁进行广泛编辑,以响应细化问题的讨论 首先,我想从一个更简单的代码开始。我相信这与您的代码完全相同,没有,而[true]。为了清晰起

我用总共1000只海龟创建了一个矩形海龟网格

let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]

让[pxcor>=-50和pxcor=-5以及pycor对形状补丁进行广泛编辑,以响应细化问题的讨论

首先,我想从一个更简单的代码开始。我相信这与您的代码完全相同,没有
,而[true]
。为了清晰起见,我删除了您正在创建的额外1000只海龟,并将种子是否为两个独立的
ask
语句分隔为
ifelse
。我还移动了颜色,直到值计算完成

globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed? gradient_value]

to setup
  clear-all
  resize-world -60 60 -20 20
  let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
  ask in-shape
  [ sprout 1
    [ set shape "circle"
      set gradient_seed? false
    ]
  ]
  set talk_radius 4
  set gradient_max 100000
  set gradient_distance 1

  repeat 10 [ gradient-formation ]
end

to gradient-formation
  ask turtles-on patch -50 5
  [ set gradient_seed? true
    set gradient_value 0
  ]
  ask turtles with [not gradient_seed?]
  [ set gradient_value gradient_max
    ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance]
    [ let my-gradval ([gradient_value] of self)
      if my-gradval < [gradient_value] of myself
      [ ask myself [set gradient_value my-gradval]
      ]
    ]
    set gradient_value (gradient_value + 1)
  ]

  ask turtles [set color scale-color green gradient_value 0 120 ]
end
添加了一个最小工作示例来显示差异

这是
let
(全局变量)版本


针对细化问题的讨论进行了广泛编辑

首先,我想从一个更简单的代码开始。我相信这与您的代码完全相同,没有
,而[true]
。为了清晰起见,我删除了您正在创建的额外1000只海龟,并将种子是否为两个独立的
ask
语句分隔为
ifelse
。我还移动了颜色,直到值计算完成

globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed? gradient_value]

to setup
  clear-all
  resize-world -60 60 -20 20
  let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
  ask in-shape
  [ sprout 1
    [ set shape "circle"
      set gradient_seed? false
    ]
  ]
  set talk_radius 4
  set gradient_max 100000
  set gradient_distance 1

  repeat 10 [ gradient-formation ]
end

to gradient-formation
  ask turtles-on patch -50 5
  [ set gradient_seed? true
    set gradient_value 0
  ]
  ask turtles with [not gradient_seed?]
  [ set gradient_value gradient_max
    ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance]
    [ let my-gradval ([gradient_value] of self)
      if my-gradval < [gradient_value] of myself
      [ ask myself [set gradient_value my-gradval]
      ]
    ]
    set gradient_value (gradient_value + 1)
  ]

  ask turtles [set color scale-color green gradient_value 0 120 ]
end
添加了一个最小工作示例来显示差异

这是
let
(全局变量)版本


再次感谢你的回答JenB。是的,只有一个种子,你完全理解了这个练习,但我描述得不够好。事实上,还有一件事需要考虑:我正试图模拟能力有限的物理机器人,所以利用群体智能的概念,我必须建立一些分散的东西。我不是一个机器人允许使用像max这样的命令,比如说“全局”。您的解决方案都是正确的,但它们不是分散的。我的解决方案有效,如果我使用
让a
。我想知道为什么它不使用
设置a
。我认为在这种情况下它们的工作原理是一样的。如果您希望它运行循环数次,那么您需要将其放入
重复
块或类似块中。我无法复制您的prlet/set有问题。在切换到
min
版本之前,我确实
让我的gradival…
并将
我的gradival
添加到
海龟自己的
中,没有问题。如果我使用
让我的gradival…
并将
我的gradival
放在
海龟自己的
中,它会说:“已经有一个名为MY-GRADVAL的turtle变量了”。此外,在设置过程中,我调用了
gradient formation
,它只调用了一次,因此gradient没有完全构建,我不得不在[true]时将其放入
循环。正确吗?是的,你要么需要
设置
+
海龟自己的
,要么
而不是
海龟自己的
。这是因为“let”创建变量并将其设置为值。再次感谢你的回答JenB。是的,只有一个种子,你正确理解了这个练习,但我没有描述得足够好。事实上,还有另一件事需要考虑:我试图模拟能力有限的物理机器人,所以利用群体智能的概念,我必须建立一些分散的东西。我不允许使用像max这样的命令,比如说,“全局的”"。您的解决方案都是正确的,但它们不是分散的。我的解决方案有效,如果我使用
让a
。我想知道为什么它不使用
设置a
。我认为在这种情况下它们的工作原理是一样的。如果您希望它运行循环数次,那么您需要将其放入
重复
块或类似块中。我无法复制您的prlet/set有问题。在切换到
min
版本之前,我确实
让我的gradival…
并将
我的gradival
添加到
海龟自己的
中,没有问题。如果我使用
让我的gradival…
并将
我的gradival
放在
海龟自己的
中,它会说:“已经有一个名为MY-GRADVAL的turtle变量了”。此外,在设置过程中,我调用了
gradient formation
,它只调用了一次,因此gradient没有完全构建,我不得不在[true]时将其放入
循环。是否正确?是的,您需要
设置
+
海龟自己的
,或者
不使用
海龟自己的
。这是因为“let”创建变量并将其设置为值。
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed? gradient_value]

to setup
  clear-all
  resize-world -60 60 -20 20
  let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
  ask in-shape
  [ sprout 1
    [ set shape "circle"
      set gradient_seed? false
    ]
  ]
  set talk_radius 4
  set gradient_max 100000
  set gradient_distance 1

  repeat 10 [ gradient-formation ]
end

to gradient-formation
  ask turtles-on patch -50 5
  [ set gradient_seed? true
    set gradient_value 0
  ]
  ask turtles with [not gradient_seed?]
  [ set gradient_value gradient_max
    ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance]
    [ let my-gradval ([gradient_value] of self)
      if my-gradval < [gradient_value] of myself
      [ ask myself [set gradient_value my-gradval]
      ]
    ]
    set gradient_value (gradient_value + 1)
  ]

  ask turtles [set color scale-color green gradient_value 0 120 ]
end
to gradient-formation
  ask turtles-on patch -50 5
  [ set gradient_seed? true
    set gradient_value 0
  ]
  ask turtles with [not gradient_seed?]
  [ set gradient_value 1 + min [gradient_value] of 
      other turtles in-radius min (list talk_radius gradient_distance)
  ]

  ask turtles [set color scale-color green gradient_value 0 120 ]
end
turtles-own [testval]

to testme
  clear-all
  create-turtles 500
  [ setxy random-xcor random-ycor
    set color blue
    set testval 1 + random 10
  ]
  ask one-of turtles
  [ set color red
    inspect self
    type "testval of asking turtle is " print testval
    ask turtles-on neighbors
    [ set color yellow
      let my-testval [testval] of self   ;; creates a temp global variable
      type "my-testval is " print my-testval
      if my-testval < [testval] of myself
      [ ask myself
        [ set testval my-testval        ;; copies the global variable value
        ]
      ]
    ]
  ]
end  
turtles-own [testval my-testval]

to testme
  clear-all
  create-turtles 500
  [ setxy random-xcor random-ycor
    set color blue
    set testval 1 + random 10
  ]
  ask one-of turtles
  [ set color red
    inspect self
    type "testval of asking turtle is " print testval
    ask turtles-on neighbors
    [ set color yellow
      set my-testval [testval] of self
      type "my-testval is " print my-testval
      if my-testval < [testval] of myself
      [ ask myself
        [ set testval my-testval    ;; copies value from one attribute to other
        ]
      ]
    ]
  ]
end