Anonymous Anonymous - 1 month ago
479 0

No description

Other

for emily

; TODO
; - work out a balancing equation between the spending-on-land and spending-on-goods in the utility function, think of it like a see-saw
; - study emergence through parameters



globals [wage-min wage-max wage-list    amount-people-moved perc-best-homed-people]

breed [people person]
breed [landlords landlord]
breed [firms firm]

people-own [firm! home! selected-patch land-cost lov budget product-cost spending-on-land spending-on-goods utility income]
landlords-own [base-land-cost net-stock-level home-x home-y p-color]
firms-own [wage-output base-product-cost]
patches-own [p-land-cost belongs-to]

to setup
  set amount-people-moved 0
  clear-all
  ask patches [set pcolor black]
  setup-landlords
  setup-firms
  setup-people
  reset-ticks
end


to setup-landlords
  create-landlords num-landlords
  ask landlords [
    set base-land-cost ((round random 40) + 1) * 0.25
    set p-color ( one-of [0 10 20 30 40 50 60 70 80 90 110 120 130] + one-of [3 4 5 6 7 8 9] )
    ifelse (landlords-visible = true)
    [ set color (p-color - 3)]
    [ set hidden? true ]
    setxy random-xcor random-ycor
    ;ask patch-here [set pcolor p-color]
    set pcolor p-color
    ask patch-here [
      set p-land-cost [base-land-cost] of myself
      set belongs-to myself
    ]
    set home-x pxcor
    set home-y pycor
  ]
  while [any? patches with [pcolor = black]] [
    let patches-count count patches
    let patches-assigned count patches with [pcolor != black]
    let assigned% (patches-assigned / patches-count * 100)

    ifelse (assigned% < p-tipping-point)
    [ setup-landlord-patches ]
    [ setup-cellular-landlord-patches ]
  ]

  ask landlords [
   setxy home-x home-y
  ]
end

to setup-landlord-patches
  ask landlords [
    set heading 0
    right one-of [ 0 90 180 270 ]
    fd 1
    ifelse (pcolor = black)
    [
      set pcolor p-color
      ask patch-here [
        set p-land-cost [base-land-cost] of myself
        set belongs-to myself
      ]
    ]
    [
      ifelse (pcolor = p-color)
      [  ] ;do nothing
      [ right 180 fd 1 ]
    ]
  ]
  ask patches [
    if pcolor = black [
      if ( all? neighbors4 [belongs-to = [belongs-to] of one-of neighbors4 and belongs-to != 0])
      [
        set pcolor [pcolor] of one-of neighbors4
        set p-land-cost [p-land-cost] of one-of neighbors4
        set belongs-to [belongs-to] of one-of neighbors4
      ]
    ]
  ]
end

to setup-cellular-landlord-patches
  ask patches with [pcolor = black] [
    let clone-patch one-of neighbors4 with [pcolor != black]
    if(clone-patch != nobody) [
     set p-land-cost [p-land-cost] of clone-patch
     set belongs-to [belongs-to] of clone-patch
     set pcolor [pcolor] of clone-patch
    ]
  ]
end

to setup-firms
  create-firms num-firms
  ask firms [
    set wage-output round (random (wage-gap * num-firms) + 0.5)

    ifelse (num-firms = 1)
    [ set base-product-cost 1 ]
    [ set base-product-cost one-of [1 2 3 4] ]

    set label wage-output
    set label-color white
    set color color;(wage-output + 2)
    set shape "pentagon_ol"
    set size 6

    while [(xcor = 0 and ycor = 0)]
    [
      ; radius around firms/ number of firms limits the size you are able to set the city radius
      move-to one-of patches in-radius ((city-radius% / 100) * (max-pxcor * 2)) with [ not any? firms in-radius 5 ]
    ]

    set wage-list sentence wage-list wage-output
  ]
end

to setup-people
  create-people num-people
  ask people [
    set firm! one-of firms
    set lov ( lov-median + random (lov-range * random+-) )

    set label [wage-output] of firm!
    set label-color [color] of firm!
    set color [color] of firm!
    set shape "person"
    setxy random-xcor random-ycor
  ]
end

; GO

to go
  people-set-attributes
  people-search
  tick
end

to people-set-attributes
  ask people [
   set home! patch-here
   set budget get-budget(home!)
   set product-cost get-product-cost(home!)
   set spending-on-goods get-spending-on-goods(home!)
   set spending-on-land get-spending-on-land(home!)
   set utility calculate-utility(home!)
  ]
end

to people-search
  ask people [
    let ten-random-patches []
    ask n-of 10 patches with [not any? people-here] [set ten-random-patches lput self ten-random-patches]

    let i 0
    while [ i < length ten-random-patches] [
      let p-utility calculate-utility(item i ten-random-patches)
      if (p-utility >= 0) [
        if (p-utility < utility) [
          set selected-patch item i ten-random-patches
        ]
      ]
      set i (i + 1)
    ]
    if (selected-patch != 0) [
      move-to selected-patch
      set amount-people-moved (amount-people-moved + 1)
    ]
  ]
end

to-report get-budget [patch!]
  report [wage-output] of firm! - (commute-cost-per-patch * calculate-patch-firm-distance-pythagoras(patch!))
end

to-report get-product-cost [patch!]
  report [base-product-cost] of firm! + ((delivery-cost-per-patch)); * calculate-patch-firm-distance-pythagoras(patch!))
end

to-report get-spending-on-goods [patch!]
  report ( ( ( get-product-cost(patch!) ^ (1 / (lov - 1)) ) * get-budget(patch!)) / ( [p-land-cost] of patch! ^ (lov / (lov - 1)) ))
end


; -- BROKEN --

to-report get-spending-on-goods2 [patch!]
  report ( stuff1(patch!) / stuff2(patch!) )
end

to-report stuff1 [patch!]
  report ( ( get-product-cost(patch!) ^ (1 / (lov - 1)) ) * get-budget(patch!))
end

to-report stuff2 [patch!]
  report ( [p-land-cost] of patch! ^ (lov / (lov - 1)) )
end

; -- BROKEN --

to-report get-spending-on-land [patch!]
  report ( [p-land-cost] of patch! ^ ((1 / (lov - 1)) * get-budget(patch!)) ) / ( get-product-cost(patch!) ^ (lov / (lov - 1)) )
end

to-report get-utility
  report (( spending-on-goods ^ lov ) + ( spending-on-land ^ lov )) ^ (1 / lov)
end

to-report calculate-utility [patch!]
  ifelse (get-budget(patch!) <= 0)
  [ report -1]
  [ report (( get-spending-on-goods(patch!) ^ lov ) + ( get-spending-on-land(patch!) ^ lov )) ^ (1 / lov) ]
end

to-report calculate-patch-firm-distance-pythagoras [patch!]
  report sqrt ((distance patch!)^(2) + (distance firm!)^(2))
end





; RANDOM UTILITIES

to-report check-val[val]
  if(val < 0) [
    report(0)
  ]
  report(val)
end

to-report random+-
  report one-of [1 -1]
end

to percentage-of-best-homed-people
  set perc-best-homed-people 0
  let num 0
  ask people [
    let best-utility 999
    let all-patches []

    ask n-of (count patches) patches [set all-patches lput self all-patches]
    let i 0
    while [ i < length all-patches] [
      let temp-util calculate-utility (item i all-patches)
      if (temp-util < best-utility)
      [ set best-utility temp-util ]
      set i (i + 1)
    ]
    if (best-utility = utility and best-utility != 999)
    [ set num (num + 1) ]
  ]
  set perc-best-homed-people num
end

to help-people-search
;  let available-destinations patches
;  ;random 10 patches
;  ;patches with >0 people choosing
;  turtles with [ member? matching-patches
;  ;list of turtles on this patch
;
;
end