Make reproducible simulation runs with nw:generate-preferential-attachment? - netlogo

I want to make a reproducible simulation run which starts with the generation of a network using nw:generate-preferential-attachment.
I used random-seed to set the seed for random number generation. This works fine when min-degree = 4 or less. Surprisingly, it does not work for min-degree = 5 or higher. When I clear-all, set the seed, and generate the network and randomly place nodes, this always looks different, with min-degree >= 5. It looks identical (as it should) for min-degree <=4. I provide a minimal example.
extensions [nw]
to setup
clear-all
random-seed 1
nw:generate-preferential-attachment turtles links 100 5
ask turtles [setxy random-xcor random-ycor]
end
Replace the "5" in line 5 with "4" and run setup two times each to see the difference.
Is there a way to make this work also for larger min-degree? What is the reason for this difference?

Related

setting incubation period for mosquitoes and human

I have infection model which is working fine. now I want to add incubation period for infection to be realized either in mosquito or human. incubation period is 10 days,
when infected mosquito interact with susceptible human the human become exposed for 10 days the after that changes to infected state.
**ask turtles
[
if exposed?;; [set incubation-period incubation-period + 10 ]
[if random-float 10 < incubation-period
[set infected? true set color red]]]**
after adding above code turtles jump to infected, do not get exposed first
(A note: I understand that incubation is not the period of time after which an organism is infected, but that incubation starts upon infection and it is the period of time after which symptoms show. However, in my reply and in the example it contains, I'll follow the scheme of things that I seem to understand from your question: first exposure, then incubation, finally infection. I am pointing this out to make sure that there are no ambiguities between your question and my answer)
I am not sure why you are using random numbers, and you did not share a relevant parte of your code, so I am not sure I really understand how you want to implement this.
However, let's start from what is surely not working there: you say set incubation-period incubation-period + 10. Whatever the value of incubation-period was before, now it is at least 10 (assuming incubation-period is never negative, of course).
Then you check the condition random-float 10 < incubation-period. On the one hand, we already said that incubation-period is at least 10 now; on the other hand, the value resulting from random-float 10 is strictly less than 10. This means that the condition always evaluates as true, and that therefore every single agent running these lines will jump to set infected? true set color red.
Now the question is: how are you intending to implement incubation? Why are you thinking about a condition of that type?
I am not a medical person but, given my understanding, I can imagine you want your incubation to be a period of time after which something happens.
In that case, rather than checking a condition based on a random number, you need to implement a counter: each agent that is exposed to an infected mosquito starts a countdown which equals the duration of incubation; when the countdown reaches zero, the agent becomes infected.
You said you want the incubation to last 10 days. Assuming that in your model one tick equals one day, you can do something like:
breed [humans human]
breed [mosquitos mosquito]
turtles-own [
infected?
incubation
]
to setup
clear-all
reset-ticks
set-default-shape humans "person"
set-default-shape mosquitos "bug" ; The closest to a mosquito that was in the library!
create-humans 100
create-mosquitos 10
ask turtles [
setxy random-xcor random-ycor
set infected? FALSE
set color white
]
ask n-of 2 mosquitos [
become-infected
]
end
to go
ask turtles with [incubation > 0] [
set incubation incubation - 1
if (incubation = 0) [
become-infected
]
]
ask turtles [
right random 360
forward 1
]
ask mosquitos with [infected?] [
let targets turtles-here with [not infected?]
if (targets != NOBODY) [
ask targets [
start-incubation
]
]
]
tick
end
to start-incubation
set incubation 10
set color orange + 1
end
to become-infected
set infected? TRUE
set color red
end
As you can see in the third block of code in go, when a healthy agent is on the same patch as an infected mosquito, the healthy agent goes to start-incubation and sets incubation to 10.
In the first block of code in go, every agent that is incubating the disease will reduce by 1 the value of incubation so that, after 10 iterations, it will reach 0. When this happens, the agent goes to become-infected.
The reason why this countdown is implemented at the beginning of go, and not at some point after the lines that check for exposure, is simple: if the countdown was after exposure, then it would mean that agents would set incubation as 10 but would also reduce it by 1 right after it, with the result that the real incubation would last 9 days and not 10 days.
You might want to include some randomness in the actual length that incubation takes from individual to individual. I am not sure this is what you were thinking about when you introduced randomness in the condition to finish incubation, but I'm going to put this here anyway.
If you want to do so, randomness has to be used only once upon determining the length of incubation for that individual. For example, if you want to the duration of incubation to be somewhere between 5 and 10 days, you can do:
to start-incubation
set incubation 5 + random 6
set color orange + 1
end
This way, you make sure that incubation starts with a value of at least 5, plus a number between 0 and 5.
This is a useful scheme to use in NetLogo. To generalise it:
to start-incubation
let min-incubation 5
let max-incubation 10
set incubation min-incubation + random (max-incubation - min-incubation + 1)
set color orange + 1
end
Final note: I strongly suggest you avoid using random-float in this case, unless it is absolutely necessary for some reason that I don't see. Use random instead.
If you use random-float, you get a decimal number. This means that, if you take the approach of setting the incubation period within a range (as in my example of a range between 5 and 10), you may get incubation starting at 8.5, which means that after 8 ticks it would be at 0.5 and after 9 ticks it would be at -0.5. This way, the condition incubation = 0 will never evaluate as true. You could change it to incubation <= 0, sure, but this has the exact same result of using random (i.e. incubation lasting a number of ticks between 5 and 10) but with a bug waiting to happen.

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 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.

How to randomly place turtles at each model opening?

Every time that I open my Netlogo model, the created turtles are placed at the same place. I have 1000 simulations and I use the behaviorSpace. I tried random-seed but how can I use this function without writing, for each simulation in the behaviorSpace:
random-seed 0
create-turtles
random-seed 1
create-turtles
random-seed 2
create-turtles
....
random-seed 1000
create-turtles
?
Update
I don't use behaviorSpace to repeat simultaneously 1000 times my model but I open 1000 times my file .nlogo. I would like to find a way to have locations of turtles that are different at each file opening (I used one-of to place turtles in patches).
Thank you very much for your help.
(UPDATED)
In your comment, you supply the crucial information that you are using import-world. This restores the exact state of the entire world, including the state of the random number generator! As a result, you get the same model run every time afterwards.
If you want to re-seed the random number generator so you get a different run each time, then, after import-world, say random-seed new-seed.
random-seed behaviorspace-run-number