How to make a variable belong to a range Netlogo - range

I have a turtle who can eat different amount of food for every tick, updating its stomach content every time. I would like to round the stomach content value so that it belongs to a range x.
this is the forage function that updates the stomach-content :
to forage
;; item=0.064g
set patch-n random-float 100
if patch-n <= 8 [set stomach-content (stomach-content + 0.00) ] ; does not find any item
if patch-n > 8 and patch-n <= 99 [set stomach-content (stomach-content + 0.192) ] ; finds 3 items
if patch-n > 99 [set stomach-content 0.4 ]; full stomach
ifelse stomach-content >= 0.132
[set fat-reserves (fat-reserves + 0.132 ) set stomach-content (stomach-content - 0.132)]
[set fat-reserves (fat-reserves + (stomach-content * 1)) set stomach-content 0] ;;
set fat-reserves (fat-reserves - (8 * bmr)) ; metabolic rate removes fat from fat reserves
end
the range I would like the stomach-content to belong to is
set x (range 0 0.4 0.04)
Is there a way to make my stomach-content value to be in this finite range of 11 values?
Something like to round stomach-content to the nearest value with mod=0.04 in the interval (0 , 0.4)

You could do something fancy with the mod reporter, but if you don't need it to be super fast, the following is easy enough, and also more flexible, as it would work with any list of values:
to-report nearest-in-list [ the-value the-list ]
report first sort-by [ [a b] ->
abs (a - the-value) < abs (b - the-value)
] the-list
end
You can then use it like this:
observer> show nearest-in-list 0.11 (range 0 0.4 0.04)
observer: 0.12
observer> show nearest-in-list 0.021 (range 0 0.4 0.04)
observer: 0.04
observer> show nearest-in-list 0.02 (range 0 0.4 0.04)
observer: 0
Note that, in case of ties (like with 0.02 in the example, which is halfway between 0 and 0.04) it gives you the lowest value.

Related

Action not happening every certain amount of ticks

this code is for the reproduction of an animal. It should happen every 10 ticks, but it is happening instantly and i dont know why. They also have energy. Reproduction is set to be random to show females.
thanks in advance for the help.
turtles-own[ energia edad]
breed[perros perro]
to setup
resize-world 0 80 0 60
clear-all
ask patches[
set pcolor green]
create-perros 50
[
set size 3 ;; easier to see
set color yellow
setxy (41 + random 39) random 60
set heading random 360
set energia 100
set edad 1
]
reset-ticks
end
to go
ask perros [ set edad edad + 1]
ask perros [
morir-perros
moverse-perros
reproducirse
]
tick
end
to morir-perros
if edad > 15 [die]
if energia < 1 [die]
end
to moverse-perros
set heading random 360 fd 5
set energia energia - 5
end
to reproducirse
if ticks mod 10 = 0 [ if random 100 > 50 [set energia energia - 60
hatch 1 [ rt random-float 360 fd 2
set energia (energia + 60) / 3]]]
end
Right now you have tick to increment your time step at the end of your go procedure. In any model, ticks start at 0 by default, so your model is evaluating ticks mod 10:
observer> show 0 mod 10
observer: 0
Since that would satisfy the condition in reproducirse, the code is evaluated. You can handle this in a variety of ways. The simplest may be to modify your if statement in reproducirse- for example:
to reproducirse
if ticks > 0 and ticks mod 10 = 0 [ if random 100 > 50 [set energia energia - 60
hatch 1 [ rt random-float 360 fd 2
set energia (energia + 60) / 3]]]
end

How do I find find out probability of Y occurring depends on both X1 and X2 in Netlogo? Y , X1 and X2 are turtle variables

I have specifically two questions:
How do I find out the probability of leadershipactivation depends on both leaderactivation and othersperception.
How to I find out that the probability of leaderactivation depends on motivation (condition motivation > 3.32), selfschemata (selfschemata >7) and neti (social factors, neti>0) at a particular time.
'''
globals [taskdemand ;; the taskdemand of the turtles
percent-LA;
percent-LP;
percent-LI;
probLA;
probLP;
probLI;
err;;
;percent-LIr
;percent-aLAnLp
;percent-nLALp
;percent-LALP
;percent-nLAnLP
]
turtles-own [
motivation ;;motivation to lead of each turtle
selfschemata ;; self-schemata of each turtle
status ;;status of each turtle
others;;agentset of agents within same group
;;teammates;; agentset of teammates
othersactivation;; othersactivation computes leaderactivation of each turtle within the group of turtle
allothers;;agentset of all other agents within the collective
neti;;neti computes socialfactors and taskdemand of each turtle
x;;x is prospective leader's perceived characteristics
y;;y is perceivers ILT for various dimensions
othersperception ;; leadershipperception of all other turtles in collective level
leaderactivation;;leader self-schema activation
leadershipperception;; leadershipperception of each turtle in relational level
leadershipactivation;; leadershipactivation in a collective level
leadershipactivationr;;
mygroup;; group of turtles
LA?;;
aLA?;;
aLP?;;
aLi?;;
aLir?
aLAnLp?
nLALp?
LALP?
nLAnLP?
]
to setup
clear-all; clears ticks, patches, plots
ask patches[ set pcolor white];;blank background
crt 100 [
setxy random-xcor random-ycor
set shape "person"
set motivation random-normal 3.32 0.75;;motivation to lead is normally distributed with mean 3.32 and standard deviation 0.75
set selfschemata (random 11) + 1 ;;sets selfschemata 1 to 11
set status random-normal 4 0.49 ;;status is normally distributed with mean 4 and standard deviation 0.49
set leaderactivation random 1 ;;sets leaderactivation values 0 to each turtle (self-schema activation, individual level)
set leadershipperception random 1 ;; sets leadershipperception values 0 to each turtle (relational level)
set neti random 1 ;;sets neti values 0 to each turtle, neti calculates the social factors
set othersactivation random 1 ;;sets each turtles, othersactivation (i.e., leaderactivation) value in a group to 0
set othersperception random 1;;sets othersperception values 0 to each turtle
set leadershipactivation random 1;; sets leadershipactivation values 0 to each turtle (collective level)
set leadershipactivationr random 1;;
set others no-turtles
set allothers no-turtles
if ( status > 4) OR (status = 4)[set color blue]
if ( status < 4)[set color red]
set x n-values 5 [random 1];; setting random values 0 or 1 for 5 dimensions of prospective leader's perceived characteristics
set y n-values 5[random 1];;perceiver's ILT for various dimensions
set mygroup -1
]
assign-by-size
set taskdemand [0.1 0.9]
set err random-normal 0 0.01;;setting error with mean 0 and standard deviation 0.01
reset-ticks
end
to assign-by-size
let unassigned turtles
let current 0
while [any? unassigned]
[
;; place a randomly chosen set of group-size turtles into the current
;; group. or, if there are less than group-size turtles left, place the
;; rest of the turtles in the current group.
ask n-of (min (list group-size (count unassigned))) unassigned
[ set mygroup current ]
;; consider the next group.
set current current + 1
;; remove grouped turtles from the pool of turtles to assign
set unassigned unassigned with [mygroup = -1]
]
end
to go
if ticks >= 300 [ stop ]
ask turtles [
if (mygroup != -1) [leader-activation] ;; individual level
if (mygroup != -1) [ checksocialfactors] ;; individual level
if (leaderactivation > 2) OR (leaderactivation = 2) [ set x n-values 5 [random 2]]
if (mygroup != -1) [ ilt-match];;relational level
ifelse (leaderactivation > 2) OR (leaderactivation = 2)[ set leadershipperception leadershipperception ][set leadershipperception 0]
if (mygroup != -1) [set leadershipactivationr leadershipperception + leaderactivation]
if (mygroup != -1) [collectivelevel];;collective level
if (mygroup != -1)[set leadershipactivation othersperception + leaderactivation]
]
update-variables
tick
end
to leader-activation ;; leader activation at the individual level
ifelse (motivation > 3.32) OR (motivation = 3.32) [set leaderactivation leaderactivation + 1 ][set leaderactivation leaderactivation + 0]
if (selfschemata > 0) AND (selfschemata < 5)[set leaderactivation leaderactivation + 0]
if (selfschemata > 4) AND (selfschemata < 8)[set leaderactivation leaderactivation + 0.5 ]
if (selfschemata > 7) AND (selfschemata < 12)[set leaderactivation leaderactivation + 1 ]
ifelse (status > 4) OR (status = 4) [set leaderactivation leaderactivation + 1][set leaderactivation leaderactivation - 1]
show leaderactivation
end
to checksocialfactors ;; turtle procedure to check social factors of the turtles
find-others
if any? others
[ find-others-activation
find-othersactivation-and-taskdemand
let lachange 0
let s (1 - leaderactivation) * neti
let t (leaderactivation + 0.2) * neti
ifelse (neti > 0) [set lachange lachange + (0.4 * s) - (0.1 * leaderactivation)] [set lachange lachange + (0.4 * t) - (0.1 * leaderactivation)]
set leaderactivation leaderactivation + lachange
ifelse (leaderactivation > maxleaderactivation)[ set leaderactivation maxleaderactivation][set leaderactivation leaderactivation]
]
end
to find-others ;; turtle procedure to find others in the group
set others other turtles with [mygroup = [mygroup] of myself]
end
to find-others-activation ;; turtle procedure to find others activation in a group
set othersactivation sum [leaderactivation] of others
end
to find-othersactivation-and-taskdemand
set neti othersactivation + 0.1 + err
end
to ilt-match ;; turtle procedure to match prospective leader's perceived characteristics and perceiver's ILT for various dimensions using hamming distance
find-others
find-yothers
let d 0
let xn 0
let yn 0
let x1 0
let y1 0
let n 1
while [n < 6];; there are 5 values
[ set x1 item xn x
set y1 item yn y
set n n + 1
if (x1 = y1) [ set d d + 0 ]
if (x1 > y1) [ set d d + (x1 - y1) ]
if (y1 > x1) [ set d d + (y1 - x1) ]
]
ifelse d <= 2
[ set leadershipperception leadershipperception + (5 - d)]
[ set leadershipperception leadershipperception + 0]
end
to collectivelevel
find-others
if any? others[
set othersperception sum [leadershipperception] of others
]
end
to find-yothers
if any? others[
set y n-values 5[random 2]
]
end
to update-variables
update-turtles
update-globals
end
to update-turtles
ask turtles[
if(leaderactivation > 2) [ set aLA? 1]
if(leadershipperception != 0) [ set aLP? 1]
if(leadershipactivation > 2) [set aLi? 1]
; if(leadershipactivationr > 2) [set aLir? 1]
;if(leaderactivation > 2) AND (leadershipperception = 0) AND (leadershipactivationr > 0)[ set aLAnLp? 1]
;if(leaderactivation < 2) AND (leadershipperception != 0) AND (leadershipactivationr > 0)[ set nLALp? 1]
; if(leaderactivation > 2) AND (leadershipperception != 0) AND (leadershipactivationr > 0)[ set LALP? 1]
;if(leaderactivation < 2) AND (leadershipperception = 0) AND (leadershipactivationr > 0)[ set nLAnLP? 1]
]
end
to update-globals
set percent-LA (count turtles with [ aLA? = 1]) / (count turtles) * 100
set percent-LP (count turtles with [aLP? = 1]) / (count turtles) * 100
set percent-LI (count turtles with [aLI? = 1]) / (count turtles) * 100
set percent-LIr (count turtles with [aLIr? = 1]) / (count turtles) * 100
;set percent-LAmp (count turtles with [ LA? = 1 AND motivation = 1 AND priorexp = 1]) / (count turtles) * 100
set probLA (count turtles with [aLA? = 1]) / (count turtles)
set probLP (count turtles with [aLP? = 1]) / (count turtles)
set probLI (count turtles with [aLi? = 1]) / (count turtles)
;set percent-aLAnLp (count turtles with [aLAnLp? = 1]) / (count turtles) * 100
;set percent-nLALp (count turtles with [nLALp? = 1]) / (count turtles) * 100
;set percent-LALP (count turtles with [LALP? = 1]) / (count turtles) * 100
;set percent-nLAnLP (count turtles with [nLAnLP? = 1]) / (count turtles) * 100
end
'''

Problems creating a x% fraction of turtles using n-of command

I want x% of turtles, called pholders, to change their choice from a good 1 to a good 2.
The code is as follows:
ask pholders [ifelse random-float 1 <= probkauf
[ask (n-of (count pholders with [choice-num = 1] * 0.01) pholders with[choice-num = 1]) [set choice-num 2]]
[ifelse random-float 1 < 0.5[imitation set typeofchoice 1][beratung set typeofchoice 4]]
]
Initially 100% of the pholders chose good 1. The Problem is as follows: When i rise the number of pholders above something between 102 and 108 n-of doesn't calculate a 1%-fraction anymore, it calculates 10%. The higher the number of pholders the bigger the fraction: for 200 pholders the code calculates 60%. When i leave the number of pholders constant and below 108 but change the percentage from 0.01 to 0.02 it calculates something like 55% or 58%. Is the problem probably coming from ask n-of in an ask environment?
Thank you very much in advance.
Your problem is that you are running the probabilistic code multiple times. Your code has this structure:
ask pholders
[ ifelse random-float 1 <= probkauf
[ ask (n-of (count pholders with [choice-num = 1] * 0.01) pholders with [choice-num = 1])
[ set choice-num 2]
]
[ <do something else> ]
]
If you have 500 pholders, then there will be 500 times that a pholder selects a random number and, if the number is lower than your value probkauf, it instructs a number of pholders with choice-num of 1 to change it to choice-num 2. 500 potential occasions of 1% conversion is why you have so many being converted.
Based on the description in your comments, I think you want this:
globals [probkauf]
turtles-own [choice-num]
to setup
clear-all
set probkauf 0.5
create-turtles 1000
[ setxy random-xcor random-ycor
set color blue
set choice-num 1
]
reset-ticks
end
to go
update-choices
tick
end
to update-choices
ifelse random-float 1 < probkauf
[ ask turtles with [choice-num = 1]
[ if random-float 1 < 0.01
[ set choice-num 2
set color red
]
]
]
[ ; whatever happens with other part of probability
]
end

How can I move a turtle to a patch with a probability?

It is a pretty simple model so far. I want the walker to move to the patches, but not randomly how it is now but with a probability. In this case it is the list "priority" which describes the probability for each patch (used vehicle).
So my question: How can I implement the calculated priority to the move statement?
Here's the code:
breed [nodes node]
breed [walkers walker]
globals [
cost
time
wellbeing
efficiency
vec1
vec2
vec3
nodelist
; rain
]
walkers-own [
Temp_best ;aim temp
capital ; capital for each timestep to invest in transportation
adaptation
location
priority
delta-priority
] ;; holds a node
patches-own [
]
to setup
clear-all
set-default-shape nodes "circle"
;; create a random network
create-nodes 1 [ set color white ]
create-nodes 1 [ set color blue ] ;car
create-nodes 1 [ set color yellow ] ;bike
create-nodes 1 [ set color green ] ;public transportation
ask node 0 [ create-link-with node 1 ]
ask node 0 [ create-link-with node 2 ]
ask node 0 [ create-link-with node 3 ]
;; lay it out so links are not overlapping
repeat 500 [ layout ]
;; leave space around the edges
ask nodes [ setxy 0.95 * xcor 0.95 * ycor ]
;; put some "walker" turtles on the network
create-walkers 1 [
set color red
set location node 0
move-to location
]
reset-ticks
ask walkers [
set capital 5
set adaptation 0.1
set wellbeing n-values 3 [0]
set priority n-values 3 [0] ;sum of all must be 1
set delta-priority n-values 3 [0] ;sum of all must be 0
set efficiency n-values 3 [0]
]
set cost [3.5 1 0.1]
set time [0.3 0.6 0.7]
set vec1 [0.7 0.7 0.7] ;if change one, than also the other --> sum must be 1
set vec2 [0.3 0.3 0.3] ;
set vec3 [1 1 1]
ask walkers[
output-print wellbeing
]
set cost (map * cost vec1)
set time (map * time vec2)
ask walkers[
output-print cost
output-print time
]
ask walkers [
set wellbeing (map + cost time)
set wellbeing (map / vec3 wellbeing) ;devided by 1 to have a high number for wellbeing
set priority [0 0.1 0.9]
; set priority (map + priority delta-priority)
if rain? [
set priority replace-item 2 priority 0
set priority replace-item 1 priority (1 - item 0 priority + item 2 priority)
]
]
ask walkers[
output-print wellbeing
output-print priority
]
;set efficiency replace-item 1 efficiency (wellbeing 1 / )
set efficiency (map / wellbeing cost)
output-print efficiency
;end
;to calculate
;ask walker[
;set delta-priority replace-item 0 delta-priority ( adaptation * item 0 priority * ((item 0 wellbeing) - ) - ((item 1 priority * item 1 wellbeing)+(item 2 priority * item 2 wellbeing)))
; set priority replace-item 0 priority (priority 0 + delta-priority 0)
;set priority replace-item 1 priority (priority 1 + delta-priority 1)
;set priority replace-item 2 priority (priority 2 + delta-priority 2)
; set priority (map + priority delta-priority)
;ifelse wellbeing 2 > wellbeing 1
; output-print delta-priority
; ]
end
to layout
layout-spring nodes links 0.5 2 1
end
to go
ask links [ set thickness 0 ]
ask walkers [
let new-location one-of nodes ;i want to introduce the priority for each node
;one-of [link-neighbors] of location
;; change the thickness of the link I just crossed over
;ask [link-with new-location] of location [ set thickness 0.5 ]
face new-location ;; not strictly necessary, but improves the visuals a bit
move-to new-location
set location new-location
]
tick
ask nodes
[show count walkers-here]
end

Probability in NetLogo - using one-of or creating a random number

I was wondering if by using same random seed, the result of following functions differs significantly in randomness! and which one is better? provided Code is just an example.
let b random 3
if b = 0 [set Output "Output1"]
if b = 1 [set Output "Output2"]
if b = 2 [set Output "Output3"]
Or this one :
set output one-of ["Output1" "Output2" "Output3"]
UPdate:
I just checked it with a simple netlogo program:
turtles-own [X Y A]
to setup
__clear-all-and-reset-ticks
create-turtles 50
[set A random 3000
set y random-normal 0.5 0.1
set x random-normal 0.5 0.1
move-to patch random 20 random 20]
end
to go
ask turtles
[ set A A + 1
fd 1
rt random 10
if A > 4000 [die]
if random-float 1 < 0.003 and a > 1000 and A < 3000 [
let xx x
let yy y
hatch 1
[ set A 0
set x one-of (list (random-normal 0.5 0.1) (xx + 0.1) (XX - 0.1))
let b random 3
if b = 0 [set y random-normal 0.5 0.1]
if b = 1 [set y yy + 0.1]
if b = 2 [set y yy - 0.1 ]
]
]
]
tick
end
The results is as follow:
There is not much difference is using any of these methods :)
There is a bug in your measurement code. Change b = 3 to b = 2 and I predict you will see any difference go away.
Unless there is some weird bug in NetLogo (or the JVM) that no one has discovered yet, it shouldn't make any difference whether you use one-of or random. To my knowledge, there is no such bug.
The relevant source files are:
https://github.com/NetLogo/NetLogo/blob/5.0.x/src/main/org/nlogo/prim/_randomconst.java
https://github.com/NetLogo/NetLogo/blob/5.0.x/src/main/org/nlogo/prim/_oneof.java
The only difference is that the first calls context.job.random.nextLong and the latter context.job.random.nextInt (because the size of a NetLogo list is an int, not a long, while the input to random is a long). But both nextInt and nextLong are of equally high quality; they're just calls into the standard MersenneTwisterFast class which is widely used by people doing simulation work.