shaden
shaden

Reputation: 1

How to store different list values for different turtles

Edited: Each turtle has different values depending on its connections, I have several lists for different purposes. one of those lists seems to be stored once for all the turtles. My question is, how can I store values that are only related to each turtle in its list and not mix up all the values in one list. I am getting something like this as a result:

(turtle 0): [3.1198376765467213 2.296024229601798 3.42548843517858 -1.259846009171373 -0.7503525744180024 0.8329075988682271 2.5179361772122446 2.499561039717374] (turtle 1): [3.1198376765467213 2.296024229601798 3.42548843517858 -1.259846009171373 -0.7503525744180024 0.8329075988682271 2.5179361772122446 2.499561039717374] (turtle 2): [3.1198376765467213 2.296024229601798 3.42548843517858 -1.259846009171373 -0.7503525744180024 0.8329075988682271 2.5179361772122446 2.499561039717374] (turtle 3): [3.1198376765467213 2.296024229601798 3.42548843517858 -1.259846009171373 -0.7503525744180024 0.8329075988682271 2.5179361772122446 2.499561039717374]

when in fact the result should be like this

(turtle 0): [3.1198376765467213 2.296024229601798] (turtle 1): [3.42548843517858 -1.259846009171373] (turtle 2): [-0.7503525744180024 0.8329075988682271] (turtle 3): [2.5179361772122446 2.499561039717374]

Here is the codes I tried: (please note that calc-payoff is a function that calculates the payoff values for each turtle and it works perfectly fine)

ask turtles [ calc-payoff set p_list lput ([payoff] of self) p_list reset]

AND

Added part: the problem is that some of the turtles have turtle_list2 = 0 by the end since the if condition was not fulfilled for them. However, I want the round to keep going until all the turtles find their stable values. (this part is after the tick)

ask turtles [ calc-payoff set p_list lput payoff p_list reset]

ask turtles [create-links-to other turtles 
calc-payoff2
set turtle_list lput payoff turtle_list 
reset]

ask turtles [
ask one-of links [die]
calc-payoff2
set turtle_list lput payoff turtle_list
reset]

tick
set tickcount tickcount + 1
if tickcount >= 3[  
ask turtles   
[
let p1 item (length turtle_list - 3) turtle_list
let p2 item (length turtle_list - 2) turtle_list
let p3 item (length turtle_list - 1 ) turtle_list
if p1 < p2 [if p2 > p3 [ set turtle_list2 (list p2) ]]
set payoff_list lput (sum turtle_list2 ) payoff_list]
user-message (word "stability is reached at payoff = "  sum payoff_list)]
end



Upvotes: 0

Views: 233

Answers (1)

Luke C
Luke C

Reputation: 10301

Hard to say for sure without seeing your code, but my guess is that p_list is a globals variable instead of a turtles-own variable. Have a look at the variables section of the NetLogo user manual for some more details.

For an example, have a look at the following toy setup:

globals [ global-list ]

turtles-own [ turtle-list ]

to setup
  ca
  set global-list []
  crt 10 [
    move-to one-of patches
    set global-list lput xcor global-list
    set turtle-list ( list xcor )
  ]  
  reset-ticks
end

After running that setup, if you do print global-list, you should see output like:

[-14 4 -3 11 -16 -2 4 8 -1 -9]

If we ask the turtles to show their own list with

ask turtles [ show turtle-list ]

you should see something like:

observer> ask turtles [ show turtle-list ]
(turtle 3): [-14]
(turtle 5): [8]
(turtle 9): [4]
(turtle 7): [-16]
(turtle 2): [4]
(turtle 0): [11]
(turtle 6): [-3]
(turtle 8): [-9]
(turtle 1): [-1]
(turtle 4): [-2]

Where each turtle has their own version of that list.

Edit:

To have your turtles update their lists, you can use exactly the same lput syntax you used in your question- for example, the following loops until a randomly drawn float value is less than 0.25, then has turtles print out their turtle-list. For each iteration of the loop, the turtles will move, then add to their turtle-list:

to go 
  loop [
    if random-float 1 < 0.25 [
      ask turtles [ show turtle-list ]
      stop
    ]
    ask turtles [
      fd 1 
      set turtle-list lput xcor turtle-list
    ] 
  ]
end

You get an output like:

(turtle 2): [13 13 13 13]
(turtle 5): [-4 -4.3583679495453005 -4.716735899090601 -5.0751038486359015]
(turtle 9): [14 13.015192246987791 12.030384493975582 11.045576740963373]
(turtle 7): [-3 -2.5 -2 -1.5]
(turtle 3): [-2 -2.3420201433256693 -2.6840402866513386 -3.026060429977008]
(turtle 0): [7 7.951056516295154 8.902113032590307 9.853169548885461]
(turtle 4): [-14 -13.947664043757056 -13.895328087514113 -13.842992131271169]
(turtle 8): [12 11.35721239031346 10.71442478062692 10.071637170940381]
(turtle 1): [16 15.674431845542843 15.348863691085686 15.02329553662853]
(turtle 6): [-16 16.426423563648953 15.852847127297908 15.279270690946863]

Upvotes: 2

Related Questions