1

我是 netlogo 的新手,并且一直在结合派系模型和遵循/避免模型的代码。

我已经将我的海龟分成派系(显示为不同的颜色),我试图让它们跟随它们上方的派系,并避免它们下方的派系。如果有人可以看看我的代码会很棒,我认为它不起作用,因为我试图使用“品种”来区分海龟在他们之上/之下的判断,并且所有海龟都需要不同。我已经把我认为不允许它工作的区域以粗体显示..

breed [ above ]
breed [ below ]

turtles-own
[ faction ]

to setup
  clear-all
  ask patches [ set pcolor white ]
  set-patch-size 10
  resize-world min-pxcor max-pxcor min-pycor max-pycor 
  ask patch 0 0
   [ ask patches in-radius ( max-pxcor * .9) with [  random-float 100 < density ]
     [ sprout 1
       [ 
         set shape "circle"
         set faction random factions
         set color faction-color
         set size 1.1
         judge
       ]  ]   ]
   reset-ticks
end

to go
  ask turtles [ avoid ]   
  ask turtles
  [ fd 0.1
    if any? above in-radius 360; vision in patches and degrees
    [ set heading (towards min-one-of above [distance myself]) ] ]; adjusts heading to point towards   

 ask turtles
  [ fd 0.1
    if any? below in-radius 360; vision in patches and degrees
    [ set heading (towards min-one-of below [distance myself]) + 180 ] ] ; adjusts heading to point away from wanderer

  tick
end

to judge

  if turtle color = faction-color + 30 
  [ set breed above ]

  if turtle color = faction-color 
  [ set breed same ]

  if turtle color = faction-color - 30 
  [ set breed below ]

end

;; EXTRAS

to avoid
      ifelse not any? other turtles-on patch-ahead 1
      [ fd 0.1 ]
      [ rt random 360 ]
end

to-report faction-color
   report red + faction * 30
end

如果有人能指出我正确的方向,那就太好了。再次感谢。

4

1 回答 1

3

你离得并不远。您已正确确定您对品种的使用不恰当。“上方”或“下方”的属性是相对于正在询问的海龟而言的,而不是海龟本身的属性。可能有更好的方法来执行此操作,但只需对代码进行最少的更改,您就可以使用以下内容:

to go
  ask turtles [ avoid ]    
  ask turtles [
    fd 0.1
    let above turtles with [ color = [faction-color] of myself + 30 ]
    if any? above in-radius 360; vision in patches and degrees
      [ set heading (towards min-one-of above [distance myself]) ] ; adjusts heading to point towards   
  ]
  ask turtles [
    fd 0.1
    let below turtles with [ color = [faction-color] of myself - 30 ]
    if any? below in-radius 360; vision in patches and degrees
      [ set heading (towards min-one-of below [distance myself]) + 180 ] ; adjusts heading to point away from wanderer
  ]
  tick
end

(然后您可以完全摆脱品种和您的judge程序。)

于 2013-01-23T16:06:08.277 回答