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

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).

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.

Moving turtles towards two different colors of the patch in NetLogo

I have a basic NetLogo question.
I would like to encode the following:
The turtle asks what color the patch is in if it is green, it can walk on the green patches.
I know it is a basic question of NetLogo. But, I'm trying and some errors appear, such as: MOVE-TO expected input to be an agent but got NOBODY instead
Could someone help me understand what I am wrong with, or give any suggestions or even models that do this for me to explore? Every help is welcome. Thanks
globals [ edge-size ]
to setup
clear-all
set edge-size 10
set-patch-size 20
let pcolors []
set pcolors [135 55 105 85]
ask patches [
set pcolor item (random 4) colors
]
crt 1 [
set size 1
set color black
;pen-down
]
reset-ticks
end
to go
ask turtles [
if pcolor = green ;; if own pcolor patch = green
[ move-to one-of patches with [ pcolor = "green" ] ]
end
This code move-to [ veg ] of patch-under-me suggests you have several fundamental misunderstandings about how NetLogo thinks. I recommend that you do some more of the tutorials and look at the Model Library built in to NetLogo to find models that are similar to what you want to do. You also need to build in much smaller pieces and get a piece working properly before moving to the next piece.
What is wrong with move-to [ veg ] of patch-under-me?
[ veg ] of patch-under-me is a variable with values like pink or green. So you are saying 'move to green'
What's more, the colour that it gives back is the colour where the turtle is standing now, not where you want it to go to
You probably want something more like:
move-to one-of patches with [veg = "green"]
So go back to basics. Have just two colours (say red and blue) and move turtles around trying to get onto a red patch. Make that work before doing anything else. Now have only a few red patches, does your code still work? Then you can start adding a bunch of different conditions and colours

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.

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]

NetLogo static turtle

I am new at NetLogo and I have been working on this assignment for school, and I can't seem to find an answer to one question. I do apologize if this has been answered somewhere else before.
I am trying to make two turtles. One turtle has to be moving around the area until it finds the second turtle, whereas the second turtle is staying still. I have been trying different things, and even used to wait time, but it worked for both turtles, which is not what I want.
How do I apply the code just to one turtle out of the two? And how do I make that turtle stay in one position?
Thanks for help!
Responding to your comment- yes it works with any number of turtles if you like. For example:
breed [seekers seeker]
breed [waiters waiter]
to setup
ca
reset-ticks
create-seekers 1 [
setxy random 30 - 15 random 30 - 15
]
create-waiters 1 [
set shape "circle"
]
end
to go
ask seekers [
if not any? waiters in-radius 1 [
rt one-of [ 90 0 -90]
fd 1
]
]
tick
end
If you need them to be the same breed in your model, you can assign turtles-own variables to sort them however you like.