5

I want agents to remember the values of that global variable of only last ticks and I want to store them in a list and use it later wherein agents should compare the list items and take a decision for the current tick. I have tried implementing the following code but the efforts are going in vain.

    `set time-car_t-2 time-car of tick [n - 2]
     set time-car_t-1 time-car of last tick
     set history-time-car [list time-car_t-1 time-car_t-2 time-car_t]

Logic to calculate time-car is already in place and working wherein all three are global variables "time-car", "time-car_t-1" and "time-car_t-2"

Any suggestions would help and I would be grateful. Thanks in advance.

1 Answers1

3

NetLogo doesn't remember past values, so it cannot give you the value of a variable (or result of a reporter) at a past tick. You need to save those values in your model as they are generated and this is normally done by using a list. In the code below, each turtle is setup with a time-car-history of length 5 (specified by history-length), that is initially filled with -1's. Then, in test, each turtle gets of value of time-car (here just a random number) and adds it to the beginning of its history. The current value is thus item 0 in its time-car-history, the value of one tick before is item 1 time-car-history, etc., going back four ticks. Note that in adding the current value to time-car-history I drop the last value using but-last, so only the most recent five values are saved. If you paste this code into a blank model, type "setup" at the command line and then repeatedly type "test", you should see how it works.

turtles-own [time-car-history]

to setup
  clear-all
  let history-length 5 ; the number of periods you want to save
  create-turtles 10 [
    ; creates a history list of the right length, filled with -1's.
    set time-car-history n-values history-length [-1]
  ]
  reset-ticks
end

to test
  ask turtles [
    set time-car-history fput time-car but-last time-car-history
  ]

  ask turtle 3 [
    show time-car-history
    show item 0 time-car-history
    show item 1 time-car-history
    show item 2 time-car-history
  ]
  tick
end

to-report time-car
  report random 10
end
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Charles
  • 3,888
  • 11
  • 13
  • But, I do have an extension to my question. In your example code, history-length is both a local variable and a list? and if that is the case, if I have to call the list else-where in a different procedure other than setup, like for example, "min histroy-length", it is not possible. On the other hand, making history-length a global variable and using it as setting the value to 5 will not allow me to make it a list either. Do you have any suggestions for this probelm? Hope my question is clear enough. Appreciate your support in advance. Thank you. – Shreesha Vaidhya Jan 23 '20 at 09:38
  • Shreesha, `history-length` is a local variable, set to the number 5. It is used simply to specify the length of the `time-car-history` list, which is a turtles-own variable initialized in setup. There is no need to use `history-length` again as it is `time-car-history` which holds the information you want to be able to set and access as the model runs. I could have just used `set time-car-history n-values 5 [-1]` and not had the `history-length` variable at all, but I find it useful to give such parameters a name so it is easier to know what they mean and where to change them. Charles – Charles Jan 23 '20 at 15:18
  • By the way, if you ever need to know what the value of `history-length` was during setup, it will simply be `length time-car-history`. Charles – Charles Jan 23 '20 at 15:20
  • Hello Mr. Charles, As an extension to the question, after storing the values in the list as well as updating the list with the latest values, suppose I want to choose time-car as the "mean" value of all the 5 time-car values stored in the list, is it possible? I understand we cannot apply mathematical operations like mean and all to the list of items. Any alternate suggestion to achieve this would also help. Thank you so much in advance. – Shreesha Vaidhya Feb 13 '20 at 14:31
  • 1
    Shreesha, `mean` is designed to work on lists, so `mean time-car-history` will give you the average of the 5 entries in the list, which is to say the average of the most recent 5 observations. This mean would, of course, be for a given turtle. If you wanted the mean over all turtles, that is easy to get too, with `mean [mean time-car-history] of turtles`, i.e., the mean of the means. – Charles Feb 14 '20 at 16:19