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
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 would like to make things with patches in-radius but excluding the patch with the agent itself, the center patch, so I modify the Myself example from the model library:
to splotch
ask turtles [
ask one-of patches in-radius 2 with [not any? turtles-here] [
set pcolor [ color ] of myself
]
]
tick
end
but this code also excludes other patches with turtles so it should be something like
to splotch
ask turtles [
ask one-of patches in-radius 2 [not self][
set pcolor [ color ] of myself
]
]
tick
end
But this code isn't working and I don't figure out how it has to be.
You need the other primitive. However, other excludes agents of the same type and you are wanting a turtle to exclude a patch. So, you need to get the relevant patch to ask the other patches. Here's one approach:
to testme
clear-all
create-turtles 3 [setxy random-xcor random-ycor]
splotch
end
to splotch
ask turtles
[ let mycolor color
ask patch-here
[ ask other patches in-radius 4
[ set pcolor mycolor
]
]
]
end
If you want something more like the way you were doing it, you can create a local variable to store the patch and then exclude it like this:
to splotch
ask turtles
[ let mypatch patch-here
ask patches in-radius 4 with [self != mypatch]
[ set pcolor [color] of myself
]
]
end
How do I get a patch set that contains all patches that the turtle is facing?
I know patch-ahead report the patch with a specific distance. But what if I want to get all patches in this direction instead of the single one with specific distance?
What you can do is hatch a turtle and move it forward until it reaches the edge of the world, adding all the patches it crosses.
Here's a visible version to see the approach:
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set pcolor red
hatch 1
[ while [can-move? 1]
[ forward 1
set pcolor red
]
die
]
]
end
To actually do the patchset version, you need to start with the current patch and add the patches as the hatched turtle moves over them. Try this for a procedure version and a demonstration of how it can be used:
turtles-own [ my-path ]
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set my-path get-patches-forward self
print my-path
]
end
to-report get-patches-forward [ #me ] ; turtle procedure
let front-patches patch-here
hatch 1
[ while [can-move? 1]
[ forward 1
set front-patches (patch-set front-patches patch-here)
]
die
]
report front-patches
end
This will return the wrong answer if the world is wrapped because the hatched turtle can keep on going indefinitely. Instead, you would need to check its coordinates rather than relying on the can-move? primitive.
I want to place turtles on each of the black patches(below Figure) such that there is no gap between turtles at all:
Code I use right now:
ask patches with [pcolor = black][sprout-dead-turtles wall-agents [set color red]]
This gives the following result:
But I want to place turtles in between each of the two patches as well. So that I can cover the showing black part.
Note: Changing the shape of turtles is no use to my purpose though it would cover the black area. My aim to create a replusion force field from these agents and gaps in between are loop holes from where agents may escape.[Somewhat similar to agents bouncing back on a wall].
Here is a fun solution:
breed [ dead-turtles dead-turtle ]
to setup
ca
; draw the background:
ask patches with [ abs pxcor != max-pxcor and abs pycor != max-pycor ] [ set pcolor grey ]
ask patches with [ pycor = max-pycor and abs pxcor <= 1 ] [ set pcolor white ]
set-default-shape dead-turtles "circle"
; sprout a first set of turtles:
ask patches with [ pcolor = black ] [
sprout-dead-turtles 1 [ set color red ]
]
; create temporary links between these, and use the
; links to place a new set of turtles in between:
ask dead-turtles [
create-links-with turtles-on neighbors4
]
ask links [
let target end2
ask end1 [
hatch 1 [
face target
fd distance target / 2
]
]
die ; remove the link
]
end
I'm not saying that it is the only possible solution, but it's simple enough, and it works. (World wrapping has to be turned off, though, which I assume is the case.)
I'm trying to model the colors of bulletins in a mobile bulletin board with NetLogo. I'm able to have the bulleting change their colors when they meet but the color change is random and sometimes bulletins having the same color are touching or close together in my radius I'd like to have the bulletins have a unique color in a given raduis.Here is a fraction of my code.Can anyone help me out?
to color-bulletins
ask bulletins [
ask other bulletins in-radius 2[
ask one-of bulletins [ set color green]
ask one-of bulletins [ set color white ]
ask one-of bulletins [ set color yellow]
ask one-of bulletins [ set color blue ]
]]
end
Here is one way to do it:
breed [ bulletins bulletin ]
to setup
ca
create-bulletins 1000 [ setxy random-xcor random-ycor ]
end
to color-bulletins
ask bulletins [
let used-colors [ color ] of other bulletins in-radius 2
let available-colors filter [ not member? ? used-colors ] base-colors
set color ifelse-value (length available-colors > 0)
[ one-of available-colors ]
[ one-of base-colors ]
]
end
This assumes that you want to use only the base-colors and that they could all be used already, in which case you'd still get a "color collision", but there is nothing you could do about it. Unless the spatial distribution of your agents is fairly dense, tough, it should not happen too often.