Question

I am fairly new to Netlogo and have been working through Railsback & Grimms excellent book while trying my own stuff on the side

Currently, I am trying to code a kind of money transfer whereby two turtles meet (e.g. Turtle 1 and Turtle 2), and the two turtles then share money (e.g. T1 has $4 and T2 has $6, they both leave with $5) The code I have tried using is as below

 to currency-share
   let neighbor one-of bystanders-here                    ; identify neighbor around
   if neighbor != nobody                             ;; is there a neighbor?  if there is
    [ set currency round ((currency + neighbor currency) / 2)] ; share money
 end

Unfortunately the code isn't working and I can't find any examples through the forums which use a similar idea. Perhaps I'm searching incorrectly (using key words like turtle exchange, share etc. normally comes up with patch sharing). If anyone has any models they could recommend where such exchanges happen, or if anyone knows how I may improve my code, please let me know. Thank you.

Était-ce utile?

La solution

At first glance, this seems like a simple syntax problem. To get the currency of the neighbor, you should be using of:

set currency round ((currency + [ currency ] of neighbor) / 2)

But since you also said that you want both turtles to get the new amount, you also need to add:

ask neighbor [ set currency [ currency ] of myself ]

Or, perhaps less confusingly you could do something like:

set new-amount round ((currency + [ currency ] of neighbor) / 2)
set currency new-amount
ask neighbor [ set currency new-amount ]

One last variant, slightly better in my opinion because more general (and perhaps even clearer) is:

let sharers (turtle-set self one-of bystanders-here)
let new-amount mean [ currency ] of sharers
ask sharers [ set currency new-amount ]

In this last one, you don't even need the if neighbor != nobody check because if there are no bystanders, turtle-set will build an agentset containing only self and the mean currency of that set will just be the present currency value.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top