San88
San88

Reputation: 1

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 (party) 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.

How to create a turtle variable that is conditionalised on some patch variable value?

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

Upvotes: 0

Views: 41

Answers (1)

Wade Schuette
Wade Schuette

Reputation: 1473

I answered in google groups. The routine "Tally" does relevant calculations and can easily be tweaked to cope with ties if you want to. I didn't even try to compress this into one long command line. I added party names. Here's the tally section, the rest is in netlogo-users group. I didn't try to make it polished but it seems to work.

By doing things the long way I never needed to use an anonymous block so I think it should run in any version of NetLogo back to and including 5.

;;==========================  Wade added 6/17/2022

to tally
  ;; to make party-names equal to party-number so my code fits your code
  set party-names []
  ask parties [ set name self set party-names lput name party-names]
  set party-names sort party-names



  print (word "Number of PATCHes: "  ( count patches  ) )
  print (word "Number of VOTES: " (   sum [votes] of patches ) )
  print " "
  ask parties [ print (word name " got this many PATCHES: " (count patches with [closest-party = [name] of myself ])) ]

  print " "
    ask parties
  [
    print
    (word
        name " got this many VOTES: "
      (sum [votes] of patches with [closest-party = [name] of myself ])
    )
  ]


  ;; generate a list of districts in use
  let district-list  ( remove-duplicates ( sort [district] of patches ) )
  print (word "list of districts in use: "  district-list )

  ;;===================================================================================
  let working-dislist district-list
  let dist 0
  let dist-count 0
  let total-vote 0

  while [ length working-dislist > 0 ][
    set dist first working-dislist
    set dist-count sum [votes] of patches with [ district = dist ]
    print (word " for district: " dist " this many votes: " dist-count)
    set total-vote total-vote + dist-count
    set working-dislist butfirst working-dislist
  ]
    print ( word "========================\nTotal votes by district: " total-vote )

  print " now analyzing by district and party "
  set working-dislist district-list
  let working-party-list party-names
  let pname " unkown "

  set dist 0
  set dist-count 0
  set total-vote 0
  let dp-sum 0

  while [ is-list? working-dislist and length working-dislist > 0 ][
    set dist first working-dislist
    set dist-count sum [votes] of patches with [ district = dist ]
    print (word " ------> for district: " dist " this many votes: " dist-count " broken out below" )

    set working-party-list party-names
    set   dp-sum 0
    let district-winner ""
    let winning-votes -99

    while [ is-list? working-party-list  and length working-party-list > 0 ][
      set pname first working-party-list
      set working-party-list butfirst working-party-list
      let details sum [votes] of patches with [ district = dist and closest-party = pname ]
      set dp-sum (dp-sum + details)
      print ( word ".....in district " dist " party " pname ": has this many votes: " details )
      if ( details > winning-votes ) [
        set winning-votes   details
        set district-winner   pname
      ]

    ]
    print (word ".................... subtotal: " dp-sum "  District Winner with " winning-votes " is " district-winner "\n")
      set dp-sum 0
    set working-party-list party-names

    set total-vote total-vote + dist-count
    set working-dislist butfirst working-dislist
  ]
    print ( word "======== \nTotal votes by party by district: " total-vote )
   ;;===================================================================================




end

The output produces totals at increasing levels of granularity ending with what I THINK you want. You still need to break ties but that's easy to do with this code.

------> for district: 49 this many votes: 12801.954344857973 broken out below .....in district 49 party (party 0): has this many votes: 302.9649675865895 .....in district 49 party (party 1): has this many votes: 1457.5052709454735 .....in district 49 party (party 2): has this many votes: 0 .....in district 49 party (party 3): has this many votes: 830.4743848217157 .....in district 49 party (party 4): has this many votes: 1393.6496323186223 .....in district 49 party (party 5): has this many votes: 4418.5271517613455 .....in district 49 party (party 6): has this many votes: 147.01914867815847 .....in district 49 party (party 7): has this many votes: 3005.0957136873785 .....in district 49 party (party 8): has this many votes: 976.2462740954787 .....in district 49 party (party 9): has this many votes: 270.4718009632105 .................... subtotal: 12801.954344857973 District Winner with 4418.5271517613455 is (party 5)

Upvotes: 0

Related Questions