reset password
Author Message
rabbott
Posts: 1649
Posted 18:45 Apr 03, 2020 |

I'm splitting Ricardo's three NetLogo procedures into three posts so that (a) they are not so forbidding and (b) we can comment on them separately.

===============================================================================================================

#netlogo
to spawn-commuters  ;turtle procedure
  ; spawns new commuters and sets up their properties
  ifelse spawn-time > (250 / spawn-rate) [
    ask patch -22 22 [
      sprout-commuters 1 [
        set cars-spawned cars-spawned + 1
        set color one-of [99]
        set birth-tick ticks
        set ticks-here 1
        set route new-route
        ifelse route = 0  or route = 2 [ facexy 22 22 ][ facexy -22 -22 ]
        set size 3.5
        set shape one-of [ "car" "truck" ]
      ]
    ]
    set spawn-time 0
  ]
  [ set spawn-time spawn-time + 1 ]
end

#python
def spawn_commuters():
    #waits the appropriate spawn time to create a new commuter
    #spawn rate is given by the gui
    if spawn_time > (250 / spawn_rate):
        #make a new commuter in the starting patch with the values\
    #in this case the method is in the start patch class, but this could be written in the World class and given the start patch as an argument
        new_commuter = start_patch.sprout_commuter(color=random_color, birth_tick=ticks, ticks_here=1, route=new_route(), size=3.5, shape='square')
        cars_spawned += 1
        #check to see which route the commuter chose and face it in the correct direction
        if new_commuter.route in [0,2]:
            new_commuter.face('top_right')
        else:
            new_commuter.face('bottom_left')
        #reset spawn timer
        spawn_time = 0
    else:
        #increase spawn timer
        spawn_time += 1

This method creates the commuters for the model. They spawn at the rate given in the gui and give them a route to compelte. 

Last edited by rabbott at 18:46 Apr 03, 2020.
tkitcha
Posts: 19
Posted 15:21 Apr 05, 2020 |

My understanding of this function in English:

Generate a single agent at a certain time and then reset the time back to 0, otherwise increment the time. The agent will be assigned to one of the three routes and appropriate direction.

rabbott
Posts: 1649
Posted 16:38 Apr 05, 2020 |

Thanks, Tanya,

1. What does the constant 250 represent? What about 250/spawn-rate? What does it represent? Can we say anything more specific about what these mean? What is the test 

spawn-time > (250 / spawn-rate)

testing for?

2. What is patch -22 22?

3. What does the construct sprout-commuters 1 [ ... ] mean? (Hint: it's a control structure similar to ask ... [ ... ].)

4. The line set route new-route seems to be setting the new commuter's route. What is new-route? Where/how is it defined?

Last edited by rabbott at 17:03 Apr 05, 2020.
sdo8
Posts: 54
Posted 23:49 Apr 05, 2020 |
rabbott wrote:

Thanks, Tanya,

1. What does the constant 250 represent? What about 250/spawn-rate? What does it represent? Can we say anything more specific about what these mean? What is the test 

spawn-time > (250 / spawn-rate)

testing for?

2. What is patch -22 22?

3. What does the construct sprout-commuters 1 [ ... ] mean? (Hint: it's a control structure similar to ask ... [ ... ].)

4. The line set route new-route seems to be setting the new commuter's route. What is new-route? Where/how is it defined?

Here's some answers that I have for spawn-commuters that may come in handy for some people

The number 250 is somewhat arbitrary, since the comparison spawn-time > (250 / spawn-rate) checks the global variable spawn-time with the value (250/spawn-rate). The spawn-rate is determined by the GUI and can go range from 1-10, meaning that we can have a value from 250 to 2.5. Why do we compare this value with spawn-time? Well, spawn-time is just a counter that keeps track of the ticks that have passed since the last time "spawn-commuters" is called. If the spawn-time exceeds the value (250 / spawn-rate), the main bulk of code for actually spawning a commuter will commence. Then we re-set the global spawn-time to 0 so we can repeat the process. If the spawn-time doesn't exceed the value (250/ spawn-rate), then spawn-time is incremented by 1. You can test this out by setting your spawn-rate to 1 on the gui, then waiting for the 251st tick to spawn a single commuter. If you set this number to 100, you can have commuters spawn even faster (from a spawn rate from 1 commuter every 100/1 ticks all the way 100/10 ticks).

Patch -22 22 indicates the top-left patch (or the top left corner node of the traffic grid). You can easily replace "Patch -22 22" with "top-left" and it will still work, since it is a global that is defined in the setup.

Sprout-commuters asks the patch to "sprout" (Create) a number of "commuters" (Turtles) on that specific patch. In this case it's 1 commuter. Immediately following "sprout-commuters", is a [ ... ]. The lines between the brackets are commands that the turtles will follow immediately after they are sprouted. Within these commands, the turtle is asked to set it's route to a new-route. We know so far that there are 3 types of routes that turtles can take (Thanks to Ricardo on one of his forum posts)... "0 = top route, 1 = bottom route, and 2 is the route taken with braess road." When we call new-route, we check which of the three route selection modes are currently selected and run the selected analysis given our current world. These routes will "report" (or return) a road to take .. 0, 1, or 2 given whichever algorithm you choose via gui.

rabbott
Posts: 1649
Posted 18:46 Apr 06, 2020 |

Very good explanation!