Moving turtles to specific coordinates of other agents in NetLogo - coordinates

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]
]

Related

Linking turtles to another turtle with highest value in NetLogo. Limit the number of links

I am a beginner with NetLogo and I am trying to ask some turtles (students from different social classes) to link to other turtles (schools). What I would like is for the working class students to look for the school with the highest achievement which is at the same not expensive and has not reached the max number of links allowed. If the desired school has reached the max number of links allowed I want the student to look for the next school with the highest achievement which has not reached the max number of links allowed and so on.
This is the code. I get the following message "ANY? expected input to be an agentset but got the turtle (school 1) instead."
breed [schools school]
breed [upperclass upperclass-student]
breed [workingclass workingclass-student]
upperclass-own [achievement enrolled? target]
workingclass-own [achievement enrolled? target]
schools-own [schoolachievement expensive? ]
to setup
clear-all
set-default-shape schools "house"
set-default-shape upperclass "person"
set-default-shape workingclass "person"
ask patches [ set pcolor 8 ]
create-schools num-of-schools [ setxy random-xcor random-ycor set schoolachievement random-normal 5 1
set expensive? one-of [ true false ] ]
create-upperclass num-of-upperclass [ set color white setxy random-xcor random-ycor set achievement
random-normal 5 1 ] ;Students from upper class have higher achievement
create-workingclass num-of-workingclass [ set color red setxy random-xcor random-ycor set achievement
random-normal 4 1 ]
end
to go
ask workingclass [
choose-school ]
end
to choose-school
if breed = workingclass [
set target one-of schools with-max [ schoolachievement ] with [ expensive? = false ] ]
if any? target with [ count link-neighbors < max-link-count ] [
create-link-with target ]
set enrolled? TRUE
end
Your problem is the difference between an agent and an agentset, which is a somewhat subtle problem. The with-max returns an agentset - a set of agents (in this case turtles). That agentset can have 0 members, 1 member, 2+ members but is a set even if it is empty. However, the one-of selects one agent from the agentset and returns it as an agent, not an agentset. That is, NetLogo knows anything that is returned by one-of must be exactly one agent. At this point, primitives that are for agentsets (like any?) will throw an error unless they can also be used on individual agents.
So, back to your code. I like the readability of checking whether there are viable schools and then selecting one, which is what I think you meant to do. That would be:
to choose-school
if breed = workingclass
[ set targets schools with-max [ schoolachievement ] with [ expensive? = false ]
set candidates targets with [ count link-neighbors < max-link-count ]
if any? candidates
[ create-link-with one-of candidates
set enrolled? TRUE
]
]
end
Note that I also changed to targets instead of target, which is one way to keep track of whether something is an agent or an agentset.
The other way you could do this and keep it as an agent is:
to choose-school
if breed = workingclass [
set target one-of schools with-max [ schoolachievement ] with [ expensive? = false ] ]
if target != nobody and [count link-neighbors] of target < max-link-count [
create-link-with target ]
set enrolled? TRUE
end
So you can use nobody instead of any? but you can't also use with in that line because the with is really a filter on a set.
I also think you have a bracketing issue - I assume you want set enrolled? TRUE inside the brackets. I left it in the second fix, but changed in the first error (as well as changing bracket position convention to make the code block structure more visible)

How does one report the distance between links and use those values reported for other calculations in code?

I'm trying to calculate and report the distance (link-length) between specific agentsets in NetLogo? Is there a way to calculate link length into a list?
The movement of the agents is based on whether the distance(connection) value is below/above a threshold. However, I'm having difficulty setting the values of the link length to variable connection. (preferably in a list). I'd appreciate any help.
globals[hourly-wage connection]
breed[offices office]
breed[employees employee]
offices-own [
pay-high ;; 7 offices pay well
pay-low ;; 3 offices dont pay well
]
to setup
clear-all
create-offices 10 [
set size 1.0
set color blue
set shape "house"
setxy random-xcor random-ycor
ask offices [create-link-with one-of other offices] ;; undirected links
ask links [set color red]
]
create-employees 2 [
set size 1
set color brown
set shape "person"
]
set hourly-wage 20
end
;;;;
to go
cal-dist
ask employees [
if connection > 15
move-to one-of high-pay office
if connection <= 15
move-to one-of low-pay office
]
end
to cal-dist
set connection [list print link-length] ;;
ask links [show link-length]
set salary (hourly-wage * connection) ;;; salary printed in a list
end
Not exactly sure what you're trying to do here with connection etc, but you can put any link variables into a list using of- for example:
to setup
ca
; First create the agents
crt 5 [
while [ any? other turtles in-radius 5 ] [
move-to one-of neighbors
]
set color blue
set shape "house"
]
; Once they're created, have them link with
; one of the other agents
ask turtles [
create-link-with one-of other turtles [
set color red
]
]
let link-lengths [ link-length ] of links
print link-lengths
reset-ticks
end
I don't know that this actually answers your question, so you may want to provide more detail as to what you're trying to accomplish with these links.

is it possible variable inhrit the my turtles label

i want my variable to inherit the label of my turtles.
im making routes for them and want them to remember the last place they visited.
so theyll continue to the next place in the chain.
ifelse last_place = home
[set place min-one-of (turtles with [label = "mall"])[distancemyself]]
[set place min-one-of (turtles with [label = "home"])[distancemyself]]
i cant use my actual code in here but hopefully, you get the gist
if
place = one-of turtles with [label = "mallI]
I want to add
set last_place label of place
i want last_place to get the label of place.
i know it can create loops if i have the same place twice in the same route but i want to create a list to prevent them but right now i need a sort of where flag that will make my turtles keep going to the end.
Hard to say without seeing more of your code- it's hard to know what turtle is doing what. If your code is sensitive, I'd recommend following the tips in the MCVE guidelines to make a reproducible example- it might be easier to address your exact problem that way!
As an alternative, instead of using a label it's probably better to just have the turtles store the "location" turtle or patch in a turtle-variable. Using this simple example setup:
breed [ walkers walker ]
breed [ locations location ]
walkers-own [ location-list ]
to setup
ca
create-walkers 10 [
setxy random-pxcor random-pycor
set location-list []
pd
]
create-locations 20 [
set size 1.5
set shape "house"
setxy random-pxcor random-pycor
]
reset-ticks
end
You can have turtles store the places they visit in a list and reference them that way.
to go
ask walkers [
; Build an agrentset of locations that do not belong
; to each turtle's 'location-list'
let unvisited-locations locations with [
not member? self [location-list] of myself
]
; Target the nearest unvisited location
let target min-one-of unvisited-locations [ distance myself ]
; if the target exists, move towards it
if target != nobody [
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
set location-list lput target location-list
]
]
; if a turtle visits all locations, remove the
; first location visited from the 'location-list'
; so that it will follow the same pattern continuously
if length location-list = count locations [
set location-list but-first location-list
]
]
tick
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 choose the not previously used links by an agent

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