NetLogo hatch and die - netlogo

I want to make turtles move around and look for a partner (a turtle that is not already partnered that is on the same patch) , and when they found one I want them to hatch a certain number of turtles and then die. But what happens is that when I start the simulation, all turtles die at the first tick. What am I doing wrong?
to go
ask turtles [
partner-up
]
birth
death
tick
end
to partner-up
if (not partnered?) [
rt (random-float 90 - random-float 90) fd 1
set partner one-of (turtles-at 0 0) with [ not partnered? ]
if partner != nobody [
set partnered? true
ask partner [
set partnered? true
set partner myself
]
]
]
end
to birth
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
calculate-score
hatch (score + 1)
]
end
to death
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
die
]
end

Two things stand out- one is the call to one-of (turtles-at 0 0) with [ not partnered? ]. This includes ALL turtles on the patch that you indicate (equivalent to turtles-here), which means including the turtle executing the command. I think you want other turtles-here or (other turtles-at 0 0).
Next, your 'baby turtles' are inheriting all the attributes of their parents, including values in their partnered? and partner variables. So, the turtles are being hatched with partnered? = true, and so when the call to death occurs in your go procedure, they qualify as partnered-turtles and therefore die. To correct this, you can explicitly set the variables for your hatched turtles- for example:
to birth
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
let score 1
hatch (score + 1) [
set partnered? false
set partner nobody
]
]
end
Also, not sure if this is intentional but wanted to point out that both 'partners' will hatch an offspring. If you want a more standard biological model you may want only one of the parents to hatch.
Revised toy model:
turtles-own [ partnered? partner]
to setup
ca
reset-ticks
ask n-of 10 patches [
sprout 1 [
set partnered? false
set partner nobody
]
]
end
to go
ask turtles [
partner-up
]
birth
death
tick
end
to partner-up
if (not partnered?) [
rt (random-float 90 - random-float 90) fd 1
set partner one-of (other turtles-at 0 0) with [ not partnered? ]
if partner != nobody [
set partnered? true
ask partner [
set partnered? true
set partner myself
]
]
]
end
to birth
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
let score one-of [ 0 1 ]
hatch (score + 1) [
set partnered? false
set partner nobody
]
]
end
to death
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
die
]
end

Related

Creating conditional links between two breeds

I am writing a NetLogo model of a housing market and its political ramifications. There are two breeds in the model: households and houses. An early step in my development with which I am having difficulty is having households match to houses via one of two types of links, own or rent, defined by nested conditional statements. This has resulted in two difficulties I haven't been able to overcome as of yet.
Within the command setup-market command, I'm trying to define a set of possible houses to purchase for each household which, if they meet a set of conditions, the household then buys (and creates a link). If it cannot afford to buy, then it will try to rent. If it cannot afford to rent the household will die.
My code continually results in the following error:
IFELSE expected input to be a TRUE/FALSE but got the turtle (house XXX) instead.
There is a further issue I'm having as well later in the code (in the two lines commented out with ";") where I attempt to set the variables owner-occupied and renter to 1 based on the presence of the appropriate link (they should remain 0 and the household should die if it remains unlinked).
The full code is below. The line with ";; This is the line giving me trouble" denotes where the error seems to be occurring.
UPDATE:
Code has been updated with JenB's solution. Resulting error is now:
CREATE-LINK-WITH expected input to be a turtle but got NOBODY instead. which occurs at the line: create-link-with one-of potentialHomes [ set color red
undirected-link-breed [own-links own-link]
undirected-link-breed [rent-links rent-link]
breed [city-centers city-center]
breed [households household]
households-own
[
age
money
income
monthly-income
consumption
monthly-consumption
hh-size race
preference
net-income
net-monthly-income
myHouse
]
breed [houses house]
houses-own
[
cost
down-payment
mortgage-payment
rent
rent-premium
rooms
onMarket
owner-occupied
rental
onMarket?
]
patches-own [
seed? ;;district seed
district ;;district number
full? ;;is the district at capacity?
quadrant
]
to setup
clear-all
reset-ticks
setup-patches
set-default-shape households "person"
create-households num-households [ setxy random-xcor random-ycor ]
set-default-shape houses "house"
create-houses num-houses [ setxy random-xcor random-ycor ]
setup-households
setup-houses
setup-market
generate-cities
end
to generate-cities
let center-x random-xcor / 1.5 ;;keep cities away from edges
let center-y random-ycor / 1.5
end
to setup-patches
ask patches with [pxcor > 0 and pycor > 0] [set quadrant 1 set pcolor 19 ]
ask patches with [pxcor > 0 and pycor < 0] [set quadrant 2 set pcolor 49 ]
ask patches with [pxcor < 0 and pycor < 0] [set quadrant 3 set pcolor 139 ]
ask patches with [pxcor < 0 and pycor > 0] [set quadrant 4 set pcolor 89 ]
end
to setup-households
ask households
[ set age random-poisson 38
set money random-exponential 30600
set income random-exponential 64324
set monthly-income income / 12
set consumption .5 * income
set monthly-consumption consumption / 12
set hh-size random 6 + 1
set net-income income - consumption
set net-monthly-income monthly-income - monthly-consumption
]
end
to setup-houses
ask houses
[ set cost random-normal 300000 50000
set down-payment cost * down-payment-rate
set mortgage-payment (cost - down-payment) / 360
set rooms random-exponential 3
set onMarket 1
set rent mortgage-payment + mortgage-payment * .25
set owner-occupied 0
set rental 0
]
end
to setup-market
ask houses
[ set onMarket? TRUE ]
ask households
[ ifelse any? houses with [ [money] of myself > down-payment and [net-monthly-income] of myself > mortgage-payment ]
[ let potentialHomes houses with [[money] of myself > cost and onMarket? ]
create-link-with one-of potentialHomes [
set color red
]
]
[
ifelse any? houses with [ [net-monthly-income] of myself > rent]
[ let potentialRentals houses with [ [net-monthly-income] of myself > rent and onMarket? ]
create-link-with one-of potentialRentals [ set color blue ]
]
[ die ]
]
]
ask houses
[ if any? link-neighbors [set onMarket FALSE ]
;if any? link-neighbors and color red [ set owner-occupied 1 ]
;if any? link-neighbors and color blue [ set rental 1 ]
]
end
to go
move-households
tick
end
to move-households
ask households [
move-to myHouse
]
end
You don't need to "suspect" where the problem is, NetLogo points to the problem line. Running your code, the problem is actually ifelse one-of houses with [ [net-monthly-income] of myself > rent]. Looking at that line, you pull out a randomly selected house from the pool with rent less than income. But you don't have a condition for the ifelse to test.
In previous constructions you have had != nobody at the end but you forgot that in this line. That will fix the error, but your code would be much less error prone if you used any? instead. You seem to be using one-of .... != nobody to test whether there are any turtles that satisfy the condition. That's what any? is for.
So instead of:
ifelse one-of houses with [ [net-monthly-income] of myself > rent] != nobody
[ let potentialRentals houses with [[money] of myself > rent and onMarket = 1 ]
create-link-with one-of potentialRentals [ set color blue ]
]
[ die ]
you can have:
ifelse any? houses with [ [net-monthly-income] of myself > rent]
[ let potentialRentals houses with [[money] of myself > rent and onMarket = 1 ]
create-link-with one-of potentialRentals [ set color blue ]
]
[ die ]
I should add that there is a potential logic problem here. Say there are houses with rent lower than income, the code goes to the first (true) actions. But there's no guarantee that there are any houses that satisfy the new conditions, which are different.
Also, NetLogo has the concept of true and false so you don't need to use 1 and 0. By convention (but not required), boolean variable names end with a question mark. So you could have set onMarket? true instead of set onMarket 1. Why would you do this? It makes logical operators cleaner and easier to read (which reduces bugs). Your line:
let potentialRentals houses with [[money] of myself > rent and onMarket = 1 ]
would look like:
let potentialRentals houses with [[money] of myself > rent and onMarket? ]
And you can do things like if not onMarket? instead of if onMarket? = false or if onMarket = 0

Catch nobody for target patch

I want to check if target patches fulfil a condition. If a patch is found that fulfils the condition,
then the turtles should move there. If 'nobody' fulfils this condition, then an error message should be printed.
The condition is that a patch should have in radius 10 2 turtles of the same breed.
I try to achieve this with ifelse and nobody. However, at the moment I always get the error message, even though the
target variable is not empty (you can check this with the if loop).
breed [ breed1s breed1 ]
breed [ breed2s breed2 ]
globals [target1 target2]
to setup
ca
create-breed1s 1000 [
setxy random-xcor random-ycor
]
create-breed1s 1000 [
setxy random-xcor random-ycor
]
end
to go
ask turtles [
set target1 ( count turtles in-radius 10 with [breed = breed1s] ) >= 2
set target2 ( count turtles in-radius 10 with [breed = breed2s] ) >= 2
new-position
]
end
to new-position
ifelse target1 != nobody
[ if (breed = breed1s) [ move-to one-of patches with [ target1 ] ] ]
[ print "Not enough agents in the neighborhood" ]
ifelse target2 != nobody
[ if (breed = breed2s) [ move-to one-of patches with [ target2 ] ] ]
[ print "Not enough agents in the neighborhood" ]
; if (breed = breed1s)
; [ move-to one-of patches with [ target1 ] ]
end
A remark to the efficiency of the model: as I want to add turtles later in every tick, target has to be reevaluated
in every tick (therefore it is in "go" and not in "setup").
Another question: is there a possibility to do something like [ breed = myself ] instead of [ breed = breed1s ], so
I don't have to type the breed for every breed?
Edit:
the turtles that move to the target patch should have the same breed that is also addressed in the target patch.
The problem is actually how you are creating target1, not the check as to whether it is nobody. You have:
set target1 ( count turtles in-radius 10 with [breed = breed1s] ) >= 2
This line first identifies all the nearby turtles with the appropriate breed and counts them. If the count is 2 or higher, then the variable target1 is set to true and to false if the count is 0 or 1. So you are comparing a boolean true or false to nobody (a special type of agent). That will always be a mismatch and therefore print the error.
Just a note on debugging - when you get this sort of problem, it's always useful to have a print statement for each side of the check just before doing the check. You would have immediately spotted that target1 wasn't what you thought it was.
Since you are asking to move to one-of the patches, you probably really want to store the available patches that are within 10 distance (I think) and have enough of the right type of turtles. So, you need something like:
to go
ask turtles [
set target1 patches in-radius 10 with [count breed1s-here >= 2]
set target2 patches in-radius 10 with [count breed2s-here >= 2]
new-position
]
end
Then your emptiness test is any?
to new-position
ifelse any? target1
[ move-to one-of target1 ]
[ print "Not enough agents in the neighborhood" ]
ifelse any? target2
[ move-to one-of target2 ]
[ print "Not enough agents in the neighborhood" ]
end
Assuming I have correctly interpreted that you want patches within 10 of the asking turtle (as compared to any patch with sufficient turtles within a distance of 10) and all you care about is the number of turtles of its own breed, then:
to go
ask turtles [
let target-breed [breed] of myself
set targets patches in-radius 10 with [count turtles-here with [breed = target-breed] >= 2]
new-position
]
end
to new-position
ifelse any? targets
[ move-to one-of targets ]
[ print "Not enough agents in the neighborhood" ]
end
On the efficiency, it depends on how many turtles you have. If you have quite a lot of turtles, then asking each to count its own neighbourhood will be expensive. Instead, you could have patchsets for each breed. That is, set up target1 as patches with [count breed1s-here >= 2] at the beginning of the go procedures. Then you can just do:
to go
let targets1 patches with [count breed1s-here >= 2]
let targets2 patches with [count breed2s-here >= 2]
ask turtles
[ set targets targets1 in-radius 10
new-position
]
end
However, you can no longer use the breed of the turtle and the myself trick to pick the correct patchset. There are ways to get around this (for example, using a list with two items, breed at first position and the patchset as second position) but that's getting well off track for this answer.
to new-position
ifelse any? targets
[ move-to one-of targets ]
[ print "Not enough agents in the neighborhood" ]
end

Evolutionary dynamics on a Prisoners Dilemma N-Person Iterated program

So, this is a part of a task I have to get done: ''Create a version of this model that rewards successful strategies by allowing them to reproduce and punishes unsuccessful strategies by allowing them to die off.''
But the thing is I can't get it to work, it displays an error every number of ticks, this is the code I have so far (I removed the commands of scores, dying and reproducing, so this is the pure code you could say) what should I add to make it work?
Help would be much appreciated, thanks!
This is the code
globals [
;;number of turtles with each strategy
num-random
num-cooperate
num-defect
num-tit-for-tat
num-unforgiving
num-unknown
;;number of interactions by each strategy
num-random-games
num-cooperate-games
num-defect-games
num-tit-for-tat-games
num-unforgiving-games
num-unknown-games
;;total score of all turtles playing each strategy
random-score
cooperate-score
defect-score
tit-for-tat-score
unforgiving-score
unknown-score
;;Noise
noise-active ;;turn noise on and off
noise-prob-defect ;;probability to flip a cooperate into a defect
noise-prob-cooperate ;;probability to flig a defect into a cooperate
noise-defect-true ;;Counter of times a defect turned into a cooperate
noise-cooperate-true ;;Counter of times a cooperate turned into a defect
]
turtles-own [
score
strategy
defect-now?
partner-defected? ;;action of the partner
partnered? ;;am I partnered?
partner ;;WHO of my partner (nobody if not partnered)
partner-history ;;a list containing information about past interactions
;;with other turtles (indexed by WHO values)
]
;;;;;;;;;;;;;;;;;;;;;;
;;;Setup Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;
to setup
clear-all
store-initial-turtle-counts ;;record the number of turtles created for each strategy
setup-turtles ;;setup the turtles and distribute them randomly
noise-setup ;; setup of the noise variables
reset-ticks
end
;;record the number of turtles created for each strategy
;;The number of turtles of each strategy is used when calculating average payoffs.
;;Slider values might change over time, so we need to record their settings.
;;Counting the turtles would also work, but slows the model.
to store-initial-turtle-counts
set num-random n-random
set num-cooperate n-cooperate
set num-defect n-defect
set num-tit-for-tat n-tit-for-tat
set num-unforgiving n-unforgiving
set num-unknown n-unknown
end
;;setup the turtles and distribute them randomly
to setup-turtles
make-turtles ;;create the appropriate number of turtles playing each strategy
setup-common-variables ;;sets the variables that all turtles share
end
;;create the appropriate number of turtles playing each strategy
to make-turtles
create-turtles num-random [ set strategy "random" set color gray - 1 ]
create-turtles num-cooperate [ set strategy "cooperate" set color red ]
create-turtles num-defect [ set strategy "defect" set color blue ]
create-turtles num-tit-for-tat [ set strategy "tit-for-tat" set color lime ]
create-turtles num-unforgiving [ set strategy "unforgiving" set color turquoise - 1 ]
create-turtles num-unknown [set strategy "unknown" set color magenta ]
end
;;set the variables that all turtles share
to setup-common-variables
ask turtles [
set score 0
set partnered? false
set partner nobody
setxy random-xcor random-ycor
]
setup-history-lists ;;initialize PARTNER-HISTORY list in all turtles
end
;;initialize PARTNER-HISTORY list in all turtles
to setup-history-lists
let num-turtles count turtles
let default-history [] ;;initialize the DEFAULT-HISTORY variable to be a list
;;create a list with NUM-TURTLE elements for storing partner histories
repeat num-turtles [ set default-history (fput false default-history) ]
;;give each turtle a copy of this list for tracking partner histories
ask turtles [ set partner-history default-history ]
end
;;;;;;;;;;;;;;;;;;;;;;;;
;;;Runtime Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;;;
to go
clear-last-round
ask turtles [ partner-up ] ;;have turtles try to find a partner
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [ select-action ] ;;all partnered turtles select action
ask partnered-turtles [ play-a-round ]
do-scoring
tick
end
to clear-last-round
let partnered-turtles turtles with [ partnered? ]
ifelse (random 100 > prob-of-staying)
[ask partnered-turtles [ release-partners ]]
[]
end
;;release partner and turn around to leave
to release-partners
set partnered? false
set partner nobody
rt 180
set label ""
end
;;have turtles try to find a partner
;;Since other turtles that have already executed partner-up may have
;;caused the turtle executing partner-up to be partnered,
;;a check is needed to make sure the calling turtle isn't partnered.
to partner-up ;;turtle procedure
if (not partnered?) [ ;;make sure still not partnered
rt (random-float 90 - random-float 90) fd 1 ;;move around randomly
set partner one-of (turtles-at -1 0) with [ not partnered? ]
if partner != nobody [ ;;if successful grabbing a partner, partner up
set partnered? true
set heading 270 ;;face partner
ask partner [
set partnered? true
set partner myself
set heading 90
]
]
]
end
;;choose an action based upon the strategy being played
to select-action ;;turtle procedure
if strategy = "random" [ act-randomly ]
if strategy = "cooperate" [ cooperate ]
if strategy = "defect" [ defect ]
if strategy = "tit-for-tat" [ tit-for-tat ]
if strategy = "unforgiving" [ unforgiving ]
if strategy = "unknown" [ unknown ]
add-noise ;;adds noise to the games
end
to play-a-round ;;turtle procedure
get-payoff ;;calculate the payoff for this round
update-history ;;store the results for next time
end
;;calculate the payoff for this round and
;;display a label with that payoff.
to get-payoff
set partner-defected? [defect-now?] of partner
ifelse partner-defected? [
ifelse defect-now? [
set score (score + 1) set label 1
] [
set score (score + 0) set label 0
]
] [
ifelse defect-now? [
set score (score + 5) set label 5
] [
set score (score + 3) set label 3
]
]
end
;;update PARTNER-HISTORY based upon the strategy being played
to update-history
if strategy = "random" [ act-randomly-history-update ]
if strategy = "cooperate" [ cooperate-history-update ]
if strategy = "defect" [ defect-history-update ]
if strategy = "tit-for-tat" [ tit-for-tat-history-update ]
if strategy = "unforgiving" [ unforgiving-history-update ]
if strategy = "unknown" [ unknown-history-update ]
end
;;;;;;;;;;;;;;;;
;;;Strategies;;;
;;;;;;;;;;;;;;;;
;;All the strategies are described in the Info tab.
to act-randomly
set num-random-games num-random-games + 1
ifelse (random-float 1.0 < 0.5) [
set defect-now? false
] [
set defect-now? true
]
end
to act-randomly-history-update
;;uses no history- this is just for similarity with the other strategies
end
to cooperate
set num-cooperate-games num-cooperate-games + 1
set defect-now? false
end
to cooperate-history-update
;;uses no history- this is just for similarity with the other strategies
end
to defect
set num-defect-games num-defect-games + 1
set defect-now? true
end
to defect-history-update
;;uses no history- this is just for similarity with the other strategies
end
to tit-for-tat
set num-tit-for-tat-games num-tit-for-tat-games + 1
set partner-defected? item ([who] of partner) partner-history
ifelse (partner-defected?) [
set defect-now? true
] [
set defect-now? false
]
end
to tit-for-tat-history-update
set partner-history
(replace-item ([who] of partner) partner-history partner-defected?)
end
to unforgiving
set num-unforgiving-games num-unforgiving-games + 1
set partner-defected? item ([who] of partner) partner-history
ifelse (partner-defected?)
[set defect-now? true]
[set defect-now? false]
end
to unforgiving-history-update
if partner-defected? [
set partner-history
(replace-item ([who] of partner) partner-history partner-defected?)
]
end
;;defaults to tit-for-tat
;;can you do better?
;;Generous Tit-for-Tat, cooperates 10% of the time that it would otherwise defect.
to unknown
set num-unknown-games num-unknown-games + 1
set partner-defected? item ([who] of partner) partner-history
ifelse (partner-defected?)
[ifelse (random 100 < 10) ;;be generous in 10% of the cases you would defect
[set defect-now? false]
[set defect-now? true]
]
[set defect-now? false]
end
;;defaults to tit-for-tat-history-update
;;can you do better?
to unknown-history-update
set partner-history
(replace-item ([who] of partner) partner-history partner-defected?)
end
;;;;;;;;;;;
;;;Noise;;;
;;;;;;;;;;;
;;read values from slider and store them
to noise-setup
set noise-active noise ;;Set if noise is active or not depending on switch
set noise-prob-defect prob-def-to-cop ;;Passes the current probability of defect turning into cooperate
set noise-prob-cooperate prob-cop-to-def ;;Passes the current probability of cooperate turning into defect
end
;;changes the decision according to the noise setup
to add-noise
;;check if noise is activated
if (noise-active) [
;;check decision
ifelse (defect-now?)
[
if (random 100 < noise-prob-defect)
[
;;flip defect -> cooperate
set defect-now? false
set noise-defect-true (noise-defect-true + 1) ;;Counter of times a flip has been made
]
]
[
if (random 100 < noise-prob-cooperate)
[
;;flip cooperate -> defect
set defect-now? true
set noise-cooperate-true (noise-cooperate-true + 1) ;;Counter of times a flip has been made
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;
;;;Plotting Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;;;;
;;calculate the total scores of each strategy
to do-scoring
set random-score (calc-score "random" num-random)
set cooperate-score (calc-score "cooperate" num-cooperate)
set defect-score (calc-score "defect" num-defect)
set tit-for-tat-score (calc-score "tit-for-tat" num-tit-for-tat)
set unforgiving-score (calc-score "unforgiving" num-unforgiving)
set unknown-score (calc-score "unknown" num-unknown)
end
;; returns the total score for a strategy if any turtles exist that are playing it
to-report calc-score [strategy-type num-with-strategy]
ifelse num-with-strategy > 0 [
report (sum [ score ] of (turtles with [ strategy = strategy-type ]))
] [
report 0
]
endenter code here

Preferential attachment: Selecting a node to attach

I've been struggling with this one for the last 24 hours or so I feel like I'm missing something relatively simple here.
to setup-scale-free-network
clear-all
;; Make a circle of turtles
set num-nodes (num-children + num-adults + num-toddlers)
create-children num-children
create-adults num-adults
create-toddlers num-toddlers
layout-circle turtles (max-pxcor - 8)
ask turtles[
create-links-with turtles with [self > myself and random-float 5 < probability]
]
setup
end
to-report find-partner
report [one-of both-ends] of one-of links
end
The above code creates a set number of turtles of various breeds and creates a number of links between these breeds.
to go
reset-ticks
make-link find-partner
tick
end
The two procedures would be called until the needed level of degree distribution as been met.
What I want to do is use the find-partner procedure to move towards preferential attachment to do this I need to modify this code to create a link from the node find partner has selected to one of each of the other three types of breeds in my network.
to make-node [old-node]
crt 1
[
set color red
if old-node != nobody
[ create-link-with old-node [ set color green ]
;; position the new node near its partner
move-to old-node
fd 8
]
]
end
My own attempts have lead no where to be honest. I know I'm asking for a lot of help but I'm at my wits end here, thank you for your help and patience.
I was unable to completely understand your question. I am guessing that you wish to create another type of link which you call preferential attachment (with green color) between turtles who are already a part of the network.
One thing you might want in this situation is that you do not pick turtles which are already in a preferential attachment network. [this is just my assumption]
I have modified your code (as follows) to get the desired network with preferential attachment shown with green colored links, have added a turtle-variable already-attached which is used to exclude turtles which are already preferentially attached to others.
Hope this helps!
breed [ children child ]
breed [ adults adult ]
breed [ toddlers toddler ]
children-own [ already-attached ]
adults-own [ already-attached ]
toddlers-own [ already-attached ]
to setup-scale-free-network
clear-all
let num-children 5
let num-adults 5
let num-toddlers 5
let probability 1
;; Make a circle of turtles
create-children num-children [ set color orange ]
create-adults num-adults [ set color green ]
create-toddlers num-toddlers [ set color blue ]
layout-circle turtles (max-pxcor - 8)
ask turtles
[
create-links-with turtles with [self > myself and random-float 5 < probability]
]
end
to go
setup-scale-free-network
ask turtles with [already-attached = 0]
[
attach-to-one-of-each-breed self
]
end
to attach-to-one-of-each-breed [ source-node ]
ask source-node
[
if any? other children with [ already-attached = 0 ]
[
ask one-of other children
[
set already-attached 1
create-link-with source-node [ set color green ]
]
]
if any? other adults with [ already-attached = 0 ]
[
ask one-of other adults
[
set already-attached 1
create-link-with source-node [ set color green ]
]
]
if any? other toddlers with [ already-attached = 0 ]
[
ask one-of other toddlers
[
set already-attached 1
create-link-with source-node [ set color green ]
]
]
]
end

NetLogo releasing isolated patches from patch-set

I am growing animal territories. Animal territories may cleave parts of other animal territories during the process of expand. So instead of being one contiguous territory, the territory may include more than one cluster (i.e., unattached clusters). This is what happens in the model below. I'd like to have the territory recognize this and release whichever cluster of cells (or a single unattached cell) is smallest so that the territory remains one contiguous cluster. I'm not sure where to start with this. Any help would be great.
breed [animals animal]
breed [homeranges homerange]
animals-own
[
Name
orig
territory
food
status
]
patches-own
[
owner
prey
]
to setup
clear-all
random-seed 2234
ask patches
[
set owner nobody
set prey 2
set pcolor scale-color (black) prey 1 4
]
let $colors [brown orange violet sky lime]
let $Name ["t6" "t7" "t8" "t9" "t10"]
let $status [0 0 0 0 5]
ask n-of 5 patches
[
sprout-animals 1
[
set shape "circle"
set orig patch-here
set territory patch-set orig
set status item who $status
set size 0.3 + 0.1 * status
set color item who $colors
set pcolor color
set Name item who $Name
set owner self
]
]
reset-ticks
end
to go
if all? animals [food >= 350] [ stop ]
if ticks = 70 [ stop ]
expand
tick
end
to expand ; animals procedure
repeat 10
[
ask animals
[
let vacant no-patches
let subord no-patches
let target nobody
let new-patches no-patches
let status-of-calling-tiger status ;
let calling-tiger self ;
; If territory not yet good enough:
if food < 500
[
ask territory
[
; Add unoccupied neighbor patches as potential targets:
set vacant (patch-set vacant neighbors with [owner = nobody])
; Add occupied neighbor patches as potential targets if their tiger has a lower status than me:
set subord (patch-set subord neighbors with [owner != nobody and [status] of owner < status-of-calling-tiger])
]
ask subord [ set pcolor red ]
; Set of all potential targets:
set new-patches (patch-set new-patches vacant subord)
; Choose as target the one potential target with highest prey:
if any? new-patches
[
ask new-patches
[ifelse any? vacant
[ifelse any? subord
[ifelse [prey] of max-one-of vacant [prey] = [prey] of max-one-of subord [prey]
[set target max-one-of vacant [prey]]
[set target max-one-of new-patches [prey]]
]
[set target max-one-of vacant [prey]]
]
[set target max-one-of subord [prey]]
]
move-to target
if-else member? target subord
[ set shape "triangle" ] ; so you can see that the target patch was from "subord"
[ set shape "circle" ] ; or from "vacant"
]
;ifelse any? target with [owner != nobody]
if target != nobody
[
; Add target patch to territory of the current animal:
set territory (patch-set territory target) ; this is the territory of the calling tiger
let old-owner [owner] of target; this needs to be memorized
; Tell target patch that is has new owner:
ask target [ set owner calling-tiger ]
; Tell the original owner of the target patch to remove the target patch from its territory:
if old-owner != nobody ;
[
ask old-owner
[
set territory territory with [ owner != calling-tiger ]
]
]
]
set food sum [prey] of territory
]
]
]
ask animals
[
ask territory
[
set pcolor [color] of myself
set plabel (word [status] of owner [status] of myself)
]
if food < 10 [die]
]
end
Patch Clusters Example, in the Code Examples section of NetLogo's Models Library, has code for identifying a contiguous cluster of patches. The core code is as follows:
patches-own [cluster]
to setup
...
ask patches [ set cluster nobody ]
...
end
to grow-cluster ;; patch procedure
ask neighbors4 with [(cluster = nobody) and
(pcolor = [pcolor] of myself)]
[ set cluster [cluster] of myself
grow-cluster ]
end
But see the rest of the example as well.
In your use case, instead of placing the "seeds" randomly for growing the clusters, you'll start growing the clusters in the patches where the animals are standing.