memory for turtles

223 views
Skip to first unread message

parichehr mandegaran

unread,
Apr 8, 2021, 6:19:21 AM4/8/21
to netlogo-users
How do I define memory for turtles in Netlogo that dumps the results of each tick into memory? Each turtle will choose according to its memory in the next ticks.

Michael Tamillow

unread,
Apr 8, 2021, 8:01:34 AM4/8/21
to parichehr mandegaran, netlogo-users
It would almost certainly be a case by case basis, depending on how much of the previous state needed to be remembered by each turtle.

It comes back to two questions. What specifically are you trying to accomplish, and why are you trying to accomplish it?


On Apr 8, 2021, at 5:19 AM, parichehr mandegaran <parichehrm...@gmail.com> wrote:

How do I define memory for turtles in Netlogo that dumps the results of each tick into memory? Each turtle will choose according to its memory in the next ticks.

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/52668c98-ddf2-4c1c-b287-9083f17dfde9n%40googlegroups.com.

Aaron Andre Brandes

unread,
Apr 8, 2021, 9:54:51 AM4/8/21
to Michael Tamillow, parichehr mandegaran, netlogo-users

Hi,

The solution does depend on the case, but typically you will need to make use of at least one turtles-own or <breeds>-own variable to capture the information for each turtle. You could have variables such as previous-xcor, previous-number-of-neighbors etc.

You can also use a list, for example to store a value for each previous tick.

Aaron

 

 

-- 

Aaron Brandes, Software Developer

Center for Connected Learning and Computer-Based Modeling

Michael Tamillow

unread,
Apr 8, 2021, 10:12:56 AM4/8/21
to Aaron Andre Brandes, parichehr mandegaran, netlogo-users
Hey Aaron,

parichehr mentioned that his problem looks like this:

" Some of farmers choose the most profitable crop as their crop this year based on the profit from the crops of the last five years."

My thought is to use a table. I would make each turtle own the table corresponding to its profits per crops, and with rows as history, and columns as crops.

However, I would sincerely advise against directly recording history as it means your model no longer has the markov property, which is valuable in a number of ways. Instead it makes much more sense to use rolling updates that reflect the knowledge contained by the agent, rather than trying to remember everything and then do computation over historical values. The agent's state should encapsulate everything important for the future and nothing more.

Aaron Andre Brandes

unread,
Apr 8, 2021, 10:56:13 AM4/8/21
to Michael Tamillow, parichehr mandegaran, netlogo-users

Hi Michael,

I agree that a table makes sense to deal uniformly with multiple crops without repeating code unnecessarily.

 

Rolling updates also makes sense. I would see that as an enhancement because handling lists is more straight forward.

 

Although rolling updates just require basic list operations that would not be totally straight forward for some users.

 

If you want to share some code that illustrates rolling updates and/or using a table as a turtles-own variable I’m sure many netlogo-users would appreciate it.

Michael Tamillow

unread,
Apr 8, 2021, 12:39:22 PM4/8/21
to Aaron Andre Brandes, parichehr mandegaran, netlogo-users
Sure, here's some code fresh off the press!

turtles-own
[
  best-prices ; list of best prices by crops [corn soy wheat]
  last-patch
]

patches-own
[
  price ; price for a crop
  crop  ; crop
]

globals
[
  crops
]

to setup
  ca
  create-turtles 50
  [
    setxy random-xcor random-ycor
  ]
  set crops ["corn" "soy" "wheat"]
  ask patches [ set crop one-of crops]
  ask patches [ set price random 10]
  ask turtles[
    set best-prices map [cr -> mean [price] of patches with [crop = cr]] crops  
  ]
end

to go
  ask turtles [
    ifelse patch-here != last-patch [
      let alpha .1
      let cr [crop] of patch-here
      let pr [price] of patch-here
      let idx position cr crops
      let new_prc (item idx best-prices * (1 - alpha) + alpha * pr)
      set best-prices replace-item idx best-prices new_prc
      set last-patch patch-here
    ][
      set heading heading + (random-float 1 - random-float 1)
      forward .001
    ]
  ]
end

You could copy and past it and add a setup and go button in the interface, but it won't visually display much useful info,

If you want to get something interesting, you should click on the different agents and inspect their variables.

each turtle's initial prices for the crops are:

ask turtles[
    set best-prices map [cr -> mean [price] of patches with [crop = cr]] crops  
  ]

which is a list of the mean price of all patches for each crop. A lot of the code in there is just for manipulating lists, getting values from indices and replacing them. This is the rolling update:

let new_prc (item idx best-prices * (1 - alpha) + alpha * pr)

You could implement this differently but this essentially does the update weighting the current best new price information as 10% of the total contribution and the accumulation of all historical prices as 90% of the contribution.

Aaron Andre Brandes

unread,
Apr 8, 2021, 1:10:58 PM4/8/21
to Michael Tamillow, parichehr mandegaran, netlogo-users

Hi Micheal,

Thanks sharing this excellent example of saving and using the memory of multiple previous values using an array.

Michael Tamillow

unread,
Apr 8, 2021, 2:49:33 PM4/8/21
to Aaron Andre Brandes, parichehr mandegaran, netlogo-users
You're welcome.
Reply all
Reply to author
Forward
0 new messages