Netlogo: How to create links to turtle's two immediate neighbours? - netlogo

I want to ask all turtles to link with their two immediate neighbors.
Below is my code, but all of them are wrong.
ask turtles [ create-links-with n-of 2 other turtles] ;; should link with 2 immediate neighbours rather than 2 random turtles
ask turtles [ create-links-with n-of 2 neighbors] ;; Run mistake: CREATE-LINKS-WITH expected input to be a turtle agentset but got the agentset (agentset, 2 patches) instead.
ask turtles [ create-links-with min-n-of 2 turtles [distance myself] ] ;; Run mistake: A turtle cannot link to itself. Error while turtle 10 running CREATE-LINKS-WITH
Any one any idea how to fix my code? Thank you very much!

If you use [create-links-with min-n-of 2 turtles...], the min-n-of 2 turtles part makes the turtle executing the command count itself as one of the 2 target turtles. So the error
A turtle cannot link to itself. Error while turtle 10 running
CREATE-LINKS-WITH
occurs since you want to create links with immediate 2 turtles.
Try:
min-n-of 2 other turtles
Here, other gives all the turtles except the turtle executing the command.
Overall: create-links-with min-n-of 2 other turtles [distance myself]

Related

Different types of turtles in a lattice grid

In Netlogo there are some default models, including the lattice grid.
I am wondering how to add three types/breeds (40%, 40%, 20%) of turtles connected to each others only at the beginning, then, when tick > 0, some new links can be created between turtles of different type.
In the models library in Netlogo, I found the code for a regular lattice:
to setup-square
clear-all
ask patches [ sprout 1 ]
ask turtles [ create-links-with turtles-on neighbors4 ]
reset-ticks
end
Instead of turtles, I should consider the three types (types1, types2, types3) in order to have a lattice with:
40% of turtles as breed type1 (red);
40% of turtles as breed type2 (blue);
20% of turtles as breed type3 (white).
Initially, all the turtles above are connected to other turtles of the same type. Links between turtles may change through time, so links between turtles of different breeds can exist when tick>0.
I would do as follows (instead of ask turtles [...]):
ask types1 [ create-links-with types1-on neighbors4 ]
ask types2 [ create-links-with types2-on neighbors4 ]
...
but the problem is that I have no control on the proportion of turtles for each type. Also, a condition on the breed of the neighbors of a turtle should be considered as well.
How can I set the proportion of these types and make connections only between turtles on the same type, then mix links?
There are several ways to do this, some of them more exact than others. It is especially important to realise that you can change a turtles breed after it has been created, simply by asking it to set breed <breed>.
In the first example, I give all turtles type1 as they are created, and then change a predefined number of them to another type. Keep in mind how you get that number as rounding errors can have an effect here (see further down for the counts I had). It is important that you give them all a breed at first, as this version will not work with ask n-of <percentage> turtles [set breed <breed>]. That is because the agentset turtles will always contain all turtles, even those who you have given a different breed.
breed [types1 type1]
breed [types2 type2]
breed [types3 type3]
to setup-shape
set-default-shape types1 "default"
set-default-shape types2 "x"
set-default-shape types3 "square"
end
to setup-1
ca
setup-shape
ask patches [sprout-types1 1 ]
let total count turtles
ask n-of (total * 0.4) types1 [set breed types2]
ask n-of (total * 0.2) types1 [set breed types3]
end
A second version that works with a more sochastic approach is to let each newly created turtle choose random number, and give it a breed depending on the number. Keep in mind that the proportions will vary more, as they are determined by chance rather than exact numbers.
to setup-2
ca
setup-shape
ask patches [ sprout 1 [
let number random-float 1.0
show number
(ifelse number < 0.4 [set breed types1]
number < 0.8 [set breed types2]
[set breed types3])
]
]
end
The counts I had of the different versions for each type. Notice that in version 1, type1 and type2 have a different count because I let all turtles start with the breed types1.
to count-types
;standard world of 33x33
show count types1 ; version 1: 437 , version 2: 454
show count types2 ; version 1: 435 , version 2: 432
show count types3 ; version 1: 217 , version 2: 203
end

How to create a given number of turtles based on a distance criterion

We're creating code and we're having trouble doing the following:
We would like to create a specific number of turtles (for example 100 turtles) based on the following criteria:
distance between agents must be greater than or equal to 2
We've already tried:
to setup
ask n-of 100 patches [
if not any? turtles in-radius 2 [
sprout-turtles 1 [ setup-turtles ] ]
]
]
end
to setup-turtles
set energy 0
set size 1
set color orange
end
it didn't work, because less than 100 agents are being born even though the world holds the required amount of agents, which in this case is 100
Does anyone have any suggestions on how we can resolve this issue?
Thanks in advance
The approach you outline is running into issues because you are asking patches to sprout turtles if they meet some condition. Because the patches are operating in a random sequential order, you are selecting some patches that, by the time it is their turn to act, no longer fulfill the condition to sprout since other nearby patches have already sprouted a nearby turtle.
One option is to use a while loop to keep trying to sprout turtles until the desired number is reached:
to setup
ca
while [ count turtles < 100 ] [
ask one-of patches with [ not any? turtles in-radius 2 ] [
sprout 1
]
]
reset-ticks
end
Be careful with while loops- if you do not code such that the while condition can eventually become false, your model will run forever stuck in the loop.
Another option that will give a more explicit error if it fails would be to just create your number of turtles, then have them move to a space that fulfills the condition:
to setup-2
ca
crt 100 [
move-to one-of patches with [ not any? turtles in-radius 2 ]
]
reset-ticks
end

Comparing patches

I want to compare the patches in a certain radius regarding the amount of a certain class of agents on them. The agents should move to the patch where the most agents (in this case humans) are. If they are already on the patch with the most humans then they must not move. I coded it and the humans group but most of them don't stay and run around in lines (one behind the other). Would be great if anyone of you could have a quick look at my code. Thanks
if Strategy = "Gathering-Simple" [
if ((count(humans-on max-one-of patches in-radius rad [count(humans-here)] )) ) >= count(humans-here) [
if count(humans-on patches in-radius rad) - count(humans-here) > 0 [
face max-one-of patches in-radius rad [count(humans-here)]
fd 1
]]
]
This is a complete working example that uses your code. Is this displaying the behaviour you mean? It does have turtles chasing each other.
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
to go
let rad 5
ask turtles
[ let target-patch max-one-of patches in-radius rad [count turtles-here]
if count turtles-on target-patch >= count turtles-here ; comment 1
[ if count turtles-on patches in-radius rad > count turtles-here ; comment 2
[ face target-patch
forward 1
]
]
]
tick
end
If so, have a look at the two lines I have comments on.
Comment 1: The >= means that, even if the turtles are already on the highest count patch, this condition will be satisfied because count turtles-here will be equal to the count of the turtles on the highest count patch (this patch).
Comment 2: This line means that as long as there are any turtles on any patch within the radius but not on the particular patch where the asking turtle is located, then the turtle will move forward.
If you want to only have turtles move if not on a maximum count patch, try this instead:
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
to go
let rad 5
ask turtles
[ let target-patch max-one-of patches in-radius rad [count turtles-here]
if count turtles-on target-patch > count turtles-here
[ face target-patch
forward 1
]
]
tick
end
I took out the = in the comment 1 line and removed the second condition entirely so now the turtles move if their current patch has fewer (strictly, not <=) turtles than the patch they've spotted.
I agree with the previous post, but have some additional information.
If you want to move entirely to the target patch on each iteration, instead of moving just one step towards the target patch, in the above answer you could replace the code which produces one step of motion
[ face target-patch
forward 1
]
with
[
move-to target-patch
]
I confirmed by experimentation that the results of the two methods of moving will produce similar but somewhat different results.

define neighbor turtles using in-radius or distance

I would like to define neighbors using in-radius or distance. My solution so far is:
turtles-own [
my-neighbors
num-neighbors]
to setup
ca
crt 100 [
move-to one-of patches with [ not any? turtles-here ]
set my-neighbors (other turtles) in-radius 3
set num-neighbors count my-neighbors
]
end
The problem with this is that most of the turtles have between 0 to 4 neighbors, but a few of them have a relatively huge number of neighbors (e.g., 34 and 65). Those turtles are located close to the center of the world.
Any ideas about what I am doing wrong?
It has to do with the timing of side effects in your program.
Suppose that the very first turtle to move moves near the center. None of the other turtles have moved yet, so they're all still on patch 0 0 and set my-neighbors (other turtles) in-radius 3 will capture them all. And even after they move elsewhere, they will still be included in the first turtle's my-neighbors agentset.
You can avoid the problem by first moving all the turtles and then calculate their neighbors:
to setup
clear-all
crt 100 [
move-to one-of patches with [ not any? turtles-here ]
]
ask turtles [
set my-neighbors (other turtles) in-radius 3
set num-neighbors count my-neighbors
]
end

How to set follower follow any leader nearest to them in NetLogo

I really need some advice on this, I try to create few leaders and some amount of followers, however it seems that followers do not follow any leader, how do I make a follower follow the nearest leader to them.thanks
turtles-own
[
is-leader?
follower
]
to setup
clear-all
reset-ticks
ask n-of 50 patches
[sprout 1
[set color blue
set follower self]]
choose-leaders
end
to choose-leaders
ask max-n-of 5 turtles [count turtles in-radius 3]
[
set is-leader? true
set color white
]
end
to go
ask turtles [follow_leader]
tick
end
to follow_leader
ask follower
if any? is-leader? in-radius 30
[ set heading (towards min-one-of is-leader? [distance self]) fd 1]
end
It's a bit hard to make sense of what you are trying to do with the follower turtle variable. With the code you posted, all turtles have themselves as follower, which I am almost certain is not right. In other words, currently the variable doesn't do anything, and you can delete it unless you want to do something else with it.
Regardless, the problem with your code is in your follow_leader procedure. This will work - I added comments so you can see what
to follow_leader
ask follower
;; since all turtles are asked to run this procedure, and all turtles have themselves as
;;follower, this asks turtles to ask themselves to do something.
if any? is-leader? in-radius 30
;; this is incorrect syntax.
[ set heading (towards min-one-of is-leader? [distance self]) fd 1]
;; 'self' refers to the turtle itself, so this will always return 0, and turtles will face themselves
end
Given these errors, this is probably what you want:
to go
ask turtles with [not leader?] [follow_leader];; we only want to ask turtles that are not leaders
tick
end
to follow-leader ;; changed just for NetLogo convention
let nearby-leaders turtles with [leader? and distance myself < 30] ;; find nearby leaders
if any? nearby-leaders [ ;; to avoid 'nobody'-error, check if there are any first
face min-one-of nearby-leaders [distance myself] ;; then face the one closest to myself
fd 1
]
end
Make sure to initialize the leader? boolean in all your turtles. Add set leader? false in the command block that you send to turtles that you sprout in your to setup procedure.