如何让一些海龟再次移动?

时间:2014-01-14 17:06:56

标签: simulation agent netlogo

在这个模型中,它所做的只是龟会找到一个座位(可用的红色补丁和拍摄的黄色)。一旦座位全部被占用,一切都会停止。

现在如何让一些海龟再次移动?就像坐着它会再次移动并尝试去另一个地方或它会熄灭。

breed [kids kid]
breed [adults adult]
breed [oldies old]    
kids-own [step]    
adults-own [step]    
oldies-own [step]
turtles-own [seated?]

to setup

  __clear-all-and-reset-ticks

  ask patches [setup-world]
  ask patches with [pcolor = red ][set plabel count turtles-here]
  set-default-shape turtles "person"
  create-kids number-of-kids 
  create-adults number-of-adults   
  create-oldies number-of-oldies

  ask kids[

    set color green
    set size 1              
    setxy -10 0
    set heading random-float 90
    rt 45 - random-float 90]

  ask adults[

    set color orange
    set size 1                                    
    setxy -10 0
    set heading random-float 45
    rt 45 - random-float 90]

  ask oldies[

    set color blue
    set size 1                                    
    setxy -10 0
    set heading random-float 45
    rt 45 - random-float 90]
end
to setup-world

  set pcolor white

   if ( pxcor = 10 ) and ( pycor < 10 and pycor > -11 ) [ set pcolor brown ]

   if ( pxcor = -10 ) and ( pycor < 10 and pycor > 1 ) [ set pcolor brown ]

   if ( pxcor = -10 ) and ( pycor < -1 and pycor > -11 ) [ set pcolor brown ]

   if ( pycor = 10 ) and ( pxcor < 11 and pxcor > -11 ) [ set pcolor brown ]

   if ( pycor = -10 ) and ( pxcor < 10 and pxcor > -11 ) [ set pcolor brown ]


   if ( pxcor = 8 ) and ( pycor < 8 and pycor > 2 ) [ set pcolor red ]

   if ( pxcor = 8 ) and ( pycor < -2 and pycor > -8 ) [ set pcolor red ]

end
to go

   if count patches
    with [pcolor = yellow and any? other turtles-here] = 10
    [stop]

   ask kids with [seated? = 0][

rt random 10
fd 2

        if pcolor = red and not any? other turtles-here [
          move-to patch-here
          set seated? true
          set pcolor yellow

        ]     

      ]

      ask adults with [seated? = 0]
      [
    rt random 10
    fd 1.5
        if pcolor = red and not any? other turtles-here[
          move-to patch-here
          set seated? true
          set pcolor yellow


          ]
     ]

       ask oldies with [seated? = 0]
      [
    rt random 10
    fd 1
        if pcolor = red and not any? other turtles-here[
          move-to patch-here
          set seated? true
          set pcolor yellow

          ] 
  ]
tick  
end

1 个答案:

答案 0 :(得分:0)

就像你ask ... with [Seated? = 0]提问非坐着龟的方式一样,你可以ask ... with [Seated? = 1]

询问其他人

然而,我会怀疑他们会进入一个循环,因为他们靠近替补席并且替补席是空的,所以一旦他们站起来他们将再次坐在同一个位置。也许你可以有一个memory []存储最后几个动作,如果有一个坐姿,比如最后10个项目,它会选择不坐。我不确定你的要求是什么,这只是一个例子。

      ask adults with [seated? = 1]
          [
  ; Stand up set seated? = 0
  ; go around         
          ]

       ask kids with [seated? = 1]
          [
  ; Stand up set seated? = 0
  ; go around         
           ]

       ask oldies with [seated? = 1]
          [
    ; Stand up set seated? = 0
    ; go around
         ]

<强>更新

我已经改变了你的代码,它与你的代码差别不大,但它有记忆功能,以确保海龟在离开工作台后四处移动,不会再次坐在同一个长凳上,在某种程度上避免墙壁(你需要改进避免,因为每个代理的步长不同):

   breed [kids kid]
breed [adults adult]
breed [oldies old]
Globals [out-of-boundry]    
turtles-own [seated? memory step]
to setup
  clear-all 
  reset-ticks
  ask patches [setup-world]
  ask patches with [pcolor = red ][set plabel count turtles-here]
  set out-of-boundry patches with [ pycor > 10 or pxcor < -10 or pxcor > 10 or pycor < -10 or pcolor = brown]
  set-default-shape turtles "person"
  create-kids 5 [
    set memory [] 
    set seated? false
    set color green
    set size 1              
    setxy -10 0
    set heading random-float 90
    rt 45 - random-float 90
    set step 2
  ]
  create-adults 5 [
    set memory [] 
    set seated? false
    set color orange
    set size 1                                    
    setxy -10 0
    set heading random-float 45
    rt 45 - random-float 90
    set step 1.5
  ] 
  create-oldies 5 [
    set memory [] 
    set seated? false 
    set step 1
    set color blue
    set size 1                                    
    setxy -10 0
    set heading random-float 45
    rt 45 - random-float 90
  ]
end
to setup-world
  set pcolor white
  if ( pxcor = 10 ) and ( pycor < 10 and pycor > -11 ) [ set pcolor brown ]
  if ( pxcor = -10 ) and ( pycor < 10 and pycor > 1 ) [ set pcolor brown ]
  if ( pxcor = -10 ) and ( pycor < -1 and pycor > -11 ) [ set pcolor brown ]
  if ( pycor = 10 ) and ( pxcor < 11 and pxcor > -11 ) [ set pcolor brown ]
  if ( pycor = -10 ) and ( pxcor < 10 and pxcor > -11 ) [ set pcolor brown ]
  if ( pxcor = 8 ) and ( pycor < 8 and pycor > 2 ) [ set pcolor red ]
  if ( pxcor = 8 ) and ( pycor < -2 and pycor > -8 ) [ set pcolor red ]

end
to go

  if count patches with [pcolor = yellow and any? other turtles-here] = 10 [stop]

  ask turtles [
    set memory lput Seated? memory
    restrict-memory
    ifelse seated?
    [stand-up ]
    [ move-in-the-brown-area step
      sit]

]
tick  
end






to move-in-the-brown-area [step-size]

  ifelse not member? patch-ahead step-size out-of-boundry [

    fd step-size
    rt random 10

  ]
  [ face one-of patches with [pcolor = white]
    fd step-size

  ]

  If  member? patch-here out-of-boundry[ 
    let target patch -10 0 
    face target 
    fd step-size]

end

to restrict-memory  
  ;assume your memory-limit is 5
  let memory-limit 5
  if length memory >= memory-limit
  [ set memory but-first memory ]
end


to stand-up 
  if seated? 
  [ set seated? false
    set pcolor red
    fd 1
  ] 




end

to sit
  if pcolor = red and not any? other turtles-here with [not member? true memory][
    move-to patch-here
    set seated? true
    set pcolor yellow
  ]
end

enter image description here