NetLogo - plot sum of all turtles with value in certain range - netlogo

I would like to plot the sum of all turtles holding values in a range from 2 to 4. How can I achieve that?
I only get the sum of turtles holding a value of 4 with plot count turtles with [value = 4], however, I would need something like plot count turtles with [2 <= value => 4].
How can I achieve this?
My MWE is:
breed [ turtles ]
turtles-own [ value ]
to setup
clear-all
create-turtles 100
[
set value random 4
]
reset-ticks
end
to go
ask turtles [
rt random 360
fd 1
]
tick
end

NetLogo doesn't support the 2 <= value => 4 syntax. You need to write both conditions separately:
plot count turtles with [ value >= 2 and value <= 4 ]
By the way, if you wanted to plot the sum of the values instead of the count of turtles with values in that interval, you could write:
plot sum [ value ] of turtles with [ value >= 2 and value <= 4 ]

Related

calculate steps of turtles among neighbors

In the world, the two types of agents, individuals and messages, are randomly positioned in the two-dimensional attitudinal space.
If an individual believes the messages, he or she creates links with the messages.
Then, an individual adjusts his or her attitudinal position based on the calculation of distances from oneself to the messages.
Here, I have a problem.
I would like to make an individual move twice more when exposed to a message with a high value than when exposed to one with a low value.
But what I can do right now is just averaging the distance among the messages.
Here's what I've done so far
breed [individuals individual]
breed [messages message]
messages-own [value]
undirected-link-breed [messagelinks messagelink]
to setup
ca
create-individuals 100 [initiate-individuals]
reset-ticks
end
to go
new-messages
end
to initiate-messages
ifelse random-float 1 < value-p [ set value "H" ] [set value "L"]
end
to initiate-individuals
setxy random-xcor random-ycor
end
to new-messages
create-messages 30 [
initiate-messages
ask individuals [
integrate-messages myself
]
]
end
to integrate-messages [newmessages]
if random-float 1 < 0.3
[create-messagelink-with newmessages
setxy mean [xcor] of messagelink-neighbors mean [ycor] of messagelink-neighbors]
end
I am making the assumption that integrate-messages is intended to handle a set of messages, even though when a receiver executes it in your code it is only the newly created message itself, not all newly created messages. There are likely several ways to handle the problem of giving "H" messages more influence than "L" messages, but perhaps the most straightforward to simply to use a weighted average of the messages' xcors and ycors. I've written a short model that does that in integrate-messages.
breed [messages message]
breed [receivers receiver]
undirected-link-breed [messagelinks messagelink]
messages-own [value]
to setup
clear-all
create-receivers 1 [
setxy random-xcor random-ycor
set color green
]
create-messages 5 [
setxy random-xcor random-ycor
set value ifelse-value (random-float 1 < 0.5) ["H"] ["L"]
set color blue
set label value
]
reset-ticks
end
to go
ask one-of receivers [
integrate-messages n-of 3 messages
]
end
to integrate-messages [newmessages]
;to indicate which messages are being received.
ask newmessages [set color red ]
create-messagelinks-with newmessages [set color red]
;now calculate the weighted position relative to ALL linked messages.
let mssgs [other-end] of my-messagelinks
let wghts map [x -> ifelse-value ([value] of x = "H") [2] [1]] mssgs
let xc sum (map [[m w] -> w * [xcor] of m ] mssgs wghts)
let yc sum (map [[m w] -> w * [ycor] of m ] mssgs wghts)
set xcor xc / sum wghts
set ycor yc / sum wghts
;to show where the receiver ends up.
set color red
end

How to divide turtles in groups by percent?

In my world, turtles are firms. 30 % of all firms have a low output (Y = 1), 60 % a medium (Y = 2) and 10 % a high production output (Y = 3).
How can I assign an input for Y to 30 % of my turtles/firms? Best would be to have a slider in order to change the values if needed.
I have given the turtles a firm-own variable called Y which is their output. I have also created number-of-firms slider where I can decide how many firms will be in the world for each setup.
breed [ firm firms ]
firm-own [
Y ;; output
]
to setup
clear-all
setup-industry
reset-ticks
end
to setup-industry
create-firm number-of-firms [ ;; number of firms to be defined through slider
ask n-of ( count firms * 0.3 ) firms [
set Y 1 ]
ask n-of ( count firms * 0.6 ) firms [
set Y 2 ]
ask n-of ( count firms * 0.1 ) firms [
set Y 3 ]
]
end
The error message says that when I have "ask n-of ( count firms ... )" firms are expected to have 1 input, meaning a number.
The first input in breed defines the agentset whereas the second input goes for a single member. Consider following modifications:
breed [ firms firm ]
firms-own [
Y ;; output
]
...
create-firms number-of-firms [ ;; number of firms to be defined through slider
Now you won't have any error messages.

How to set a turtle variable in a range of values randomly?

I need to set a turtle variable in a range of values randomly, the range must go from 800 to 1000.
I wrote this code:
turtles-own [income]
to setup
create-turtles 100 [
set income range 800 1000
]
end
The code is not working.
I tried ramdom-float but the values range from 0 to max value.
range returns a list of numbers between the start and stop, where you want to choose a number from a range---these are different.
I didn't find a random-range or a random-uniform, so these are some approaches:
You can do a random number between 0 -200 and just add 800 to scale to your range.
turtles-own [income]
to setup
create-turtles 100 [
set income (random 200) + 800
]
end
Alternatively, if you want to create a list using the range function and choosing one of them.
turtles-own [income]
to setup
create-turtles 100 [
set income one-of (range 800 1000)
]
end

In NetLogo how can I extract the x and y coordinates of a subset of turtles every nth tick?

I have a very simple model of 50 turtles moving away from a central point. I would like to be able to extract the spatial coordinates (xcor, ycor) of a subset of them every nth tick in behaviour space. Hope you can help!
The modulo operator mod is probably the simplest way to do this. It outputs the remainder from a division operation, so you can just use a logical flag such that the coordinates are only extracted when ticks divided by n is equal to 0. For example:
to setup
ca
crt 10
reset-ticks
end
to go
; set up lists for example output
let tlist []
let xlist []
let ylist []
ask turtles [
rt random 60 - 30
fd 1
]
tick
; If ticks is not zero, and the remainder of
; the number of ticks / 3 is zero, extract
; some info about the turtles and print it.
if ticks > 0 and ticks mod 3 = 0 [
ask turtles with [ xcor > 0 ] [
set tlist lput self tlist
set xlist lput xcor xlist
set ylist lput ycor ylist
]
print tlist
print xlist
print ylist
]
end
Run this several times and you'll see that on tick 3 (and 6, 9, 12, etc), the lists are printed out. Note that where you have your tick increment will affect when this output is actually extracted- in the example above, tick happens at the end of the go procedure but before the if statement is evaluated.

Calculating turtle mortality based on distance from origin in netlogo

I am writing a procedure (Pass-Away-Space) to calculate mortality of turtles moving from the origin (start-patch) through out the world. Each turtle calculates its own mortality based on its distance from the origin (start-patch). The code I am attempting to implement for this procedure is as follows:
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * [distance start-patch] of turtles)
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
The error I am getting is expected input to be a number but got the list. I am not sure what the issue is and I was wondering if anyone could point out and rectify the problem with the code.
The problem here is your of turtles. Since an ask procedure affects one turtle at a time, each turtle in your procedure above is evaluating the [distance start-patch] of all turtles instead of just its own distance to the start patch. To clarify, check out the following setup:
globals [ start-patch ]
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
]
set start-patch patch 0 0
end
to incorrect-example
ask turtles [
print ([ distance start-patch ] of turtles)
]
end
to correct-example
ask turtles [
print distance start-patch
]
end
Compare the print output of the incorrect-example and the correct-example procedures, and you'll see that when you use [distance start-patch] of turtles you get the list of distances of all turtles. When you ask turtles to evaluate a turtles-own variable (including color, size, etc) each turtle will automatically access its own version of that variable- there's no need to specify which turtle. So, your pass-away-space might look something more like below (untested):
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * (distance start-patch) )
if chances >= 1 [
die
set dead-count dead-count + 1
]
]
end