NetLogo - 由乌龟管理创建群集作为服务器场

时间:2014-01-16 22:06:49

标签: netlogo

在设置中,我将创建一个命令,生成随机数随机数的簇。每个集群都由代理(农民)管理。每个补丁代表农民可以用不同种子种植的潜在作物。值得使用半径函数?如果您需要更多详细信息,请问我。

非常感谢您的回答,这正是我的需要!现在我还有另一个问题,我按照你的建议实施了这个程序,现在我展示了一个像你这样的世界,但我不知道如何让我的经纪人(农民)按照他们的农场规模进行操作。在我的模拟中,农民只在他们所在的一个补丁上行动。我尝试以不同的方式扩展我的代理的操作(函数补丁集),但每次我在程序运行期间出错时。 正如Marzy上一篇文章中所建议的那样,我包含了我模型的代码。 主要问题是将程序“培养”(在代码的底部)扩展到属于农民的每个补丁。

非常感谢

turtles-own  [
   profit-from-fuel
   profit-from-food
   expected-fuel-sell-price
   expected-food-sell-price
   profit
   farm
   farm-size
   ;risk-attitude-food
   ;risk-attitude-fuel
 ]


patches-own  [
  fuel-yeld
  food-yeld
  land-sustainability
  water-level
  belongs-to

 ]

globals      [
  fuel-sell-price
  food-sell-price
  governs        
   ]


to setup        
 clear-all
 clear-all-plots
 create-farmers
 setup-land
 reset-ticks

  ask turtles
     [     set-farm-in-radius farm-size   ]

set fuel-sell-price 30 ;+ random 2 + random -2 
set food-sell-price 30 ;+ random 2 + random -2 

end


to create-farmers


create-turtles 30

[
 set shape "person farmer"
 setxy random-pxcor random-pycor 
 set profit-from-fuel  0  ; indicizzazione del profitto iniziale a 0
 set profit-from-food 0  ; indicizzazione del profitto iniziale a 0 

 set farm-size random 5 + 1
 set label farm-size
 ]
 end


 to setup-land

 ask patches [set belongs-to nobody]

 ask patches
  [

   set pcolor 3
   set food-yeld 10000 
   set fuel-yeld 10000
   set land-sustainability random 5
   set water-level random 3
    ]

  end


to set-farm-in-radius [d]
 move-to one-of patches with [not any? other patches in-radius d with [belongs-to !=    nobody]]
 set farm patches in-radius farm-size
 ask farm [set belongs-to myself]
 let c random 6 + 61
 ask farm [set pcolor c]
 end


to set-farm-distance [d]
  move-to one-of patches with [not any? other patches with [belongs-to != nobody and   distance myself < d]]
  set farm patches with [distance myself < d] 
  ask farm [set belongs-to myself]
  let c random 6 + 61
  ask farm [set pcolor c]
end



to go

  tick

   ask turtles [

  set expected-fuel-sell-price fuel-sell-price + random 5 + random -5           
  if expected-fuel-sell-price < 0 [set expected-fuel-sell-price 1]
  set expected-food-sell-price food-sell-price  + random 5 + random -5
  if expected-food-sell-price < 0 [set expected-food-sell-price 1]
  set profit profit-from-fuel + profit-from-food
  if profit = 0 [ set profit 1 ]
  ]


  set fuel-sell-price fuel-sell-price + random 5 + random -5 
  if fuel-sell-price < 0 or fuel-sell-price = 0 [set fuel-sell-price 1 ]     
  set food-sell-price food-sell-price + random 5 + random -5  
  if food-sell-price < 0 or food-sell-price = 0 [set food-sell-price 1]       



  ask turtles [ 

     cultivate

     set profit profit-from-food + profit-from-fuel
    ;if water-level > 0.95 [ set profit profit - (profit * ( 2 / profit ))  ] valutare se inserire anche una failing probability
     ]




 if ticks =  Duration [ stop ] 
 if ticks > Duration [stop]

end

to cultivate


  ifelse land-sustainability < 2.1 or water-level = 1
  [ set pcolor green set profit-from-food  food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 
    ]

  [
  let utility-from-food ((food-yeld * expected-food-sell-price * land-sustainability) ^ risk-attitude ) / risk-attitude 
  let utility-from-fuel ((food-yeld * expected-fuel-sell-price * land-sustainability) ^ (1 - risk-attitude) ) / ( 1 - risk-attitude)


  ifelse utility-from-food < utility-from-fuel
  [
    set pcolor red  
   set profit-from-fuel fuel-sell-price * (((fuel-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 
   ]

  [
    set pcolor green 
    set profit-from-food food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 
    ]

]



end

新版本_ _ __ _ __ ----

to cultivate
 ifelse land-sustainability < 2.1 or water-level = 1
[
   set profit-from-food  food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 
 set food 1 
  ]
 [
  let utility-from-food ((food-yeld * expected-food-sell-price * land-sustainability) ^ risk-attitude ) / risk-attitude 
  let utility-from-fuel ((food-yeld * expected-fuel-sell-price * land-sustainability) ^ (1 - risk-attitude) ) / ( 1 - risk-attitude)
  ifelse utility-from-food < utility-from-fuel
  [
   set profit-from-fuel fuel-sell-price * (((fuel-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 
   set fuel 1 
    ] 
   [
  set profit-from-food food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 
   set food 1
 ]
  ]
  ask farm [
  if food = 1 [set pcolor green]
  if fuel = 1 [set pcolor red]
 ]
  end

2 个答案:

答案 0 :(得分:0)

正如Seth所说,它与stackoverflow中的其他问题类似,这里是更相关的答案:

turtles-own [farm farm-size]
patches-own [belongs-to]

to setup
  clear-all
  ask patches [set belongs-to nobody]
  create-turtles 9
  [
    set farm-size random 5 + 1
    set label farm-size
  ]
  ask turtles
  [ 
    set-farm-in-radius farm-size
  ]
  ask patch -8 14 [set plabel "set-farm-in-radius"]
end

to set-farm-in-radius [d]
  move-to one-of patches with [not any? other patches in-radius d with [belongs-to != nobody]]
  set farm patches in-radius farm-size
  ask farm [set belongs-to myself]
  let c random 8 + 21
  ask farm [set pcolor c]
end


to set-farm-distance [d]
  Move-to one-of patches with [not any? other patches with [belongs-to != nobody and distance myself < d]]
  set farm patches with [distance myself < d] 
  ask farm [set belongs-to myself]
  let c random 8 + 21
  ask farm [set pcolor c]
end

enter image description here

使用距离导致以下设计,我设置了每个补丁的varibele belongs_to,显示谁在plabel中拥有该补丁,以便您可以更好地看到它。

enter image description here

更新:如果要对服务器场的每个补丁进行更改,则应ask farm []

to cultivate


  ifelse land-sustainability < 2.1 or water-level = 1
  [ 
    ask farm [
    set pcolor green 
    ]
    set profit-from-food  food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 

  ]
  [
  let utility-from-food ((food-yeld * expected-food-sell-price * land-sustainability) ^ risk-attitude ) / risk-attitude 
  let utility-from-fuel ((food-yeld * expected-fuel-sell-price * land-sustainability) ^ (1 - risk-attitude) ) / ( 1 - risk-attitude)


  ifelse utility-from-food < utility-from-fuel
  [ ask farm [
    set pcolor red  
  ]
   set profit-from-fuel fuel-sell-price * (((fuel-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 

  ]
  [
    ask farm [
    set pcolor green ]
    set profit-from-food food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 
    ]

]



end

更新

to cultivate

  ifelse land-sustainability < 2.1 or water-level = 1
  [

    set profit-from-food  food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 

    ask farm [
      set food 1 

    ]


  ]

  [
    let utility-from-food ((food-yeld * expected-food-sell-price * land-sustainability) ^ risk-attitude ) / risk-attitude 
    let utility-from-fuel ((food-yeld * expected-fuel-sell-price * land-sustainability) ^ (1 - risk-attitude) ) / ( 1 - risk-attitude)


    ifelse utility-from-food < utility-from-fuel
    [

      set profit-from-fuel fuel-sell-price * (((fuel-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 

      ask farm [
        set fuel 1 


      ]
    ]
    [

      set profit-from-food food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 

      ask farm [
        set food 1

      ]

    ]
  ]



end

to recolor-farm
      if food = 1 [set pcolor green ]
      if fuel = 1 [set pcolor red]
end

在培养

之后添加ask farm [recolor-farm]

答案 1 :(得分:0)

你对第二部分的回答:

to cultivate
 ifelse land-sustainability < 2.1 or water-level = 1
[
   set profit-from-food  food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa))
ask farm 
[
 set food 1 
]
  ]
 [
  let utility-from-food ((food-yeld * expected-food-sell-price * land-sustainability) ^ risk-attitude ) / risk-attitude 
  let utility-from-fuel ((food-yeld * expected-fuel-sell-price * land-sustainability) ^ (1 - risk-attitude) ) / ( 1 - risk-attitude)
  ifelse utility-from-food < utility-from-fuel
  [
   set profit-from-fuel fuel-sell-price * (((fuel-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 
ask farm
[
   set fuel 1 ]
    ] 
   [
  set profit-from-food food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) 

    ask farm [
       set food 1]

 ]
  ]
  ask farm [
  if food = 1 [set pcolor green]
  if fuel = 1 [set pcolor red]
 ]
  end