In my model I have option to choose agents (1,2,3 or 15). Each of them has same variables and I need to print them into CSV files(depending on number of agents, they may be 2 files or 15 files). What is the best way to do it? I have been looking in dictionary & StackOverflow, but I didn't found this case.
Thank you for your help.
So ... it is certainly possible but perhaps not convenient. For example,
extensions [csv]
turtles-own [var1 var2 att-file]
to setup
ca
crt 15 [setup-att-file]
end
to setup-att-file ;turtle proc
set att-file (word "turtle" who ".csv")
carefully [file-delete att-file] []
end
to-report get-att-vals ;turtle proc
report (list var1 var2)
end
to export-att-vals ;turtle proc
ask turtles [
file-open att-file
file-print csv:to-row get-att-vals
file-close
]
end
looks I don't need to use csv extension - just need to plot variables and then export plots.
ask turtles [
create-temporary-plot-pen (word who)
set-plot-pen-color color
plotxy xcor ticks
]
This works pretty much, answer was replied by Seth Tisue in this topic How to properly plot variables
Related
i wonder if any netlogo expert can help me with the following: supposed my patches have 2 attributes (attr1 and attr2, say) and i need to compute a function of both attributes, as in this silly example
patches-own [ attr1 attr2]
to setup
clear-all
ask patches [set attr1 random 10]
ask patches [set attr2 random 20]
let mean-1 mean [attr1] of patches
let mean-2 mean [attr2] of patches
show mean-1
show mean-2
end
i would like to write a procedure which computes the mean (say) of any attribute i pass to it. something like:
ask patches [set attr1 random 10]
ask patches [set attr2 random 20]
compute-mean attr1
compute-mean attr2
end
to compute-mean #attr
let mean-1 mean [#attr] of patches
show mean-1
end
would anyone be able to help? thank you in advance
It would be a lot easier to compute-mean [attr1] of patches and not have to bother with extracting the values within your procedure. It also makes your procedure more flexible, allowing you to use it for subsets of patches.
If you want a procedure to be a calculation, it is better to write it down as a reporter procedure using to-report. This way you can use the output it generates in other parts of your code as well instead of just showing it.
to go-1
show compute-mean-1 [attr1] of patches
show compute-mean-1 [attr2] of patches
end
to-report compute-mean-1 [#attr-list]
let mean-1 mean #attr-list
report mean-1
end
Now if you really just want to pass your procedure the name of the attribute and not the list of values, you will need to pass it the name of the attribute as a string and then use run-result to interpret that string ("attr1") as if it is a reporter (attr1)
to go-2
show compute-mean-2 "attr1"
show compute-mean-2 "attr2"
end
to-report compute-mean-2 [#attr-name]
let mean-2 mean [run-result #attr-name] of patches
report mean-2
end
Picture of my behaviourspace menu
I'm working on an agent based model where a variable (agentvariable1) owned by all agents changes every tick. I want to report a time series for the values of this variable for every agent using Behaviourspace.
However, when I measure runs using the following reporter
[agentvariable1] of turtles
the values that are reported for agentvariable1 are randomly shuffled, because "turtles" calls all turtles in a random order, which is different every tick. Because of this the data that is exported is not usable to create a time-series.
Is it posstible to create a reporter in Behaviourspace that reports the values of the agentvariable1 in a sequence that remains the same every tick?
Using sort on an agentset creates a list of those agents sorting them by some criteria. In the case of turtles, they are sorted by their who which means that their relative order will always be the same.
However you cannot directly do [agentvariable1] of sort turtles, because of expects an agent/agentset but you are giving it a list.
What you can do is creating a global variable as a list: at each tick the list is emptied, and later all turtles (sorted as per sort) will append their value to the list.
That list is what you will report in your Behavior Space.
globals [
all-values
]
turtles-own [
my-value
]
to setup
clear-all
reset-ticks
create-turtles 5
end
to go
set all-values (list)
ask turtles [
set my-value random 10
]
foreach sort turtles [
t ->
ask t [
set all-values lput my-value all-values
]
]
show all-values
tick
end
As an alternative to Matteo's answer (which is perfectly suitable and directly addresses your intention, I just present another option depending on preference) you could also pair the variable of interest with some turtle identifier and report that as a list of lists. This adds a bit of flexibility in cases where the number of turtles increases or decreases. In this example, I use who and xcor for simplicity, but you may want to create your own unique turtle identifier for more explicit tracking. With this toy model:
to setup
ca
crt 5
reset-ticks
end
to go
ask turtles [
rt random 30 - 15
fd 1
]
tick
end
to-report report-who-x
report list who xcor
end
At any point, you can call the list with [report-who-x] of turtles to get a list of lists. With a behaviorspace setup such as:
you get an output that would look something like:
I am trying to create a code that will allow me to get the who numbers of 10 turtles and store them in a list. However, the "who" number can only be added to the list if it has not been added to the list before.
I have to use the "foreach" command to check if the number has already been added to the list.
Any suggestions on how to do so?
My code:
turtles-own [contact-list]
to setup
clear-all
create-turtles 10 [
set contact-list []
print who
]
reset-ticks
end
to go
ask turtles [
set contact-list lput [who] of one-of other turtles-here contact-list
foreach contact-list[
; this is where the command goes
]
print contact-list
]
end
Might it be easier to add all the who numbers to the list and then use the remove-duplicates primitive to remove the duplicate numbers? I assume that you have a reason for putting who numbers into the list rather than having a list of the agents themselves, or an agentset. In general, who numbers are to be avoided.
Kindly advise as to what I might be doing wrong in the following netlogo procedure for per-step mortality (I am implementing the "Pass-Away" procedure as a step in a "GO" procedure):
to Pass-Away
ask turtles [
let chances 1 - exp( -1 * mortality * ticks )
let dead-count 0
if chances >= 1 [die]
set dead-count dead-count + 1
]
end
Is the above code correct? Even if it is correct, is there a modified or better alternative means to achieve a constant (cummulative) per-time-step turtle mortality as they move through the world? Finally, how do I get the variable "dead-count" to report to a monitor on the netlogo interface?
This will mostly work, except that your dead-count will always be 1. But it will probably not work as you intend. You should embark on an exploration of what you have written. The first thing to do when you get confused is to break things into smaller pieces. The second thing is to add some helpful visuals. In this case, you will learn a lot by plotting your representation of chances. I'm going to have to guess that your mortality attribute is a random float, since you did not say so. The following is a partial fix of your code, which provides enough clues to continue. You will need to add a plot in the interface tab -- see https://subversion.american.edu/aisaac/notes/netlogo-basics.xhtml#core-concepts-plotting -- and you can add a monitor in the same way if you find monitoring with print too clunky.
globals [dead-count]
turtles-own [mortality]
to setup
ca
set dead-count 0
crt 100 [set mortality random-float 1]
reset-ticks
end
to go
ask turtles [pass-away]
print (word "dead count = " dead-count) ;easiest monitor
clear-plot ;you'll need to have added a plot
foreach sort [chances] of turtles [
[?] -> plot ?
] ;keep an eye on the largest value in this plot ... get it?
tick
end
to-report chances ;turtle proc
report 1 - exp( -1 * mortality * ticks )
end
to pass-Away ;turtle proc
if chances >= 1 [
set dead-count dead-count + 1 ;*inside* the conditional! must come first!
print (word "chances: " chances)
die
]
end
In my model I have a lot of turtles being produced which slows it down. I can get around this by killing them off and having a global counter:
ask turtles [
if energy < 0 [
set turtle-count turtle-count + 1
die
]]
But I'd like to be able to extract the 'who', 'xcor' and 'ycor' of these agents too. How can I achieve this?
Thanks
you can save these values in an external file. I suppose this will help you a bit with setting it up, not sure whether you can just copy-paste.
setup the simulation with:
to setup
....
set-current-directory user-directory ;;choose directory of file
file-open "database.txt" ;;choose any name for your file
ask turtles
[
if energy < 0
[
write-to-file ;;go to the writing section
set turtle-count turtle-count + 1
die
]
]
....
end
to write-to-file
file-write who
file-write xcor
file-write ycor
file-print "" ;;new line for next turtle
end
end your simulations with
file-close-all ;;save the file
if you open the txt file it might look like a mess, but import it, for example, in excel with space delimited, and you will be able to read everything nicely
EDIT1
you could also do the following, which might speed up your simulations (rather than asking every turtle an if-function)
ask turtles with [energy < 0]
[
write-to-file ;;go to the writing section
set turtle-count turtle-count + 1
die
]
maybe someone with a bit more experience could comment on whether this is the case?