Counting breeds with neighbors (to plot) - netlogo

I have two breeds: supras and subs.
I'd like to draw two lines:
Number of subs who have neighbors that are supras (divided by total
population of turtles)
Number of supras who have neighbors that
are subs (divided by total population of turtles)
How can I do this? I've tried this:
plot count (subs with [one-of neighbors = supras]) / num-turtles
plot count (supras with [one-of neighbors = subs]) / num-turtles
The number is always 0 for each population, which should not be the case. Here is my code:
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
;; Color the patches so they're easier to see
ask patches [ set pcolor random-float 2 ]
;; 1/2 of num-turtles patches will sprout subs
ask n-of (num-turtles / 2) patches [
if not any? turtles-on patch-set self [
sprout-subs 1
]
]
;; 1/2 of num-turtles patches will sprout supras
ask n-of (num-turtles / 2) patches [
if not any? turtles-on patch-set self [
sprout-supras 1
]
]
;; Set breed colors and own-variables
ask subs [
set color blue
set shape "dot"
set age 0
set status random 10
]
ask supras [
set color pink
set shape "dot"
set age 0
]
reset-ticks
end
to go
ask turtles [
let empty-patches neighbors with [not any? turtles-here]
if any? empty-patches[
let target one-of empty-patches
face target
move-to target
]
]
;; Mating conditions
ask supras [
if any? subs-on neighbors [
;; Mate with highest status sub
mate
]
]
tick
end
to mate
move-to max-one-of subs [status]
end

neighbors returns an agentset of patches, so saying neighbors = supras is not going to get your what you need- no patches are supras or subs. Instead, you want to check if any of the neighbors have any supras-here or subs-here. This worked for me:
plot (count ( subs with [ any? neighbors with [ any? supras-here ] ] ) ) / ( count turtles )
plot (count ( supras with [ any? neighbors with [ any? subs-here ] ] ) ) / ( count turtles )
You will probably want to scale your Y max down to 1 in order to see much.

Related

How to compare the local variables of a turtle with its neighbors

I am trying to compare the local variables of a turtle, with its neighbors and trying to find out the total number of neighbors which match this criterion
total-nearby = total number of neighbors.
I am checking according to the color of the turtles, if the color is different, then I will check for the attributes/variables
Error: A patch can't access a turtle or link variable without specifying
which agent
CODE:
turtle-own[
total-similar-nearby ; sum of previous two variables
total-other-nearby
total-nearby
native
language
income
maritalstatus
]
;;then assigning multiple number of turtles with different values to the local variables.
ask turtles[
repeat total-nearby
[
if color = [color] of one-of neighbors
[set d1 d1 + 1]
if color != [color] of one-of neighbors
[
if native = [ native ] of one-of neighbors
[set a 1]
if language = [ language ] of one-of neighbors
[set b 1]
if income = [ income ] of one-of neighbors
[set c 1]
if maritalstatus = [ maritalstatus ] of one-of neighbors
[set d 1]
] set p a + b + c + d
if p >= 50 [set d1 d1 + 1]
]
]
neighbors is a patch variable, not a turtle variable. So, the turtles in your model use the primitive neighbors, they are querying an agentset of patches when they want to query an agentset of turtles. There are several ways for turtles to evaluate nearby turtles, such in-radius or in-cone, but in this case if you're wanting the turtles that are specifically on the patches directly adjacent, you could use the other, turtles-on, and neighbors primitives to get what you're looking for. For a simple example, have a look at this toy model:
to setup
ca
ask n-of 300 patches [
sprout 1 [
set color one-of [ red blue ]
]
]
reset-ticks
end
to go
ask turtles [
; Make an agent-set of turtles on neighboring patches
let nearby-turtles other turtles-on neighbors
; If there are any turtles on neighboring patches,
; assume the color from one of them.
if any? nearby-turtles [
set color [color] of one-of nearby-turtles
]
]
tick
end
Check out the dictionary definitions for other, turtles-on, and neighbors for more information.

How to spawn turtles a certain amount of patches away from each other

I am trying to spawn turtles 5 patches away from each other but I'm not sure how, right now they all spawn on green patches (I don't want them to spawn on brown ones) and I'm not sure how exactly you control the distance between the spawning of turtles, thanks.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches [
set pcolor green
]
ask n-of 100 patches [
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown][sprout-humans 1 [set size 5
set color blue
set shape "person"]]
ask n-of 5 patches with [pcolor != brown][sprout-zombies 1 [set size 4
set color red
set shape "person"]]
end
Have you read this question: NetLogo Create turtle at regular distance from each other?
Anyway, I thought that showing you some working functions would be helpful, here I made two alternatives, sprout-distanced1 and sprout-distanced2, you can test them both by alternating which line is commented; I also added a slider called Min-Distance to control the turtles spacing.
sprout-distanced1 uses the keyword carefully with is basically a try-else block, it's there in case that the turtle doesn't find a patch distanced enough to move to, in which case rather than sending a warning the turtle will stay where it is and print its distance to the closest turtle.
sprout-distanced2 uses a while loop, in case that the turtle doesn't find a place to move to that is at least Min-Distance from another turtle it will reduce the minimum radius by a small amount until it can distance itself from other turtles, if it had to move to a patch where it is less than Min-Distance away from other turtles it will log the distance at the Command Center.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask n-of 100 patches
[
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown]
[
sprout-humans 1
[
set size 5
set color blue
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
ask n-of 5 patches with [pcolor != brown]
[
sprout-zombies 1
[
set size 4
set color red
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
end
to sprout-distanced1
carefully
[
; try to move at least Min-Distance away from other turtles
move-to one-of patches with [not any? other turtles in-radius Min-Distance]
]
[
; if can't move Min-Distance away from other turtles
; stay put and log the min distance to other turtle, just for reference
show distance min-one-of other turtles [distance myself]
setxy random-xcor random-ycor
sprout-distanced1
]
end
to sprout-distanced2
let min-dist Min-Distance
let moved? FALSE
while [not moved? and min-dist > 0]
[
; can distance it self somewhere?
ifelse any? patches with [not any? other turtles in-radius min-dist]
[
; if yes, go there
move-to one-of patches with [not any? other turtles in-radius min-dist]
set moved? TRUE
; if had to reduce the distancing radious log it
if moved? and min-dist < Min-Distance
[
show distance min-one-of other turtles [distance myself]
]
]
[
; no where to go, reduce the distancing radious
set min-dist min-dist - 0.1
]
]
end
Choose whichever suits better your model.

How can I make the model run?

I am essentially trying to combine elements of the 'Segregation' model and 'Rebellion' model to form a model that is representative of alliance forming.
Here is what I have so far- when I attempt to run it I receive the error: THREATS breed does not own variable ACTIVE?
error while threat 0 running ACTIVE?
called by procedure GO
called by Button 'go'
breed [ agents an-agent]
breed [ threats threat ]
globals [
k ; factor for determining attack probability
threshold ; by how much must D - BS > A to make a state burden share
percent-similar ; on the average, what percent of a turtle's neighbors
; are the same color as that turtle? Likely to ally
percent-unhappy ; what percent of the turtles are unhappy? Or percieve threats?
visualization
]
agents-own [
allied-states ; R, fixed for the agent's lifetime, ranging from 0-1 (inclusive)- for each turtle, indicates whether at least %-similar-wanted percent of
; that turtle's neighbors are the same color as the turtle
perceived-threat ; T, also ranging from 0-1 (inclusive)- how many have a turtle of another color?
active? ; if true, then the agent is actively allied
; if false, then the agent is free-riding
conflict ; how many turns in conflict remain? (if 0, the agent is not in conflict)- sum of previous two variables
total-nearby ; sum of previous two variables
]
patches-own [
neighborhood ; surrounding patches within the vision radius
]
to setup
clear-all
; set globals
set k 2.3
set threshold 0.1
ask patches [
; make background a slightly dark gray
set pcolor gray - 1
; cache patch neighborhoods
set neighborhood patches in-radius vision
]
if initial-threats-density + initial-agent-density > 206 [
user-message (word
"The sum of INITIAL-THREATS-DENSITY and INITIAL-AGENT-DENSITY "
"should not be greater than 206.")
stop
]
; create threats
create-threats round (initial-threats-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
display-threats
]
; create agents
create-agents round (initial-agent-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
set heading 0
set allied-states random-float 1.0
set perceived-threat random-float 1.0
set active? false
set conflict 0
display-agent
]
; start clock and plot initial state of system
reset-ticks
end
to go
if all? turtles [ active? ] [ stop ]
move-unhappy-turtles
update-turtles
update-globals
tick
end
; unhappy turtles try a new spot
to move-unhappy-turtles
ask turtles with [ not active? ]
[ find-new-spot ]
end
; move until we find an unoccupied spot
to find-new-spot
rt random-float 360
fd random-float 10
if any? other turtles-here [ find-new-spot ] ; keep going until we find an unoccupied patch
move-to patch-here ; move to center of patch
end
to update-turtles
ask turtles [
; in next two lines, we use "neighbors" to test the eight patches
; surrounding the current patch
set allied-states count (turtles-on neighbors) with [ color = [ color ] of myself ]
set perceived-threat count (turtles-on neighbors) with [ color != [ color ] of myself ]
set total-nearby allied-states + perceived-threat
set active? allied-states >= (percent-similar * total-nearby / 100)
; add visualization here
if visualization = "old" [ set shape "default" set size 1.3 ]
if visualization = "square-x" [
ifelse active? [ set shape "square" ] [ set shape "X" ]
]
]
end
to update-globals
let similar-neighbors sum [ allied-states ] of turtles
let total-neighbors sum [ total-nearby ] of turtles
set percent-similar (similar-neighbors / total-neighbors) * 100
set percent-unhappy (count turtles with [ not active? ]) / (count turtles) * 100
; Agents engaged in conflict have the duration reduced at the end of each clock tick
ask agents [ if conflict > 0 [ set conflict conflict - 1 ] ]
; update agent display
ask agents [ display-agent ]
ask threats [ display-threats ]
; advance clock and update plots
tick
end
; AGENT AND THREAT BEHAVIOR
; move to an empty patch
to move ; turtle procedure
if movement? or breed = threats [
; move to a patch in vision; candidate patches are
; empty or contain only jailed agents
let targets neighborhood with [
not any? threats-here and all? agents-here [ conflict > 0 ]
]
if any? targets [ move-to one-of targets ]
]
end
; AGENT BEHAVIOR
to determine-behavior
set active? (burden-sharing - allied-states * estimated-conflict-probability > threshold)
end
to-report burden-sharing
report perceived-threat * (1 - alliance-protection)
end
to-report estimated-conflict-probability
let t count (threats-on neighborhood)
let a 1 + count (agents-on neighborhood) with [ active? ]
; See Info tab for a discussion of the following formula
report 1 - exp (- k * floor (t / a))
end
to alliance
if any? threats [attack]
set active? true
end
; THREAT BEHAVIOR
to attack
if any? (agents-on neighborhood) with [ active? ] [
; arrest suspect
let suspect one-of (agents-on neighborhood) with [ active? ]
move-to suspect ; move to patch of the jailed agent
ask suspect [
set active? false
set conflict random conflict-term
set color pink
]
]
end
; VISUALIZATION OF AGENTS AND COPS
to display-agent ; agent procedure
set color cyan
set shape "triangle"
end
to display-active?
set color pink
set shape "triangle"
end
to display-threats
set color red
set shape "circle 2"
end
The problem is that you have two breeds of turtles, agents and threats, and only agents "own" the variable active?. turtles is a superset of all breeds, so when the all? primitive tries to query the active? variable of literally all turtles, it tries to query active? for threats too, and can't find it. The line should be
if all? agents [ active? ] [ stop ]

How to extract a highly linked node from a network

I want to extract a node with highest degree centrality from the network. I don't want to extract a node with max links only. I want to extract the node along with the nodes adjacent to it.
Below is the code. In this code, I have loaded a network using nw extensions.
extensions [nw]
turtles-own [ explored? ]
to setup
ca
crt 25
ask turtles [fd random 15]
load-graph
extract_deg
end
to load-graph
let filename user-file
if (filename != false) [
nw:load-graphml filename [
set shape "circle"
set size 1
]
nw:set-context turtles links
]
end
to extract_deg
let n turtles with [my-links = max [count link-neighbors] of turtles]
ask n [show other turtles network:in-link-radius 1 turtles]
end
to layout
ask turtles [ set size sqrt count my-links ]
layout-spring turtles links 0.5 2 1
ask turtles [
facexy 0 0
fd (distancexy 0 0) / 100 ]
end
The code below will choose one of the nodes with largest degree (just remove one-of if you want all of them), turn it red and make its network neighbours green.
You don't need the expression [my-links = max [count link-neighbors] of turtles], standard NetLogo includes the very useful with-max primitive. However, I think your construction would have worked if you had counted my-links (like let n turtles with [count my-links = max [count link-neighbors] of turtles]). Then you have some syntax errors in the next line (the extension is nw and you don't need the turtles.
to extract_deg
let maxk-set one-of turtles with-max [count my-links]
ask maxk-set
[ set color red
ask other nw:turtles-in-radius 1 [set color green]
]
end

Initialize Netlogo world with two breeds; only one turtle per patch

I'm trying to set up a world in Netlogo where there are two breeds, but there is only one turtle per patch:
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
;; Color the patches so they're easier to see
ask patches [ set pcolor random-float 2 ]
;; num-turtles patches will sprout one turtle each
ask n-of (num-turtles / 2) patches [
if not any? turtles-on patch-set self [
sprout-subs 1
]
]
ask n-of (num-turtles / 2) patches [
if not any? turtles-on patch-set self [
sprout-supras 1
]
]
;; Set breed colors and own-variables
ask subs [
set color blue
set shape "dot"
]
ask supras [
set color pink
set shape "dot"
]
reset-ticks
end
to go
ask turtles [
fd 1
]
tick
end
This seems to work but I can't quite tell if it's technically correct. What would be a good test to write to make sure I don't have some patches with multiple turtles on initialization?
I am actually going to suggest a different approach; instead of randomly selecting some patches for one breed and some patches for the other and trying to avoid each other, you can just select the full number of patches to sprout initially and then convert half your turtles into the other breed.
globals [num-turtles]
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
set num-turtles 99
ask n-of num-turtles patches [sprout-subs 1]
ask n-of (num-turtles / 2) subs [set breed supras]
<procedures to set colours etc>
end
Try to strip your code down to what is needed for a complete example.
globals [num-turtles]
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
set num-turtles 99
;; num-turtles patches will sprout one turtle each
ask n-of (num-turtles / 2) patches [sprout-subs 1]
ask n-of (num-turtles / 2) patches with [not any? turtles-here] [
sprout-supras 1
]
end
to test-setup
if (int (num-turtles / 2) != count supras) [error "setup error: supras"]
if (int (num-turtles / 2) != count subs) [error "setup error: subs"]
if any? patches with [count turtles-here > 1] [error "setup error: patches"]
end