⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ---veblen05.nlogo

📁 NETLOGO
💻 NLOGO
📖 第 1 页 / 共 2 页
字号:
;;; being a model for veblen conspicuous consumption, ENVY rule
;;; created thu 05 aug 2004 by ralph abraham and dan friedman
;;; written in Netlogo 2.0.0 in Mac OSX 1.2.8 with Java 1.3.1_01
;;; rev 5.3 on 22 sep 2004, chg slopecolors
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

breeds [ testers consumers ] ;;; one tester will draw a graph of patch variables

;;; global variables
globals [ totalpop totalsteps delta-x width-x totaltime ]
;;;
;;; globals defined in interface
;;; by sliders
;;;    slider for amp from 0 to 1 step 0.01, default 0.5 (constant in payoff function)
;;;    slider for population from 0 to 200 step 1, default 50 (total size of herd)
;;;    slider for center (of init turtle herd)
;;;    slider for width (of initial herd, as percentage of screen width)
;;;    slider for stepsize (for euler method)
;;; by choices 
;;;    choice for puff-row (to add turtles)

;;; reporters
to-report slopecolor [number] ;;; this is a color look-up-table for phisubx
  if number >= 0.1 [ report red ]
  if number >= -0.1 [ report yellow ]
  if number < -0.1 [report green ]
end

;;; declare patch variables
patches-own [ 
  x        ;;; varies from 0 to 1 as patch xcor from -(screen-size-x) to +(screen-size-x)
  Fraw      ;;; raw distribution (# of turtles per patch)
  F        ;;; normed Fraw, F / totalpop
  D        ;;; definate integral of F, used in phisubx definition, first integral
  envy     ;;; int D, second integral
  phi      ;;; the landscape, c ln(x) - envy(x) (utlity, envy rule)
  phisubx ;;; gradient of phi, c/x - D(x), envy rule
]

;;; setup procedures
;;; note: screen is (screen-size-x) by (screen-size-y) patches

to setup
  ca               ;;; clear screen
  setup-test-turtle  ;;; place turtle ZERO, a tester
  puff             ;;; one puff to start, place consumers
  set width-x (1 / screen-size-x)
  setup-plot       ;;; for tester to report values
  setup-plot-2     ;;; for tester to report values
  set delta-x (1 / screen-size-x) ;;; need for integration
  do-math          ;;; compute and color three vars
  do-sprint-1         ;;; draw histogram
  do-sprint-2        ;;; turtle ZERO runs one loop, do-plot-2 is within
end

to setup-test-turtle ;;; place turtle 0, a tester, at left end of row 0
    cct 1
  [
    set breed testers
    set color gray
    set xcor ( - screen-edge-x + 0.5)   ; set the turtle's initial position
    set ycor 0
    set heading 90  ;;; to right
    pd                  ;;; if DEBUG (pen down)
  ]
end

to do-sprint-1  ;;; this runs the tester turtle once around row 0 and plots findings
  locals [ counter ]
  set counter 0
  set-current-plot "Density of Consumers"
    clear-plot
  ask turtle 0
    [ set xcor ( - screen-edge-x) ]  ;;; move to x = 0
  while [ counter < screen-size-x - 1 ]
    [
      ask turtle 0
      [
        fd 1
        set counter ( counter + 1 )
      ]
      do-plot
    ]
end

to do-sprint-2  ;;; this runs the tester turtle once around row 0 and plots findings
  locals [ counter ]
  set counter 0
  set-current-plot "Landscape"
    clear-plot
  ask turtle 0
    [ set xcor -17 ]  ;;; move to x = 0
  while [ counter < 34 ]
    [
      ask turtle 0
      [
        fd 1
        set counter ( counter + 1 )
        ;show xcor  ;;; if DEBUG
      ]
      do-plot-2
    ]
end

to puff ;;; set consumers on a row 1-5, in a single square wave (herd)
  locals [ herdcenter herdwidth leftedge ] ;;; change percent to pxcor, simplify arith
  set herdcenter ( center * ( screen-size-x - 1 ) / 100 - screen-edge-x ) ;;; as pxcor
  set herdwidth  ( width * ( screen-size-x - 1 ) / 100 )  
  set leftedge ( herdcenter - herdwidth / 2 ) ;;;  pxcor from left dge
  create-consumers population ;;; create consumers at origin, various colors, headings
    ;;; new turtles are on row 0
  ask consumers [ 
    if (pycor = 0) [
      set xcor leftedge + random-float ( herdwidth ) ;;; random position
      set heading 90 ;;; now turn to the right
      set ycor puff-row ;;; on rows 1-5, like boats in a river
    ]
  ]
  set totalpop ( totalpop + population )
  do-math          ;;; compute and color display all variables
  setup-plot       ;;; histogram, correct y axis scale if totalpop changed
  do-sprint-1      ;;; update denstiy plot
  do-sprint-2      ;;; update landscape plot
end

to do-math         ;;; compute and show Fraw, phi, phisubx
  count-turtles    ;;; count turtles rows 1-5, record Fraw on row 0
  compute-D        ;;; compute D on row 0
  compute-slope    ;;; compute phisubx on row 0
  color-slope      ;;; color pycor = -6 row red, yellow, green acc to slope value
  compute-phi      ;;; for display only, compute envy and phi
end

to count-turtles ;;; compute the distribution functions Fraw and F
  locals [ Fraw0 Fraw1 Fraw2 Fraw3 Fraw4 Fraw5 ]
  ;; compute on row zero, cf eqn R5 of Tucson paper
  ask patches [
    if (pycor = 0) [
      set Fraw1 count turtles-at 0 1
      set Fraw2 count turtles-at 0 2
      set Fraw3 count turtles-at 0 3
      set Fraw4 count turtles-at 0 4
      set Fraw5 count turtles-at 0 5
      set Fraw ( Fraw1 + Fraw2 + Fraw3 + Fraw4 + Fraw5 )
      set F ( screen-size-x * Fraw / totalpop ) ;;; normal distribution (RHO) 
    ]
  ]
end

to compute-D ;;; compute the integral of F, row 0, cf eqn R6 of Tucson paper
  locals [ oldD ]
  ask patches [
    if (pycor = 0) [
      ifelse (pxcor = ( - screen-edge-x) ) [
        set D 0   ;;; initialize integral
      ]
      [
      set oldD D-of patch-at -1 0
      set D ( oldD + F * delta-x ) ;;; this is definate integral from zero to here
      ]
    ]
  ]
end

to compute-phi ;;; compute the envy (integral of D) and phi, row 0
  locals [ oldenvy ]
  ask patches [
    if (pycor = 0) [
      ifelse (pxcor = ( - screen-edge-x) ) [
        set envy 0   ;;; initialize integral
        ]
      [
      set oldenvy envy-of patch-at -1 0
      set envy ( oldenvy + D * delta-x ) ;;; this is definate integral from zero to here
      set phi amp * ln x - envy ;;; be sure to compute phisubx first (to set x)
      ]
    ]
  ]
end

to compute-slope ;;; compute the slope function phisubx on row 0
  ask patches [
    if (pycor = 0) [
      set x ( pxcor + (screen-edge-x + 1 ) ) / screen-size-x
      set phisubx ( amp / x ) - D ;;; for turtle moves
    ]
  ask patch screen-edge-x 0 [
      set phisubx -10 ;;; repel from right edge
    ]
  ]
end

to color-slope ;;; and color row -2
  ask patches [    
    if (pycor = -2) [
      set phisubx phisubx-of patch-at 0 2
      set pcolor slopecolor phisubx
      ]
    ]
end

to step ;;; move one step up slope (each row of turtles move separately)
  locals [ temp ] ;;; to lookup value at another patch
  ask consumers [
    if (ycor = 1) [
      set temp phisubx-of patch-at 0 -1
      set xcor xcor + stepsize * temp
      if (xcor < (- screen-edge-x)) [ set xcor (- screen-edge-x) ] ;;; clipping left edge
    ]
    if (ycor = 2) [
      set temp phisubx-of patch-at 0 -2
      set xcor xcor + stepsize * temp
      if (xcor < (- screen-edge-x)) [ set xcor (- screen-edge-x) ]
    ]
    if (ycor = 3) [
      set temp phisubx-of patch-at 0 -3
      set xcor xcor + stepsize * temp
      if (xcor < (- screen-edge-x)) [ set xcor (- screen-edge-x) ]
    ]
    if (ycor = 4) [
      set temp phisubx-of patch-at 0 -4
      set xcor xcor + stepsize * temp
      if (xcor < (- screen-edge-x)) [ set xcor (- screen-edge-x) ]
    ]
    if (ycor = 5) [
      set temp phisubx-of patch-at 0 -5
      set xcor xcor + stepsize * temp
      if (xcor < (- screen-edge-x)) [ set xcor (- screen-edge-x) ]
    ]
  ]
  set totalsteps (totalsteps + 1)
  set totaltime (totaltime + stepsize)
  do-math
  if totalsteps mod 10 = 0   ; every 10th time step, redraw the two graphs
    [ 
    do-sprint-1
    do-sprint-2
    ] 
end

to go ;;; continue stepping
  step
end

to setup-plot   ;;; this will plot the density, F
  set-current-plot "Density of Consumers"
  set-plot-x-range 0 1
  set-plot-y-range 0 screen-size-x
  set-plot-pen-interval width-x
end

to do-plot 
  set-current-plot "Density of Consumers"
  set-current-plot-pen "f"
  plot F-of turtle 0            ; plot F
end

to setup-plot-2 ;;; this will plot the landscape
  set-current-plot "Landscape"
  set-current-plot-pen "p"
  set-plot-x-range 0 1                           
  set-plot-y-range -1 0
  set-plot-pen-interval width-x
end

to do-plot-2
  set-current-plot "Landscape"
  set-current-plot-pen "p"
  plot phi-of turtle 0   ;;; ok, even though phi is patches-own           
end

;;; to read and execute an external ascii file of commands as if typed in the CC
;;; use this way: load "name" to read name.txt and execute each line as a command
to load [ handle ]
  locals [ fn cmd ] ;;; filename string
  set fn handle + ".txt"
  file-open fn
  while [ file-at-end? = false ]
    [ run file-read-line ]
  file-close-all 
end

;;; end of veblen05
@#$#@#$#@
GRAPHICS-WINDOW
289
11
614
177
17
7
9.0
1
10
1
1
1

CC-WINDOW
140
563
665
651
Command Center

BUTTON
27
10
93
43
NIL
setup
NIL
1
T
OBSERVER
T

SLIDER
25
303
197
336
amp
amp
0
0.3
0.1
0.01
1
NIL

SLIDER
27
99
199
132
population
population
0
500
30
1
1
NIL

BUTTON
26
398
89
431
NIL
step
NIL
1
T
OBSERVER
T

PLOT
268
192
644
342
Density of Consumers
Strategy
Density
0.0
1.0
0.0
5.0
true
true
PENS
"f" 0.02857 0 -65536 true

SLIDER
28
135
200
168
center
center
0
100
15
5
1
NIL

SLIDER
26
171
198
204
width
width
0
100
5
5
1
NIL

SLIDER
25
347
197
380
stepsize
stepsize
0
0.01
0.01
0.0010
1
NIL

BUTTON
127
398
190
431
NIL
go
T
1
T

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -