如何让龟在具有特定规则的补丁中移动?

时间:2014-06-24 05:38:36

标签: netlogo

我是Netlogo的新手,也有一些问题。如果你能帮助我,那将是很好的。

我想创造一些果蝇在绿色斑块组成的树周围移动。果蝇被树吸引。如果果蝇从树上移开一定距离(例如5个网格),它们将转回树上。

一开始,他们不会停留在绿色补丁上,因为他们有足够的能量。随着时间的推移,他们将失去能量。他们将找到最接近的绿色斑块,并在它们的能量达到0时保持一段时间。它们之后获得能量,并且它们只能从底部分支到顶部分支(3个跃点)进行短跳。当它们位于树的顶部时,苍蝇将移回到底部分支。我想我需要做一个WHILE循环,但我不知道该怎么做。请查看我的代码。

breed [flies fly]
breed [suns sun]

turtles-own [energy]
flies-own [count-down]


to setup 

  clear-all 
  setup-suns
  setup-flies 
  setup-patches 

  reset-ticks 

end

to setup-suns
  ;; Create the sun

   set-default-shape suns "sun"

   create-suns 1 [
     setxy max-pxcor - 3
     max-pycor - 3
     set color yellow
     set size 7
   ]
end

to setup-flies

  set-default-shape flies "bee 2"
  create-flies number-fly [ set color white setxy random-xcor random-ycor set count-down 15]

end

to setup-patches

  ;; Create sky and grass

  ask patches
    [ set pcolor blue  ]
  ask patches with [pycor < min-pycor + 2]
    [ set pcolor 66 ]

  ;; Create trunk and branches

   ask patches with [ pxcor = -15 and pycor <= 0 ] [ set pcolor brown ]

   ask patches with [ pxcor = -15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
   ask patches with [ pxcor = pycor - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
   ask patches with [ pxcor = pycor - 8 and pycor <= 2 and pxcor > -15 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) - 22 and pycor <= 2 and pxcor < -15 ] [ set pcolor lime ]
   ask patches with [ pxcor = pycor - 1 and pycor <= -1 and pxcor > -15 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) - 29 and pycor <= -1 and pxcor < -15 ] [ set pcolor lime ]

   ask patches with [ pxcor = 15 and pycor <= 0 ] [ set pcolor brown ]
   ask patches with [ pxcor = 15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
   ask patches with [ pxcor = pycor + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
   ask patches with [ pxcor = pycor + 22 and pycor <= 2 and pxcor > 15 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) + 8 and pycor <= 2 and pxcor < 15 ] [ set pcolor lime ]
   ask patches with [ pxcor = pycor + 29 and pycor <= -1 and pxcor > 15 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) + 1 and pycor <= -1 and pxcor < 15 ] [ set pcolor lime ]   

   ask patches with [ pxcor = -26 and pycor = -3 ] [ set pcolor red ]  
   ask patches with [ pxcor = -10 and pycor = 5 ] [ set pcolor red ]            
   ask patches with [ pxcor = 21 and pycor = -1 ] [ set pcolor red ] 



end 

to go 

  move-flies 
  tick

end

to move-flies

  ask flies [ 

    set energy 6

    ifelse energy > 10 [

      ;rt random 50 lt random 50 jump random-float 1 ;

      let nearest-leaf min-one-of (patches with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 5 grids - long range search.

      if is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf. 
       face nearest-leaf 
       ;set heading 45
       ;fd distance nearest-leaf
       rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
       ;move-to nearest-leaf ;
     ]
    ]

    [
     ifelse pcolor != lime 
     [ rt random 50 lt random 50 jump random-float 1 ; Initialization - flies fly around to search their resources.
       continue]
     [ stay ] 

    ]
 ] 

end

to continue 

  let nearest-leaf min-one-of (patches in-radius 1 with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 2 grids - short hops.

  ifelse is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf. 
     face nearest-leaf 
     fd random distance nearest-leaf
     ;ask patches in-radius 1 [set pcolor red]
     ;rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
     ;move-to nearest-leaf ;
  ]
  [ 
    let turn-back min-one-of (patches with [pcolor = lime] ) [distance myself] ;
    ;set heading 180
    face turn-back
    jump random-float 5  ] 

    move-up ;Flies tend to move up through all branches by short hops. Need a while loop.

    ; Move down if they reach the top of tree
    let canopy patch-at-heading-and-distance 0 1
    let canopy-left patch-left-and-ahead 45 1
    let canopy-right patch-right-and-ahead 45 1

    if canopy != lime or canopy-left != lime or canopy-right != lime
       [
         move-down
       ] 

end

to move-up
  ask flies [
     set heading one-of [0 30 -30] 
  ]
end 

to move-down
  ask flies [
     set heading one-of [180 120 -120] 
  ]
end 

to stay

 set count-down count-down - 1 ;;decrement timer
 set label count-down
 if count-down = 0
  [
    rt random 50 lt random 50 jump random-float 1
    set label ""
    reset-count-down ;;it's another procedure   
  ]

end

to reset-count-down

  set count-down 30

end

如果您对我的代码感到困惑,我很抱歉。我感谢您的帮助。感谢。

亲切的问候,

1 个答案:

答案 0 :(得分:1)

根据您的描述,我认为您几乎肯定不想要while循环。在NetLogo模型的go过程中,您只需指定单个刻度中发生的事情。因此,如果需要while来表达在一瞬间发生的事情,那么你只会使用{{1}},而不是在多个滴答中展开的过程。

(与弗兰克同意,这个代码太多,无法发布以获得帮助。我需要花一点时间阅读和研究这么多代码,更不用说尝试帮助你。回答我试图提取并解决一个方面。)