NetLogo:按选定的补丁值更新每个时间步的全局变量?

时间:2016-03-31 22:10:31

标签: netlogo

我有一个非常简单的问题,但我无法弄明白。

我每年都有一些钱或total_money_to_spend。这笔款项将于明年续签。我的乌龟每次购买一个补丁,因此我的total_money_to_spend应减少price值,而我的'cash_balance'应为total_money_to_spend - price。在total_money_to_spend允许的情况下,Turtle会购买尽可能多的补丁。从而: total_money_to_spend = 50 price = 10 我的'cash_balance'应该是40,30,20,10,0 ......

到目前为止,概念非常简单。

但是,如何让它在Netlogo中运行?当我使用total_money_to_spend作为全局变量时,我的'cash_balance'不会更新每个绑定步骤,它仍然保留在50.如何在龟购买补丁时更新我的​​'cash_balance'?

非常感谢你!

globals [
  cash_balance        ; value total_money_to_spend - price of patch
  total_money_to_spend  ; sum of money
]

patches-own [
  price                 ; cost of the patch      
]

to setup
  ca
  set total_money_to_spend 50
  crt 1 [
    set color red ]
  ask patches [
    set price 10
  ]
  reset-ticks
end

to go
  if not any? patches with [price > 0] [
   stop ]
  spend-money
  tick
end

to spend-money            ; turtle goes shopping
  ask turtles [
    let price_of_patch [price] of patch-here
    move-to one-of patches with [price > 0]
    set pcolor magenta
    set price 0

   ; here is the problem - how to update my cash_balance??
; ------------------
    set cash_balance (total_money_to_spend - price_of_patch) 
  ]
end

1 个答案:

答案 0 :(得分:1)

我刚才弄明白:在我的程序globals[]中更新spend-money

to spend-money            ; turtle goes shopping

  set cash_balance (total_money_to_spend - 10) ; set the actual cash balance first
  ask turtles [        
    move-to one-of patches with [price > 0]
    set pcolor magenta
    set price 0
    set total_money_to_spend  (total_money_to_spend - 10) ; change the global variable within the procedure
  ]
end

但我会感激任何其他可能性! ;) 谢谢 ! ;)