Query / create subset of turtles for each turtle - simulation

I have an agentset where all the turtles have different values for age/experience. What I would like to do is find the more experienced turtles for each turtle and then follow those turtles. Unfortunately, I get the following error for the ifelse [ age-experience > my-own-age-experience ] line:
Expected a TRUE/FALSE here, rather than a list or block.
Here's my code:
turtles-own [
age-experience
more-dominant
dominant-flockmates
]
to setup
clear-all
create-turtles 10 [ set age-experience random-float 1 ]
reset-ticks
end
to go
ask turtles [
find-dominant-flockmates
ifelse any? dominant-flockmates
[ show "follow the more dominant flockmates" ]
[ show "take the lead" ]
]
tick
end
to find-dominant-flockmates
let my-own-age-experience age-experience
ask other turtles [
ifelse [ age-experience > my-own-age-experience ]
[ set more-dominant true ]
[ set more-dominant false ]
]
set dominant-flockmates other turtles with [ more-dominant ]
end

Okay, you have several choices here. You can get rid of the error by removing the []
to find-dominant-flockmates
let my-own-age-experience age-experience
ask other turtles [
ifelse age-experience > my-own-age-experience
[ set more-dominant true ]
[ set more-dominant false ]
]
set dominant-flockmates other turtles with [ more-dominant ]
end
But there's much more efficient ways to code this. If you are going to use the variable more-dominant for other purposes (so you need it), you can use ifelse-value like this (and because it's true/false, you don't actually need ifelse-value but it's a good thing to know about):
to find-dominant-flockmates
let my-own-age-experience age-experience
ask other turtles
[ set more-dominant ifelse-value (age-experience > my-own-age-experience)
[ true ]
[ false ]
]
set dominant-flockmates other turtles with [ more-dominant ]
end
But if the only reason to have the variable is to create the agentset, you can do the whole thing directly:
to find-dominant-flockmates
let my-own-age-experience age-experience
set dominant-flockmates other turtles with [ age-experience > my-own-age-experience ]
end

Related

add another turtle's id to another turtle's attribute

In the model below, a bank can only fund customers with a credit score that matches the scores in their acceptable_scores attribute. and if it doesn't match any of the banks acceptable_scores it is rejected.
globals [
scores
]
breed [ customers customer ]
breed [ banks bank ]
customers-own [
loan_amount
credit_score
status
]
banks-own [
acceptable_scores
list_of_customers
]
to setup
clear-all
ask patches [ set pcolor 8 ]
set scores n-values 10 [random 900 + 10]
create-customers 100 [
set loan_amount random-exponential 20000
set credit_score one-of scores
set status "applied"
]
create-banks 10 [
set acceptable_scores n-of (random 3 + 1) scores
set list_of_customers []
]
reset-ticks
end
to go
ask one-of banks [
fund-loan
reject-loan
]
tick
end
to fund-loan
let person one-of customers-here with [
(member? credit_score [acceptable_scores] of myself)
]
if person != nobody [
ask person [
set status "funded"
]
set list_of_customers lput person list_of_customers
]
end
to reject-loan
let person one-of customers-here with [
(not member? credit_score [acceptable_scores] of myself)
]
if person != nobody [
ask person [
set status "funded"
]
set list_of_customers lput person list_of_customers
output-print list_of_customers
]
end
I want to add each funded customer to the list_of_customers after funding here:
set list_of_customers lput person list_of_customers
but the list is always empty when printed out. What is the right way to add another turtle's id to another turtle's attribute? In general, is this the best way to handle the funding and rejection procedures? Can it be done in one function?
Here is a pared-down version of your model using agentsets. The bank behavior may not be just what you want, but it should get you started.
globals [
scores
]
breed [ customers customer ]
breed [ banks bank ]
customers-own [
loan_amount
credit_score
status
]
banks-own [
acceptable_scores
my_customers
]
to setup
clear-all
ask patches [ set pcolor 8 ]
set scores n-values 10 [random 900 + 10]
create-customers 100 [
set loan_amount random-exponential 20000
set credit_score one-of scores
set status "applied"
]
create-banks 10 [
set acceptable_scores n-of (random 3 + 1) scores
set my_customers no-turtles
]
reset-ticks
end
to go
ask banks [
fund-loan
]
ask banks [ show my_customers ]
tick
end
to fund-loan
let person one-of customers-here
ifelse (member? [credit_score] of person acceptable_scores) and ([status] of person != "funded") [
ask person [
set status "funded"
]
set my_customers (turtle-set person my_customers)
]
[
ask person [
set status "rejected"
]
]
end

what is wrong with the way I am coding the probability of surviving?

I'm trying to create a model in which turtles move through a landscape and each type of patch means a different probability of survival for the turtles (representing patches that have more resources = higher chance of survival, patches with less resources = lower chance of survival).
However, all my turtles die out super quickly and don't seem to be following the differences in the probability of survival.
The only thing I have identified is that only if they are in a patch with probability of survival = 1, they survive. If not (even if the probability is .96), they die.
The probability of survival that each type of patch represents is determined by a slider.
extensions [
gis
palette
]
patches-own [
TipoRaster
tipoDeUso
**probSupervivencia**
]
breed [bichos bicho]
to go
if not any? turtles [stop]
if ticks = 120 [stop] ;; representa un ciclo de vida de las hembras adultas (donde cada tick = 1 dia). ***180
ask turtles [
**move
if random-float 1.00 > probSupervivencia [die]**
]
tick
end
to initHabitat
if Habitat = "bichos" [
ask patches with [ tipoDeUso = "Urbano" ][
set probSupervivencia probabilidad-supervivencia-urbano
]
ask patches with [ tipoDeUso = "Nada" ][
set probSupervivencia probabilidad-supervivencia-nada
]
ask patches with [ tipoDeUso = "Bosque" ][
set probSupervivencia probabilidad-supervivencia-bosque
]
ask patches with [ tipoDeUso = "CuerposDeAgua" ][
set probSupervivencia probabilidad-supervivencia-agua
]
ask patches with [ tipoDeUso = "Pastizal" ][
set probSupervivencia probabilidad-supervivencia-pastizal
]
if calidad-matriz = "actual" [
ask patches with [ tipoDeUso = "AgriculturaTemporal" ][
ifelse random-float 1.00 < 0.32 [ set probSupervivencia probabilidad-supervivencia-agricultura-calidad-baja ][ set probSupervivencia probabilidad-supervivencia-agricultura-calidad-alta ]
]
ask patches with [ tipoDeUso = "AgriculturaRiego" ][
ifelse random-float 1.00 < 0.63 [ set probSupervivencia probabilidad-supervivencia-agricultura-calidad-baja ][ set probSupervivencia probabilidad-supervivencia-agricultura-calidad-alta ]
]
]
if calidad-matriz = "alta" [
ask patches with [ tipoDeUso = "AgriculturaTemporal" or tipoDeUso = "AgriculturaRiego" ][
set probSupervivencia probabilidad-supervivencia-agricultura-calidad-alta
]
]
if calidad-matriz = "baja" [
ask patches with [ tipoDeUso = "AgriculturaTemporal" or tipoDeUso = "AgriculturaRiego" ][
set probSupervivencia probabilidad-supervivencia-agricultura-calidad-baja
]
]
if calidad-matriz = "contrastante" [
ask patches with [ tipoDeUso = "AgriculturaTemporal"][
set probSupervivencia probabilidad-supervivencia-agricultura-calidad-alta
]
ask patches with [ tipoDeUso = "AgriculturaRiego" ][
set probSupervivencia probabilidad-supervivencia-agricultura-calidad-baja
]
]
]
end
**to move**
if random 100 < 10 [move-to one-of patches in-radius (tasa-movimiento / longitud-step)]
end
Can anybody figure out what it is I am doing wrong?
Your agents need to inquire about the patch they occupy.
Something like
ask turtles [
if ([probSupervivencia] of patch-here < random-float 1.00) [die]
]

how to identify the breed of a turtle within a dynamic agentset by using a foreach loop in netlogo?

I am trying to scan all the turtles in the vicinity of a turtle and if they are eligible, and if the ratios of the different breeds within a cluster of size greater than 10 turtles is okay, then I link the turtle up with the leader of the cluster.
But I am having trouble identifying the breed of each item within the agentset 'candidates', which is a list of all turtles within the vicinity of the turtle I am asking. According to the breed of the item/turtle, I want to execute a command block.
Can anyone help please?
let candidates turtles in-radius radius_scan with [ leader != [leader] of myself and exempt_from_immobilisation = false and aggregated = false]
if (any? candidates) ;; if there are candidates around and the cluster is too small, then proceed to increase the size of the cluster without taking into account any ratios
[
ifelse
length(aggregate-list) < 10 [
if who not member? aggregate-list [ set aggregate-list lput myself aggregate-list ]
create-links-with candidates [show-link ]
ask candidates [
set aggregated true
if who not member? aggregate-list [ set aggregate-list lput myself aggregate-list ]
;set aggregate-list lput myself aggregate-list
merge ]
]
[ ;; if cluster is big enough, start checking if the ratios are approximately making sense
(foreach candidates [ t ->
;let t-breed breed of t
(ifelse
breed = sykfactive [
ifelse ( ( (length(filter is-monomer1? aggregate-list)) + (length(filter is-dimer2? aggregate-list)) + (length(filter is-monomer2syk1active_crosslink? aggregate-list))) <= ( (length(filter is-sykfactive? aggregate-list)) + 5 ))
[
if who not member? aggregate-list [ set aggregate-list lput myself aggregate-list ]
create-links-with t [show-link ]
ask t [
set aggregated true
if who not member? aggregate-list [ set aggregate-list lput myself aggregate-list ]
;set aggregate-list lput myself aggregate-list
merge ]
]
[;;else commands
print "nothing to print or do"
]
]
is-monomersykactive? [
if ( (length(filter is-monomersykactive? aggregate-list)) <= ( (length(filter is-monomer1? aggregate-list)) + 5 ))
[
if who not member? aggregate-list [ set aggregate-list lput myself aggregate-list ]
create-links-with t [show-link ]
ask t [
set aggregated true
if who not member? aggregate-list [ set aggregate-list lput myself aggregate-list ]
;set aggregate-list lput myself aggregate-list
merge ]
]
]
is-dimer2syk1active? [
if ( ( (length(filter is-monomer1? aggregate-list)) + (length(filter is-dimer2? aggregate-list)) ) >= ( (length(filter is-dimer2syk1active? aggregate-list) + 5 )) )
[
if who not member? aggregate-list [ set aggregate-list lput myself aggregate-list ]
create-links-with t [show-link ]
ask t [
set aggregated true
if who not member? aggregate-list [ set aggregate-list lput myself aggregate-list ]
;set aggregate-list lput myself aggregate-list
merge ]
]
]
[;;else commands
print "nothing to print or do"
])
]
)
]
]
I can't work out what all the filtering and merging is supposed to do so I can't fix your code directly. Breed is a variable of the turtle in the same way as any other attribute, so I would count the different breeds and store that in local variables, then use the local variables for the if/then decisions.
Something like (assumes there are two breeds called breedA and breedB):
if any? candidates
[ let numA count candidates with [breed = breedA]
let numB count candidates with [breed = breedB]
if numA < numB / 2
[ print "A is rare here"
]
]
If you want to do something specifically with the different candidate types, then you can subset candidates by breed and do the counting after:
if any? candidates
[ let candidatesA candidates with [breed = breedA]
let numA count candidatesA
let candidatesB candidates with [breed = breedB]
let numB count candidatesB
if numA < numB / 2
[ ask candidatesA
[ set color red
]
]
]

going to target and home within one tick

I want my turtles to go to a target and go home again (using fd) within the time period of one tick. The model works fine when I execute the go procedure one by one ("go once"). Execution as a loop (without altering the code), however, doesn't work: The turtles don't move; the tick counter is updated though. It seems that the functions are executed correctly but the movement is not visualized. Thanks for your help!
to go
if ticks >= 365 [ stop ]
move
if not any? turtles with [ shape = "person" and movement-done? = false ]
[
tick
ask turtles with [ shape = "person" ] [
set movement-done? false
set reached? false
set at-home-again? false ]
]
end
to move
ask turtles with [ shape = "person" ]
[
if movement-done? = false
[
ifelse reached? = false
[
ifelse patch-here = target [ set reached? true set at-home-again? false ]
[
face target
fd 1
]
]
[
ifelse at-home-again? = false
[
ifelse patch-here = myhome [ show "patch-here = myhome" set at-home-again? true ]
[
face myhome
fd 1
]
]
[
set movement-done? true
]
]
]
]
end

Changing the color of myself

I have the following code where I ask some agents to die:
breed [ readers reader ]
breed [ pallets pallet ]
breed [ trucks truck ]
readers-own [
truck-being-served
pallet-being-served ]
to complete-service [ ?reader ]
ask ?reader [
ask pallet-being-served [ die ]
set pallet-being-served nobody
ask truck-being-served [
if not any? pallets-in-truck [
ask self [ die ]
ask myself [
set truck-being-served nobody
set color green
set next-completion-time 0
]
]
]
]
end
Where pallet-being-served and truck-being-served are pallets and truck agents.
The problem is that the inner ask myself [... it seems not working, bacause the color of the ?reader agent does not change to green. Here I am not sure if I am referring to ?reader or to truck-being-served agent which I have just killed. I would like to change the color to green. Is ask myself [ ... referring to the reader agent?
Regards.
Asking a dead turtle to do something has no effect. Also, please provide a minimal runnable example that illustrates your problem.
globals [next-completion-time]
breed [ readers reader ]
breed [ pallets pallet ]
breed [ trucks truck ]
readers-own [
truck-being-served
pallet-being-served ]
trucks-own [pallets-in-truck]
to setup
ca
create-readers 1
create-trucks 1
create-pallets 1
ask reader 0 [set truck-being-served truck 1]
ask truck 1 [set pallets-in-truck pallets]
ask reader 0 [set pallet-being-served one-of [pallets-in-truck] of truck 1]
end
to complete-service ;reader proc
ask pallet-being-served [ die ]
ask truck-being-served [
if not any? pallets-in-truck [
ask myself [
set color green
set next-completion-time 0
]
die
]
]
end
to test
setup
ask reader 0 [complete-service]
end