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

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.

Related

Tell agent to not cross a road in Netlogo model

I'm trying to add a condition that doesn't allow an agent to cross over a road. The road patches are set to color red. I can't seem to figure out how to get this condition to work. I ultimately want the agent to turn around if the road is in the patch ahead. Here is my net logo code so far.
to go
ask turtles [
move
]
tick
if ticks >= 60 [stop]
end
to move
ifelse random-float 1 < q
[
ifelse random-float 1 < w
[let target-patch max-one-of neighbors [veg-suitability]
face target-patch]
[let target-patch max-one-of neighbors [pelev]
face target-patch]
]
[
ifelse [pcolor] of patch-ahead 1 = red
[lt random-float 180]
move-to one-of neighbors
ldd-normal
]
end
to ldd-normal
let ldd-distance (ldd-scale)
fd ldd-distance
end
The logic of your move procedure is a bit confused I think. First you have a random chance to either move to a patch with a higher value of a variable of interest (with the uphill primitive) or, if the random draw fails, it moves to a random neighbour. If you don't want it to move onto a red patch then you need to test if the patch that is chosen is red, but you just move it without checking.
After you have moved the turtle, you then check the colour of patch-ahead. Your problem here is that patch-ahead depends on the direction the turtle is facing, which has nothing to do with the direction it has already been moving. You either make it turn (though it may not turn enough) OR move forward. So it never actually moves away.
I can't give you an actual answer because I don't know what your logic is supposed to be. But you could look at structures like:
move-to one-of neighbors with [pcolor != red]
Or, if there are enough red patches that it is possible that there aren't any non-red neighbours (which would cause an error if you tried to move to one), you could use:
let okay-patches neighbors with [pcolor != red]
if any? okay-patches [move-to one-of okay-patches]
Another option is that you only meant to face rather than move to the patch in the first sections of code, then test whether it is facing a red patch and turn around if it is.

NetLogo - turtles to imprint 'who' on patches and only move-to patches which match their 'who'

I'm working on a land-use model featuring a forested World where turtles (smallholders and companies) have the ability to convert forest into crop-land. I would like to introduce a feature that turtles 'own' the patches they convert and are able to revisit them later to get these patches certified. The main issue is that when turtles move-to crop-land patches to get them certified, they do not only move to those they 'own' but also jump across the world to other turtles' crop-land patches and certify those. I've tried a few different workarounds, but I seem to run into the same two issues eventually:
#1 - error: can't use who in a patch context
I wanted to use the 'who' variable to mark crop-land patches as belonging to the turtle that converted the patch, e.g., turtle 0 goes to the forest, converts it to crop-land and that patch of cropland should be 'owned' by turtle 0, i.e., the patches owned-by variable should be equivalent to the turtle's 'who'. The issue here is that 'who' is a turtles-own variable. So, when I use it in a patch-context it produces an error. For example, ask smallholders [move-to one-of patches with [[owner = who]] --> error.
#2 - can't manage to set a global variable = 'who'
Two, I tried to work around this by using a proxy variable: a globals-variable called 'owner-ID'. I would use set owner-ID who to imprint the turtles individual number to the owner-ID. This seems to work to some extent, namely that the patches' 'owner' variable corresponds to the turtle that converted the patch. It also works when counting how many patches of certified and conventional crop-land turtles own (see set-land-ownership command below). However, when the smallholders-certify-crop-land commands are triggered, turtles don't stick to the patches they own, but 'jump' across the world. When prompting turtles through the command-center ask turtles [print owner-ID] they all return the same owner-ID value. I feel there might be a mistake in my move-to command-line but I just can't find it.
Summary & Question
I want crop-land patches to be 'owned by' the turtles that converted them, and want turtles to move only to the patches they 'own' when certifying crop-land patches, not to patches they don't own. I guess my questions revolve around whether it's possible to somehow use the 'who' variable in a patch-context. And, if not, what a good workaround for the problem could look like.
Relevant code is below (I hope)!
globals [owner-ID]
turtles-own [conventional-land-ownership certified-land-ownership]
patches-own [owned-by owner certified?]
to setup [
ask patches [
set pcolor green ;; green = forest
set certified? "no"
set owner "nobody"
]
]
to go
ask turtles [set-land-ownership]
ask smallholders [check-smallholder-status]
tick
end
to set-land-ownership
ask smallholders [
set owner-ID who
set conventional-land-ownership count patches with [owner = owner-ID and certified? = "no"]
set certified-land-ownership count patches with [owner = owner-ID and certified? = "yes"]
]
end
to check-smallholder-status
if wealth >= 0 and (conventional-land-ownership + certified-land-ownership) < SH_max-land-ownership [
smallholders-choose-activity
]
if wealth < 0 [
set color red
set shape "cow skull"
]
if (conventional-land-ownership + certified-land-ownership) >= SH_max-land-ownership [
set color orange + 2
]
end
;; smallholders-choose-activities is a reporter-based command where turtles choose the most economical option available. One of the outcomes is: smallholders-certify-crop-land
to smallholders-certify-crop-land
let available-patch max-one-of patches with [owner = owner-ID and certified? = "no"] [count neighbors with [certified? = "yes"]]
ifelse not any? turtles-on available-patch [
move-to available-patch
]
[]
set wealth wealth - smallholder-certification-cost
set pcolor brown + 1
set certified? "yes"
end
Your first approach is definitely the way to go and could be fixed with one small adjustment.
ask smallholders [move-to one-of patches with [owner = who]]
should be
ask smallholders [move-to one-of patches with [owner = [who] of myself]]
Within the block after with, variables are in the context of patches, but myself refers to the agent that asked the patches to check their owner, in this case, each smallholder. The global variable owner-ID is then unnecessary. If you carry this through the rest of the code, your second problem may solve itself.
BUT, in general it is best not to use who numbers at all, but rather refer to the agent directly. (You have actually taken that approach implicitly when you initialize owner to nobody, which is "no agent".) I don't see where you ask a patch to set its owner, but if a smallholder is on a patch, the smallholder would
ask patch-here [set owner myself]
and the line above would now read
ask smallholders [move-to one-of patches with [owner = myself]]
The NetLogo gurus suggest that we use who numbers only when there is no other approach.

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]

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

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