How to select/ask more breeds at once? - netlogo

I have 8 different breeds in my model. Some of them share similar traits.
Anyway, lets suppose that my breeds are
breed [ humans human ]
breed [ dogs dog ]
breed [ cats cat ]
breed [ horses horse ]
breed [ elephants elephant ]
breed [ mouses mouse ]
breed [ monkeys monkey ]
breed [ birds bird ]
My question is how to ask for example everyone except birds. I don't want to ask each breed individually. I know that when you want to ask every breed than you go with ask turtles.

You will want some combination of turtle-set and member? primitives. For example, let wanted (turtle-set humans horses mouses) will get an agentset named 'wanted' with all the humans, horses and mouses. For your specific example, let wanted turtles with [not member? self birds] will get all the turtles except birds.

Don't forget that turtles have a built-in variable breed (look it up). All you need is ask turtles with [breed != birds]

Related

Getting breed type

In my go procedure, I want to check the breed of my turtles. But I cannot find a way to do it simply. I want to execute different code depending upon the breed. How should I do this?
I define breeds as:
breed [late-adopters late-adopter]
and in go
to go
ask turtles [
if is-late-adopter [do something...]
]
end
You just need to check the breed turtle variable against the breed agentset itself. The Bug Hunt Predators model is a good example of this. Here is the relevant code:
to go
; ...
ask turtles [
;; we need invaders and bugs to eat at the same time
;; so one breed doesn't get all the tasty grass before
;; the others get a chance at it.
(ifelse breed = bugs [ bugs-live reproduce-bugs ]
breed = invaders [ invaders-live reproduce-invaders ]
breed = birds [ birds-live reproduce-birds ]
[ ] ; anyone else doesn't do anything
)
]
; ...
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

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.

How to get different breeds to be `ask`ed in a random order together

Suppose we have two breeds, cats and dogs. On go, we want cats to meow and dogs to bark. ask causes the agents in the given agentset to run the code block in a random order, which is great. However, if we do:
to go
ask cats [ meow ]
ask dogs [ bark ]
tick
end
dogs always go after cats. We could solve this problem with conditionals:
to go
ask turtles [
if breed = cats [ meow ]
if breed = dogs [ bark ]
]
tick
end
but this is gross (note that you can replace the turtles with (turtle-set cats dogs) for more complicated situations with breeds that you don't want mixed in). Any better solutions?
Use tasks:
turtles-own [noise]
to setup
...
ask cats [ set noise task meow ]
ask dogs [ set noise task bark ]
...
end
to go
...
ask turtles [ run noise ]
...
end
How about a shuffled list of paired agentsets and tasks?
to go
let actions (list
(list cats task meow)
(list dogs task bark)
)
foreach shuffle actions [
ask first ? [ run last ? ]
]
end