首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据海龟的颜色和最小距离将它们聚在一起

如何根据海龟的颜色和最小距离将它们聚在一起
EN

Stack Overflow用户
提问于 2020-08-10 14:46:06
回答 2查看 317关注 0票数 1

我正在用NetLogo写一个模型,从5只海龟的颜色(蓝色、绿色、黄色、红色、白色)开始,并随机开始协调。当我运行模型时,我希望当白海龟遇到绿色、黄色或红色的海龟时,它们会和它结合在一起,并作为一个团体一起移动。当白色的海龟遇到蓝色的海龟时,它就会与它分离,两者都会继续随机移动。我在模型库中看到了鸟群的示例模型,我一直试图为我的模型修改它(参见所附代码)。

在模特儿库中的“成群例子”中,海龟被设置成对齐、合拢和分开,当它们彼此太近时(取决于最近邻居的距离和最小的距离)。这很好用,我可以用它告诉海龟羊群,保持完整的团体,因为他们继续移动。我的问题是,在遇到白海龟的时候,怎样才能让(绿色、黄色、红色)海龟成群,让蓝色的海龟随意移动。

代码语言:javascript
复制
turtles-own [
  flockmates         ;; agentset of nearby turtles
  nearest-neighbor   ;; closest one of our flockmates
]
;Setting up the 5 turtles here
to setup
  clear-all
  create-turtles pop1
    [ set color blue ; all pop
      set size 1.5  ;; easier to see
      setxy random-xcor random-ycor
      set shape "person"]
        
  create-turtles pop2
    [ set color green ; stage1
      set size 1.5  ;; easier to see
      setxy random-xcor random-ycor
      set shape "person"]
  
    create-turtles pop3
    [ set color yellow ; stage2
      set size 1.5  ;; easier to see
      setxy random-xcor random-ycor
      set shape "person"]
  
    create-turtles pop4
    [ set color red ;stage3
      set size 1.5  ;; easier to see
      setxy random-xcor random-ycor
      set shape "person"]
  
    create-turtles pop5
    [ set color white ; chv
      set size 1.5  ;; easier to see
      setxy random-xcor random-ycor
      set shape "person"]
  reset-ticks
end
to go
  ask turtles [ flock ]
  ;ask turtles [ meet]
  ;; the following line is used to make the turtles   ;; animate more smoothly.
  repeat 5 [ ask turtles [ fd 0.2 ] ] ;this seems much faster
  ;; for greater efficiency, at the expense of smooth
  ;; animation, substitute the following line instead:
  ;ask turtles [ fd 1 ] ;than this
  tick
end
;How do I make to get only the (green, yellow, red) turtles to flock when 
;they meet the white turtles. Leaving the blue turtles moving randomly. How can do that? 
;  find-samecolor
;  if any? flock
;   ifelse separate
;end
;If green, yellow, red meets white turtle, they move together; nearest distance
to flock  ;; turtle procedure
  find-flockmates
  if any? flockmates
    [ find-nearest-neighbor
      ifelse 
        distance nearest-neighbor < minimum-separation
        [ align ]
        [ cohere ]    
      ;[separate]
  ]
       
end
;If blue meets white turtle, they move together; nearest distance
to find-flockmates  ;; turtle procedure
  set flockmates other turtles in-radius vision
end
to find-nearest-neighbor ;; turtle procedure
  set nearest-neighbor min-one-of flockmates [distance myself]
end
;;; SEPARATE
;to separate  ;; turtle procedure 
;  turn-away ([heading] of nearest-neighbor) max-separate-turn
;end
;;; ALIGN
to align  ;; turtle procedure
  turn-towards average-flockmate-heading max-align-turn
end
to-report average-flockmate-heading  ;; turtle procedure
  ;; We can't just average the heading variables here.
  ;; For example, the average of 1 and 359 should be 0,
  ;; not 180.  So we have to use trigonometry.
  let x-component sum [dx] of flockmates
  let y-component sum [dy] of flockmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end
;;; COHERE
to cohere  ;; turtle procedure
  turn-towards average-heading-towards-flockmates max-cohere-turn
end
to-report average-heading-towards-flockmates  ;; turtle procedure
  ;; "towards myself" gives us the heading from the other turtle
  ;; to me, but we want the heading from me to the other turtle,
  ;; so we add 180
  let x-component mean [sin (towards myself + 180)] of flockmates
  let y-component mean [cos (towards myself + 180)] of flockmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end
;;; HELPER PROCEDURES
to turn-towards [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings new-heading heading) max-turn
end
;to turn-away [new-heading max-turn]  ;; turtle procedure
;  turn-at-most (subtract-headings heading new-heading) max-turn
;end
;; turn right by "turn" degrees (or left if "turn" is negative),
;; but never turn more than "max-turn" degrees
to turn-at-most [turn max-turn]  ;; turtle procedure
  ifelse abs turn > max-turn
    [ ifelse turn > 0
        [ rt max-turn ]
        [ lt max-turn ] ]
    [ rt turn ]
end
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-19 14:11:49

抱歉,你第一次问这个问题的时候,由于某种原因,我没有看到这个问题。本代码的关键步骤是:

代码语言:javascript
复制
to find-flockmates  ;; turtle procedure
  set flockmates other turtles in-radius vision
end

它所做的就是说,任何海龟可以看到的都被认为是一只发条鱼。你想要做的是说,蓝海龟不可能是成群的(也就是说,它们不会自己创造羊群,也不会被其他颜色的海龟认为是联锁的)。试试这个:

代码语言:javascript
复制
to find-flockmates  ;; turtle procedure
  if color != blue
  [ set flockmates other turtles with [color != blue] in-radius vision
  ]
end

第一条线是为了让蓝海龟找不到成群的伴侣。第二行把蓝海龟排除在其他人的羊群之外。

-更新

实际上,在海龟的成群过程中,你有这个程序。这个程序将变量“flockmates”设置为找到的一组海龟,但是,如果海龟是蓝色的,则没有flockmates,也不会返回空的turtleset。修复它的最好方法是使用一个to-report过程,这样所有的海龟都会有一个适合他们的变量的数据类型(海龟集)。

代码语言:javascript
复制
to-report find-flockmates  ;; turtle procedure
  ifelse color = blue
  [ report no-turtles ]    ; this is an empty turtle-set
  [ report other turtles with [color != blue] in-radius vision ]
end

然后,要使用此过程,需要更改:

代码语言:javascript
复制
to flock  ;; turtle procedure
  find-flockmates
  if any? flockmates

对此:

代码语言:javascript
复制
to flock  ;; turtle procedure
  set flockmates find-flockmates
  if any? flockmates
票数 0
EN

Stack Overflow用户

发布于 2020-08-19 10:52:10

你可以让两种不同类型的海龟拥有自己的规则。我做了一个多代理的工作,每个人都有自己的规则,有了执法者、海龟、自己的团队,一切都像魅力一样运作。

代码语言:javascript
复制
ant-own[ velocita-ant metabolismo-ant sf ;;scorta ants massimo_sf] killer own[velocita-killer metabolismo-killer sk ;;scorta killers massimo_sk]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63342515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档