Insurgents & Soldiers Fighting

Insurgents & Soldiers Fighting preview image

1 collaborator

Default-person David Knoke (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.3.0 • Viewed 7 times • Downloaded 0 times • Run 0 times
Download the 'Insurgents & Soldiers Fighting' modelDownload this modelEmbed this model

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


Info tab cannot be displayed because of an encoding error

Comments and Questions

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

Click to Run Model

;;Insurgents & Soldiers Fighting
;; David Knoke, University of Minnesota
;; August 9, 2024

globals[
     jungle
     Ninsurgentsalive    ;; number of insurgents alive
     Ninsurgentsdead     ;; number of insurgents dead
     Pinsurgentsalive    ;; percentage of insurgents alive
     Pinsurgentsdead     ;; percentage of insurgents dead
     Nsoldiersalive      ;; number of soldiers alive
     Nsoldiersdead       ;; number of soldiers dead
     Psoldiersalive      ;; percentage of soldiers alive
     Psoldiersdead       ;; percentage of soldiers dead
     InsurgentsWin       ;; Insurgents eliminated all soldiers
     SoldiersWin         ;; Soldiers eliminated all insurgents
]

breed [ insurgents insurgent ]
breed [ soldiers soldier ]

insurgents-own [ detect detect_time ]
soldiers-own [ flockmates alert ]
patches-own [ density ]

to setup
  clear-all
  setup_patches
  setup_agents
  reset-ticks
end 

to setup_patches
   ask patches
  [ set density (random 10000) ]
  repeat 2 [  diffuse density 1 ]
  ask patches
  [ set pcolor scale-color green density 9000 1000 ]
  set jungle max-n-of 20 patches [ density ]     ;; Create some high-density jungle patches
end 

to setup_agents
   create-insurgents N-Insurgents
  [ set size 4
    setxy random-xcor random-ycor
    set color red
    set shape "person"
    set detect False
    set detect_time 0
    set InsurgentsWin 0
  ]

  create-soldiers N-Soldiers
  [ set size 4
    set xcor random-normal 0 1
    set ycor random-normal 0 1
    set color blue
    set shape "person"
    set flockmates no-turtles
    set alert False
    set SoldiersWin 0
  ]
end 

to go
  if not any? insurgents [ stop ]
  if not any? soldiers [ stop ]
  ask insurgents [ insurgents_movement ]
  ask soldiers [ soldiers_movement ]
  tally
  tick
end 

to insurgents_movement
  let p max-one-of patches in-radius 20 [ density ]
  if [ density ] of p > density [           ;;  Go to nearest high-density jungle patches to hide from soldiers
    face p
    forward  1 ]
  if any? soldiers in-radius 3              ;; If seen by soliders, stay detected for 20 ticks
  [ set detect_time 20 ]
  ifelse detect_time > 0
  [ set detect True
    face min-one-of turtles with [ color != red ] [ distance myself ]
    set heading heading + 180 + random-normal 0 30
    forward 1
  set detect_time detect_time - 1 ]
  [ set detect False ]
  if any? soldiers with [ alert = False ] in-radius 8   ;; Insurgents  attack nonalert soliders
  [
   insurgents-attack-soldiers
  ]
end 

to soldiers_movement
  if any? insurgents in-radius 3
  or any? soldiers with [ alert = True ] in-radius 1
  [ set alert True ]
  ifelse any? insurgents with [ detect = True ]  in-radius 15
  [
   ifelse alert = True
  [ face min-one-of insurgents with [ detect = True ] [ distance myself ]
    set heading heading
    forward 1 ]
  [ flock
    forward 1 ]
  if any? insurgents-here
  [ soldiers-attack-insurgents ]
  ]
   [ set alert False
    flock
    forward 1 ]
end 

to soldiers-attack-insurgents
  let x random 100
  ifelse x > Soldier_kill_rate           ;; Default kill rate is 50% of insurgents, 50% of soldiers
  [
  ask min-one-of insurgents with [ detect = True ] [ distance myself ] [ die ]
  ]
  [ die ]
end 

to insurgents-attack-soldiers
  set detect True
  let x random 100
  ifelse x > Insurgent_kill_rate                          ; 70% kill soldier and 30% insurgent dies
  [ die ]
  [ let insurgent-target one-of soldiers with [ alert = False ] in-radius 8
    ask insurgent-target [ die ]
  ask soldiers in-radius 8 [ set alert True ]]
end 


;;  Count the numbers and percentages of alive and dead insurgents and soldiers

to tally
  set Ninsurgentsalive count turtles  with [ color = red ]
  set Ninsurgentsdead (N-Insurgents - Ninsurgentsalive)
  set Nsoldiersalive count turtles  with [ color = blue ]
  set Nsoldiersdead (N-Soldiers - Nsoldiersalive)
  set Psoldiersalive (Nsoldiersalive / N-Soldiers) * 100
  set Psoldiersdead (Nsoldiersdead / N-Soldiers) * 100
  set Pinsurgentsalive (Ninsurgentsalive / N-Insurgents) * 100
  set Pinsurgentsdead (Ninsurgentsdead / N-Insurgents) * 100
;; Change one of two monitors to indicate which side eliminated the other
  if not any? turtles with [ color = red ] [ set SoldiersWin 1 ]
  if not any? turtles with [ color = blue ] [ set InsurgentsWin 1 ]
end 




;; flocking code

to flock
  find-flockmates
  let nearest-neighbor min-one-of flockmates [distance myself]
  if any? flockmates
    [
      ifelse distance nearest-neighbor < 1
        [ separate ]
        [ align
          cohere ] ]
end 

to find-flockmates
  set flockmates other soldiers in-radius 8
end 

to find-nearest-neighbor
  let nearest-neighbor min-one-of flockmates [distance myself]
end 

;;; SEPARATE

to separate
  let nearest-neighbor min-one-of flockmates [distance myself]
  turn-away ([heading] of nearest-neighbor) 1.5
end 

;;; ALIGN

to align  ;; turtle procedure
  turn-towards average-flockmate-heading 5
end 

to-report average-flockmate-heading
  let x-component sum [dx] of flockmates
  let y-component sum [dy] of flockmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end 

to cohere
  turn-towards average-heading-towards-flockmates 3
end 

to-report average-heading-towards-flockmates
  let x-component mean [sin (towards myself + 180)] of flockmates
  let y-component mean [cos (towards myself + 180)] of flockmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end 

to turn-towards [new-heading max-turn]
  turn-at-most (subtract-headings new-heading heading) max-turn
end 

to turn-away [new-heading max-turn]
  turn-at-most (subtract-headings heading new-heading) max-turn
end 

to turn-at-most [turn max-turn]
  ifelse abs turn > max-turn
    [ ifelse turn > 0
        [ rt max-turn ]
        [ lt max-turn ] ]
    [ rt turn ]
end 

There is only one version of this model, created 3 days ago by David Knoke.

Attached files

File Type Description Last updated
Insurgents & Soldiers Fighting.png preview Preview for 'Insurgents & Soldiers Fighting' 3 days ago, by David Knoke Download

This model does not have any ancestors.

This model does not have any descendants.