Releasing two turtles per tick netlogo

Viewed 126

I'm currently working on a model where I could simulated pedestrian movement while shopping. So, I have already figure some of things I need but I have been trying to figure out how to release 2 turtles per tick (like a pair) in a certain patch. Both turtles are release at the same time. My code is base from ant lines but it just releases all the num-of-pedestrians in one time and then the turtles start walking. the ticks also start after all turtles are release. I want the turtles to start "walking" as they enter. Here is my code:

breed [ leadvisitors leadvisitor ]
breed [ visitors visitor ]

to setup
  clear-all
   setup-visitors
reset-ticks
end

to setup-visitors 
  create-leadvisitors num-of-pedestrians * 0.1    ;;create 10% of the total number of pedestrians
  [ 
    set demand-type "none"
    set color black
    set size 1
    setxy 0 16
    set heading 180
    set pen-size 1
    set destination one-of patches
    set wait-time -1
    set demand-lvl 0    
  ]
  create-visitors (num-of-pedestrians - (num-of-pedestrians * 0.1)) 
  [ set demand-type 0
    set size 1
    setxy 0 16
    set heading 180
    set pen-size 1
    set destination one-of patches
    set wait-time -1
    set demand-lvl 1
    set attracted? false
  ]  ]
end

to go
  if turtles = 0  [ stop ]

  ask turtles 
  [ 
    set-demand-type  
    have-demand
  ]  


  if ticks > 100 [ stop ]
  tick
  display-labels
end

;;;;; visitor's internal state of demand ;;;;;

to set-demand-type
  if demand-type = 0
  [ set demand-type "food"
    set color red
    let target (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = red] 
  ]
end 

to have-demand  
  if demand-lvl = 0 
  [
    ifelse wait-time = -1 
    [ stroll ]

    [ set wait-time wait-time - 1
      if wait-time = 0
      [ stroll ]]
    ]

  if demand-lvl = 1
    [ ifelse wait-time = -1 
      [ stroll
        evaluate ]

      [ set wait-time wait-time - 1
        if wait-time = 0
        [ stroll 
          evaluate ]] 
    ] 
end 

to stroll 
if any? neighbors with [ pcolor = gray - 3 ]
  [ die ]

  ifelse any? neighbors with [ pcolor = gray or pcolor = orange or pcolor = blue or pcolor = red]
  [ facexy exitpt-x exitpt-y ]
  [ rt random-float visitor-view-angle lt random-float visitor-view-angle ]
  fd walking-speed  
end

to evaluate 
  if any? neighbors with [ pcolor = gray or pcolor = orange or pcolor = blue or pcolor = gray - 4 or pcolor = gray - 3 or pcolor = red + 2]
  [ facexy exitpt-x exitpt-y 
    rt random-float visitor-view-angle lt random-float visitor-view-angle 
    fd walking-speed ]

  let _mycolor color
  if any? (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = _mycolor]
  [ let new-target max-one-of ( patches with [pcolor = _mycolor] in-cone visitor-vision-depth visitor-view-angle) [patch-influence]
    let dist-to-new-target min-one-of (patches with [pcolor = _mycolor] in-cone visitor-vision-depth visitor-view-angle) [distance myself]
    face new-target
    set heading towards new-target
    fd walking-speed   
    set attracted? true       

    attracted-and-visiting
    re-evaluate 
   ]


end 

to attracted-and-visiting        
   if pcolor = red + 2
   [ set heading towards one-of patches with [pcolor = red + 2]
     fd 0
     set patch-popularity patch-popularity + 1
     set wait-time avg-waiting-time 

    if count turtles-here > 0
        [ set num-of-visitors num-of-visitors + 1 ]
          set plabel num-of-visitors 
   ]      
end


to re-evaluate
  let _mycolor color
  if not any? (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = _mycolor]
  [ set attracted? false ]

  ifelse choose? [set heading towards one-of patches with [pcolor = _mycolor]] [facexy exitpt-x exitpt-y]    
end


to-report choose?
  report random 2 = 0
end
1 Answers

As a result of our back-and-forth comments, let me take a stab at what you are looking for. I assume that you want to create leadvisitors at the beginning of the run and then add two visitors per tick as the run progresses. If so, then your setup procedure would look something like

globals [num-visitors-created]

to setup
  clear-all
  set num-visitors-created 0
  create-leadvisitors num-of-pedestrians * 0.1  [
    set demand-type "none"
    set color black
    set size 1
    setxy 0 16
    set heading 180
    set pen-size 1
    set destination one-of patches
    set wait-time -1
    set demand-lvl 0
    set num-visitors-created num-visitors-created + 1
  ]
  reset-ticks
end

creating your leadvisitors, but no visitors. Note that the global variable num-visitors-created keeps track of the number of leadvisitors and visitors created, being incremented by one each time a leadvisitor or visitor is created.

In your go procedure, you would then create two visitors each tick until the total number of visitors and leadvisitors created reaches num-of-pedestrians. (If there is room for only one new visitor, would you want just one to be created, or do they need to be created in pairs? I'm assuming the latter.) Since you don't want the death of a visitor to open up space for a new one, we test the number created, not the number still alive.

to go
  if turtles = 0  [ stop ]

  ; create a new pair of visitors if there is room.
  if num-visitors-created <= (num-of-pedestrians - 2)
    create-visitors 2 [ 
      set demand-type 0
      set size 1
      set heading 180
      set pen-size 1
      setxy 0 16
      set destination one-of patches
      set wait-time -1
      set demand-lvl 1
      set attracted? false
      set num-visitors-created num-visitors-created + 1
    ]

  ask turtles 
  [ 
    set-demand-type  
    have-demand
  ]

  if ticks > 100 [ stop ]
  tick
  display-labels
end

(Since it appears that leadvisitors may die as well, do you want to keep a certain minimum number or proportion of leadvisitors? If so, you should open up a new question.)

The code above will create the pair of new visitors on patch 0 16 at each tick. If, however, you want to have the new visitors created on a different patch, say one of the red ones, you could in your go procedure have that patch sprout the new visitors.

  ask one-of patches with [pcolor = red] [
    sprout-visitors 2 [ 
      set demand-type 0
      set size 1
      set heading 180
      set pen-size 1
      set destination one-of patches
      set wait-time -1
      set demand-lvl 1
      set attracted? false
      set num-visitors-created num-visitors-created + 1
    ]
  ]

Note that here the xy coordinates of the new visitors are not set, so they start on the patch that sprouted them.

Related