Maximum integer value for a variable in NetLogo - netlogo

I'm trying to increment an agent property on each tick in the "go" procedure. But it will not increment after some value above 10000's.
What could be the issue? Is my code wrong?
What is the maximum value that can hold by a variable?
to go
ask people with [diagnosis = "infected" and tested? = true] [
set infected-time infected-time + 1
]
ask people with [infected-time > 80000] [
set color white
set infected-time 0
]
......
......
tick
end

Related

How to implement a timer for each turtle in netlogo

I am trying to implement a timer for each turtle in Netlogo
so I can take the minimum value, maximum and average time taken for each turtle.
Can anyone help
You haven't said what you actually want timed (or given any code attempt). Any time you need a turtle to remember anything from one tick to the next, you need a variable. The simplest code for a timer is to have a variable that you set to the current value of ticks when the process starts and subtract that start time from the current value of ticks when the process stops. Here is a complete model example:
turtles-own
[ start-time
time-taken
]
to setup
clear-all
create-turtles 20
[ set start-time 2 + random 10
set time-taken -1
]
reset-ticks
end
to go
let movers turtles with [time-taken = -1 and start-time <= ticks]
ask movers
[ set heading random 360
forward 1 + random 3
if random-float 1 < 0.05 [ set time-taken ticks - start-time ]
]
tick
end
And check out the new Time extension if you want your timers to use real time units (seconds, minutes, years...) and have non-integer values.
https://github.com/NetLogo/Time-Extension
Look at primitives such as time:difference-between
For example, you could do this coding that flags the turtle you want to monitor and increments the flag variable when an event occurs. You can then monitor it in the BehaviorSpace and analyses the results output in a csv file. For example, the following codes:
globals [ID]
turtles-own [special-turtle]
to create-turtle
crt 1 [
setxy min-pxcor 0
set heading 90
set special-turtle false
set ID who]
end
to go
;omitted
special-turtles
tick
end
to special-turtles
ask turtles-on patch 0 0 [set ID who]
ask max-one-of turtles [who] [set special-turtle true]
ask turtles with [special-turtle = true][set special-turtle (special-turtle + 1)]
end
I think there is something that none of the previous answers considered: the way you implement your timer depends on how you want to use the measurement.
1 - If you want to read the measurement only after the measurement is completed
The expression "after the measurement is completed" can mean both that you want to read it at some point later during the model, or maybe even just from some model's output.
The approach I'd take in this case is similar to what JenB suggested, but I believe you can put it a bit simpler because here I only use one extra turtle variable (apart from my-variable, that is there only to represent something that you already have in your model):
turtles-own [
my-variable ; This represents something you have in your model, to be used as a condition for the timer
my-timer
]
to setup
clear-all
reset-ticks
create-turtles 10
end
to go
; Here you have your normal code. When you need, just include
; 'start-timer' and 'stop-timer'. For simplicity, let's
; say you want to count how long does each turtle take
; to get from a value of 1 to a value of 5 for my-variable.
; You can just do:
ask turtles [
if (random 10 < 2) [
set my-variable (my-variable + 1)
]
if (my-variable = 1) [
start-timer
]
if (my-variable = 5) [
stop-timer
type "I am turtle " type who type " and it took me " type my-timer print " ticks."
die
]
]
tick
if (not any? turtles) [stop]
end
to start-timer
set my-timer ticks
end
to stop-timer
set my-timer (ticks - my-timer)
end
Note that most of the code is there only to make a full reproducible example, but the actual implementation only consists of my-timer, to start-timer and to stop-timer.
You can take this approach because the hypothesis here is that you will be interested in reading the measurement only after to stop-timer happened.
Otherwise, see point 2.
2 - If you want to be able to read the measurement at any moment during the simulation
In this case, you need to make the timer progress as time progresses (as opposed to point 1, where you could just take the initial and final time, and make the difference between the two).
I guess the easiest way to do this is to conditionally increment the timer every time there is a tick.
Taking the same example as before, it would be something like:
turtles-own [
my-variable ; This represents something you have in your model, to be used as a condition for the timer
timer-active?
my-timer
]
to setup
clear-all
reset-ticks
create-turtles 10 [
set timer-active? FALSE
]
end
to go
ask turtles [
if (random 10 < 2) [
set my-variable (my-variable + 1)
]
if (my-variable = 1) [
set timer-active? TRUE
]
if (my-variable = 5) [
set timer-active? FALSE
]
]
tick
ask turtles with [timer-active?] [
set my-timer (my-timer + 1)
]
if (count turtles with [my-variable < 5] = 0) [stop]
end
This way, you will be able to see at any moment what is the current value of my-timer for each turtle.

Netlogo, how to capture values of variables of turtles and add each turtles value, map them, and reduce them to a single #

I am trying to write a procedure where a turtle of a certain breed asks turtles of the same breed, within a certain distance, the value of a certain variable. The asking turtle will then capture the values add them to it's own, map + them and then reduce + to a single number. Here's the code
ask Teams
[ if AsgnE = "E 1"
[
ask Teams with [ distance myself < 25]
[
; assuming that there are no more then 2 teams within distance
let Val1 []
let Val2 []
let Val3 []
set Val1 Value
set Val2 Value
set Val3 [Value] of self
let Val4 (map + Val1 Val2 Val3)
set Val4 (reduce + Val4)
set Storys1 [Stories] of Epic 0
if Storys1 > 0 [ set TotValue1 Val4 ]
]
]
]
The values of each Team continuously update as long as the go button is pressed. The issue is that the resulting number never matches the aggregate of all the values. As the number updates they never match the totals of the separate Teams. Sometimes the number drops to a lower number (I'm assuming it's representing a single teams value) before jumping back to a higher number.
Any idea on how to fix this?
Thanks
Rudy
My guess is that it's a synchronicity problem. The ask will iterate (in random order) through all the turtles. Let's say it starts with turtle 1 - so turtle 1 updates its value to be the sum of its old value and all the values of the nearby turtles. Then the ask moves on to turtle 2, and turtle 2 happens to be nearby to turtle 1. That means turtle 2 adds all the numbers again, with turtle 1 having its adjusted value. With just these two turtles, the value for turtle 2 gets added in twice because turtle 1 also has it hidden in its new value.
If this is not the behaviour you want, the easiest thing to do is to have an extra variable called something like next-value. Calculate next-value for each turtle as the appropriate sum. Then, in a new ask, get each turtle to set value next-value to update them all at the same time.
Also, your map and reduce seems unnecessarily complicated. If what you are trying to achieve is to add the value of a variable over a bunch of turtles, then you can simply do a sum of the variable after constructing the relevant turtle agentset. But it may be that you simplified for the purposes of the question, in which case just ignore this!
UPDATE ---- added complete model example
turtles-own
[ team
myval
nextval
]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set team one-of ["A" "B"]
set myval 1
]
reset-ticks
end
to go
ask turtles
[ let myteam turtles with [team = [team] of myself]
set nextval sum [myval] of myteam
]
type "total before: " print sum [myval] of turtles
ask turtles
[ set myval nextval
]
type "total after: " print sum [myval] of turtles
end

How to compare the current value of a label to its previous one?

I am trying to move the turtles one patch to the right if the value of their label is bigger than the previous value, from the previous tick.
How can I do this?
From your comment, your calculation is:
to calculate-value
ask n-of (count turtles * 0.5) turtles
[ set value value + 100
ifelse show-value?
[ set label value ]
[ set label " " ]
]
end
So, you have half the turtles increase their value (which is also their label) by 100 and the other half have no change. Since the turtles that increase their value are the same turtles as the ones that you want to move to the right, you can simply add the instructions for moving inside the code block, something like this:
to calculate-value
ask n-of (count turtles * 0.5) turtles
[ set value value + 100
set heading 90
forward 1
ifelse show-value?
[ set label value ]
[ set label " " ]
]
end

Netlogo different global values from turtles

I don't understand why something appened in my netlogo code, but finally I found that a global variables change values from turtles to turtles, is this possible?
I noticed this because I have 2 print value in my code like this:
breed [players player]
breed [balls ball]
globals [
TURNS;
TEAM-OWNER
]
to setup
clear-all
reset-ticks
set TURNS 0
setup-players
setup-ball
end
to setup-ball
create-balls 1 [
setxy 0 0
set color orange
set shape "bug"
set size 2.5
]
end
to setup-players
create-players 5 [
setxy 0 0
set color white
set shape "bug"
set size 2.5
]
end
to go
set TURNS TURNS + 1
print word "this is the turns " TURNS
print ( word " values " TEAM-OWNER)
if ticks >= 1000 [ stop ]
ask players [
print ( word " values " TEAM-OWNER)
fd 2
set TEAM-OWNER "red"
]
ask balls [
fd 2
set TEAM-OWNER "black"
]
end
Is this possible? Global values doesn't change it values in all turtles?
This is the print that I got:
this is the turns 1
values 0
values 0
values red
values red
values red
values red
this is the turns 2
values black
values black
values red
values red
values red
values red
It has not be like black 1 times ? and red the other times? And Why I get 2 blacks?
A global variable is global - there is only ONE copy of it and all turtles retrieve and modify the same copy.
Look at this piece of your code:
ask players [
print ( word " values " TEAM-OWNER)
fd 2
set TEAM-OWNER "red"
]
ask balls [
fd 2
set TEAM-OWNER "black"
]
First, every one of your players moves forward two distance units, then sets the value of TEAM-OWNER to "red". If you have 10 players, then the global variable TEAM-OWNER is being set to red 10 times. Then every one of your balls moves forward two distance units and sets the variable TEAM-OWNER to "black". As long as you have one (or more) balls, then the global variable will be overwritten.
A global variable is used for something that is the same for every turtle in your model.

How to get the id number of a new turtle just created?

I have the following code where I hatch a new agent,
to t-of-slowdown [ es-poi ]
if we-look > 0 [
set we-look (we-look - 1)
if (we-look <= 0) [
if es-poi and (not any? events-here) [
hatch-events 1 [
set color green
set size 5
set is-poi? true
set new-poi true
let m [[ end2 ] of cur-link] of myself
move-to m ]
set events-x ([who] of events-here)
show events-x
set we-poi-var va-geometric (1 / 1500) + we-ticks poi
set sera-poi false
]
set impregna true
set color red
set seguir true
set we-look random-normal 120 20 ;time to watch an event
]
]
end
which is run in a turtle context (walkers breed)
A walker is moving by a 'link' (another procedure which calls this one), and when a counter is <0,
this code generates a new event (events breed) and places it in the same place where the walker is (cur-link is the current walker link).
After that, the walker must get the id number of the new event
set events-x ([who] of events-here)
The problem here is that variable events-x get an empty list []. The next time the walker passes by the same event it does get the number-id of the event.
Something must be wrong but I can not guess what it is.
I would appreciate very much if someone could take a look and point me some help.
Regards
You could:
let child-who -1
hatch-events 1 [
...
set child-who who
...
]
set events-x child-who
Or:
hatch-events 1 [
...
let child-who who
ask myself [ set events-x my-who ]
...
]
Both of these are a bit clunky, sadly. The second one avoids needing to initialize child-who to a meaningless value, but it requires using myself, a primitive that is likely to mystify the reader.
You could avoid both problems with:
let parent self
hatch-events 1 [
...
let child-who who
ask parent [ set events-x child-who ]
...
]
(But note that using who numbers at all, for anything, is rarely the best and most idiomatic solution to any problem. It's almost always better to store a reference to the turtle itself.)