This page was automatically generated by NetLogo 4.1.3.

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Java site.

powered by NetLogo

view/download model file: Servers.nlogo

WHAT IS IT?

This model explores "queueing theory" (waiting in lines . . .)


HOW IT WORKS

Each time step (tick), a client wanders around. When a client's wandering time gets to zero, it gets in line and waits for its turn to be served. When it gets to the front of the line, it is served for a while. When it is finished being served, it starts wandering again.

Servers do just that, serve clients.


HOW TO USE IT

Using the sliders, choose the various variables.

The Average wait time plot shows the average time clients waited in line.


THINGS TO NOTICE

???


THINGS TO TRY

???


EXTENDING THE MODEL

???


RELATED MODELS

???


NETLOGO FEATURES

Links are used for modeling the network. The LAYOUT-SPRING primitive is used to position the nodes and links such that the structure of the network is visually clear.


CREDITS AND REFERENCES

This model was originally started by Tom Carter for his Simulations class . . .


PROCEDURES

breed [servers server]
servers-own
  [client-list
   serving
   current-client
   number-served
   average-time
   my-x
   my-y
  ]
breed [clients client]
clients-own
  [serve-time
   wait-time
   total-wait
   average-wait
   times-in-line
   wander-time
   is-in-line
   place-in-line
   is-being-served
   my-x
   my-y
  ]
 globals [plotted-ave-wait-time client-list-all]

to setup
  clear-all
  setup-servers
  setup-clients 
  set client-list-all []
end

to setup-servers
 set-default-shape servers "square"
 create-servers number-of-servers
 [set my-x ((1.3 * who * max-pxcor / number-of-servers) - max-pxcor / 2 )
  set my-y ( 18 )
  setxy my-x my-y
  set color yellow
  set client-list []
  set current-client nobody
  set serving false
  set number-served 0
  set average-time 0.0]
end

to setup-clients
  set-default-shape clients "circle"
  create-clients number-of-clients
  [ set my-x (random-xcor * .85)
    set my-y ((random-ycor * .5) - max-pycor * .48)
    setxy my-x my-y
    set color green
    set serve-time random max-serve-time
    set wait-time 0
    set total-wait 0
    set average-wait 0.0
    set times-in-line 0
    set wander-time random max-wander-time
    set is-in-line false
    set place-in-line 0
    set is-being-served false
  ]
end

to update-plot
    set-current-plot "Average Wait Time"
    set-current-plot-pen "ave-wait-time"
    plot mean [ average-wait ] of clients
    set plotted-ave-wait-time mean [ average-wait ] of clients
end

to go
  ask clients with [ (not is-in-line) and (not is-being-served) ]
    [ set wander-time wander-time - 1
      ifelse wander-time < 1
      [ ifelse not one-line
        [ ifelse short-line
          [ let serve one-of servers with [ length client-list = min [length client-list] of servers ]
            ask serve [ set client-list lput myself client-list ]
            set is-in-line true
            set times-in-line times-in-line + 1
            set my-x ([my-x] of serve)
            set my-y (([my-y] of serve) - (2 + [length client-list] of serve))
            setxy my-x my-y
            set color 12 + 10 * floor (129 * (serve-time / max-serve-time) / 10) + serve-time mod 8
            
          ]
          [ let serve one-of servers
            ask serve [ set client-list lput myself client-list ]
            set is-in-line true
            set times-in-line times-in-line + 1
            set my-x ([my-x] of serve)
            set my-y (([my-y] of serve) - (2 + [length client-list] of serve))
            setxy my-x my-y
            set color 12 + 10 * floor (129 * (serve-time / max-serve-time) / 10) + serve-time mod 8
          ]
        ]
        [ set is-in-line true
          set times-in-line times-in-line + 1
          set client-list-all lput self client-list-all
          set place-in-line length client-list-all
          set my-x ((place-in-line mod 30) - 15) * ( 1 - (2) * ((floor (place-in-line / 30)) mod 2 ))
          set my-y 12 - 2 * (floor (place-in-line / 30))
          setxy my-x my-y
          set color 12 + 10 * floor (129 * (serve-time / max-serve-time) / 10) + serve-time mod 8
        ]
      ]
      [ set my-x (random-xcor * .85)
        set my-y ((random-ycor * .5) - max-pycor * .48)
        setxy my-x my-y
      ]
    ]
  ask clients with [ is-in-line ]
    [ set wait-time wait-time + 1 ]
  ask servers with [ serving ]
    [ ask current-client
      [ set serve-time serve-time - 1
        set color floor ( 139 * serve-time / max-serve-time) 
      ]
      if ( 1 > ([serve-time] of current-client) )
        [ ask current-client
          [ set color green
            set serve-time random max-serve-time
            set wait-time 0
            set wander-time random max-wander-time
            set is-in-line false
            set is-being-served false
            set my-x (random-xcor * .85)
            set my-y ((random-ycor * .5) - max-pycor * .48)
            setxy my-x my-y
          ]
          set current-client nobody
          set serving false
          set color yellow
        ]
    ]
  ifelse one-line
  [ ask servers with [ (not serving) ]
    [ if 0 < length client-list-all
      [ set serving true
        set color blue
        let serv-my-x my-x
        let serv-my-y my-y
        set current-client first client-list-all
        ask current-client
        [ set color red
          set my-x [ my-x ] of myself
          set my-y ([ my-y ] of myself) - 1
          setxy my-x my-y
          set is-in-line false
          set is-being-served true
          set place-in-line 0
          set total-wait total-wait + wait-time
          set wait-time 0
          set average-wait total-wait / times-in-line
        ]
        set client-list-all remove first client-list-all client-list-all
        foreach client-list-all
          [ ask ? [ set place-in-line place-in-line - 1 ]
            ask ? [ set my-x ((place-in-line mod 30) - 15) * ( 1 - (2) * ((floor (place-in-line / 30)) mod 2 ))]
            ask ? [ set my-y 12 - 2 * (floor (place-in-line / 30)) ]
            ask ? [ setxy my-x my-y ]
          ]
        ]
      ]
    ]
    [ ask servers with [ (not serving) and ( 0 < length client-list) ]
      [ set serving true
        set color blue
        set current-client first client-list
        ask current-client
        [ set color red
          set my-y my-y + 2
          setxy my-x my-y
          set is-in-line false
          set is-being-served true
          set place-in-line 0
          set total-wait total-wait + wait-time
          set wait-time 0
          set average-wait total-wait / times-in-line
        ]
        set client-list remove first client-list client-list
        foreach client-list
          [ ask ? [ set my-y  my-y + 1 ]
            ask ? [ setxy my-x my-y ]
            ask ? [ set place-in-line place-in-line - 1 ]
          ]
      ]
   ]

  tick
  update-plot

end