Community detection in NetLogo - netlogo

Does anyone have any code to detect communities in NetLogo by some attribute, let's say colour? By community I mean a group of turtles of the same colour surrounded by turtles of other colours. It's easy to do visually when the world doesn't wrap but viciously difficult when it does.
To be clear, there could be two distinct red communities that I would like to count as two (like islands in a sea of blue), and I've got multiple (more than two) colours.

If I set a link between a turtle and it's neighbour, if they are of the same colour, then using the Networks Extension I can do this using nw:weak-component-clusters. I just then need to count the number of items in the resulting list.
breed [people person]
to communities
ask links [die]
ask people [ask people-on neighbors [if color = [color] of myself [create-link-with myself]]]
nw:set-context people links
show length nw:weak-component-clusters
end

Related

NetLogo: Measure total area covered by turtles

After my NetLogo simulation, I would like to measure the total area covered by only the turtles. Is there a simple way to implement this in NetLogo, or will I have to do this in another program?
My simulation has clusters that form and I would eventually like to calculate the cluster agent density. 1
NetLogo is not aware of where the edge of the turtles’ shapes are. So, if you are trying to work out what proportion of the screen has shapes on it, you need to do some complicated programming to calculate where the edges of the shapes are, how they overlap and so on. However, if all you care about is where the turtles are, then it's much easier. For example, to work out the proportion of patches where there are at least one turtle, you could do:
count patches with [any? turtles-here] / count patches

What is the difference between turtle size, patch size and frame rate in netlogo?

I want to know the Relationship between turtle size and patch size in Netlogo. Also I want to make those sizes in a way such that any turtle moves from one patch to another at every tick.
Patch size is measured in pixels. It is configurable in the "Model Settings" dialog.
You can think of turtle size as measured in "patches". A turtle of size 1 should appear to be the size of one patch. (Note, however, that turtle size is a "display only" property. Conceptually, turtles are just points. They don't really occupy space in the NetLogo world even if they appear to do so.)
Changing the xcor or the ycor of a turtle by 1 should move it by exactly one patch. If you want them to move diagonally, things are a bit more messy. You might be better off targeting destination patches directly, e.g.:
; move to the patch north-east of current position:
ask one-of turtles [ move-to patch-at 1 1 ]
; move to an adjacent patch at random:
ask one-of turtles [ move-to one-of neighbors ]
Those are just examples, of course. The code to use will depend on what exactly you are trying to do. If you tell us more in a separate question, we might be able to help you.

simultaneously coordination in Netlogo

I'm currently trying to implement a model in Netlogo where the turtles’ behaviors depend on all of their neighbors.
My point of departure is the coordination game code provided by:
http://modelingcommons.org/browse/one_model/2549#model_tabs_browse_info
According to this model the payoff of for the turtle is determined by introducing a variable which takes the color of neighbor as its value.
ask turtles [
let his-color [color] of one-of turtles-on neighbors
if color = yellow and his-color = yellow [set payoff A-yellow-yellow set alt-payoff B-red-yellow]
However, I need to my turtles to gain their payoff by comparing their color with all of their neighbors simultaneously. The last part is problematic due to Netlogo's default synchronic update
Can anyone guide me in how to make the update simultaneously and depending on all of the neighbors, or does someone have a reference to a place where this is discussed?
Just collect all colors before changing any of them. E.g.,
turtles-own [nbr-colors]
to go
ask turtles [
set nbr-colors [color] of neighbors ;get list of current colors
]
ask turtles [
set payoff compute-payoff nbr-colors
set color anything-you-want
]
end

How to create a fixed number of turtles at a certain point?

This may seem like a very simple question to ask but at present I do not know how to create a controllable number of turtles. In addition to this I do not know how to set the coordinates where the turtles should spawn. Ideally I would like the turtles to spawn on a patch that the user will draw out in the U.I. so that the turtles can then move along the network the user will have drawn in the U.I. Any answers would be greatly appreciated.
Patches use the sprout command
Ask patches [
Sprout 5 [set color red ]
]
Asks all the patches to make 5 red turtles.
Netlogo has fantastic documentation
The netlogo dictionary in help is insanely useful.
Or just ask one
Ask patch 4 5 [sprout 1]
create-turtles takes in the number of turtles to create. It also allows you to pass in a block of code that will then be run by the created turtles. So you can do:
create-turtles 50 [
setxy 3.5 4.7
]
This will create 50 turtles at the coordinates (3.5, 4.7).

Maintaining two states of turtles netlogo

I am doing a small university project. In which i have to maintain 2 states of turtles.
1. Disperse
2. Explore
Disperse :
In dispersion, when at the start all the turtles (20 turtles) are at 0,0 they should disperse from each other. Every turtle has a radius of 2 patches around it, no other turtle should be in that radius. All turtles should go far until they all attain this radius. then other behavior will be called i.e. Explore.
Explore:
In explore , they have to explore the world and avoid different types of obstacles. When ever two turtles come close to each other above mentioned radius then state should be changed to disperse.
I have procedures for obstacle avoidance, and move-speed, and all other individual behaviors under Disperse and Explore. But i don't know how to join all this in one simulation.
It is unclear that you really need to maintain turtle state, since you will have to repeatedly check for other turtles in any case. But since you said you wanted that, you can use turtles-own. For example:
turtles-own [state]
to setup
ca
crt 20
end
to go
ask turtles [set-state]
ask turtles [move]
end
to set-state ;;turtle proc
ifelse (any? other turtles in-radius 2) [
set state "disperse"
] [
set state "explore"
]
end
to move ;;turtle proc
if (not member? state ["disperse" "explore"]) [
error "unknown state"
]
if (state = "disperse") [
disperse
]
if (state = "explore") [
explore
]
end
to disperse ;;turtle proc
move-to one-of patch-set [neighbors] of neighbors
end
to explore
move-to one-of neighbors
end
You might want to take a look at Moore machine and Automata, NetLogo works great with those.
A Moore machine can be seen as a set of 5 elements that interact with each other, in this particular example the start state(S0) would be Dispersing. In NetLogo you can use the word run that receives a string. You'd had to make a procedure that returns a string (say "explore") by checking the actual state of a turtle.
I made something like that a few months ago. We were trying to make a hunter-prey model for polar bears and seals (or wolves and sheeps) based on Moore Machines.
You can use the example of #Alan of course, I just skimmed through and I believe it was fine.
Here is my example based on Moore Machine. It's in spanish but the idea is the same.