Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetLogo: Can the value of a global variable of last two ticks be stored in a list and be called in a procedure?

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.

like image 711
Shreesha Vaidhya Avatar asked Oct 15 '22 07:10

Shreesha Vaidhya


1 Answers

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
like image 145
Charles Avatar answered Oct 20 '22 16:10

Charles