How do I raise a variable randomly between 20% and 30%? - netlogo

I have turtles defined by their wealth and every 30 ticks i want to "spend" 20%-30% of their wealth (like the spendings of a family). How do i set the new wealth randomly between 20%-30% of my old wealth?

random-float 0.1 will generate a random number in the range 0 to 0.1. So you can do something like let spend 0.2 + random-float 0.1 will generate an appropriate number and assign it to a variable named spend. You can then do set wealth spend * wealth, which will reduce the wealth accordingly. Note that spending 20-30% will set their new wealth at 70-80% of their old wealth. If you want to end up with 20-30, you will need to have the spend higher.

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.

Netlogo: express probability to a turtle

I'm new to Netlogo and I'm now stuck with coding this sentence:
There should be a chance of 10% divided by the total number of turtles that a turtle will hatch a child.
The initial total number of turtles is 1.
So my code is:
let p (0.1 / 1)
ask n-of (0.1 / 1) turtles
[hatch 1]
But it seems to me that my code may not be correct. Anyone any ideas how to change it?
I would appreciate any kinds of help. Many thanks in advance!
"ask n-of" is clearly wrong, if there's only one turtle. And the random function only returns integers, so instead of checking for 0.1, let's choose a random number from 0 to 10 times the number of turtles. Something like this should work.
if random 10 < 1 [
ask one-of turtles
[hatch 1]
]
Here's my justification. Any given turtle has a 10%/N chance of hatching. That means that, 10% of the time, one random turtle has a 100% chance.
Say there are 5 turtles. By the spec, each turtle has a 2% chance of hatching (10%/5). 90% of the time, no one hatches. In the remaining 10% of the cases, 1 of the 5 will definitely get a chance. That means each individual turtle's chances are 10% x 20% which is 2%, as the spec says.
That's what the code does. "random 10" chooses a random number from 0 to 9. If that number is "< 1" (which means 0), then we choose a turtle at random to hatch.
How about this, put the random chance inside the ask
ask turtles
[ if random-float 1 < 0.1
[ hatch 1
]
]
Part of the problem is that the question is badly expressed. "10% divided by the total number of turtles" doesn't really make sense, so I have interpreted it that approximately 10% of the turtles hatch a child turtle

Assigning numbers in Netlogo to agents randomly

I am implementing a model in Netlogo in which i want to divide agents into income groups. My question is: assuming 5% of agents, representing households earn incomes between $500-$600. Is there a code i can write that would allocate this income range randomly to the agents?-so that some of them will have 500, 550, 590 and so on up to 600
many thanks
if you want exact numbers 500 to 600 by 10s, then you want something like set income 500 + 10 * random 11. If you want any number in the range 500 to 600 then something like set income 500 + random-float 100. Have a look at the various random functions (listed in the mathematical section of the user manual) for other ideas.
To have this only happen to 5% of agents:
ifelse random-float 1 <= 0.05
[ set income .... (whichever from above) ]
[ ... whatever you want to happen for the other 95% ]

NetLogo: Finding the average value of a set of turtles

I'm trying to implement a monitor in the user interface that displays the average value of a variable shared by a breed of turtles (turtles own). Does anyone know of a method of collecting all the values, adding them together and dividing by the quantity of turtles to get the value or know of an easier method?
If the variable that each turtle has is shell-size, for example, then:
print mean [shell-size] of turtles
will do it. It might be useful to know how to do this by hand, so that you can do other calculations if you want. Here's one way:
print (sum [shell-size] of turtles) / (count turtles)
Here's another
let total 0
ask turtles [set total total + shell-size]
print total / (count turtles)
Obviously, you'll want to replace the print statements with whatever suits your needs. For a monitor, you should be able to enter this code directly into the interface, or wrap it up in reporter and then use that in the monitor.

Performance Issues with Calculation during Turtle Procedure

I am building a type of 'honey-bee' model, where the bees are turtle agents and the honey is a patch specific variable. In my model, each patch is assigned a value of 'honey-here' between 1-100 based on a specific distribution.
The model starts with the bees only being able to collect honey from flowers with honey = 1, for which they then receive 1 unit of honey in return. Before the bees can 'target' flowers with honey = 2, they need to occupy (i.e. a bee on a flower) X% of the total flowers with honey = 1. For example, I might require the bees to achieve 80% occupancy, which means if there are 10 flowers total with the variable honey = 1 then the bees need to occupy 8 of those flowers before they are allowed to start looking/targeting flowers with honey = 2. As each bee individually acts, the % occupancy value will change.
I'm having performance issues for the occupancy calculation. Ideally the calculation is updated within a turtle procedure as it needs to be applied to each turtle within the loop. Here's what I currently have to find the value of the variables I need to set current % occupancy before each bee is allowed to act:
ask bees
[
;; set up variable based on ratio of number of turtles occupying target patch size against total number target patch size
;; note -- don't do this in one step to avoid divide by 0
let patch-count-current (count patches with [honey-here = bee-honey-target-size])
;; don't want number of bees, want number of unique patches
let patch-target-occupy count patches with [ (count bees-here > 0) and (honey-here = bee-honey-target-size) ]
...
Later in the code, after checking to make sure patch-count-current isn't 0, I find my % occupancy via patch-target-occupy / patch-count-current
It turns out this is a very expensive hit on my processor performance. Especially as my number of bees grows, which is exponential in my model.
Is there a better way that won't cost me so much processor for each iteration of the loop?
Thanks!
-dp
Given your response to StephenGuerin, you could manually track the counts with global variables. If there is only one bee-honey-target-size at a time, this is pretty simple. Just make a patch-count-current global and a patch-target-occupy. Then, increase them or decrease them as things change.
The Sandpile model (from the models library) uses this technique to keep track of the total amount of sand without having to iterate over the patches. Check out how the total global variable is used.
If you need to keep track of the counts for all bee-honey-target-sizes at once, you could store a list of the counts in a global variable, where the index of the list corresponds to a value of bee-honey-target-size. This is much messier unfortunately, so you would have to write helper functions for sure.
Let me know if any of this needs clarification.
Percentage occupancy is a property of the group and only needs to be calculated once per simulation step. Your performance hit is due to calculating occupancy for every bee in your simulation. The calculation for percentage occupancy for a given 'target-honey' could look like this:
to-report calc-occupancy [target-honey]
report (sum [count turtles-here] of patches with [honey-here = target-honey]) / count turtles
end