reset password
Author Message
rabbott
Posts: 1649
Posted 18:41 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.

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

#runs on a single agent
to commuter-move ; turtle procedure
  ifelse ticks-here > delay ; advance if the turtle has waiting long enough
  [
    set last-here ticks
    move-to patch-ahead 1
    set ticks-here 1
  ]
  [ set ticks-here ticks-here + 1 ]
  if patch-here = top-right and route = 0  [face bottom-right ] ; turn if at corner patch
  if patch-here = top-right and route = 2 [ face bottom-left]
  if patch-here = bottom-left [ face bottom-right ]
  if patch-here = bottom-right [ end-commute ]; if turtle finished route
end

#python equivalent
def commuter_move(commuter):
    #delay is a global variable
    #commuter will only move after it has waited the appropriate number of ticks
    if commuter.ticks_here < delay:
        #tickes_elapes defined in world
        commuter.last_here = ticks_elapsed
        #move this commuter to the next patch in the current heading
        commuter.move_to(patch_ahead)
        #reset the commuter tick counter
        commuter.ticks = 1
    else:
        #increment the amount of ticks this agent has been on this patch
        commuter.ticks +=1

    #check to see if the agent is at any corner patch and turn directions appropriately, or end commute
    if commuter.patch == top_right and commuter.route == 0:
        commuter.face('bottom_right')
    if commuter.patch == top_right and commuter.route == 2;
        commuter.face('bottom_left')
    if commuter.patch == bottom_left:
        commuter.face('bottom_right')
    if commuter.patch == bottom_right:
        commuter.end_commute()

The things here to consider are the assumptions take, in this case we assume that commuter has a patch and route attribute, as well as a face method and end_commute method. 

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

My understanding of this function in English:

Move an agent forward to the next patch after it spent enough time on the current patch on a road(a road consists of patches). If the agent is at a corner of the road, turn to an appropriate direction. If the agent is at the finished destination, execute end-commute procedure of this agent.

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

Thanks, Tanya,

Here are a couple of questions.

1. What is delay? Ricardo says it's a global variable. But the NetLogo code says its a Patch instance variable, i.e., something that patches-own. What is its function?

2. What is last-here? (in the fifth line down) It appears only once in the code on this page. How is it used elsewhere?

tkitcha
Posts: 19
Posted 18:24 Apr 05, 2020 |
rabbott wrote:

Thanks, Tanya,

Here are a couple of questions.

1. What is delay? Ricardo says it's a global variable. But the NetLogo code says its a Patch instance variable, i.e., something that patches-own. What is its function?

2. What is last-here? (in the fifth line down) It appears only once in the code on this page. How is it used elsewhere?

1. I also agreed that delay is a Patch instance variable. This variable is used in commuter-move function to compare its value with the agent's variable called tick-here(the agent has to be on the patch) to determine whether or not the agent is allowed to advance to the next patch.

2. last-here is also a Patch instance variable. Although each and every patch has it, only patches that defined as dynamic patches will utilize this variable in determine-congestion function(it appears to be the only place where last-here variable is used).

rabbott
Posts: 1649
Posted 20:06 Apr 05, 2020 |

Thanks, Tanya,

One way to find out where a variable is used is to go the actual NetLog code and search for that variable. The other use of last-here is in the procedure determine-congestion. The variable last-here records the most recent time that a commuter moved off that patch. At the start of the model, last-here is set to 0. But once an agent goes to a patch, the patch's last-here is set to the time that the agent leaves the patch. (That's what we see above.)

In that case, as Tanya said, its value is used to set the delay for the patch. Look at line 138 in the NetLogo code. (You can turn on line-numbers under Tools > Preferences.) The line in question is:

set delay floor (((250 / spawn-rate)/(ticks - last-here + 1)) * base-delay)

Can anyone explain this expression?

 

sdo8
Posts: 54
Posted 00:39 Apr 06, 2020 |
rabbott wrote:

Thanks, Tanya,

One way to find out where a variable is used is to go the actual NetLog code and search for that variable. The other use of last-here is in the procedure determine-congestion. The variable last-here records the most recent time that a commuter moved off that patch. At the start of the model, last-here is set to 0. But once an agent goes to a patch, the patch's last-here is set to the time that the agent leaves the patch. (That's what we see above.)

In that case, as Tanya said, its value is used to set the delay for the patch. Look at line 138 in the NetLogo code. (You can turn on line-numbers under Tools > Preferences.) The line in question is:

set delay floor (((250 / spawn-rate)/(ticks - last-here + 1)) * base-delay)

Can anyone explain this expression?

 

 

I think I can. Please correct me if I am wrong.

set delay floor (((250 / spawn-rate)/(ticks - last-here + 1)) * base-delay)

You are essentially setting the delay for a patch (or how many ticks a turtle has to stay on that patch before it can move), but it varies depending on the spawn rate and (ticks-last-here +1).
We use (250 / spawn-rate) because that is the constant used in spawn-commuters. If we use 250 as a constant for the spawn-rate, and 1000 in determine-congestion, we will end up with a lot of commuters spawning (and stacking up a lot) because the cars are stopping way too long. (It's a traffic jam!)  
In order to keep the flow nice and steady, we use 250 as the constant number between spawn-commuters and determine-congestion because it lets cars spawn and wait at relatively the same rate. (Which also means we can kind of see more of the individual cars).

The second part of the computation, or the divisor, (ticks - last-here + 1)  is the current number of ticks minus the number of ticks that was previously recorded before the last turtle left the patch. We add 1 because I'm assuming we don't want any divisions by 0, if ticks and last-here were both the same. Basically, the frequency at which turtles enter and leave the patches determines the delay. The higher the frequency, the more of a delay that patch will get. 

The final part of the computation involves multiplying the division by the base-delay , which is a constant of 10. It seems like the number 10 is used because it's makes a reasonable number when it comes to dynamic delays. The dynamic delay does not go too high nor too low which may look unnatural had static patch delays not been 10 ticks as well.

Once we have the final result, we floor it and get the largest integer less than or equal to whatever that number is and make it the final patch delay.

 

Last edited by sdo8 at 00:39 Apr 06, 2020.
rabbott
Posts: 1649
Posted 18:50 Apr 06, 2020 |

Thanks Stanley.