2022-06-18

Netlogo: calculate turtle variable for different patch types

I'm trying to calculate a turtle variable (district-winner) for every value of a certain patch variable (district). Patch "district" ranges from 0 to 100. The patches are also endowed with a feature votes. The turtle (called party in the code) receives a patches' votes if it is the closest turtle to that patch. For every patch district I wish to indicate the turtle that receives most votes in that particular patch district, and stall the information in some variable (e.g. district-vote or district-winner). That is where I got stuck.

I have tried working with "foreach" and "sort-by", but so far I haven't managed to create a code without error that accounts for every different district value. The code for assigning votes to some closest turtle works (see code). But I haven't figured out how to calculate the votes won by turtles per district for each district.

Do you know how to create a turtle variable that is conditionalised on some patch variable value?

Any help would be much appreciated!

Working code, relevant lines:

to update-support
  ask patches [set closest-party min-one-of parties [distance myself]]
      ;;patches find their closest party
  ask parties [set mysize sum [votes] of patches with [closest-party = myself]] 
      ;;each party sums the votes on patches for which it is the closest party    
end

Some attempt to run the code for different patch districts:

to update support
 ask patches [
   set closest-party min-one-of parties [distance myself]
     ;;patches find their closest party
   set closest-party-list [ (list closest-party) ] of patches
      (foreach district-number
        [set district-vote-map map [ifelse-value (? != district) [? = district] [?]] closest- 
        party-list])
   ;;and then link this closest-party-list to some code for asking parties?? 
   ]
  ]
end

Another attempt

to update support
  ask patches [
    set closest-party min-one-of parties [distance myself]]
        ;;patches find their closest party
  ask parties [
    set district-vote [
      (foreach [(district) of patches] sum [votes] of patches with [closest-party = myself] and 
      [district = [?]])]
end


No comments:

Post a Comment