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

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.

Related

Can't run Netlogo code - asking turtles to look around themselves and select the lowest patch variable

I am trying to get my turtles to look around themselves in netlogo and select a patch with the lowest slope variable in radius 2 - and if there isn't one to just select any patch.
I check my code, and it says everything is fine, but when I run it I keep getting this error: this code can't be run by a patch - error while patch X X running ifelse - called by procedure move - called by procedure go - called by button "go"
This is my move code:
to move
ask turtles [ask patches in-radius 2
[ifelse slope < 5
[fd 1]
[move-to one-of patches in-radius 2]
]
]
end
I have already tried downhill and downhill4 but my agents seemed to get stuck at the bottom of the slope and couldn't move anywhere.
Thank you for any help and advice!
Thank you - my code now works!
Because you are asking turtles to ask patches, the code inside the ask patches is run by the patch! A patch can’t use turtle variables, and doesn’t know that you mean to refer to the variables of the turtle that asked the patch.
This is what “of myself” is for. It lets an agent (self) talk to the agent that is telling it what to do. You’ll see stuff like “set heading [heading] of myself” But that’s not what you need here.
we could use an ask patches like you are doing here, but we really don’t want them to do anything, and it’s going to make the code much more complex looking. Anyway, We just want to find a patch that meets the turtle’s needs and move to it.
So instead. We can query the nearby patches using a WITH and store the set of patches found in a “patch set” variable.
If there are any, we can move to one of them.
Else, we can just move forward.
So
To move
Ask turtles
[
;; here the turtle tells patches to test the patch’s own slope
Let good-spots patches in-radius 2 with [ slope < 5 ]
;; are there some patches that pass the test?
If-else any? Good-spots
[ ;; Yes, pick one and go there
move-to one-of good-spots
]
[ ;; No, just move forward
Fd 1
]
]
End
Edit to add : Matteo’s answer correctly identifies the actual requirement, based on the question: move directly to the flattest patch, versus what the code above does, move to one of the flatter patches, if there is one.
Move-to min-one-of patches in-radius 2 [ slope ]
As you noted, This is similar but not identical to
Downhill slope
And neither of these may prevent turtles from getting stuck in a pit. You may need more code to detect pits and jump out of them.
The problem is not about variables but about commands: you are asking patches to run forward or to run move-to, while these are turtles' commands.
Now, for the purpose of the question, there is another issue: you said that you want
turtles to look around themselves in netlogo and select a patch with the lowest slope variable in radius 2 - and if there isn't one to just select any patch
However, even if we ignore the problem about asking patches to run forward or move-to, the structure of your code implies something very different. Your code seems to intend:
Turtles, check if there are any nearby patches with slope lower than a specific threshold;
If there are, move forward in whatever direction you are already facing;
If there are not, move to a random nearby patch.
The solution below is based on the assumption that what you want is what you said, and not what your code seems to imply.
The solution is very simple and only needs min-one-of (check it here):
to move
ask turtles [
move-to min-one-of patches in-radius 2 [slope]
]
end
As a demonstration, consider this full example:
patches-own [
slope
]
to setup
clear-all
ask patches [
set slope random 10 + 5
set pcolor scale-color green slope 5 14
]
create-turtles 10 [
setxy random-xcor random-ycor
set color yellow
]
end
to go
ask turtles [
move-to min-one-of patches in-radius 2 [slope]
]
end
You will see that turtles tend to go to darker patches, that are those with lower slope.

In Netlogo, how do you move a turtle to the other end of its link?

I've created two breeds of turtles in my simulation: one is a regular turtle and the other is a halo that is intended to overlap each turtle. Whenever a turtle is hatched (either created as part of the setup procedure or created with netlogo's hatch function), a halo is also hatched and linked by calling a separate make-halo function.
create turtles turtle-initial-number
;;(all the turtle genes are set here)
if halos-enabled [make-halo]
to make-halo
hatch-halos 1
[ set size sight-radius * 2 + 1
set shape "square"
set color lput 64 extract-rgb color
__set-line-thickness 0.5
create-link-from myself
[ tie
hide-link ] ]
end
Due to some interactions I've implemented, sometimes the turtles and the halos become detached from one another, so I'd like to add a step at the end of each tick where all halos snap back to their turtles where they belong. Is there a way to move the halo or set its coordinates to the turtle at the other end of the link?
The other option is to solve whatever is happening when the disconnect occurs. I have another breed of turtle (people) who can "push" others with this push-away function below. Turtles (and their halos) occupying the 9 squares in front of the person are pushed forward along the same direction at the person is facing. When they are pushed, for some reason the turtle isn't at the centre of the halo anymore.
to push-away
ask people [
let push-dir heading
ask patch-ahead 2
[ask turtles-here
[set heading push-dir
fd 2]
ask neighbors
[ask turtles-here
[set heading push-dir
fd 2]
]
]
]
end
In theory, the tie should link the movements. But to snap the halo to its turtle, you can ask the halo to move-to the turtle. The only trick will be identifying the correct turtle and you haven't shown enough of your code for me to sort out the identification for you.
I suggest you actually add a variable to the halos that records their turtle rather than using a link. If the link has no other purpose, there is no need to create all those extra model entities. You would use it like this:
halos-own [my-owner]
to make-halo
hatch-halos 1
[ set size sight-radius * 2 + 1
set shape "square"
set color lput 64 extract-rgb color
__set-line-thickness 0.5
set my-owner myself ; this is the new line
]
end
to push-away
<all the code you have already>
ask halos
[ move-to my-owner
]
end

Netlogo Reporter Not Reporting

I've made a animal behavior model involving "turtles" and "roads" and I want the model to report back to me when the turtle "crosses" a road. All I want is that it tells me when the turtle moves from a patch that is the grey color to the red color. I've included the code asking it to report this and the program has no issue with the code. To give me a visual representation of what I want it to report, I put a monitor on interface. But it always gives me a "0" for road crossings, even as I can see that my turtle has crossed roads. I would count it by hand, but it's impossible to tell for certain how many road crossings there are and this is for scientific publication. My code is as follows...
turtles-own [
road-crossings
]
to setup
clear-all
;; create turtles on random patches.
ask patch 6 -15 [
sprout 1 [
set color one-of [green]
set size 1
set road-crossings 0
]
]
ask turtles [
if [pcolor] of patch-here = 14.9 [
set road-crossings road-crossings + 1
]
]
reset-ticks
end
to go
ask turtles [
repeat 100 [
repeat 39 [
pen-down
rt random-float 360
lt random-float 360
fd random-float 1.375
]
setxy 6 -15
]
]
tick
end
Any help is appreciated! Thank you!
There are several potential problems with this that I can see.
First, road-crossings is a turtle variable, which is the correct thing to do if you want each turtle to remember how many times it crosses a road. If so, however, the monitor must report sum [road-crossings] of turtles to get the road crossings of all turtles.
Second, which I think is actually your problem: you have the turtle checking whether it crosses the road in the setup procedure rather than the go procedure. The setup procedure is only run at the beginning.
Third, you don't actually have any roads in your example code, but I suspect that's just a failure to create a proper example. I assume that there are patches with pcolor of 14.9 in your real code. If not, though, that would also cause your error. You can make sure by going into the command center and asking count patches with [pcolor = 14.9]

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]

NetLogo: Combine and form a new turtle

I am currently learning NetLogo and I need help. In my model I have same sized 10 turtles which moves randomly. When 2 or more turtles are on the same patch they will combine and form a new turtle with the double size. In this manner, the main rule is max. 5 turtles can combine to each other. And this formation will continue until the there will be 2 turtles (with each contain 5 turtles) remain.
I had created turtles and made them move randomly, but I could not managed to combine them. Can you show me a way to do this? Any help appreciated. Regards.
EDIT: I tried the "in-radius" command unsuccessfully. 5-5 distribution of the turtles (as you can can see from the code, they represent H2O molecules) is vital for the system definition and any other distributions are not allowed in the model.
In detail, when randomly moving 2 H2O molecules meet on the same patch, they will combine to form a new molecule (2H2O). The main rule is as previously mentioned, max. 5 molecules can combine which ends with forming 5H2O. Since, initially there are 10H2O molecules in the system, there will be 2 5H2O molecules at the end.
The code I tried to implement is as follows,
breed [h2o-molecules h2o]
to setup
clear-all
reset-ticks
create-h2o-molecules h2o-num [
set color 105
set sIze .5
set shape "circle"
setxy random-xcor random-ycor
set pen-mode "up"
]
end
to setup-patches
ask patches [set pcolor 0]
show count turtles
end
to set-label
ask patches [
ifelse count turtles-here > 0
[set plabel count turtles-here]
[set plabel ""]
]
end
to move-h2o-molecules
ask h2o-molecules [
let dice random 1000
let change (dice - 1)
forward 2
set HEADING (HEADING + change * 2)
]
end
to go
setup-patches
move-h2o-molecules
ask turtles [rt random 1
fd 0.3]
set-label
tick
end
Thanks for your time and patience. Regards,
Using turtles-here
You don't need to ask patches for turtles-here (as you did to set patches labels). The function runs as well if called by a turtle (and is more efficient when there are more patches than turtles). But take care to use other turtles-here if you don't want to include the calling turtle.
Combine procedure
If you declare
a turtle variable after your breed declaration:
h2o-molecules-own [
turtles-inside
]
(set the variable value inside your create-h2o-molecules)
and your combination limit max-inside as a global variable (use slider widget with 5 as default value)
then the combine procedure can look like:
to combine ;; turtle procedure
; take one turtle from the same patch as a target
; which has turtles-inside low enough to combine with
let target one-of other h2o-molecules-here with
[turtles-inside <= max-inside - [turtles-inside] of myself]
if target != nobody
[
set turtles-inside turtles-inside +
[turtles-inside] of target ;; increase turtles-inside
ask target [ die ] ;; kill the target
set size sqrt turtles-inside ;; increase size
]
end
Stop
You can stop the simulation by
if not any? h2o-molecules with [turtles-inside < max-inside] [ stop ]
Comment
The condition used to select the target turtle is using turtles-here, other and the maximum constraint which is compared to the sum of turtles inside the target and turtles inside the calling turtle (using myself function).