qg_gp 发表于 2008-5-3 20:47:29

求教:海龟的遗产传递问题

请问 我现在希望设计海龟死后他的财产可以以一定的比例留给后代。这些海龟如果代表的是穷人则它死后(年龄大预期值)会生出3个孩子,其生前的财产的90%均分给其孩子。若海龟是富裕的,其死后有随机1~2个孩子,财产已70%均分。请问大家如果加到下述程序中怎么实现
globals
[
max-grain   

]

patches-own
[
grain-here      
max-grain-here
]

turtles-own
[
age            
wealth         
life-expectancy
metabolism      
vision
inherited         
]


to setup
ca
set max-grain 50
setup-patches
setup-turtles
setup-plots
update-plots
end
to setup-patches
ask patches
    [ set max-grain-here 0
      if (random-float 100.0) <= percent-best-land
      [ set max-grain-here max-grain
          set grain-here max-grain-here ] ]
repeat 5
    [ ask patches with
      [ set grain-here max-grain-here ]
      diffuse grain-here 0.5 ]
repeat 10
    [ diffuse grain-here 0.5]         
ask patches
    [ set grain-here floor grain-here   
      set max-grain-here grain-here      
      recolor-patch ]
end
to recolor-patch
set pcolor scale-color sky grain-here 0 max-grain
end
to setup-turtles
set-default-shape turtles "person"
crt num-people
    [ move-to one-of patches
      set size 1.5
      set-initial-turtle-vars-age
      set-initial-turtle-vars-wealth
      set age random life-expectancy ]
recolor-turtles
end

to set-initial-turtle-vars-age
let max-wealth max of turtles
   
   ifelse (wealth <= max-wealth / 3)
      [ set color red
          set age 0
          face one-of neighbors4
          set life-expectancy life-expectancy-min +
                        random life-expectancy-max
          set metabolism random 1 + metabolism-low
          set wealth metabolism + random 30
          set vision 1 + random max-vision
             set wealthwealth +Wealth-inherited-low ]
      [ ifelse (wealth <= (max-wealth * 2 / 3))
            [ set color yellow
            set age 0
            face one-of neighbors4
            set life-expectancy life-expectancy-min +
                        random life-expectancy-max + 1
            set metabolism1 + random metabolism-mid
            set wealth metabolism + random 30
            set vision 3 + random max-vision
                set wealthwealth + Wealth-inherited-mid]
            [ set color green
            set age 0
            face one-of neighbors4
            set life-expectancy life-expectancy-min +
                        random life-expectancy-max+ 2
            set metabolism 2 + random metabolism-up
            set wealth metabolism + random 30
            set vision 3 + random max-vision
            set wealthwealth + Wealth-inherited-up ] ]

end
to set-initial-turtle-vars-wealth
let max-wealth max of turtles
          set age 0
          face one-of neighbors4
          set life-expectancy life-expectancy-min +
                        random life-expectancy-max
          set metabolism 1 + random metabolism-up
          set wealth metabolism + random 30
          set vision 1 + random max-vision
end
to redistribution
let max-wealth max of turtles
let min-wealth min of turtles
if (wealth <= max-wealth / 3)

end
         
to recolor-turtles
let max-wealth max of turtles
ask turtles
   [ ifelse (wealth <= max-wealth / 3)
      [ set color red ]
      [ ifelse (wealth <= (max-wealth * 2 / 3))
            [ set color yellow ]
            [ set color green ] ] ]
ask turtles [ifelse show-wealth?
    [ set label wealth ]
    [ set label "" ]]
end

to go
ask turtles
    [ turn-towards-grain ]
harvest
ask turtles
    [ move-eat-age-die ]
recolor-turtles
if ticks mod grain-growth-interval = 0
    [ ask patches [ grow-grain ] ]
   
if ticks mod 11 = 0
[ask turtles
[ redistribution ]]
if ticks mod 5 = 0
   [ask turtles
[ visions ]]
tick
update-plots
end
to visions
set vision vision + 1
end



to turn-towards-grain
set heading 0
let best-direction 0
let best-amount grain-ahead
set heading 90
if (grain-ahead > best-amount)
    [ set best-direction 90
      set best-amount grain-ahead ]
set heading 180
if (grain-ahead > best-amount)
    [ set best-direction 180
      set best-amount grain-ahead ]
set heading 270
if (grain-ahead > best-amount)
    [ set best-direction 270
      set best-amount grain-ahead ]
set heading best-direction
end


to-report grain-ahead
let total 0
let how-far 1
repeat vision
    [ set total total + of patch-ahead how-far
      set how-far how-far + 1 ]
report total
end

to grow-grain
if (grain-here < max-grain-here)
    [ set grain-here grain-here + num-grain-grown
      if (grain-here > max-grain-here)
      [ set grain-here max-grain-here ]
      recolor-patch ]
end
to harvest
ask turtles
    [ set wealth floor (wealth + (grain-here / (count turtles-here))) ]
ask turtles
    [ set grain-here 0
      recolor-patch ]

end

to move-eat-age-die
fd 1
set wealth (wealth - metabolism)
    set age (age + 1)
if (age >= life-expectancy)
    [ set-initial-turtle-vars-age ]
if (wealth < 0)
    [ set-initial-turtle-vars-wealth ]
   
end


to setup-plots
set-current-plot "Class Plot"
set-plot-y-range 0 num-people
set-current-plot "Class Histogram"
set-plot-y-range 0 num-people
end

to update-plots
update-class-plot
update-class-histogram
update-lorenz-and-gini-plots
end

to update-class-plot
set-current-plot "Class Plot"
set-current-plot-pen "low"
plot count turtles with
set-current-plot-pen "mid"
plot count turtles with
set-current-plot-pen "up"
plot count turtles with
end

to update-class-histogram
set-current-plot "Class Histogram"
plot-pen-reset
set-plot-pen-color red
plot count turtles with
set-plot-pen-color yellow
plot count turtles with
set-plot-pen-color green
plot count turtles with
end
to update-lorenz-and-gini-plots
set-current-plot "Lorenz Curve"
clear-plot

set-current-plot-pen "equal"
plot 0
plot 100

set-current-plot-pen "lorenz"
set-plot-pen-interval 100 / num-people
plot 0

let sorted-wealths sort of turtles
let total-wealth sum sorted-wealths
let wealth-sum-so-far 0
let index 0
let gini-index-reserve 0

repeat num-people [
    set wealth-sum-so-far (wealth-sum-so-far + item index sorted-wealths)
    plot (wealth-sum-so-far / total-wealth) * 100
    set index (index + 1)
    set gini-index-reserve
      gini-index-reserve +
      (index / num-people) -
      (wealth-sum-so-far / total-wealth)
]

set-current-plot "Gini-Index v. Time"
plot (gini-index-reserve / num-people) / area-of-equality-triangle
end
to-report area-of-equality-triangle
report (num-people * (num-people - 1) / 2) / (num-people ^ 2)
end

qg_gp 发表于 2008-5-4 15:49:14

自己写了用的hatch 但加在上面的程序中运行的时候总是出现问题T_T
页: [1]
查看完整版本: 求教:海龟的遗产传递问题