How to extract the path taken by each turtle in the world and save it in a .csv file? - netlogo

I'm still trying to extract the coordinates of the path taken by every turtle in the world. For example, I would like to know the path taken by turtle 0 was patch 00 patch 0 5 patch 0 2 and patch 1 4 and save this information in a .csv file. In this way, I would like to extract the coordinates of the path taken by all the turtles in the world. I'm thinking about creating a list that gives me this information and then saving it.
The problem is that I cannot create an empty list for each turtle created and then, before every move, add the current patch location to the lists. And finally, generate the file of this information in .csv
That is, when inspecting the turtle I don't see the list and when creating the file in .csv it is not generated. Can anyone help me with the code?
Thanks in advance
turtles-own [ mypathx mypathy mytimer]
to setup
ca
reset-ticks
crt 5
ask turtles [
setxy random-xcor random-ycor
let i [ ]
pen-down
]
end
to go
move
let n count turtles
if n = 0 and output? = true [output] ;; ouput? is an switch on interface
if n = 0 [ stop ]
tick
end
to move
ask turtles [
rt random 360
fd 1
if ticks >= 10
[
die
]
]
end
To output
file-open "test.txt"
ask turtles [
set mypathx lput pxcor mypathx
set mypathy lput pycor mypathy
let maxer length mypathx
let i 0
repeat maxer
[
file-print (word item i mypathx " " item i mypathy " " item i mytimer)
let x ( i + 1 )
]
]
file-close
end

There are a number of things to note here, so I hope I understood what you inteded to do.
First of all, note that your code goes to output only after all the turtles died (that is infact one of the conditions to go to output: if n = 0). Therefore, it is normal that there is no data to be exported: in output you ask turtles, but when output is executed all turtles already left the simulation.
However, there is also something that Filip rightly noticed in the comment to the question: it seems logical to ask turtles to save the coordinates of their journey as they move - as opposed to asking them to do so at the end of the simulation.
Based on the current code, turtles simply move in the simulation, and only at the end (e.g. when output is called) they are are asked to save their current location. It would be ideal to include a piece of code where you ask turtles, after each move, to record their location in the list.
To combine this point and the previous one, I would do something like:
to setup
clear-all
reset-ticks
create-turtles 5 [
; I talk about this piece in point 3.
]
end
to go
move
tick
end
to move
ask turtles [
right random 360
forward 1
register-coordinates
]
end
to register-coordinates
set mypathx lput pxcor mypathx
set mypathy lput pycor mypathy
set mytimer lput ticks mytimer ; This is what I assumed you want mytimer to do.
end
And here comes the other problem. You are not initiating the turtles-own variables as lists.
If you run your piece of code now, nothing happens because all turtles are dead by the time you ask them to output. But if you just remove the die command, or if you ask turtles to output before they die, you would get an error - saying that lput expected a list but got something else instead.
From the NetLogo Dictionary you can see that the list primitive is used to create a list with two values.
If you want to create a list containing any other amount of values, you have to use (list ...). This includes the case in which you want to create an empty list. To do that, I would therefore do:
create-turtles 5 [
setxy random-xcor random-ycor
set mypathx (list)
set mypathy (list)
set mytimer (list)
pen-down
]
Now, mypathx, mypathy and mytimer are legal inputs to the lput procedure.
Finally, you can include the output at the end of your simulation (note how I changed go compared to the previous code example):
to go
if ticks = 11 [
ask turtles [output]
stop
]
move
tick
end
...
to output
file-open "test.csv"
file-print (word who "," mypathx "," mypathy "," mytimer)
file-close
end
So, overall, improtant adjustments are: initiate variables as lists if you want to treat them as such; ask your turtles to save their coordinates at every step in their turtles-own list variables; only at the end, ask them to write their list variables in the output file; do not kill your turtles before you ask them to output information.
Integrating all of the above, the code would look like:
turtles-own [mypathx mypathy mytimer]
to setup
clear-all
reset-ticks
create-turtles 5 [
setxy random-xcor random-ycor
set mypathx (list)
set mypathy (list)
set mytimer (list)
pen-down
]
end
to go
if ticks = 11 [
ask turtles [output]
stop
]
move
tick
end
to move
ask turtles [
right random 360
forward 1
register-coordinates
]
end
to register-coordinates
set mypathx lput pxcor mypathx
set mypathy lput pycor mypathy
set mytimer lput ticks mytimer
end
to output
file-open "test.csv"
file-print (word who "," mypathx "," mypathy "," mytimer)
file-close
end
Final notes on the code above:
I am not sure which exact format you want your output to be, but this example code works fine in saving the lists in the .csv file - so you can take it from there and play around to make it fit your needs.
I removed the pcolor part because it seemed unnecessary for the purpose of the question.
I am not sure what you wanted i to do in setup, but it seemed unnecessary for the purpose of the question so I removed that too from my exemplar code. Be careful at how you use item: I don't know what you wanted to do, but the way you included it in your question was often giving runtime errors.
I created a .csv as per the title of the question, instead of a .txt as per the code you provided.

Related

Netlogo - Iterating through patch list and writing each output to a row in a list

I have a list of cells destination_cells, and am trying to loop through each cell and count the number of turtles and the average weight of the turtles in each. For each cell, I want to write the cell's ID (which I am assuming is just destination_cells as coordinates) and the two variables to a list cell_list. I think my confusion stems from not understanding how to use anonymous reporters in foreach. If I replace 'next cell' with destination_cells in the foreach block, I get, for example, the same total_turtle_in_destination_cells value for every single cell in destination_cells. The same occurs with using next_cells in the block. Where am I making a mistake?
set destination_cells [self] of patches with [ any? turtles-here]
foreach destination_cells [next_cell -> ask next_cell
[
; Counts number of turtles in each cell
set total_turtle_in_destination count turtles-on destination_cells
; Finds average weight of turtles in each cell
set avg_weight_turtle_in_destination mean [mass] of turtles-on destination_cells
]
set cell_list lput (csv:to-row (list
destination_cells
total_turtle_in_destination
avg_weight_turtle_in_destination
)) cell_events_list
]
Here is an alternative that:
Makes a reporter to get the info desired (the patch info as patch x and y, the total turtles on a patch, the mean weight of present turtles) from whatever patch that calls the reporter
Updates the info list directly by querying the destination_cells
turtles-own [ weight ]
globals [ destination_cells cell_info_list]
to setup
ca
resize-world 0 5 0 5
set-patch-size 30
reset-ticks
crt 20 [
set weight random 10 + 1
move-to one-of patches
]
end
to-report report-self-info
let n_turtles_here count turtles-here
let mean_weight mean [weight] of turtles-here
report (list self n_turtles_here mean_weight)
end
to create-info-list
set destination_cells patches with [ any? turtles-here]
set cell_info_list [report-self-info] of destination_cells
print cell_info_list
end
In response to your question "how can the patch call the reporter? I tried ask destination_cell [report-self-info], but a command is expected":
When you make a to-report procedure with the structure like the above that uses self, it's somewhat like a turtles-own or patches-own variable. In this case, you can call it just as you would have a turtle variable like color or xcor or a patch variable like pcolor or pxcor- if you run something like:
ask one-of patches [ print report-self-info ]
You should get something like
[(patch 5 2) 2 7.5]
This is why the set cell_info_list [report-self-info] of destination_cells above works- because you could do something like set cell_info_list [pcolor] of destination_cells in a similar way.

How to implement a timer for each turtle in netlogo

I am trying to implement a timer for each turtle in Netlogo
so I can take the minimum value, maximum and average time taken for each turtle.
Can anyone help
You haven't said what you actually want timed (or given any code attempt). Any time you need a turtle to remember anything from one tick to the next, you need a variable. The simplest code for a timer is to have a variable that you set to the current value of ticks when the process starts and subtract that start time from the current value of ticks when the process stops. Here is a complete model example:
turtles-own
[ start-time
time-taken
]
to setup
clear-all
create-turtles 20
[ set start-time 2 + random 10
set time-taken -1
]
reset-ticks
end
to go
let movers turtles with [time-taken = -1 and start-time <= ticks]
ask movers
[ set heading random 360
forward 1 + random 3
if random-float 1 < 0.05 [ set time-taken ticks - start-time ]
]
tick
end
And check out the new Time extension if you want your timers to use real time units (seconds, minutes, years...) and have non-integer values.
https://github.com/NetLogo/Time-Extension
Look at primitives such as time:difference-between
For example, you could do this coding that flags the turtle you want to monitor and increments the flag variable when an event occurs. You can then monitor it in the BehaviorSpace and analyses the results output in a csv file. For example, the following codes:
globals [ID]
turtles-own [special-turtle]
to create-turtle
crt 1 [
setxy min-pxcor 0
set heading 90
set special-turtle false
set ID who]
end
to go
;omitted
special-turtles
tick
end
to special-turtles
ask turtles-on patch 0 0 [set ID who]
ask max-one-of turtles [who] [set special-turtle true]
ask turtles with [special-turtle = true][set special-turtle (special-turtle + 1)]
end
I think there is something that none of the previous answers considered: the way you implement your timer depends on how you want to use the measurement.
1 - If you want to read the measurement only after the measurement is completed
The expression "after the measurement is completed" can mean both that you want to read it at some point later during the model, or maybe even just from some model's output.
The approach I'd take in this case is similar to what JenB suggested, but I believe you can put it a bit simpler because here I only use one extra turtle variable (apart from my-variable, that is there only to represent something that you already have in your model):
turtles-own [
my-variable ; This represents something you have in your model, to be used as a condition for the timer
my-timer
]
to setup
clear-all
reset-ticks
create-turtles 10
end
to go
; Here you have your normal code. When you need, just include
; 'start-timer' and 'stop-timer'. For simplicity, let's
; say you want to count how long does each turtle take
; to get from a value of 1 to a value of 5 for my-variable.
; You can just do:
ask turtles [
if (random 10 < 2) [
set my-variable (my-variable + 1)
]
if (my-variable = 1) [
start-timer
]
if (my-variable = 5) [
stop-timer
type "I am turtle " type who type " and it took me " type my-timer print " ticks."
die
]
]
tick
if (not any? turtles) [stop]
end
to start-timer
set my-timer ticks
end
to stop-timer
set my-timer (ticks - my-timer)
end
Note that most of the code is there only to make a full reproducible example, but the actual implementation only consists of my-timer, to start-timer and to stop-timer.
You can take this approach because the hypothesis here is that you will be interested in reading the measurement only after to stop-timer happened.
Otherwise, see point 2.
2 - If you want to be able to read the measurement at any moment during the simulation
In this case, you need to make the timer progress as time progresses (as opposed to point 1, where you could just take the initial and final time, and make the difference between the two).
I guess the easiest way to do this is to conditionally increment the timer every time there is a tick.
Taking the same example as before, it would be something like:
turtles-own [
my-variable ; This represents something you have in your model, to be used as a condition for the timer
timer-active?
my-timer
]
to setup
clear-all
reset-ticks
create-turtles 10 [
set timer-active? FALSE
]
end
to go
ask turtles [
if (random 10 < 2) [
set my-variable (my-variable + 1)
]
if (my-variable = 1) [
set timer-active? TRUE
]
if (my-variable = 5) [
set timer-active? FALSE
]
]
tick
ask turtles with [timer-active?] [
set my-timer (my-timer + 1)
]
if (count turtles with [my-variable < 5] = 0) [stop]
end
This way, you will be able to see at any moment what is the current value of my-timer for each turtle.

How to give a score when an element is pulled out from a list?

I would like to implement a part of code where agents can get a score when they pick an element from a list generated from a specific turtle.
I set
breed [playersA playerA]
breed [playersB playerB]
breed [balls ball]
playersA-own[
my-list
new_ball
score
]
playersB-own[
my-list
new_ball
score
]
to setup
clear-all
create-playersA 10
ask playerA 0 [ create-links-with other playersA ]
ask playerA 2 [ create-link-with playerA 1 ]
create-playersB 10
ask playerB 0 [ create-links-with other playersB ]
ask playerB 2 [ create-link-with playerA 1 ]
ask playersA[
set my-list []
set score 0
]
ask playersB[
set my-list []
set score 0
]
end
to go
let selected nobody
let team-player nobody
set selected one-of turtles with [breed=playersA or breed=playersB]
ifelse [breed = playersA] of selected[
ask selected [
set size [count link-neighbors] of self
show size
]
create-balls 1[
hide-turtle
]
]
[ ask selected [
set size [count link-neighbors] of self
show size
]
create-balls 1[
hide-turtle
]
]
set team-player link-neighbors with [breed = playersA]
ask team_player [
set my-list lput my-ball my-list
]
end
The above code should select on random turtle and add a new ball to its neighbours list. What I would need is probably a counter that can compute how many balls were shared between players.
Could you please help me to figure out with it?
Thanks
The code you posted has many problems that prevent it from passing error-checking in the editor. Some of these produce surprising error messages that don't even make sense, and they happen because the logic mixes contexts -- that is, some commands make sense for the "observer" level, some require being in a "turtle" context, etc.
I think you are trying to do too much at once, and trying to add a counter to code that already does not work. First you have to fix the code you have and then you can see where to add a counter.
You absolutely must understand how the unique agent id number "who" works. Each turtle has a unique who number assigned, starting with zero. It doesn't matter whether the breed of turtle is playerA or playerB or a ball, it will have a unique number. Once you create your first 10 turtles, of the PlayerA breed, they will have who numbers 0 through 9. Then, when you create the next 10 turtles, of PlayerB breed, they will get assigned who numbers of 10 through 19. If you then create a ball, say, it will have a who number of 20.
So there will never be a PlayerB with a who number of 0 or 1 or 2. Those numbers will already be used by PlayerA. Your setup will crash with the error:
playera 0 is not a PLAYERB error while observer running PLAYERB
called by Command Center
Even with just PlayerA, it is not clear what kind of network you want to build in the setup code. Why would everyone link to player 0, but then also add a single link between player 1 and player 2? Since players only "see" their linked team-mates, only player zero will see everyone else. Other players will have only one or two link-neighbors, so they will never update everyone else's my-lists.
create-playersA 10
ask playerA 0 [ create-links-with other playersA ]
ask playerA 2 [ create-link-with playerA 1 ]
Anyway, I would suggest that you get this much working correctly before trying to add counting.
I don't think you can do that by just looking at the code. You need to get rid of as much complexity as you can, and then use shapes, colors, and numerous print statements to see whether each command is doing what you think it should do. Complex working code almost always evolves from simple working code.
So get rid of PlayersB entirely ( comment out the code ), only create 5 players A, and change the colors and shapes as you process each step to confirm that it is working. The editor lets you use ctrl-; to comment out entire blocks of code, or un-comment them at once, so comment out everything you possibly can while you are getting one step to work, then uncomment the next section, get that to work, etc.
When you finally get everything working, you can comment out all your print statements that you used in development.
Anyway, I refactored your code, added many comment, and added many print statements, and finally got it to run. If you run just setup and look at the view, you will see what I mean about the network. ( I shut off wrapping in the view so the network looks right.)
Here's my revision of your code. It prints out what is in each player's my-list after each step, so you can see at a glance if it is doing what you want, or not. ( It's not.)
I added the who numbers as a label to each player in the view so you can see what I mean.
It produces helpful output like:
let's confirm that the lists have been updated. Here's the my-lists
for playersA [[5 5 5 5 10] [0 0 0 0 0] [0 8 8] [0 0] [0 9]]
Get the setup step to work correctly and generate the network you want before you even try to fix the go section.
breed [playersA playerA]
breed [playersB playerB]
breed [balls ball]
playersA-own[
my-list
new_ball
score
]
playersB-own[
my-list
new_ball
score
]
to setup
clear-all
;; for debugging, only create 3 players and inspect the results to confirm it's working as you intended
;; use labels to see player numbers in the view
create-playersA 5 [ set size 1 set color blue set shape "square" setxy random-xcor random-ycor set label who]
ask playerA 0 [ create-links-with other playersA [set color blue]]
ask playerA 2 [ create-link-with playerA 1 [set color red]]
create-playersB 5 [ set size 2 set color yellow set shape "person" setxy random-xcor random-ycor set label who]
; comment out this code until create-playersA is working properly
; ask playerB 0 [ create-links-with other playersB ]
; ask playerB 2 [ create-link-with playerA 1 ] ;; copy-and-paste error? link with playerB intended?
ask playersA[
set my-list []
set score 0
]
ask playersB[
set my-list []
set score 0
]
reset-ticks
end
to go
let selected nobody
let team-players nobody
let hot-ball nobody
set selected one-of turtles with [
breed = playersA
;; or breed = playersB ;; always select one of playersA for debugging this code
]
print ( word "At point 1, we selected turtle " [who] of selected " with breed " [breed] of selected)
;; we're still in the observer context here
ifelse [breed = playersA] of selected [ ;; by mentioning breed, we shift into a turtle context silently
print ( word " entering the TRUE part of the if-else statement " )
ask selected [
set size [count link-neighbors] of self
print ( word "at point 2 we set selected player's size to " size )
]
create-balls 1 [
set shape "circle" set size 3 set color blue set label who
set hot-ball who
; hide-turtle ;; for debugging show it so you can click on it and inspect it
print ( word "at point 3 we set created a blue hot-ball with who= " hot-ball )
]
;; it seems you want to update the selected turtle's my-ball variable here with a reference to the ball just created??
print " at point 4 we should set selected agent's my-ball to the ball we just made..."
ask selected [
set new_ball hot-ball
]
print (word " Confirming that selected player got the hot-ball " [new_ball] of selected )
;; ask ball hot-ball [ set hidden? true ]
;; this set of code seems to apply only when selected turtle is one of playersA, so it was moved INSIDE that ask-selected code
;; and put inside another ask selected [ ] context
ask selected [
set team-players link-neighbors with [breed = playersA]
print (word "At point 5, For selected player " who ", here is the team-players agent set :" )
print (sort team-players) ;; using "sort" here just to convert an agent set to a list for display
]
print " ------------- about to ask team-players to update their my-lists and change to triangles ---"
ask team-players [
set shape "triangle" set size 3 ;; to see at a glance that everyone was processed
set my-list lput new_ball my-list
print "... updated one my-list"
]
print " let's confirm that the lists have been updated. Here's the my-lists for playersA "
print map [ i -> [my-list] of i ] sort playersA ;; using "sort" to convert agent-set to a list
print (word "At the end of the go step, we have this many balls: " count balls)
]
;; else we should have breed != playersA
[
error " we should only be looking at one of playersA here for testing" ;; for debugging
]
;; tick
end

Get color of global variables with netlogo

I am trying to get the real color of my global variable.
Here my code:
breed [players player]
globals [
INITIAL-POSSESSION ;
]
to setup
clear-all
reset-ticks
set-initial-possession
end
to go
ticks
ask players [
decision
]
end
to-report initial-possession
report random 2
end
to set-initial-possession
ifelse initial-possession = 1
[
set INITIAL-POSSESSION black]
[
set INITIAL-POSSESSION white]
end
to decision
if ([color] of INITIAL-POSSESSION) = black
[] ;;do something
if ([color] of INITIAL-POSSESSION) = white
[];;do something
end
But I get this error:
OF expected input to be a turtle agentset or link agentset or turtle
or link but got the number 0 instead.
So I change it with (and it works):
to decision
if INITIAL-POSSESSION = 0
[]
if INITIAL-POSSESSION = 9.9
[]
end
But there is any other way to do that? (I am using netlogo 6.0)
I think there may be some code missing so I can't confirm, but it looks like you may not have set BALL-OWNER up as a turtle or patch, and instead assigned a value directly to that variable. of queries a variable from an agent (or a list of variables from an agentset), so if BALL-OWNER is set to a value, NetLogo gets confused. If you do assign an agent to BALL-OWNER, however, your approach should work fine. For example, try running the code below:
to setup
ca
crt 10 [
setxy random-xcor random-ycor
set color one-of [ red blue ]
]
reset-ticks
end
to go
let ball-owner one-of turtles
ifelse [color] of ball-owner = red [
print "red team has possession"
] [
print "blue team has possession"
]
end
Edit: You can definitely use a global to pick a color just as you did in your second code block- I just wanted to point out that of is specifically tied to agents. If you want to store a color in a global variable for comparison, that's possible, it's just that your comparison is simpler than using of:
globals [ initial-possession ]
to setup
ca
crt 3
set-initial-possession
reset-ticks
end
to go
ask turtles [
decision
]
end
to set-initial-possession
set initial-possession ifelse-value ( random 2 = 1 ) [black] [white]
end
to decision
ifelse initial-possession = black [
print "I see that black has possession"
] [
print "I see that white has possession"
]
end
I'm not sure if that helps, it may depend on your purpose of storing a color in the global in the first place!

Netlogo: Set specific setxy patern with set number of turtles?

Is it possible to create a set number of turtles, from a file, to have their own patches? Like always be in the same location?
I've got 106 turtles I'm reading in from a file and I was hoping to have them be created on their own patches, like a square latice kind of thing. I want to be able to look at the model world and easily identify a turtle.
file-open "turtledata_A.txt"
show file-read-line
while [not file-at-end?]
[
set param read-from-string (word "[" file-read-line "]")
create-turtles 1 [setxy ??]
]
file-close
]
Probably easiest to use the csv extension and just add xy data to the file you're reading in. For example, if you have a turtle_data.csv file that looks like:
param-to-read,x,y
John,-10,10
Billy,-5,5
Bob,0,0
Simon,5,-5
Michael,10,-10
You can do:
extensions [ csv ]
turtles-own [ param ]
to setup
ca
reset-ticks
file-close-all
file-open "turtle_data.csv"
;; read the headings line in to skip it for data extraction
let headings csv:from-row file-read-line
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set param item 0 data
setxy item 1 data item 2 data
]
]
file-close-all
end
which would give you something like:
Then you can modify the x and y values in your .csv file to place your turtles where you want them. Would that work?
Of course, you can add other columns in the .csv file (like color, size, shape, etc) that will help you identify turtles at a glance.