I'm looking for a way to rapidly place turtles at edges of patch clusters. I tried this code but I found that it is a little slow when I increase the size of my study area:
to draw-edges [ID-cluster]
ask patches with [cluster != ID-cluster] [
ask neighbors with [cluster = ID-cluster] [
if not any? turtles-here [
sprout 1 [
set shape "x" ] ] ] ]
end
Thanks in advance for your help.
You're currently asking the whole world outside your cluster to check for neighbors that are in the cluster. It should be faster to ask patches that are part of your cluster to check if they are next to an outside patch:
to draw-edges [ ID-cluster ]
ask patches with [
cluster = ID-cluster and
any? neighbors with [cluster != ID-cluster] and
not any? turtles-here
] [
sprout 1
set shape "x"
]
end
Related
I have turtles with a memory that stores the coordinates of patches they travel:
turtles-own [
memory
]
to setup
clear-all
create-turtles 1 [
set memory (list)
]
end
to go
ask turtles [
right random 360
move-to patch-ahead 1
set memory (lput patch-here memory)
]
end
From this list, I want to ask something to any patches that have these coordinates and are white. I was trying:
ask patches at-points memory with [pcolor = white] [do-something]
But getting the error
Invalid list of points [(patch 0 1)]
How can this be fixed?
Fix it with:
set memory (lput ([list pxcor pycor] of patch-here) memory)
Your answer works so feel free to ignore this. I just thought I'd offer you a few more options.
The problem with your original code is that you are not storing coordinates in a list but rather are storing patches in a list.
To work with agents that you have stored in a list, you will need to use foreach. This allows you to cycle through all agents in the list and ask them indidivually to change their color.
to change-pcolor-list
ask turtles [
foreach memory [
the-patch -> ask the-patch [set pcolor white]
]
]
end
Another option is to store all the patches a turtle passes through in a turtles-own agent-set. This allows the turtles to call upon all the patches simultaneously instead of patch by patch and allows you to bypass the need for using anonymous procedures (which is what foreach uses).
turtles-own [
memory-2
]
to setup
clear-all
create-turtles 1 [
set memory-2 (patch-set)
]
end
to go
ask turtles [
right random 360
move-to patch-ahead 1
set memory-2 (patch-set memory-2 patch-here)
]
end
to change-pcolor-agentset
ask turtles [
ask memory-2 [
set pcolor white
]
]
end
I'm trying to have agents go the most efficient possible route to their destination (randomly selected from a patchset of 10 patches). The problem is, distance seems to be an agent-only command, and the only code I've found that does anything similar seems to work by running the distance command with patches, which gives an error.
This is the troublesome procedure:
to-report best-route
let visible-patches patches in-radius turtles-vision-dist
let visible-routes visible-patches with [ pcolor = gray ]
let routes-that-take-me-closer visible-routes with [
;;THIS IS THE PROBLEM LINE RIGHT BELOW HERE
[ distance visible-routes] of destination < [ distance destination - 1 ] of myself
]
In the example I had found, Paths, this was the code:
to-report best-way-to [ destination ]
let visible-patches patches in-radius walker-vision-dist
let visible-routes visible-patches with [ pcolor = gray ]
let routes-that-take-me-closer visible-routes with [
distance destination < [ distance destination - 1 ] of myself
]
ifelse any? routes-that-take-me-closer [
; from those route patches, choose the one that is the closest to me
report min-one-of routes-that-take-me-closer [ distance self ]
] [
; if there are no nearby routes to my destination
report destination
]
end
I had originally had something more similar, but it wasn't working, so I've been playing around with it with no luck, as have a classmate and my professor.
I can't figure out how your paths algorithm is supposed to work since it doesn't look like you are restricting movement to grey patches. However, assuming your destination is stored as a patch, then you can can calculate distance to that patch directly with distance destination, like this:
turtles-own [ destination ]
to testme
clear-all
create-turtles 1
[ set destination one-of patches
ask destination [ set pcolor blue ]
]
ask one-of turtles
[ print distance destination
]
end
How can I code to know how many patches are in my world that have yellow neighbors? I'm using the following line:
ask patches [ show count neighbors with [pcolor = yellow] ]
The result is a long list of each patch answering the question, but all that I need is the number of patches that fulfill the mentioned condition. Thanks for any help.
I think you want
print count patches with [ any? neighbors with [ pcolor = yellow ] ]
I want to select the network neighbours filtered by a link attribute rather than a turtle attribute. For example, this might be selecting only the closest friends based on a strength score on the friendship link. I could do this by having different link breeds for close friends, but this would require constantly changing breeds as the condition was or was not required.
Below is an example with a boolean condition.
links-own [flag?]
to testme
clear-all
ask patches [set pcolor white]
create-turtles 20
[ setxy 0.95 * random-xcor 0.95 * random-ycor
set color black
]
ask n-of 3 turtles [set color red]
repeat 40
[ ask one-of turtles
[ create-link-with one-of other turtles
[ set flag? random-float 1 < 0.4
set color ifelse-value flag? [red] [gray]
]
]
]
; colour-neighbours
colour-neighbours2
end
to colour-neighbours
ask turtles with [color = red ]
[ ask my-links with [flag?]
[ ask other-end [ set color blue ]
]
]
end
to colour-neighbours2
ask turtles with [color = red ]
[ ask turtle-set [ other-end ] of my-links with [flag?]
[ set color blue ]
]
end
I am currently doing the equivalent of colour-neighbours, but it involves stepping through several contexts. The colour-neighbours2 version is conceptually closer because it is referring directly to the network neighbours. However, because of the of, I get a list of neighbours that I then have to convert to an agentset.
This is for teaching and, while both work, they seem very convoluted when compared to the unconditional network neighbourhood with the link-neighbors primitive. That is, if I didn't care about the flag, I could simply say ask link-neighbors [ set color blue ].
Is there a more direct way to identify network neighbours conditional on a link attribute?
You already cover most possibilities. Another way to do it would be:
to colour-neighbours3
foreach [ other-end ] of my-links with [ flag? ] [ t ->
ask t [ set color blue ]
]
end
I would avoid colour-neighbours2 because, as you stated, it requires the conversion from a list to an agentset. Whether you should use colour-neighbours or colour-neighbours3 is, I think, a matter of personal preference.
Just to add a more-convoluted (worse?) option that uses an agentset:
to colour-neighbours4
ask turtles with [ color = red ] [
let flag-links my-links with [flag?]
ask link-neighbors with [ any? my-links with [ member? self flag-links ] ] [
set color blue
]
]
end
I have two turtle breeds who populate each sides of the window and then only move round in there own side.
The problem I am having is that I want to constantly check to see if one singular instance of a turtle from each breed are both on the same y coordinate. And if this returns true i want both of those turtles to stop, but for all other turtles from each breed to carry on moving. I know you can identify a turtle by there unique ID but i don't know how to use this and how to use the correct syntax.
The best way to describe this in pseudo code would be
ask turtles [
if breed1 turtle ycor = breed2 turtle ycor
[ stop breed1 turtle and breed2 turtle ] ]
UPDATE
Tried getting the code to work but still nothing happening. Not sure if it is the way the procedure is wrote or the number I have chosen for the threshold.
to move-turtles
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ycor] of myself) < 1 ]
[
ask redteam with [pcolor = green - 3] [
right random 360
forward 1
]
ask redteam with [pcolor != green - 3] [
back 1
]
ask blueteam with [pcolor = green - 2] [
right random 360
forward 1
]
ask blueteam with [pcolor != green - 2] [
back 1
]]
]
end
Note that "same coordinate" is actually somewhat ambiguous. If one turtles ycor is 5.0000001 and another's is 5.0000000, are they at the same coordinate? Because of this, you should check to see if their coordinates are within a certain amount of each other.
Also, the best way to stop moving is to simply not move. So, here is a possible go procedure that would do what you want:
to go
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ ycor ] of myself) < threshold ] [
move ;; replace with your move procedure or code
]
]
end
Here, each turtle checks to see if there are any turtles of a different breed who's ycor is within threshold of their own ycor. If there are not, then it moves. Otherwise, it does nothing.
The myself stuff is the most confusing part here, so I recommend reading the docs.