Identifying subgroups in a flocking Netlogo model - netlogo

I am working on a flocking model with subgroups of agents that form temporary aggregations. I would like to quantify these subgroups by identifying them with a unique ID and a color. Thus I included the following subprocedure (chain-rule) in the go procedure, right before the tick:
to chain-rule
clear-links
ask turtles [
create-links-with other turtles in-radius distance-chain-rule
ask link-neighbors [
set subgroupID [subgroupID] of myself
set color 5 + 10 * subgroupID
]
]
end
This procedure is correctly linking only those agents within a certain distance, and because links die, it only links agents within what I want to call a subgroup (which is a temporary aggregation). The problem is the labelling: if they just acquire the subgroupID of their neighbors, eventually they all end up with the same subgroupID (and color), regardless of whether the agents still belong to the same temporary aggregation. How to update this labelling just like the links? Thanks in advance.

This is the solution I found with help by JenB, using the network extension's nw:weak-component-clusters. This procedure identifies, on each tick, the weak components of a network of agents linked by a given distance.
extensions [ nw ]
to chain-rule
clear-links
ask turtles [ create-links-with other turtles in-radius distance-chain-rule ]
set subgroups nw:weak-component-clusters
show subgroups
end

Related

How to kill and regenerate turtles every set number of ticks

The model I am working on simulates a group of workers working on a team project. I am trying to add a on/off switch titled "replacement." The intended outcomes are:
When it is on:
30% of existing turtles of breed employees die, and equal number of new turtles are introduced.
When it is off:
No turtles die, and all turtles generated at setup will work on the project from start to finish.
The purpose of killing off the turtles is the turtle-own variable "skills." I am trying to simulate a scenario where people resign from a company and new people are hired, which will result in a change of skill level.
If it is easier to change the skill level of 30% of turtles every 20 ticks instead of killing/regenerating, that would be perfect, but I was unable to figure it out.
My attempt at this problem is shown below. It runs without any errors, but I confirmed that it's not killing/respawning 30% of turtles every set number of ticks. Any helpful guidance will be much appreciated.
to go
if all? patches [ workload = 0 ] [ stop ]
ifelse replacement [hire] [continue]
recolor
tick
end
to hire
if ticks > 0 and ticks mod 20 = 0 [
ask n-of (count employees * 0.30) employees [die]
create-turtles number_of_workers * 0.30 [
set breed employees
setxy random-xcor random-ycor
set shape "person"
set color black
set size 1
set skills random-float 1]
]
ask employees [move]
ask leaders [move]
end
to continue
ask employees [move]
ask leaders [move]
end
I ended up finding the answer on my own. Removing "ticks > 0 and" from the hire function solved it :)
From your description of the problem, I am not sure why removing the if ticks > 0 and solved it. I am wondering if you perhaps had the "replacement" variable set to false, and the hire procedure would not have been called.
Regardless, you also asked about just changing the skill set every 20 ticks. You could do this:
to go
if all? patches [ workload = 0 ] [ stop ]
if replacement? and ticks mod 20 = 0 [hire]
move-people
recolor
tick
end
to hire
ask n-of (count employees * 0.30) employees
[ set breed employees
set skills random-float 1
]
end
to move-people
ask employees [move]
ask leaders [move]
end
I also reorganised slightly in a way that is intended to make it easier to debug. You had the moving in both the "hire" and "continue" procedures. Clearly you want that to always happen, so that is now explicit in the "go" procedure (and I changed the name to be more descriptive).
Now the "hire" procedure simply changes the skills of existing employees. Note, however, that any other variables (such as leader, size, colour and position) are not affected. If you go this approach, you may want to indicate in some way that they are new hires.
The "hire" procedure is now clearly called when the "replacement?" switch is on and also a multiple of 20 ticks. If you have part of the condition in the calling procedure ("go") and part in the procedure itself ("hire") then it's easy to forget the existence of whichever half you are not looking at.

NetLogo : getting turtles to direct-link if a condition is true

I'm trying to implement YOYO leader election algorithm in netlogo
first step in this algorithm is to orient links ( direct link )from the minimum to the maximumbut between neighbors only !
I tried the command
[`ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors ]`
this creates direct link from min to max ( neighbors ) but also creates a direct link from max to min ( neighbors )
and I don't know what's wrong :(
here's a screenshot , if you notice theres' a direct link from 0 to 2 and also from 2 to 0 and my goal is to have only from 0 to 2
Your problem is that every time you do one-of, it randomly selects. So you test against a random link-neighbor in the first line, find it's true and then randomly select a link-neighbor to connect to.
[ ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors
]
More generally, this seems an odd way to achieve your goal. To start with, link-neighbors are the turtles that the turtle is already linked to. link is the generic name for all link breeds (I think you have created a breed called direct-link).
I am not entirely clear what you mean by minimum and maximum since your code is just from smaller to larger who value, regardless of what other who values are available. If you want to create a link from every turtle to every turtle with a higher who value, here is some code:
ask turtles
[ let targets turtles with [who > [who] of myself]
create-links-to targets
]
In general, it is bad practice to use who in NetLogo code. who is a completely arbitrary identifier that simply tracks the order that turtles are created. If you have turtles that die, then your code may crash because it is referring to a turtle that no longer exists. Or perhaps at some point you will have two breeds of turtles - who doesn't care if your turtle is a person or a dog or a factory or...
This may be one of the very few exceptions, but you might want to think about what you are intending who to mean. For example, as this is a leadership model, perhaps you could have a variable called 'charisma' and all the links are from turtles with lower values of charisma to higher values of charisma.

How to have patches "belong" to turtle

I am brand new to Netlogo and am coding a world with caching animals. They will go to their caches (I set them as blue patches) if their energy value falls below 10. They can find these caches based on a random 'memory' value given to them which is used as an in-radius value. That way, they will face and go towards a cache within their in-radius memory if they are about to die. I am starting to incorporate a home-base system where the turtle remains in a smaller area with their own caches. Does anyone know how I can make a patch belong to an individual turtle? This would allow turtles to have their specific caches in their territory. I have looked into using links and breeds, but links are only between turtles and making the individual breeds for the 50+ turtles at a time seems ineffective and complex. I apologize I don't have any code attempting to have a cache (patch) belong to a turtle, I don't know where to start. Any ideas?
If you want a turtle to remember anything (patches or income or anything else), then you need to assign a variable in a turtles-own statement and then set the value appropriately. Here's some example code fragments. They won't work, and you actual code would likely look a lot different because you will need some design about the conditions under which a cache will be assigned, but they show you what a variable solution looks like.
turtles-own
[ my-caches
]
set my-caches (patch-set my-caches patch-here) ; code when a turtle finds a new cache site
If you want a patch that belongs to a turtle to make that patch unavailable to other turtles, then also set up a patch variable to store its owner.
patches-own
[ my-owner
]
ask turtles
[ if [my-owner] of patch-here = nobody [set my-caches (patch-set my-caches patch-here)]
ask patch-here [set my-owner myself]
]
I suggest you do several NetLogo tutorials, then look at some library models (and understand them) before starting your own model. You need to understand basic concepts like turtles/patches, variables, ticks before trying to build a model.

Updating Simple Preferential Attachment Model in NetLogo

There is a model in NetLogo based on simple preferential attachment. Agents create links thusly:
to go
if count turtles > num-nodes [ stop ]
let partner one-of [both-ends] of one-of links
create-turtles 1 [
set color red
move-to partner
fd 1
create-link-with partner
]
layout
tick
end
In this model, a node's chance of being selected is directly proportional to the number of connections it already has. I want to edit this code so that there is a variable, prestige, which also determines the probability that a node will be selected. I understand that I need to create a variable for turtles called prestige, but I'm stuck on how to make it so that the probability that a link will be created is also proportional to the amount of prestige a turtle has.

Communicating data between agents when they are in common scope in Netlogo

I am developing a model in Netlogo. I have many agents in my model. each agents must have a radio scope. Two agents can communicate with each other and transfer some critical data When they place in a common scope area.
Imagine that i have 100 agents with 5px scope and 200 agents with 3px scope. I have also 1 master agents that move around in the word. This master agent have a scope too(for example 7px) . Agents can communicate with each other when they place in common scope area. When they communicate they can transfer some of their data. At first just master agent have this critical data and just master agent can transfer this important data. But after he transfer its data to the other agents, those other agents that have this data can transfer this data too. The important condition is being in the common scope.
How can I do this?
Thank you
You just gave a very general statement of your problem. You will get better answers if you make the effort to actually start implementing something and ask about more specific difficulties that you are facing.
That being said, I made a small model that more or less fits with your description. Perhaps it could be useful for you as a starting point and you can ask separate (more precise) follow up questions if you have some.
turtles-own [ scope data ]
to setup
clear-all
; make a big world so agents don't
; bump into one another right away:
resize-world -100 100 -100 100
set-patch-size 3
; create turtles and distribute them around:
crt 100 [ set scope 5 set data "" ]
crt 200 [ set scope 3 set data "" ]
crt 1 [ set scope 7 set data "important data" ]
ask turtles [
set size 3
setxy random-xcor random-ycor
recolor
]
end
to go
ask turtles [ travel ]
ask turtles with [ not empty? data ] [ share-info ]
ask turtles [ recolor ]
end
to travel
; you haven't specified how turtles should move
; so here's a classic "wiggle":
rt random 30
lt random 30
fd 1
end
to share-info
ask other turtles in-radius scope with [ empty? data and distance myself < scope ] [
set data [ data ] of myself
]
end
to recolor
set color ifelse-value empty? data [ grey ] [ red ]
end
Edit:
Following Seth's comment that my first version probably didn't capture the idea of a common scope, I've added and distance myself < scope. This way, only turtles that can see each other can share information.
I've also added a with [ not empty? data ] clause when asking turtles to share info, because there is no use in having turtles with empty data share it.