How to choose the not previously used links by an agent - netlogo

I have breed [walkers walker] which walk roads in a road map represented by links in Netlogo.
The links-own [ guiri-ids ] which I intend to be an turtle-set of walkers that have already walked for the link.
I would like to use this guiri-ids to select, from the set of possible next links, which I call nextlinks, the links that the walker has not been walked before (the new ones).
If all possible links have been walked before then choose one of them.
How could obtain the set of next links which guiri-ids set does not contain myself (the walker) ?.
I am trying this line
let new-nextlinks nextlinks with [ guiri-ids != myself ]
but the keeps taking old paths.
Thank your very much for your help

breed [walkers walker]
links-own [ guiri-ids ]
to test
ca
crt 25 [setxy random-xcor random-ycor]
ask turtles [
create-link-with one-of other turtles
]
create-walkers 10
ask links [set guiri-ids n-of 3 walkers]
ask walkers [walk]
end
to walk
let _next one-of links with [not member? myself guiri-ids]
ifelse (_next != nobody) [
ask _next [set guiri-ids (turtle-set myself guiri-ids)]
] [
;do whatever you wish in this case
]
end

Related

Copy the link breed variable in the patch below

I have a network of nodes and links. This figure
is a capture of the world. The graph represents streets of a city. I have imported a shapefile with the gis extension. The gray lines are links, black dots are nodes and red dots represent people. The people move heading to the next node. In a street corner, the red dot chooses next street by examining the variable popularity owned by the link.
The links breed has a variable, popularity, whose value I would like to copy in the patches that are below.
If I try, for example, something like this to access patches under links will produce an error
ask links [show [(list pxcor pycor)] of patch-here]
Another approach can be to access links variable popularity from patches, but I do not know how to do it.
The reason why I want this is because I want to write in a file a matrix of popularity values and its position in the matrix should correspond with the position of the link in the world. Thus, the patches below the links would give me the matrix form. I have a procedure that for each patch writes the value of the patch in a file. However, I do not know how to pass the popularityvalue from the link to the patch below it.
Is there any way to copy a link owned variable to its patch?
Regards
If someone has a better way of doing this (or can simplify my code), feel free. Here is a complete working example. Copy it into an empty NetLogo model and run it to see it work.
The setup procedure just creates some nodes and links with appropriate test values and then calls the transfer-link-values procedure, which does what I think you want. The setup procedure then puts the values into the patch labels to display them and see the results.
The way the transfer-link-values procedure works is to create a turtle at one end of the link, and that turtle moves toward the other end of the link transferring the value as it goes. When it gets to the other end, the turtle dies.
patches-own [patch-popularity]
links-own [link-popularity]
to setup
clear-all
create-turtles 10 [ setxy random-xcor random-ycor]
while [ any? turtles with [not any? my-links] ]
[ let to-pair turtles with [not any? my-links]
let thisNode one-of to-pair
ask thisNode
[ create-link-with one-of other to-pair
[ set link-popularity 5 + random 5 ]
]
]
transfer-link-values
ask patches [ if patch-popularity != 0 [set plabel patch-popularity ] ]
end
to transfer-link-values
ask links
[ let start-node one-of both-ends
let this-link self
let end-node nobody
ask start-node [ set end-node [other-end] of this-link ]
let transfer-value link-popularity
ask start-node
[ hatch 1
[ face end-node
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
while [ not member? end-node turtles-here ]
[ forward 1
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
]
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
die
]
]
]
end

How to create links that thereby change a turtle's breed?

I'm trying to model families. I would like to set it so that males meet females and form a link that will subsequently allow them to reproduce. I have not been able to figure out or find online how to code links to do this although I think it is quite basic.
I have as breeds males and females and husbands and wives. This code is to be run by males.
to marry
if hunger < 10 [create-link-with one-of females]
ask my-links [set breed wives]
end
This returns a runtime error "you can't see breed to a non-link agentset". I thought this meant that I needed to use some kind of breed-command, e.g.
create-<breed>-link-with
But
create-<wives>-link-with
and
create-<a wife>-link-with
etc all generate error messages.
I have also tried making the link directed, e.g.
create-link-to one-of females
but to no avail.
You're having the turtles ask the links rather than the link-neighbors, I think that's all:
breed [ cats cat ]
to setup
ca
crt 10 [ setxy random-pxcor random-pycor ]
reset-ticks
end
to go
ask one-of turtles [
create-link-with one-of other turtles
ask link-neighbors [
set breed cats
]
]
end
Edit
I think this does what you want:
breed [ males male ]
breed [ husbands husband ]
breed [ females female ]
breed [ wives wife ]
males-own [ mood ]
to setup
ca
create-males 5 [
set color green
setxy abs random-pxcor / 2 random-pycor
set mood "lonely"
]
create-females 5 [
set color white
setxy ( abs random-pxcor ) / -2 random-pycor
]
reset-ticks
end
to go
if any? females [
ask one-of males [
set breed husbands
create-link-with one-of females
ask link-neighbors [
set breed wives
]
]
]
end
However, you may want to reconsider having the turtles switch breeds, and instead give them all a turtles-own boolean variable like married? that you can use as a flag. If you want to do the breed switching, do make sure any variables that you create for related breeds are identical- note that in the example above males have a mood variable but they lose that info when they change breeds to husbands.

How to create links in netlogo and ask link-neighbors to execute commands according to link-length?

I've been trying to link turtles from BREED1 (still) to turtles from breed2 (mobile) who are on neighbors of BREED1. I want to do so in order to change a variable according the link-length between BREED1 and breed2.
(you can say that BREED1 represent houses and breed2 represents people, I would like to change the fact that the people are "protected" or not, according to the distance that separates them from their house (BREED1 that they are linked to))
I don't know if this is the best way to do it, but here's my code, I know it's not working because the "protected" variable is always false by default.
to protect
ask n-of total-number-BREED1 BREED1
[ if any? breed2-on neighbors
[ create-link-with [who] of breed2-on neighbors]
ask link-neighbors
[ set protected true]
]
I would also like to add a part concerning the link's length
ask link-neighbors
[ ifelse link-length < 2
[set protected true]
[set protected false]]
Thank you for your help !
Try this to create links with the breed2-on the neighboring patches:
ask BREED1
[
if any? breed2-on neighbors [ create-links-with breed2-on neighbors]
ask link-neighbors [ set protected true]
]
and this, which gets the link-length between the breed1 and it's neighbors
ask BREED1
[
ask link-neighbors
[
if [link-length] of link-with myself < 2 [ do something]
]
]
Note: link-length is called from a link's perspective, so you need to get the link that's connecting two things.

Setting turtles in parcel without sprout

I been looking for the way to place each turtle in a parcel, but I do not want to have two turtles in the same parcel. Does anybody know how to do it?. (Without using sprout).
Assuming you have more patches than turtles, all you need is:
ask turtles [
move-to one-of patches with [ not any? turtles-here ]
]
Interesting side note:
People are often tempted to stick other in there, as in:
; bad code:
ask turtles [
move-to one-of patches with [ not any? other turtles-here ]
]
but this calls other within the patch context, so it doesn't actually do anything. To achieve the desired effect of not excluding the patch that the turtle is already on, you could write:
ask turtles [
let me self
move-to one-of patches with [ not any? turtles-here with [ self != me ] ]
]
Whether or not it is worth the trouble depends on your particular circumstances.
Finally, note that:
; bad code:
ask turtles [
move-to one-of patches with [ not any? [ other turtles-here ] of myself ]
]
would not work either because turtles-here would then be called in the turtle's context instead of being called in the patch context.

Moving turtles to specific coordinates of other agents in NetLogo

I have individuals (turtles) and I have households (turtles with fixed xy)
I have a variable address stored at households. I have a number of a family attached to individuals. The households has the same number.
How can I ATTACH or MOVE the individuals to their corresponding household?
I tried something like:
ask individuals
[ if family = [family-place] of household
[
move-to [address] of household
]
]
Since it is a slow monday morning, here is how I would do it.
I assume familiy-number to be the name of the common number in both moving and sessile turtles. I would use let to create a local variable that only works within the procedure. (See the procedure go-home for this)
breed [walkers walker]
breed [houses house]
houses-own [family-number]
walkers-own [family-number]
to setup
clear-all
set-default-shape houses "house"
create-houses 10 [
setxy random-xcor random-ycor
set family-number random 10000
]
reset-ticks
end
to leave-home
ask houses [
hatch-walkers 1 [
set family-number [family-number] of myself
set color [color] of myself
set heading random 360
fd 1
]
]
end
to go
ask walkers [
rt random 120
lt random 120
fd 1
]
tick
end
to go-home
ask walkers [
let family-place one-of houses with [family-number = [family-number] of myself]
move-to family-place
fd 1 ;; walker will step away one step so we can see him.
]
end
Just copy it into NetLogo, make a button for each procedure and play. Works best if in the order
setup
leave-home
go
go-home
Hope this helps!
move-to household should do it.
I'm having trouble understanding your question, so I've having to guess at what you want, but with the help of your comment on Bryan's answer, maybe I've guessed right?
ask individuals [
move-to one-of households with [address = [family-place] of myself]
]
if this seems confusing because of the myself, you could also write it as:
ask individuals [
let f family-place
move-to one-of households with [address = f]
]