Child of Waiting Bar Customers

No preview image

1 collaborator

Default-person wesley sun (Author)

Tags

(This model has yet to be categorized with any tags)
Child of model Waiting Bar Customers preview imageWaiting Bar Customers
Visible to everyone | Changeable by the author
Model was written in NetLogo 5.0.4 • Viewed 359 times • Downloaded 34 times • Run 0 times
Download the 'Child of Waiting Bar Customers' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

turtles-own 
[ 
  user-id           ;; the id of the logged in user
  drinks-had        ;; the number of drinks i've had
  start-of-thirst   ;; how long has it been since the last drink
  base-pushiness    ;; the pushiness i am born with
  pushiness         ;; how pushy i am at the moment
  vx                ;; x velocity
  vy                ;; y velocity
  desired-direction ;; my desired direction
  prev-desired-direction ;; my previous desired direction
  driving-forcex    ;; my main motivating force
  driving-forcey    
  obstacle-forcex   ;; force exerted by obstacles
  obstacle-forcey
  territorial-forcex;; force exerted by neighbors
  territorial-forcey
]

;; set everything up

to setup
  cp
  cd
  clear-output
  set-default-shape turtles "circle"
  ;; set boundary patches as walls
  ask patches with [pxcor = min-pxcor or pxcor = max-pxcor or pycor = min-pycor or pycor = max-pycor]
  [ set pcolor white ]
  
  ;; create the bar counter
  ask patches with [pycor = (max-pycor - 2) and abs pxcor < 3]
  [ set pcolor white ]
  ask patches with [pycor = (max-pycor - 1) and abs pxcor = 2]
  [ set pcolor white ]
  
  ;; create the goals
  ask patches with [pycor = (max-pycor - 1) and abs pxcor < 2]
  [ set pcolor green ]
  reset-ticks
end 

;; run the activity

to go
  ;; run the social forces model on thirsty turtles
  ;; calculate the forces first...
  ask turtles
  [ 
    calc-driving-force
    calc-obstacle-force
    if any? other turtles
      [ calc-territorial-forces ] 
  ]
  ;; then move the turtles and have them grow impatient if need be
  ask turtles
  [
    move-turtle
  ]
  ;; control the service rate of bartenders. follow an exponential distribution for service times
  let p 1 / mean-service-time
  if random-float 1 < p
  [
    ask one-of patches with [pcolor = green]
    [ service-patron ]
  ]
  listen-clients
  tick
end 

;; helper functions

;; "serve" a turtle a drink

to service-patron 
  if any? turtles in-radius 2.5
  [
    ;; default to "random" service-plan
    let next-served one-of turtles in-radius 2.5
    if service-plan = "waited-longest"
    [ set next-served max-one-of turtles in-radius 2.5 [  ticks - start-of-thirst ] ]
    ask next-served
    [
      ;; reset the turtle
      setxy 0 0
      set vx 0
      set vy 0
      set start-of-thirst ticks
      set drinks-had drinks-had + 1
      set pushiness 0
      ;; update the client with the number of drinks had
      hubnet-send user-id "drinks" drinks-had
    ]
  ]
end 
;; helper function to find the magnitude of a vector

to-report magnitude [x y]
  report sqrt ((x ^ 2) + (y ^ 2))
end 
;; returns 1 if the angle between the desired vector and the force vector is within a threshold, else return c

to-report field-of-view-modifier [desiredx desiredy forcex forcey]
  let field-of-view 200
  let c .5
  ifelse (desiredx * (- forcex) + desiredy * (- forcey)) >= (magnitude forcex forcey) * cos (field-of-view / 2)
  [ report 1 ] 
  [ report c]
end 

;; social forces functions

;; move the turtle according to the rules of the social forces model

to move-turtle
  let max-speed 0.2
  
  let ax driving-forcex + obstacle-forcex + territorial-forcex
  let ay driving-forcey + obstacle-forcey + territorial-forcey
  
  set vx vx + ax
  set vy vy + ay
  
  ;; scale down the velocity if it is too high
  let vmag magnitude vx vy
  let multiplier 1
  if vmag > max-speed
  [set multiplier max-speed / vmag]
  
  set vx vx * multiplier
  set vy vy * multiplier
  
  set xcor xcor + vx
  set ycor ycor + vy
end 
;; find the territorial force according to the social forces model

to calc-territorial-forces
  let v0 2.1
  let sigma 0.3
  
  set territorial-forcex 0
  set territorial-forcey 0
  ask other turtles with [distance myself > 0]
  [
    let to-agent (towards myself) - 180
    let rabx [xcor] of myself - xcor
    let raby [ycor] of myself - ycor
    let speed magnitude vx vy
    let to-root ((magnitude rabx raby) + (magnitude (rabx - (speed * sin desired-direction)) (raby - (speed * cos desired-direction)))) ^ 2 - speed ^ 2
    if to-root < 0
    [set to-root 0]
    let b 0.5 * sqrt to-root
    
    let agent-force (- v0) * exp (- b / sigma)
    
    ask myself
    [
      let agent-forcex agent-force * (sin to-agent)
      let agent-forcey agent-force * (cos to-agent)
      ;; modify the effect this force has based on whether or not it is in the field of view
      let vision field-of-view-modifier driving-forcex driving-forcey agent-forcex agent-forcey
      set territorial-forcex territorial-forcex + agent-forcex * vision
      set territorial-forcey territorial-forcey + agent-forcey * vision
    ]
  ]
end 
;; find the obstacle force of the turtle according to the social forces model

to calc-obstacle-force
  let u0 10
  let r 0.2
  
  set obstacle-forcex 0
  set obstacle-forcey 0
  ask patches with [pcolor = white]
  [
    let to-obstacle (towards myself) - 180
    let obstacle-force (- u0) * exp (- (distance myself) / r)
    ask myself
    [
     set obstacle-forcex obstacle-forcex + obstacle-force * (sin to-obstacle)
     set obstacle-forcey obstacle-forcey + obstacle-force * (cos to-obstacle)
    ]
  ]
end 
;; find the driving force of the turtle

to calc-driving-force
  let tau 10
  let max-speed 0.2
  
  set driving-forcex (1 / tau) * (max-speed * (sin desired-direction) - vx) * pushiness
  set driving-forcey (1 / tau) * (max-speed * (cos desired-direction) - vy) * pushiness
end 

;; hubnet functions
;; handle any incoming messages

to listen-clients
  while [ hubnet-message-waiting? ]
  [
    hubnet-fetch-message
    ifelse hubnet-enter-message?
    [ create-new-turtle ]
    [
      ifelse hubnet-exit-message?
      [ remove-turtle ]
      [ ask turtles with [user-id = hubnet-message-source]
        [ execute-command hubnet-message-tag ]
      ]
    ]
  ]
end 
;; create a turtle

to create-new-turtle
  create-turtles 1
  [
    set user-id hubnet-message-source
    set label user-id
    setxy 0 0
    set start-of-thirst ticks
    set drinks-had 0
    set base-pushiness initial-pushiness
    set vx 0
    set vy 0
    ;; update the client with the number of drinks
    hubnet-send user-id "drinks" drinks-had
  ]
end 
;; have a turtle execute a command

to execute-command [command]
  if command = "up"
  [ 
    set desired-direction 0
    set pushiness base-pushiness 
    stop
  ]
  if command = "down"
  [ 
    set desired-direction 180
    set pushiness base-pushiness 
    stop
  ]
  if command = "left"
  [ 
    set desired-direction 270
    set pushiness base-pushiness 
    stop
  ]
  if command = "right"
  [ 
    set desired-direction 90
    set pushiness base-pushiness 
    stop
  ]
end 
;; remove a turtle

to remove-turtle
  ask turtles with [user-id = hubnet-message-source]
  [ die ]
end 
;; hubnet startup

to startup
  hubnet-reset
end 

There are 2 versions of this model.

Uploaded by When Description Download
wesley sun almost 11 years ago added some comments to the code Download this version
wesley sun almost 11 years ago Corresponding HubNet activity. Download this version

Attached files

No files

Parent: Waiting Bar Customers

This model does not have any descendants.

Graph of models related to 'Child of Waiting Bar Customers'