create turtles and move to a position in a radius of another breed agent - netlogo

I would like to create some turtles in a radius of another breed agent.
I have this example code:
breed [ readers reader ]
undirected-link-breed [ rris rri ]
breed [ tags tag ]
to setup
clear-all
set xy-file "locations.txt"
setup-readers
setup-tags
end
to setup-readers
create-readers num-readers [
set shape "circle"
set color white ; means idle state 'red' is active
setxy random-xcor random-ycor
]
end
to setup-tags
create-tags tag-population [
setxy random-xcor random-ycor in-radius 6 of one-of readers
]
end
The line setxy... to place the tags in a circle of distance 6 from one of the readers it does not work, but I do not know how to fix it. I have also tested move-to primitive without good result.
What I would like is to have a population of tags in a radius of each reader and if possible select the number of tags for each reader to be different.

The sprout primitive might get you what you need- instead of creating your tags and then moving them, just have the appropriate patches spawn them directly. For example, with this setup:
breed [ readers reader ]
breed [ tags tag ]
to setup
clear-all
setup-readers
setup-tags
reset-ticks
end
to setup-readers
create-readers 5 [
set shape "circle"
set color white
setxy random-xcor random-ycor
]
end
You can make a population of patches that are within the radius of readers and have however many you like sprout a tag:
to setup-tags
let radii-patches patch-set []
ask readers [
set radii-patches ( patch-set radii-patches patches in-radius 6 )
]
; Code above defines the patch-set of patches within 6 of readers
; Then, just ask 10 of those radii patches to sprout a tag
ask n-of 10 radii-patches [
sprout-tags 1
]
end
If you are doing this on a per-reader basis and you want each to have a different number of tags, you could try something like:
to setup-tags-reader
let n-tag random 5
ask readers [
ask n-of n-tag patches in-radius 6 [
sprout-tags 1
]
]
end
but then set the n-tag value some other way (for example, from another .csv file as you seem to be setting reader location).
Side note- when you post on here try to strip out any code than cannot be directly copied and pasted into Netlogo by other users (eg the 'xy-file' line, the tag-population and num-readers variables)- it just makes things super simple!
Edit
As per your comment- try this option (using the same setup as above)
to setup-tags
create-tags 10 [
move-to one-of readers
rt random-float 360
fd random-float 6
]
end
This just creates tags, moves them to a reader, has them randomly select a direction, then has them step forward a random amount from 0 to 6.
As to how to assign patches to each reader- just make a readers-own variable then have them assign patches to that variable in their setup (eg, set my-patches patches in-radius 6). You could do a similar thing with tags to define a reader-specific set of tags (eg `set my-tags tags in-radius 6). Note that in both cases you can get overlap where two readers share patches/tags- you will have to account for that.

Related

Asking patches with coordinates contained in a list

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

Network modelling using Netlogo

I am new in using NetLogo, so I hope you can help me with this code.
I would like to build a network with two subnetwork, A and B, where A has N nodes and B beta*N nodes, where beta=0.5. Each network should be initialised with five fully connected nodes. I need to add a new node at a time, assuming that each has fixed out-degree k. Once a new node i comes into the network, it links to a randomly selected target node j. The other remaining k-1 nodes should be selected as following: with probability p, i should be linked to a random node of j's; with probability 1-p, i should be connected to another randomly selected node in A.
On the other hand, the nodes in B should be linked (directed link) to each node in A with probability P. P can vary from 0 to 1.
What I already tried is built the two networks with N and alpha*N nodes respectively. But, as I said, I am new in using NetLogo and I am finding many difficulties to build this network that should be really easy in a different programming language, I would be more familiar with.
; Global variables
breed [agents agent]
breed [bagents bagent]
to setup
clear-all
setup-agent
setup-bagent
end
; Defining agents
to setup-agent
set-default-shape turtles "person" ; agent shape
create-agents n-of-agents ; # of agents
[set size 2 ; agent size
set color white
setxy (random-xcor) (random-ycor)
]
; Random Network
ask agents [create-link-with one-of other agents with [not link-neighbor? myself]
ask links [set color white]
]
end
; Defining bagents
to setup-bagent
set-default-shape turtles "circle" ; bagents shape
set beta=0.5
let n-of-bagents beta*n-of-agents
create-bagents beta*n-of-agents ; # of bagents
[set size 2 ; bagent size
set color red
setxy (random-xcor) (random-ycor)
; Network
ask bagents [create-link-with one-of other bagents with [not link-neighbor? myself]
ask links [set color yellow]
]
end
to go
end
I hope you can help me to understand how to build such a network in NetLogo.
Many thanks
This does what you said. I don't think it's actually what you want as your algorithm is much better but still somewhat confused. But hopefully this gets you on the correct path.
UPDATED to make one node add each tick
globals
[ beta
prob
k
]
breed [A-agents A-agent]
breed [B-agents B-agent]
to setup
clear-all
set beta 0.5
set prob 0.2
set k 3
setup-A-seed
setup-B-seed
reset-ticks
end
to go
add-A-node
if random-float 1 < beta [add-B-node]
tick
end
; Defining A seed network
to setup-A-seed
create-A-agents 5
[ set shape "person"
set size 2
set color white
setxy random-xcor random-ycor
]
ask A-agents
[ create-links-to other A-agents
[ set color white ]
]
end
; Defining B seed network
to setup-B-seed
create-B-agents 5
[ set shape "circle"
set size 2
set color red
setxy random-xcor random-ycor
]
ask B-agents
[ create-links-to other B-agents
[ set color yellow ]
]
end
to add-A-node
create-A-agents 1
[ set shape "person"
set size 2
set color white
setxy random-xcor random-ycor
let target one-of other A-agents ; target is j in description
create-link-to target
repeat k - 1
[ let candidates (other [link-neighbors] of target) with [not link-neighbor? myself]
ifelse random-float 1 < prob or not any? candidates
[ create-link-to one-of other A-agents with [not link-neighbor? myself]
[ set color white ]
]
[ create-link-to one-of candidates
[ set color white ]
]
]
]
end
to add-B-node
create-B-agents 1
[ set shape "circle"
set size 2
set color red
setxy random-xcor random-ycor
let thisB self
ask A-agents
[ if random-float 1 < prob
[ create-link-from thisB
[ set color yellow
]
]
]
]
end
Some of the NetLogo issues I noticed in your code:
NetLogo does not use = for set
you must have space around mathematical operators (or NetLogo will think it's part of the name)
Some of the things you need to think about in your algorithm:
why do you have an initial B network if all Bs connect to each A with fixed probability?
what do you do if the selected A doesn't have any edges to follow?
As general advice, don't try writing something this complicated in one piece. Create your seed networks with 5 fully connected nodes. Make that work. Then do network A and make that work. Then bring in B. This iterative building is important for all programming languages. It is particularly important when using a new language so that you only have to debug one or two errors at a time and you know where the bugs are.

Set Radius to find a partner turtle

I'm very new at Netlogo and programming in general.
I want to create a netlogo model, with female and male turtles. Both populations move through the world via random walk. The female population should find a partner and has the property 'radius'. Her own radius should expand if she has not found a partner until the moment she finds one. How can I program a radius around the female turtles, that expand after each time step if she has not found a partner?
Thanks for your help!
First, you need a turtle attribute that stores the value for each turtle. The way to do that is with a turtles-own statement. Then you simply change the value as required. The primitive in-radius looks at everything within the specified distance, and then you can set a condition by whether there are any? suitable mates. Your code would look something like (this is a complete model):
turtles-own
[ search-radius
mate
]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set color blue
set search-radius 1
]
reset-ticks
end
to go
check-for-mate
tick
end
to check-for-mate
ask turtles with [color = blue]
[ let candidates other turtles in-radius search-radius
ifelse any? candidates
[ set mate one-of candidates
set color red
]
[ set search-radius search-radius + 0.5 + random-float 1
]
]
end

NetLogo - how do I get all patches the turtle facing?

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.

Conditional network neighbours

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