netlogomulti-agent

Is there any better way to access patch and turtle 's variables at each tick?


I need to update each turtle's wealth variable at each tick which by number of calls is on top of my profiler list.

I am sure the most computation time goes to calculating how many other agents are using same patch for their home and calculating the share for each turtle. food_carrying and my-home are turtle's properties and Storage is patch property.

to update-wealth
  let h my-home
  set wealth ([Storage] of my-home / (Count agents with [my-home = h]))  + food_carrying
end

Can you think of any better way to do it ?

Name                               Calls Incl T(ms) Excl T(ms) Excl/calls
UPDATE-WEALTH                    9744912 831703.608 461086.654      0.047

Solution

  • I think it would be much faster to do this by patch rather than by turtle, since each turtle on a patch seems to be calculating the same value of wealth. Perhaps something like:

    ask patches [
      let my-turtles turtles-here
      if any? my-turtles [
        let w Storage / count my-turtles
        ask my-turtles [set wealth w + food_carrying]
        ]
      ]
    ]
    

    If you kept a running set of patches with turtle homes you could just ask that subset and not have to worry about there not being any on the patch and getting a division by zero.