I have a model where patches have the value of "habitat" and I have a csv file of animal selection ratios. The goal is that I want each agent to survey the habitats within their next step to decide where to move.
The code is as follows.
to move_females_test
let this_step (one-of step_length_lsf / patch-length)
let available [habitat] of patches in-radius this_step
let unique-habitats remove-duplicates available
print available
print unique-habitats
ifelse length unique-habitats = 1 [
; If there is only one unique habitat, move as per the existing code
set heading (one-of turning_angles_lsf * 100)
fd this_step
] []
end
so for each tick the turtle generates something like this
[Pastures Pastures Pastures Pastures Pastures]
in which case there is no need to decide - so they move randomly. Or something like this
[Pastures Pastures Pastures Scrub Arable]
In which case I want them to draw from a list called selection I have initalised and populated with values from a habitat selection model. The list looks like this, as an example.
[[Pastures 1] [Pastures 1] [Arable 2.485988192] [Scrub 0.684336963] [Arable 0.994063271] [Pastures 1.009595509]
What I would like to do - is to match the habitats that present themselves in the unique-habitats list with a randomly selected matching habitat in the selection list and then select the highest value to decide where to go.
i.e in pseduocode.
to move_females_test
let this_step (one-of step_length_lsf / patch-length)
let available [habitat] of patches in-radius this_step
let unique-habitats remove-duplicates available
print available
print unique-habitats
ifelse length unique-habitats = 1 [
; If there is only one unique habitat, move as per the existing code
set heading (one-of turning_angles_lsf * 100)
fd this_step
]
[let ratios [foreach habitat in unique-habitats [select one-of habitat in select]
;ratio will look like this
;[[Pastures 1] [Arable 0.91]]
let sorted-ratios sort-by [[- item 1 ?] of ?] ratios
let highest-ratio first sorted-ratios
let target-patch one-of patches with [habitat = item 0 highest-ratio]
set heading target-patch
fd this_step
]
end
I always find lists in a list somehow nasty to handle in NetLogo. Especially in your case where they contain two different variable types. But I think I found a solution to your problem. Not sure if this is the most efficient/concise but...