How to flip the sign of a variable depending on a condition, in Netlogo - netlogo

The following doesn't work (there must also be a more succinct way of doing it):
ask turtles
[
ifelse return > 0
[
set tot-demand-contrarians (-(tot-demand-contrarians))
set tot-demand-followers ((tot-demand-followers))
]
[
set tot-demand-contrarians ((tot-demand-contrarians))
set tot-demand-followers (-(tot-demand-followers))
]
]

Half of your clauses set a variable to itself anyway so they're not needed. I suspect NetLogo is reading the '-' as an instruction to subtract, but you haven't said what to subtract it from. You could do something like:
set x (0 - x) ; the brackets are optional, for clarity
But personally I feel that it is more readable to multiply by -1
ask turtles
[ ifelse return > 0
[ set tot-demand-contrarians (-1 * tot-demand-contrarians) ]
[ set tot-demand-followers (-1 * tot-demand-followers) ]
]

Related

Operator > and combining it with number and a string in NetLogo

I want a little help. I want turtles to cooperate with 50% probability if their land parcels are > 5.
I am writing as
ifelse random 1 = 0 and land > 5
[set cooperate? true]
[set cooperate? false]
But it gives error that; The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a string and a number.
how to correct it?
Thanks
There's not quite enough information to diagnose the problem. Is this code inside an ask turtles block with the variable 'land' as a turtles-own attribute? Also, you might want to print off some values of 'land' to make sure you actually have numbers in it.
As you can see from the working example below, there is no error in the code you have provided.
turtles-own [ land cooperate? ]
to testme
clear-all
create-turtles 10
[ set land random 10
ifelse random 1 = 0 and land > 5
[ set cooperate? true ]
[ set cooperate? false ]
]
type "Cooperating: " print count turtles with [cooperate?]
type "Not cooperating: " print count turtles with [not cooperate?]
end

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.

Sampling 30 values and finding standard-deviation + mean

I was able to include the recovery or die but now I'm having trouble getting the standard deviation and of the turtles that died. I think I got the standard deviation but can't get the mean
if random-float 1 < recover-or-die [
set epi-state recovered-code
set color green
ifelse random-float 1 < 0.90[
]
]
]
I tried this for the mean and it kept saying "Expected Command"
Your problem is ifelse recovery-prob < 0.1 = true. I don't know what your recovery-prob is set to, but this line is always true or always false. What you probably want to do (and what is in your comment) is:
ask turtles with [epi-state = infectious-code]
[ ifelse random-float 1 < recovery-prob
[ set epi-state recovered-code
set color green
]
[ die
]
]
Note that you don't need to actually have the = true part.
If you have recover-prob set to 0.05 (for example), the condition is true for all turtles and they all recover. If it's set to 0.2 (for example), it is false for all turtles and they all die.
This block still has a logic problem I think. The way you have it written, any infectious turtle will either recover or die immediately. What about the turtles who stay infectious for more than one tick?

Netlogo: How to compare the ID and then flag in this case?

I would like to put a flag called "min-id" for the turtle with the smallest ID. And I want to flag other turtles as "not-min-id". However the following sample syntax has errors. The error message is as follows.
" error while turtle 0 running >
called by procedure GO
called by button 'go' "
I probably to need your advice. Thank you.
globals [ min-id not-min-id count-up ID ]
to go
reset-ticks
ask patch 0 0
[
sprout 1 ;;This model needs to use sprout.
]
ask (turtles-on patch 0 0)
[
set ID who
setxy min-pxcor 0
set heading 90
]
if (count turtles > 0)
[
ask min-one-of turtles [who]
[
set min-id TRUE
]
]
if (count turtles > 0)
[
ask (turtles-on patch 0 0)
[
if ID > min-one-of turtles [who] ;;This syntax has errors.
[
set not-min-id TRUE
]
]
]
ask (turtles-on patch 0 0) with [not-min-id]
[
set count-up count-up + 1
]
if (count turtles > 0) [
ask (turtles-on patch 0 0) with [min-id]
[
die
]
tick
end
You have some confusion in your code. From your description, I believe you want each turtle to have a flag for whether or not it has the minimum who number. This means you need a flag for each turtle. However, you have set up min-id as a global variable instead of a turtle variable. Furthermore, you only need the flag variable once (that is, you need min-id but not not-min-id) and you set it to TRUE or FALSE.
Replace
globals [ min-id not-min-id count-up ID ]
with
globals [ count-up ID ]
turtles-own [ min-id ]
and see if that fixes it. Also initialise min-id to FALSE as part of the sprout.
Having said all that, I strongly agree with Alan, if you ever use the who variable for anything except print statements in debug, you probably need to rethink your code. In your case, what is special about the turtle with the lowest who number that makes you want to keep track of it? Do you simply want a random turtle that happens to be at a particular location? Then select a random turtle at that location to do the TRUE/FALSE without going through who.

Directing an agent on a specific path (Netlogo)

there is a question that I want to ask that is when I trying to type this code, I got the error that is
The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a number and a list.
What I want to ask is how can I fix this, the false happen at this line on the code :
if pri-lev > [pri-lev] of oppoint1 and pri-lev > [pri-lev] of oppoint2
I tried to change it into "cars-on" or "cars with" but they are all useless. I also try to find on the Netlogo dictionary but I found no results on the code for directing an agent on a specific path.
What I am trying to do here is when an agent comes to a specific section, it will check if any agents listed as "oppoint1"; "oppoint2"; "oppoint3"; "oppoint4" and then compare a value call pri-lev to others value for setting its decision on keeping on moving or stopping and wait for others.
These are the part of my code:
ask cars
[
let oppoint1 (cars-at (xcor + 2) (ycor + 2))
let oppoint2 (cars-at (xcor - 1) (ycor + 1))
let oppoint3 (cars-at (xcor - 2) (ycor + 1))
let oppoint4 (cars-at (xcor - 3) (ycor + 1))
ifelse oppoint1 != nobody and oppoint2 != nobody
[
if pri-lev > [pri-lev] of oppoint1 and pri-lev > [pri-lev] of oppoint2
[
set pri-lev 4
speed-up
]
]
[
if oppoint2 = nobody and oppoint3 = nobody and oppoint4 = nobody
[
set speed 1
fd speed
if turning = "Rtrue"
[
set heading heading + 90
speed-up
]
]
]
]
Sincerely, Minh
it seems that the reason you are getting this error is that you are comparing the attribute of one (the current car) to the attributes of many (the oppoint agent-sets). Your code now says something like "If my privilege is greater than the privilege of that group, do this thing..." The problem is that [pri-lev] of oppoint1 returns a list of the pri-lev of all members of the oppoint1 agentset, like [ 10 12 13 24 ], and Netlogo won't automatically iterate over that list and compare each item to the attribute of the asking turtle.
There are several ways to deal with this. For example, you could make sure that you only ever compare two turtles- maybe by making sure that you only ever have one turtle per patch at a given time. If you are going to potentially compare one agent to an agent-set, you can use the any? primitive to check if any members of the group you're looking at satisfy your conditional statement. For example, given this setup:
turtles-own [
pri-lev
]
to setup
ca
reset-ticks
crt 10 [
set pri-lev 1 + random 10
]
end
You can ask one-of your turtles to check if not any? of the turtles on the current patch have a higher pri-lev than the asking turtle. If none of them do, the current turtle will move forward. Otherwise, it will print that there is another turtle with a higher pri-lev on the current patch.
to compare-with
ask one-of turtles [
let other-turtles other turtles-here
ifelse not any? other-turtles with [ pri-lev > [pri-lev] of myself ] [
fd 1
]
[
print ("A turtle here has a higher pri-lev than I do." )
]
]
tick
end