NetLogo monitor widget display changes when nothing is happening - netlogo

I have a NetLogo model, simplified to this:
to setup
clear-all
create-turtles 1000 [
fd 100
]
end
When I add a monitor widget to the UI, with a reporter like mean [xcor] of turtles and then run setup, the values in the monitor change a slight bit constantly. It might show 0.2305090322262271 one moment then 0.2305090322262268 the next, and then another similar number on and on.
What is making my monitor widget flicker or flash like this? How can I prevent it?

This is caused by a combination of a few things:
NetLogo's use of floating point numbers, which can produce small accuracy issues. See Floating point accuracy in the NetLogo programming guide: https://ccl.northwestern.edu/netlogo/docs/programming.html#math
Agentsets such as turtles are always returned in a random order.
Monitors re-run their reporter calculation constantly, even when you are not running any model code with a forever button or through the command center.
So the monitor constantly re-runs its mean [xcor] of turtles reporter, but the turtles agentset gives the turtles in a random order, and so the floating-point inaccuracies for mean will accumulate in a slightly different way each time due to the order differences. The end result is you see very slightly different numbers flashing through the monitor widget while nothing is happening.
You would see the same problem doing sum [xcor] of turtles or variance [xcor] of turtles - anytime you're reducing a bunch of floating point numbers from an agentset into a single value. You can also see the problem running your reporter code directly in the command center repeatedly, without a monitor widget at all.
The fixes are fortunately pretty easy:
Sort your numbers before you calculate: mean sort [xcor] of turtles, sum sort [xcor] of turtles, variance sort [xcor] of turtles. If the numbers are in the same order you'll still have small floating-point inaccuracies, but they'll be the the same every time so you won't see the values change. This is probably the best solution, but it can be slow if you have a really large agentset.
Change the Decimal places setting of your monitors to a number where you don't notice the changing values. Since the differences in results should be small, this is usually possible.

Related

keeping variable results after turtle dies netlogo

Looking for a way to store a turtle length of stay in the model after they have left the model. My model runs for several months and a few thousand turtles enter, undergo process then leave the area. It's complicate model (it's a hybrid DES and ABM) so I've tried to reproduce the simple bit below.
Turtles will be created at every tick and given a random length of stay but will only be able to begin process when they move to the right area (area-name) and when their time is up they leave the area. Their time-in-system reflects the wait for the area and the length-of-stay which I want to save once they're complete. If I leave them in the model it starts to break down after a couple of months and I suspect this is because the model has too many turtles still in the system for calculation and is inefficient.
go
create turtles 2
[
set time-in-system 0
set length-of-stay ceiling ((random-normal 48 4) + ticks)]
set shape "person"
if any? area-name with [not any? turtles-here]
[move-to one-of area-name]
]
undergo-process
end
to-undergo-process
ask turtles with [shape = "person"]
[
set time-in-system time-in-system + 1
]
ask turtles-on area-name
[if ticks = length-of-stay
[set shape "dot"
move-to exit-door]
end
I can then plot and see in realtime to make sure it is working
histogram time-in-system of turtles with [shape = "dot"]
but can't seem to figure out how to store them as unique values for plotting after the model has run and I have a dataset of outcomes without keeping them alive in the model. The real-time plot isn't necessary as long as I can store the unique values after they have left
If I ask them to die then I lose the unique values in the histogram. I don't want a tally of all values but each turtle's unique value at the end of the process after they left - at the moment the only solution I have to storing them is as an agent-set that stays alive in the exit-door patch but this takes up a lot of calculation power as the model progresses for months...
There may be a really simple command for this but I've been going round in circles through the programming manual trying to find it. Any tips appreciated
You should create a list storing the values of turtles that left.
Isolating only the code that is relevant for this purpose, it would be something like:
globals [
times
]
to setup
set times (list)
end
to leave-simulation ; This being executed by turtles.
set times lput (time-in-system times)
die
end
If your program is going to run for actual months, I recommend you use the file-write command to store your data. This way the data is preserved if the program halts for any reason; it gives you much more freedom to do the analysis you want without running the full simulation again.
If you write to a .csv (comma separated value) file, you can use almost any program (excel, R, matlab, python, C# or back to netlogo) to plot a histogram.

How to efficiently create data on one variable all agents have

I'm working with a NetLogo model on EV charging behaviour. All (500) agents monitor their my-charging-demand per tick and I want to find out what happens to this emergent behaviour when I change the policy intervention that is active (costs of electricity in this case). I am trying to show changes in charging characteristics such as charging-duration, charging power etc.
What is the best way to create data on the agents' my-charging-demand in time?
Right now I am plotting all their data in one graph using the following code:
ask adopters
[ create-temporary-plot-pen (word-who)
set-plot-pen-color color
plotxy ticks my-charging-demand
]
It works, but unfortunately it also made the model incredibly slow, as 500 pens are to be updated every tick. The model needs 105120 ticks before a whole year/run is completed, as each tick in the model represents 5 minutes. Therefore, speed does matter :-)
Is there a more efficient way to keep track / create data of one variable all agents have?
If I have understood this correctly, you want each agent to remember the value of its variable my-charging-demand across all time. If so, the easiest way (but I don't know if it's more efficient) is to have the list as a turtle variable. So, modify your turtles-own to add another variable:
adopters-own
[ ....
my-charging-demand
my-charging-demand-series
]
And wherever you have the code for calculating demand, add the result to the list
ask adopters
[ ...
set my-charging-demand ...
set my-charging-demand lput my-charging-demand my-charging-demand-series
...
]
I can't imagine a plot with 500 lines is readable. The plot should do something like the average of my-charging-demand or the proportion of turtles with my-charging-demand greater than some threshold.

NetLogo set random-seed according to the number of repetitions in the Behavior Space

I would like to know whether is it possible in NetLogo to set the random-seed according to the number of repetitions in the Behavior Space.
I know there is the command random-seed behaviorspace-run-number, but it sets a different seed for every run of the model. This is not what I want because I am trying to explore the impact of a variation in the values of a parameter on a specific random network structure. By using random-seed behaviorspace-run-number I get a different network structure for each value of the parameter within the same repetition of the experiment, which is not what I want.
Can anyone help me on this?
Thanks a lot,
Emanuele
There's not a variable that gives you exactly what you want, but BehaviorSpace runs through the parameter sets in a specific order. Say you have 5 repetitions of 20 parameter combinations, so there's 100 runs. It will do the first run of 20 combinations, then the second series etc. So you can do some mathematics or some if/then to go from the behaviorspace-run-number to the random-seed (eg floor behaviorspace-run-number / 20 if you want it to change every 20 runs).

How can I customize BehaviorSpace 's "Measure runs using reporters"?

I would like to use the BehaviorSpace's "Measure runs using reporters" to compile the number of turtles dead number-dead for each ticks ticks, ID information on living turtle who for each ticks, and information on the number of counters count-up that the living turtle has for each ticks. I am beginner of Netlogo. I do not know how to set it to "Measure runs using reporters". Already, I was referring to "Netlogo's BehaviorSpace Guide" https://ccl.northwestern.edu/netlogo/docs/behaviorspace.html , and then count turtles are successful. The following is an example of the input screen of "Measure runs using reporters". However this was a syntax error except count turtles.
count turtles
count number-dead
count count-up
count who
If number-dead and count-up are breeds that you have defined, then those parts are fine. If they are instead global variables that contain the number of something, then you want to show them rather than count them. However, who is an internal variable that belongs to each turtle so that piece of code doesn't make any sense.
What I suggest you do is create a monitor on the interface for each of the variables that you want in your BehaviorSpace output. So create one monitor for count turtles and another for show number-dead and so on. When all of those are working for a normal run, then you can do a BehaviourSpace run.

How can I stop the monitor continually running?

The monitor (below) is continually running and generating a random list of output, even though there is no tick activity.
Questions: Should it be continually running? Is there a way to monitor the list on the interface without the continual random output?
Code
to go
crt 100 [fd random 14 + 1]
end
to-report report-red-turtles
report [who] of turtles with [color = red]
end
To run:
On the interface, create a monitor report-red-turtles and a simple go button
It is by design that "Monitors automatically update several times per second". It's a convenient design in most cases, but can also have some weird consequences (be careful never to have side effects in monitor code!)
What happens in your case is that
[who] of turtles with [color = red]
produces different output each time it runs: the list produced by of is always in random order.
To get around this fact, you have two options.
Remove the randomness: sort [who] of turtles with [color = red].
Use a global variable (e.g. red-turtles), update it once per tick, and display that in your monitor.
It's a trade-off between simplicity and speed: the first option is simpler and cleaner, but more computationally expensive.