simple market times table

No preview image

1 collaborator

Default-person Kashif Zia (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.2.0 • Viewed 78 times • Downloaded 11 times • Run 0 times
Download the 'simple market times table' 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

extensions [table]
globals [stock]

breed [traders trader]
breed [shoppers shopper]

traders-own [mystock]
shoppers-own[mylist speed paces currstall lastpos]

to setup
  clear-all
  reset-ticks

  create-all-stocks
  create-all-traders
  layout-circle sort traders max-pxcor
  create-all-shoppers
end 

to create-all-stocks
  let x 0
  set stock table:make
  while [x < number-of-items][
    table:put stock x 0
    set x x + 1
  ]
  ;print stock
end 

to create-all-traders
  create-traders number-of-traders [
    set shape "house"
    set color red
    set mystock table:make
    set-mystock

    ;print mystock
    ;move-from-origin
  ]
  ask traders [
    let parner (who * mult mod modulo)
    if who != parner
      [create-link-with trader parner]
    ask links [ set color cyan ]
  ]
end 

to set-mystock
  let x random 20
  let i 0
  while [i <= x][
    let y random 50
    let z (random 20) + 1
    table:put mystock y z
    set i i + 1
  ]
end 

to create-all-shoppers
  create-shoppers number-of-shoppers [
    set shape "default"
    set color green
    ;setxy random-pxcor random-pycor
    set mylist table:make
    set-mylist
    set speed random-float walking-speed + 0.001
    set paces 0
    ;set pen-mode "down"
    ;print mylist
  ]
    ask shoppers[
    let stall one-of traders
    set currstall stall
    face currstall
    while [ patch-here != [patch-here] of stall ] [ forward 0.05 * speed]
    set lastpos patch-here
  ]
end 

to set-mylist
  let x random 10
  let i 0
  while [i <= x][
    let y random 50
    let z (random 5) + 1
    table:put mylist y z
    set i i + 1
  ]
end 

to go
  if (count shoppers <= 0) [stop]
  ask shoppers with [table:length mylist > 0]
  [
    ;let stall one-of traders
    ;print word who word ":" [link-neighbors] of currstall
    let stall one-of [link-neighbors] of currstall

    if stall != currstall [
      set currstall stall
      face currstall

      while [ patch-here != [patch-here] of stall ] [ forward 0.05 * speed]
      let stall-stock nobody
      ask stall [set stall-stock mystock]
      set paces distance lastpos
      set lastpos patch-here
      foreach ( table:keys mylist ) [
        [key] ->
        let n table:get mylist key
        if table:has-key? stall-stock key [
          let c table:get stall-stock key
          ifelse c >= n [table:put mylist key 0] [table:put mylist key n - c]
          ask stall [table:put mystock key (table:get mystock key - n)]
        ]
      ]
    ]
  ]
  clean-up
  tick
end 

to clean-up
  ask shoppers [
    foreach (table:keys mylist) [
      [key] ->
      if table:get mylist key <= 0 [table:remove mylist key]
    ]
    if table:length mylist = 0 [die]
  ]

    ask traders [
    foreach (table:keys mystock) [
      [key] ->
      if table:get mystock key <= 0 [table:remove mystock key]
    ]
    if table:length mystock = 0 [set color white]
  ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to move-from-origin
  let free-patches patches with [pcolor = black and not any? traders-here and (pycor > min-pycor and pxcor < max-pxcor)]
  ifelse any? free-patches [
    move-to one-of free-patches
  ]
  [ die ]
end 

There is only one version of this model, created about 3 years ago by Kashif Zia.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.