Election Thing

Election Thing preview image

1 collaborator

Turtlezero2-white-048 James Steiner (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.2.2 • Viewed 129 times • Downloaded 14 times • Run 0 times
Download the 'Election Thing' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals
[
district-count
  candidate-count
]

patches-own [ patch-district nearest-candidate patch-board votes patch-winner ]

breed [ boards board]
boards-own
[ board-district
  district-patches
  winner-tally
  winner-candidate
]

undirected-link-breed [ tallies tally ]
tallies-own
[ tally-board
  tally-candidate
  tally-votes
  is-winner?
]

breed [ candidates candidate ]
candidates-own
[ districts-won
  boards-won
]

to setup
  clear-all

  setup-districts
  setup-boards
  setup-candidates
  setup-tallies
  select-nearest-candidates
  setup-votes

  find-district-winners
  show-results
  reset-ticks
end 

to setup-districts
  set district-count $districts
  let d 1
  ask n-of district-count patches [ set patch-district d set d d + 1 ]
  while [ any? patches with [ patch-district = 0 ] ]
  [ ask patches with [ patch-district = 0 and any? neighbors4 with [ patch-district != 0 ] ]
    [ set patch-district [ patch-district ] of one-of neighbors4 with [ patch-district != 0 ] ]
  ]
end 

to setup-boards
  foreach range district-count
  [ i ->
    create-boards 1
    [ set board-district i + 1
      set district-patches patches with [ patch-district = [board-district ] of myself ]
      ask district-patches [ set patch-board myself ]
      set shape "token"
      set color pcolor
      set size max (list 1 (10 / patch-size) )

      ;; place the board near the geometric center of the district
      let mx mean [ pxcor ] of district-patches
      let my mean [ pycor ] of district-patches
      let mp min-one-of district-patches [ distancexy mx my ]
      move-to mp
    ]
  ]
end 

to setup-candidates
  set candidate-count $candidates
  create-candidates candidate-count
  [ set shape "token-2"
    set color red + 10 * (who mod candidate-count)
    set size max (list 1 (20 / patch-size) )
    setxy random-pxcor random-pycor
  ]
end 

to setup-tallies
  ask boards
  [
    let this-district  board-district
    create-tallies-with candidates
    [ set tally-board one-of both-ends with [ breed = boards ]
      set tally-candidate one-of both-ends with [ breed = candidates ]
      hide-link
      set is-winner? false
    ]
  ]
end 

to setup-votes
  ask patches
  [
    set votes 10 + random 1000
  ]
end 

to select-nearest-candidates
  ask patches
  [ set nearest-candidate min-one-of candidates [ distance myself ]
  ]
end 

to find-district-winners
  ask tallies
  [ let this-board tally-board
    let this-candidate tally-candidate
    set tally-votes sum [ votes ] of patches with [ patch-board = this-board and nearest-candidate = this-candidate ]
    set is-winner? false
  ]
  ask boards
  [ ;; sort tallies by votes
    ;; break ties in favor of "older" candidate
    let sorted-tallies
    sort-by
    [ [ a b ] ->
      ( [ tally-votes ] of a > [ tally-votes] of b )
      or
      ( [ who ] of [ tally-candidate ] of a < [ who ] of [ tally-candidate ] of b )
    ]
    ( my-tallies with [ tally-votes > 0 ])
    if length sorted-tallies > 0
    [ set winner-tally first sorted-tallies
      ask winner-tally [ set is-winner? true ]
      set winner-candidate [ tally-candidate ] of winner-tally
      ask district-patches [ set patch-winner [ winner-candidate ] of myself ]
    ]
  ]
end 

to-report differing [ property ]
  report run-result property != [ run-result property ] of myself
end 

to show-results
  ifelse show-tally-links?
  [ let fat-line 5 / patch-size ;; 5 pixels
  let med-line 3 / patch-size ;; 3 pixels
  ask tallies
  [ ifelse tally-votes = 0
    [ hide-link ]
    [ ifelse not is-winner?
      [ set color black
        set thickness med-line
      ]
      [ set color white
        set thickness fat-line
      ]
      show-link
    ]
  ]
  ]
  [ ask tallies [ hide-link ]
  ]
  ask patches
  [ ;; color of district is for patch-winner
    ifelse is-candidate? patch-winner
    [ ;; outline of districts
      ifelse ( any? neighbors4 with [ differing [-> patch-district ] ] )
      [ set pcolor 1 + [ color ] of patch-winner ]
      [ set pcolor [ color ] of patch-winner ]

      ;; outline around candidate showing patches that is is the nearest-candidate for
      if  ( any? neighbors4 with [ differing [-> nearest-candidate ] ] )
      [ set pcolor [ color - 2 ] of patch-winner
      ]
          ]
    [ set pcolor black
      ;; outline around the nearest candidate
      if  ( any? neighbors with [ differing [-> nearest-candidate ] ] )
      [ set pcolor 3
      ]
    ]

  ]
end 

to move-candidates
  ask candidates
  [ rt random 360
    jump 1
    setxy pxcor pycor
  ]
end 

to migrate-candidates
  move-candidates
  select-nearest-candidates
  setup-votes
  find-district-winners
  show-results
end 

to go
end 

There are 2 versions of this model.

Uploaded by When Description Download
James Steiner almost 2 years ago Improved seperation of model and visualization Download this version
James Steiner almost 2 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Election Thing.png preview Preview for 'Election Thing' almost 2 years ago, by James Steiner Download

This model does not have any ancestors.

This model does not have any descendants.