How to make n-of turtles move to the same place every loop? - netlogo

I want to specify the number of turtles to go and back the same place every of my loop.
For example, in day1, some of turtles born in place1 and some of turtles born in place 2 and turtles its selves know the place where they want to go and come back to the same place where they born. For day2, turtles its selves still go the same place and go on like this every day.
Anyone have any ideas or suggestions?

It's a little hard to work out what you are asking but hopefully this is close and will give us a starting point to work out what you actually want. This model has a variable for each turtle to remember where it was born, and each tick some (5) of the turtles move back to their home patch. The other turtles move in a random direction.
turtles-own [myhome]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set myhome patch-here
]
reset-ticks
end
to go
let homers n-of 5 turtles
ask turtles
[ ifelse member? self homers
[ move-to myhome ]
[ set heading random 360
forward 1
]
]
tick
end

Related

NetLogo: how to let turtles return after one tick

hope you can help me out! I have a NetLogo question about the following code:
breed [tourists tourist]
turtles-own [satisfaction]
to setup
clear-all
reset-ticks
end
to go
tick
ask n-of initial-number-tourists patches
[
sprout 1 [
set color white
set size 1
]
]
ask turtles
[ let nearest min-one-of turtles [distance myself]
if-else distance nearest < 0.0000001 [ set satisfaction 0 ]
[ set satisfaction 1 ]
]
ask turtles with [satisfaction = 0] [die]
end
The intention is to sprout "initial-number-tourists" at the start of each tick. Then, they calculate their 'satisfaction' based on the if there are turtles close and let the turtles that have a satisfaction of 0 die. Then, the turtles that remain (that are satisfied) will be sprouted again and their satisfaction will be calculated again.
The code is working, however, with unexpected outcomes. What happens is that the turtles are all sprouted, and they all die each tick. The next tick; they are sprouted again and all die again. Even if i set the distance threshold really low like i did in the code I provided.
Hopefully you can help me out!! Kind regards
The problem you are encountering is that the nearest turtle to each turtle is itself! Thus the distance to nearest is always zero and satisfaction will be set to zero for every turtle. What you want is the closest among the other turtles,
let nearest min-one-of other turtles [distance myself]
(Your distance threshold will mean that all turtles will have satisfaction of 1, but I assume that is what you wanted to accomplish in your testing. Since turtles are sprouted at the centers of the patches, a threshold of < 1.0 would accomplish the same thing.)
Minor point: the convention is to put tick at the end, not the beginning of the go procedure.

How to get a patch to count the turtles passing through it

I'd like to have patches count the number of turtles that have stood on them. What would be ideal is a event such as:
if turtle-lands-on-me [add one to count]
because a turtles could leave and come back and be counted twice (which is what I want) and it would avoid counting turtles who stand still twice or more (which I don't want). Is there any way to achieve this?
Thank you!
What you need is a variable for each patch (I am calling it 'landed' below). The following code assumes you want to know about the patch it lands on each time step, but ignores the ones it passes over. It also updates the counts only where the turtle changes the patch, as requested, and labels the patch with the count.
patches-own [landed]
to setup
create-turtles 20
[ setxy random-xcor random-ycor
]
end
to go
ask turtles
[ let old-patch patch-here
set heading random 360
forward one-of [0 0.5 1 3]
if old-patch != patch-here
[ ask patch-here
[ set landed landed + 1
]
]
]
ask patches [set plabel landed]
end
The problem is that a turtle can pass over multiple patches during one time step. You can see this in the example model for those turtles that move 3. If you also want them, you will need to do something like the 'Line of Sight' model in the NetLogo models library.

How would I "target" patches within a certain area?

I have set up agents and nodes to represent people and stores and it is my intention that the agents will "target" the store in their "awareness" space with the highest value ("vulnerability"). I've largely coded what I have so far through trial and error however setting the turtle's target to the patch with the highest value within a 10 unit radius is a hurdle I can't get over. Currently they target the patch with the highest value regardless of its position in the world. Could somebody suggest what I might consider to achieve this please? I have pasted what I have written so far for reference.
Thanks.
breed [shoplifters a-shoplifter]
patches-own [vulnerability]
shoplifters-own [target
awareness]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
setup-stores
end
to setup-stores
ask n-of num-stores patches [ set pcolor lime ] ;; create 'num-stores' randomly
ask patches [
if pcolor = lime
[ set vulnerability random 100
]
]
end
to setup-turtles
setup-shoplifters
setup-target
end
to setup-shoplifters
create-shoplifters num-shoplifters [ ;; create 'num-turtles' shoplifters randomly
set xcor random-xcor
set ycor random-ycor
set shape "person"
set color red
]
end
to setup-awareness
ask turtles [
set awareness
patches in-radius 10
]
end
to setup-target
ask turtles [
set target
max-one-of patches [vulnerability]
]
end
You are on the right track using max-one-of. At the moment, however, you are sending patches as the space to search through to look for the one with maximum vulnerability value, when you really want patches in-radius 10. So you could simply do this:
to setup-target
ask turtles [
set target max-one-of patches in-radius 10 [vulnerability]
]
end
However, this is going to be inefficient because NetLogo will have to first work out which are the patches within the radius. You have already asked the turtles to work this out and assign it to their variable 'awareness'. What you really want to do is therefore:
to setup-target
ask shoplifters [
set target max-one-of patches awareness [vulnerability]
]
end
Note that I also changed ask turtles to ask shoplifters. It is only shoplifters who have the attribute 'target' so you should only be asking them to calculate it. Same thing goes for 'awareness'. At the moment you don't have any other breeds so it's not causing an error, but it is good practice to use the breed, otherwise there is no point in creating it.

Changing Node ID with every Setup in Netlogo

We try to show a simple infection via Netlogo. For our purpose we need to start the infection with the same turtle for several times.
But right now with every setup another turtle begins with the infection. We already tried to work with the Node ID, but unfortunately the ID of the different turtles changes with every setup, too. We are out of ideas but
maybe there is a way to sove this problem I am happy for any answers :)
This is our Code so far:
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "JKnachnamennetlogo.graphml"
end
to spread
if (count turtles with [informed? = true] > .7 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.01)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you a lot.
I am a little unclear so am giving bits of different answers for different situations.
If the turtles are different each time, what do you mean by 'the same turtle'. For example, do you mean the turtle in a particular position? If so, you could select the turtle on the appropriate patch.
If it doesn't matter which particular turtle it is (just that it's the same turtle), then the simplest approach is to set the random-seed. Then every time you run any random process (including choosing one-of the turtles to select the starting infection, or ask turtles to do something), NetLogo will use the same chain of random numbers. Of course, if you are still building your model, then adding new pieces of code that change how many calls are made to the random number generator will lead to a different chain, but rerunning with the same code will give the identical run.
You may need to use with-local-randomness and random-seed new-seed if you want to have some parts actually change.
The problem is that nw does not store the WHO variable this is to avoid conflict with already existing turtles in a model.
A work-around would be assigning each turtle a separate id variable and setting that to who.
turtles-own [informed? id]
in turtles creation asign them each the id thus
set id who
you may want to write a conversion procedure like this
to convert
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [set id who]
nw:save-graphml file-name "JK_nachnamen(id).graphml"
end
and use the copy. Of course you would not use
turtle 74
but
one-of turtles with [id = 74]

How to get connected two or more turtles within radius NetLogo

I have more turtles moving in the world, i would like to get them connected when they are inside an area (circles).
This is an example:
I was trying with a function like this, called in the "go" procedure (tick advanced) but this doesn't work.
Any suggestion?
to connect
ask turtles in-radius radius [
create-link-from myself
create-link-to myself
]
end
If I have understood what you want, this will work
globals [radius]
to setup
clear-all
create-turtles 50 [setxy random-xcor random-ycor]
set radius 5
connect
end
to connect
ask turtles
[ ask other turtles in-radius radius
[ create-link-from myself
]
]
end
The problem is that you have ask turtles in-radius ... but you don't specify the reference point. That is, within a certain distance of what? In my code, I ask each turtle to become the reference point and then ask the turtles within distance of itself to do the linking.