Political Modeling & Election Simulations
by Mary Lynn Reed

Listing One

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; One-Dimensional Election Model
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

globals [ Prop_Indep NumDems NumReps NumIndep DemVotes RepVotes 
      DemCand RepCand Winner DemWins RepWins DemNear RepNear NumIter ]
breeds [ voters candidates centralpoint ]
turtles-own [ party ]
voters-own [ infolevel ]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
  ca
  cct 1 [  ;; creates the central "neutral line"
    set breed centralpoint
    set color white
    set shape "line"
    set size 10.0
  ]
  set Prop_Indep 1 - Prop_Dem - Prop_Rep 
  if (Prop_Indep < 0)
    [ set Prop_Rep 1 - Prop_Dem 
      set Prop_Indep 0 ]
  setup-voters
  setup-candidates
  set DemWins 0
  set RepWins 0
  set DemNear 0
  set RepNear 0
  set NumIter 0
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-voters
  set NumDems round NumberOfVoters * Prop_Dem
  set NumReps round NumberOfVoters * Prop_Rep
  set NumIndep round NumberOfVoters - NumDems - NumReps
  if NumIndep < 0 [ set NumIndep 0 ]
  foreach n-values NumDems [?] 
  [
    ask random-one-of patches with [pycor = 0 and pxcor <= Epsilon_D] [
      sprout 1 [
    set breed voters
    set color blue
    set size .6
    set shape "box"
    set infolevel random-float 1.0
      ]
    ]
  ]
  foreach n-values NumReps [?]
  [
    ask random-one-of patches with [pycor = 0 and pxcor >= 0 - Epsilon_R] [
      sprout 1 [
    set breed voters
    set color red
    set size .6
    set shape "box"
    set infolevel random-float 1.0
      ]
    ]
  ]
  foreach n-values NumIndep [?]
  [
    ask random-one-of patches with [pycor = 0] [
      sprout 1 [
    set breed voters
    set color green
    set size .6
    set shape "box"
    set infolevel random-float 1.0
      ]
    ]
   ]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-candidates
  sprout-RepCand
  sprout-DemCand
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to sprout-RepCand
  ask random-one-of patches with [pycor = 0 and pxcor >= 0] 
    [ sprout 1 
      [ ;; set up Republican Candidate
      set breed candidates
      set color red 
      set size 5.0
      set shape "person" 
      set RepCand who
      ]
    ]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to sprout-DemCand
ask random-one-of patches with [pycor = 0 and pxcor <= 0] 
  [ sprout 1 
    [  ;; set up Democratic Candidate
    set breed candidates
    set color blue
    set size 5.0
    set shape "person"
    set DemCand who
    ]
  ]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to count-votes
  locals [ dV rV distD distR r1 r2 ]
  set dV 0
  set rV 0
  ask voters 
    [
     set distD distance-nowrap turtle DemCand
     set distR distance-nowrap turtle RepCand
     if Variable_Information? [ 
       set r1 random-float 2 * screen-edge-x
       set r2 random-float 2 * screen-edge-x
       set distD distD * infolevel + r1 * (1 - infolevel)
       set distR distR * infolevel + r2 * (1 - infolevel)
     ]
     ifelse Party_Loyalty? [
       ifelse color != green ; if voter is Independent, no Party Loyalty
       [  set r1 random 100
      ifelse r1 < PartyPreferenceWeight ; if in "party loyalty region"
       [ ifelse color = blue
           [ set dV dV + 1 ]  ; if blue, vote Dem
           [ set rV rV + 1 ]  ; if red, vote Rep
       ]
       [ ifelse distD >= distR   ; outside the party loyalty region
           [ set rV rV + 1 ]     ; vote by distance to candidates
           [ set dV dV + 1 ]
       ]   
       ]
       [ ifelse distD >= distR ; if voter is Independent, no Party loyalty
     [ set rV rV + 1 ]
     [ set dV dV + 1 ]
       ]
      ]
      [  ifelse distD >= distR ;  in case No Party Loyalty
     [ set rV rV + 1 ]
     [ set dV dV + 1 ]
      ]
     ]
  set RepVotes rV
  set DemVotes dV
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to update-candidates
  locals [ dVorig rVorig iter ]
  ifelse DemVotes > RepVotes
    [ ;; Loser (Republican Candidate) Moves Platform To Get More Votes
    set rVorig RepVotes
    set iter 0
     while [ RepVotes <= rVorig and iter < 5 ]
     [
       ask turtle RepCand [ die ]
       sprout-RepCand
       count-votes
       set iter iter + 1
     ]
    ]
    [ ;; Loser (Democratic Candidate) Moves Platform To Get More Votes    
    set dVorig DemVotes
    set iter 0 
     while [ DemVotes <= dVorig and iter < 5 ] 
     [
       ask turtle DemCand [ die ]
       sprout-DemCand
       count-votes
       set iter iter + 1
     ]
    ] 
end  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to do-plots
  set-current-plot "Election Results"
  set-current-plot-pen "Dem"
  plot 100 * DemVotes / NumberOfVoters
  set-current-plot-pen "Rep"
  plot 100 * RepVotes / NumberOfVoters
  set-current-plot "Cumulative Wins"
  set-current-plot-pen "Dem"
  plot DemWins
  set-current-plot-pen "Rep"
  plot RepWins
  set-current-plot "Candidate Platform Near Median"
  set-current-plot-pen "Dem"
  plot 100 * DemNear / NumIter
  set-current-plot-pen "Rep"
  plot 100 * RepNear / NumIter
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
  count-votes
  set NumIter NumIter + 1
  ifelse DemVotes > RepVotes
  [ set Winner "Dem"
    set DemWins DemWins + 1 ]
  [ set Winner "Rep" 
    set RepWins RepWins + 1 ]
  ask turtle DemCand [ if xcor > -6  [ set DemNear DemNear + 1 ] ]
  ask turtle RepCand [ if xcor < 6 [set RepNear RepNear + 1 ] ]
  do-plots
  update-candidates
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;





4



