First, let's get this out of the way:
I can't work out how to ask the current turtle what breed it is
Turtles have a breed variable.
But I think that, more generaly, you haven't yet come to terms with the NetLogo way of doing things. Your code is very imperative, using while loops and indexes. You should avoid addressing turtles by their who
numbers (though your own layout code might be a rare exception to that). NetLogo is all about lists, agentsets, and (when possible) functional transformations.
Anyway, here is what I think is a better approach to your problem. The trickiest bit is how reverse-num-connexions
is computed, but trying to figure out how it works should be an excellent exercise for learning how to deal with lists.
Also, notice that it's possible that a turtle does not end of with the desired number of connexions because all of its targets are maxed out. It will depend on your population ratios.
breed [ toddlers toddler ]
breed [ children child ]
breed [ adults adult ]
breed [ over45s over45 ]
to setup
clear-all
create-toddlers 200
create-children 200
create-adults 200
create-over45s 200
create-network
reset-ticks
end
to create-network
let connexions (list
(list toddlers toddlers 5)
(list toddlers children 2)
(list toddlers adults 2)
(list toddlers over45s 1)
(list children toddlers 3)
(list children children 8)
(list children adults 5)
(list children over45s 1)
(list adults toddlers 1)
(list adults children 3)
(list adults adults 6)
(list adults over45s 3)
(list over45s toddlers 1)
(list over45s children 1)
(list over45s adults 5)
(list over45s over45s 5)
)
foreach connexions [
let source-breed item 0 ?
let target-breed item 1 ?
let num-connexions item 2 ?
let reverse-num-connexions item 2 first filter [
item 0 ? = target-breed and item 1 ? = source-breed
] connexions
ask source-breed [
repeat num-connexions [
let possible-targets other target-breed with [
(not member? myself link-neighbors) and
(count link-neighbors with [ breed = source-breed ] < reverse-num-connexions)
]
let target one-of possible-targets
if target != nobody [ create-link-with target ]
]
]
]
end
reverse-num-connexions
explanation (edit)
Let's say we are traversing our list of numbers of connexions, and we get to [toddlers adults 2]
. That says that each toddler should have connexions to 2 adults. But if we look further down the list, we see that adults should be connected to no more than 1 toddler: [adults toddlers 1]
. This number (1
) is what we are trying to extract for storing as reverse-num-connexions
.
The first thing to do is finding the right sublist. This will be the one where the "source" and "target" breeds are the inverse from the current one. The expression filter [ item 0 ? = target-breed and item 1 ? = source-breed ] connexions
will return a list with only that sublist: [[adults toddlers 1]]
. To extract it, we use first
, which gives us just the sublist: [adults toddlers 1]
. Now, we want the last item of that sublist, which is item 2
. This will give us the 1
that we were after.