NetLogo: avoid to have too many beetles on one patch in one time step?

六月ゝ 毕业季﹏ 提交于 2019-12-25 06:39:31

问题


I would like to implement certain rules into relationship between my turtles and my patches. My Patches variables are:

  • n_min - if there is enough turtles, change pcolor to pink, change yellow turtles on patch to orange
  • n_max - if there is too many turtles, set pcolor to brown, let all turtles to avoid this patch

My turtles states are: yellow (move) -> orange (stay) -> red (infest) Patches states are: green (n_min< then number of orange turtles on one patch) pink (number of orange turtles is > n_min and < n_max) brown (no turtles)

My problem is how can I avoid to have more then n_max turtles on one patch if they are all moving at the same time and thus are targeted to same patch? How can I include the condition that "if you see that there are some turtles of color orange/red, just keep moving to find another patch? " Also, if my patch is already pink, and n_min != n_max to ask turtles to directly change its color to red?

Thank you a lot !

my not working example:

globals [
  available-patch
]


patches-own [
  nmax   ; maximum nuber of turtles per patch, and change color
  nmin   ; minimum number of turtles to change patch color
  n.yellow  ; how many turtles are on the patch with yellow
  n.orange  ; how many orange (staying) turtles are on 1 patch?
]

to setup
  clear-all
  reset-ticks
  setup-patches
  setup-turtles
end

to setup-patches
  ask n-of n_patches patches [
    set pcolor green
    set nmin n_min     ; how many orange turtles can be there to set the patch to pink? 
    set nmax n_max     ; max number of orange beetles to turn the patch to brown
  ]
end

to setup-turtles
  crt n_turtles [
    set color yellow
  ]
end

to go
  tick
  ask turtles with [color = yellow ] [   ; move only yellow turtles
    move-turtles
  ]
  ask patches [  ; if n.orange = nmin, turn all turtles to red and turn patch to pink
    set n.orange count turtles-here with [color = orange]
  ] 
end

to move-turtles
   ;  if color = yellow ;and n.yellow < nmin 
   ;ifelse count other turtles-on
;   move-to one-of patches with [pcolor = green and n.orange <= nmin]
;   if [n.orange <= nmin] of patch-here [
;     set color orange  ; not to move anymore. However, the n.orange should be always less then nmin !!
;   ]
   set available-patch patches with [pcolor = green and count turtles-here with [color = orange] < 2] ; agentset 
   ifelse (count other turtles-on available-patch <= nmin) ; how to change the code for nmin here?
     [ move-to one-of available-patch                         
       set color orange
       ask patch-here [
         set n.orange count turtles-here with [color = orange]
       ]
     ]
     [ fd 2 ]


end

Result:

one turtle on the green patch has changed its color to orange, but I wanted 2 turtles (nmin) to find green patch and become orange. Also, the yellow turtles move next time steps just by [fd 2], the "ifelse" condition is not re-run each time step.


回答1:


First, a specific piece of advice:

You've given available-patch a singular-sounding name, but it's actually plural: it's a patchset of all patches satisfying the condition you've set.

Then when you write:

count other turtles-on available-patch <= nmin

available-patch is actually many patches, count turtles-on available-patch is summing all of the turtle counts on all of those patches. This doesn't make any sense.

Perhaps you meant:

set available-patch one-of patches with ...

OK, so that's one bug in your code, but I don't think it's the only one. I have a more general piece of advice: you are trying to write much too much code all at once. This is a recipe for flailing. You should try to write a much simpler model first, with simpler rules, and get it working. Once it's working, make it a little more complex by adding another rule, get that working, and so forth. Work your way up towards the full model step by step. If at any point you get stuck, ask a question here on Stack Overflow. You'll have a good, specific question to ask, instead of your current situation where your question seems to be "here is a bunch of code that isn't anywhere close to working, help!".

P.S. unrelated to your problem, but reset-ticks always goes at the end of setup, never near the beginning. tick always goes at the end of go, never near the beginning.




回答2:


This code should meet my needs - keep only certain number of turtles per patch, and depending on number of turtles of certain color (nmin, nmax) per patch turn patch to pink and to brown.

patches-own [
  nmax   ; maximum nuber of turtles per patch, and change color
  nmin   ; minimum number of turtles to change patch color
  n.orange  ; how many orange (staying) turtles are on 1 patch?
  n.red     ; how mony infesting turtles are on the patch??
]

to setup
  clear-all 
  setup-patches
  setup-turtles
  reset-ticks
end

to setup-patches
  ask n-of n_patches patches with [pcolor = black][
    set pcolor green
    set nmin n_min     ; how many orange turtles can be there to set the patch to pink? 
    set nmax n_max     ; max number of orange beetles to turn the patch to brown
  ]
end

to setup-turtles
  crt n_turtles [
    set color yellow
  ]
end

to go 
   ; add turtles and patches by time steps, to see the effect 
  if ticks mod 5 = 0 [
    ask n-of 1 patches with [pcolor = black][
       set pcolor green
       set nmin n_min     ; how many orange turtles can be there to set the patch to pink? 
       set nmax n_max     ; max number of orange beetles to turn the patch to brown
     ]
    ]

  if ticks mod 3 = 0 [
    crt 1 [
      set color yellow
    ]
  ] 

  ask turtles with [color = yellow ] [   ; move only yellow turtles
    move-turtles
  ]
  tick 
end

to move-turtles       
    ; specify the target
    let available-patches (patches with [(pcolor = pink and count turtles-here < nmax) or   ; 
                                         (pcolor = green and count turtles-here < nmin)] )
    ; select only one patch, nmin is related to "self"

   ifelse any? available-patches ; don't work if there is <= nmin, then there are nmin + 1
     [ move-to one-of available-patches
       if pcolor = green [
         set color orange
         set n.orange (n.orange + 1)
         infest.patch   ; if there is enough turtles, turn patch to pink, turn turtles to red
       ]
       if pcolor = pink [
         set color red
         set n.red (count turtles-here with [color = red])
         kill.patch
       ]
       ]
     [ fd 1 ]
end

to infest.patch   ; change color of patch only if there is n.orange = nmin of this specific patch 
  if n.orange = nmin [
     set pcolor pink  ; patch
     ask turtles-here with [color = orange] [
        set color red ]; turtles
  ]
end

to kill.patch
  if n.red = nmax [      ; turn patch brown when red turteles on this patch are = nmax
    set pcolor brown
;    ask turtles-here with [color = red] [
;      die ]
  ]
end


来源:https://stackoverflow.com/questions/32891984/netlogo-avoid-to-have-too-many-beetles-on-one-patch-in-one-time-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!