Emergent Polarity Model with Bargaining

Emergent Polarity Model with Bargaining preview image

1 collaborator

Default-person Jan Bakaj (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.0.2 • Viewed 102 times • Downloaded 11 times • Run 0 times
Download the 'Emergent Polarity Model with Bargaining' 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 [
  war-count
  world-casualties
  cas-count-list
]

breed [ capitols capitol ]

breed [ paths path ]

undirected-link-breed [ truces truce ]

directed-link-breed [ combat-links combat-link ]

paths-own [
  f-resource
  path-capital
]

links-own[
  victory-ratio
  casualty-count
]

truces-own[
  truce-timer
]

patches-own [
  capital
  state
  resource-total
  resource-N
  resource-split
  neighbors-list
  neighbors-set
  target
  aggresors
  combat-patches
  combat-paths
  cmbt-link
  Local-aggressors
  excl?
  Enclosed-State
  predator
  x
  cost
  target-local
]

to setup
  clear-all
  let s random 2147483647 * 2
  set s s - 2147483647
  show s
  random-seed s
  set-default-shape capitols "star"
  set war-count 0
  set cas-count-list []
  ask patches
  [
    set predator false
    set capital nobody
    set neighbors-list []
    set neighbors-set nobody
  ]
  ask n-of (states) patches [
    sprout-capitols 1 [
      set size 0.5  ;; easier to see
      let black? true
      set pcolor (sentence (random 256) (random 256) random (256))
      if max pcolor <= 80 [ set black? false ]
      if-else black? [ set color black ] [ set color white ]
      set capital self
    ]
  ]
  ask n-of ( states * predators / 100 ) patches [ set predator true ]
  grow-nations
  ask patches with [ capitols-here != no-turtles ]
  [
    update-state
    set resource-n random-normal 50 10
    if resource-n < 0 [ set resource-n 0 ]
    set resource-total resource-n
  ]
  ask patches
  [
    set target nobody
    set target-local nobody
    set state nobody
    set local-aggressors no-patches
    set combat-patches no-patches
    set combat-paths no-turtles
    set x 2
    set cost x * ( cost-lb + random-float ( cost-ub - cost-lb ) )
  ]
  reset-ticks
end 

to grow-nations
  loop [
    let empty patches with [capital = nobody]
    if not any? empty [ stop ]
    ask empty
      [ set capital [capital] of one-of neighbors4
        if capital != nobody [ set pcolor [pcolor] of capital ] ]
  ]
end 

to get-resources
 foreach ( [ patch-here ] of capitols )
  [
    [ capital-patch ] -> ask capital-patch
    [
      let i 0
      let z 0
      ask state
      [
        set i random-normal 2 5
        set z z + i
      ]
      set resource-N resource-n + z + sum [ f-resource ] of paths with [ path-capital = [ capital ] of capital-patch ]
      if resource-n < 0 [ set resource-n 0 ]
      ask paths with [ path-capital = [ capital ] of capital-patch ] [ set f-resource 0 ]
      set resource-total resource-N
    ]
  ]
end 

to go
  if count capitols = 1 [ stop ]
  if any? patches with [ capital = nobody ] = true
  [
    write "C-N: " show [ list pxcor pycor ] of patches with [ capital = nobody ]
  ]
  ask patches with [ capitols-here != no-turtles ] [ update-neighbors ]
  ep-model
  get-resources
  set cas-count-list remove-duplicates cas-count-list
  tick
end 

to EP-Model
  if-else bargaining? [ epm-decision-phase-barg ] [ epm-decision-phase ]
  epm-interaction-phase
  epm-structural-change-phase
  check-exclave
end 

;----------------- EPM DECISION COMPONENTS

to EPM-decision-phase
  let caps patches with [ capitols-here != no-turtles ]
  ask caps [ update-neighbors ]
  ask caps with [ predator ] [ epm-pick-target ]
  ask caps [ epm-allocate-resources ]
end 

to EPM-Pick-Target
  let capital-patch self
  if ( target = nobody ) and ( not any? neighbors-set with [ target = [ capital ] of capital-patch ] )
  [
    if any? neighbors-set with [ resource-total = 0 or ( [ resource-total ] of capital-patch / [ resource-total ] of patch-here ) >= superiority-ratio ]
    [
      set target min-one-of neighbors-set [ [ resource-total ] of patch-here ]
    ]
  ]
  epm-pick-local-target
end 

to EPM-Pick-Local-Target
  let capital-patch self
  ;------- SELECT COMBAT PATH IN NEW ATTACK -----
  if target != nobody and combat-patches = no-patches and any? state with [ any? neighbors4 with [ capital = [ target ] of capital-patch ] ]
  [
    ask one-of state with [ any? neighbors4 with [ capital = [ target ] of capital-patch ] ]
    [
      let local-attacker self
      let local-target one-of neighbors4 with [ capital = [ target ] of capital-patch ]
      ask capital-patch [ set combat-patches ( patch-set local-attacker local-target ) ]
    ]
    epm-create-combat-path
  ]
end 

to EPM-Create-Combat-Path
  let capital-patch self
  ask combat-patches
  [
    sprout-paths 1
    [
      let z self
      set shape "circle"
      set color red
      set size 0.5
      set f-resource 0
      set path-capital [ capital ] of patch-here
      ask capital-patch [ set combat-paths ( turtle-set combat-paths z ) ]
    ]
  ]
  ask combat-paths with [ path-capital = [ capital ] of capital-patch ]
  [
    create-combat-link-to one-of other [ combat-paths ] of capital-patch
    [
      set victory-ratio 0
      set casualty-count 0
      set thickness 0.08
      set color black
      let l self
      ask capital-patch [ set cmbt-link l ]
    ]
  ]
  set war-count war-count + 1
end 

to EPM-Allocate-Resources
  let capital-patch self
  let cpr one-of patch-set [ patch-here ] of ( turtle-set [ capital ] of ( combat-patches with [ not member? self ( [ state ] of capital-patch ) ] ) )
  update-neighbors
  set resource-split resource-N / ( count neighbors-set )
  ask paths with [ path-capital = [ capital ] of capital-patch ]
  [
    set f-resource [ resource-split ] of capital-patch
    ask capital-patch [ set resource-n resource-n - resource-split ]
  ]
  if resource-n > -1 and resource-n < 0 [ set resource-n 0]
end 

;----------------- EPM INTERACTION COMPONENTS

to EPM-Interaction-phase
  epm-front-conquest
end 

to EPM-Front-Conquest
  ask patches with [ capitols-here != no-turtles and target != nobody ]
  [
    let capital-patch self
    let cpr one-of patch-set [ patch-here ] of ( turtle-set [ capital ] of ( combat-patches with [ not member? self ( [ state ] of capital-patch ) ] ) )
    let z one-of [f-resource] of combat-paths with [ path-capital != [capital] of capital-patch ]
    let i combat-paths
    let g combat-patches
    ask i with [ path-capital = [ capital ] of capital-patch ]
    [
      let f f-resource
      ask other ( [ i ] of capital-patch )
      [
        if-else ( f-resource - f * k ) > 0
        [
          set f-resource ( z - f * k)
          ask capital-patch [ ask cmbt-link [ set casualty-count casualty-count + f * k ] ]
          set world-casualties world-casualties + f * k
        ]
        [
          ask capital-patch [ ask cmbt-link [ set casualty-count casualty-count + z ] ]
          set world-casualties world-casualties + f-resource
          set f-resource 0
        ]
      ]
    ]
    ask i with [ path-capital != [ capital ] of capital-patch ]
    [
      if-else member? ( one-of other ( [ g ] of capital-patch ) )  local-aggressors
      [
        ask other ( [ i ] of capital-patch )
        [
          if-else ( f-resource - z * k ) > 0
          [
            set f-resource ( f-resource - z * k)
            ask capital-patch [ ask cmbt-link [ set casualty-count casualty-count + z * k ] ]
            set world-casualties world-casualties + z * k
          ]
          [
            let h f-resource
            ask capital-patch [ ask cmbt-link [ set casualty-count casualty-count + h  ] ]
            set world-casualties world-casualties + f-resource
            set f-resource 0
          ]
        ]
      ]
      [
        set local-aggressors ( patch-set local-aggressors ( g with [ member? self [ state ] of capital-patch ] ) )
      ]
    ]
    ask cmbt-link
    [
      if-else one-of [ f-resource ] of i with [ path-capital != [ capital ] of capital-patch ]  = 0
      [
        if one-of [ f-resource ] of i with [ path-capital = [ capital ] of capital-patch ] != 0 [ set victory-ratio -1 ]
      ]
      [
      set victory-ratio ( one-of [ f-resource ] of i with [ path-capital = [ capital ] of capital-patch ] ) / ( one-of [ f-resource ] of i with [ path-capital != [ capital ] of capital-patch ] )
      ]
    ]
  ]
end 

;----------------- EPM STRUCTURAL CHANGE COMPONENTS

to EPM-Structural-Change-Phase
  ask patches with [ capitols-here != no-turtles ]
  [
    if target != nobody
    [
      let capital-patch self
      let cpr one-of patch-set [ patch-here ] of ( turtle-set [ capital ] of ( combat-patches with [ not member? self ( [ state ] of capital-patch ) ] ) )
      if-else [ victory-ratio ] of cmbt-link = -1
      [
        ask cmbt-link [ set cas-count-list lput casualty-count cas-count-list ]
        conquer-province
      ]
      [
        if-else [ victory-ratio ] of cmbt-link > v-ratio
        [
          ask cmbt-link [ set cas-count-list lput casualty-count cas-count-list ]
          conquer-province
        ]
        [
          if [ victory-ratio ] of cmbt-link < 1 / v-ratio
          [
            ask cmbt-link [ set cas-count-list lput casualty-count cas-count-list]
            surrender-no-capitulation
          ]
        ]
      ]
    ]
    update-neighbors
  ]
end 

to conquer-province

  let capital-patch self
  let cpr one-of patch-set [ patch-here ] of ( turtle-set [ capital ] of ( combat-patches with [ not member? self ( [ state ] of capital-patch ) ] ) )
  let i combat-patches
  let g combat-paths

  set resource-n ( resource-n + sum [ f-resource ] of g with [ capital = [capital] of capital-patch  ] )
  ask cpr
  [
    set resource-n ( resource-n + sum [ f-resource ] of g with [ path-capital = [ capital ] of cpr ] )
    if target != nobody
    [
      if one-of i with [ not member? self ( [ state ] of capital-patch ) ] = cpr  [ ask cmbt-link [ set cas-count-list lput casualty-count cas-count-list ] ]
      if one-of i with [ not member? self ( [ state ] of capital-patch ) ]  = one-of combat-patches with [  member? self ( [ state ] of cpr ) ] [ ask cmbt-link [ set cas-count-list lput casualty-count cas-count-list ] ]
    ]
    ask g [ die ]
    ask capital-patch [ set combat-patches no-patches ]
    set resource-n resource-n + sum [ f-resource ] of paths with [ patch-here = one-of i with [ capital = [ capital ] of cpr ] ]
    ask patches with [ member? (one-of i with [ member? self [ state ] of cpr ] ) combat-patches ]
    [
      let z self
      let y combat-patches
      let q combat-paths
      set resource-n resource-n + sum [ f-resource ] of q with [ path-capital = [ capital ] of z ]
      ask cpr [ set resource-n resource-n + sum [ f-resource ] of q with [ path-capital = [ capital ] of cpr ] ]
      ask cmbt-link [ set cas-count-list lput casualty-count cas-count-list ]
      ask q [ die ]
      set combat-patches no-patches
      set target nobody
      set target-local nobody
      ask i with [ member? self [ state ] of cpr ] [ set local-aggressors patch-set ( remove ( [ self ] of one-of other y ) ( [ self ] of local-aggressors ) ) ]
    ]
  ]
  if one-of i with [ not member? self ( [ state ] of capital-patch ) ] = cpr
  [
    ask cpr
    [
      set resource-n resource-n + sum [ f-resource ] of paths with [ path-capital = [ capital ] of cpr ]
      ask patches with [ target = [ capital ] of cpr ]
      [
        let z self
        set resource-n resource-n + sum [ f-resource ] of combat-paths with [ path-capital = [ capital ] of z ]
        ask combat-paths [ die ]
        set combat-patches no-patches
        set target nobody
      ]
      if combat-paths != no-turtles
      [
        ask cmbt-link [ set cas-count-list lput casualty-count cas-count-list ]
        ask combat-paths [ die ]
      ]
      let n resource-n / count state
      if other state != no-patches
      [
        ask other state
        [
          set resource-n n
          set resource-total n
          spawn-new-state
        ]
        ask other state [ update-neighbors ]
      ]
      ask capital-patch [ set resource-n resource-n + ( random-float 1 ) * n ]
      set resource-n 0
      set resource-split 0
      set combat-patches no-patches
      set target nobody
      set target-local nobody
      set neighbors-list []
      set neighbors-set no-turtles
      set resource-total 0
      set local-aggressors no-patches
      ask capital [ die ]
      set capital nobody
      set predator false
      set state no-patches
    ]
  ]

  ask i
  [
    set pcolor [ pcolor ] of capital-patch
    set capital [ capital ] of capital-patch
  ]

  if [ capital ] of cpr != [ capital ] of capital-patch [ ask cpr [ update-state update-neighbors ] ]
  set target nobody
  set target-local nobody
  set combat-patches no-patches
  update-state
  update-neighbors
end 

to surrender-no-capitulation
  let capital-patch self
  let cpr one-of patch-set [ patch-here ] of ( turtle-set [ capital ] of ( combat-patches with [ not member? self ( [ state ] of capital-patch ) ] ) )
  let i combat-paths
  let g combat-patches
  ask cpr [ set resource-n resource-n + sum [ f-resource ] of i with [ path-capital = [ capital ] of cpr ] ]
  set resource-n resource-n + sum [ f-resource ] of i with [ path-capital = [ capital ] of capital-patch ]
  ask i with [ member? self [ state ] of cpr ] [ set local-aggressors patch-set ( remove ( [ self ] of one-of other i ) ( [ self ] of local-aggressors ) ) ]
  ask combat-paths [ die ]
  set combat-patches no-patches
  set target nobody
  set target-local nobody
end 

to surrender-capitulation
  let capital-patch self
  let cpr one-of patch-set [ patch-here ] of ( turtle-set [ capital ] of ( combat-patches with [ not member? self ( [ state ] of capital-patch ) ] ) )
  let i combat-patches
  let g combat-paths

  set resource-n ( resource-n + sum [ f-resource ] of g with [ capital = [capital] of cpr  ] )
  ask capital-patch
  [
    set resource-n ( resource-n + sum [ f-resource ] of g with [ path-capital = [ capital ] of capital-patch ] )
    ask g [ die ]
    set combat-patches no-patches
    set resource-n resource-n + sum [ f-resource ] of paths with [ patch-here = one-of i with [ capital = [ capital ] of capital-patch ] ]
    ask patches with [ member? (one-of i with [ member? self [ state ] of capital-patch ] ) combat-patches ]
    [
      let z self
      let y combat-patches
      let q combat-paths
      set resource-n resource-n + sum [ f-resource ] of q with [ path-capital = [ capital ] of z ]
      ask capital-patch [ set resource-n resource-n + sum [ f-resource ] of q with [ path-capital = [ capital ] of capital-patch ] ]
      ask q [ die ]
      set combat-patches no-patches
      set target nobody
      set target-local nobody
      ask i with [ member? self [ state ] of capital-patch ] [ set local-aggressors patch-set ( remove ( [ self ] of one-of other y ) ( [ self ] of local-aggressors ) ) ]
    ]
  ]
  if one-of i with [ member? self ( [ state ] of capital-patch ) ] = capital-patch
  [
    ask capital-patch
    [
      set resource-n resource-n + sum [ f-resource ] of paths with [ path-capital = [ capital ] of capital-patch ]
      ask patches with [ target = [ capital ] of capital-patch ]
      [
        let z self
        set resource-n resource-n + sum [ f-resource ] of combat-paths with [ path-capital = [ capital ] of z ]
        ask combat-paths [ die ]
        set combat-patches no-patches
        set target nobody
      ]
      if combat-paths != no-turtles [ ask combat-paths [ die ] ]
      let n resource-n / count state
      if other state != no-patches
      [
        ask other state
        [
          set resource-n n
          set resource-total n
          spawn-new-state
        ]
        ask other state [ update-neighbors ]
      ]
      ask cpr [ set resource-n resource-n + ( random-float 1 ) * n ]
      set resource-n 0
      set resource-split 0
      set combat-patches no-patches
      set target nobody
      set target-local nobody
      set neighbors-list []
      set neighbors-set no-turtles
      set resource-total 0
      set local-aggressors no-patches
      ask capital [ die ]
      set capital nobody
      set predator false
      set state no-patches
    ]
  ]

  ask i
  [
    set pcolor [ pcolor ] of cpr
    set capital [ capital ] of cpr
  ]

  if [ capital ] of cpr != [ capital ] of capital-patch [ ask cpr [ update-state update-neighbors ] ]
  set target nobody
  set target-local nobody
  set combat-patches no-patches
  update-state
  update-neighbors
end 

to update-neighbors  ;------- CREATES AGENTSET OF CAPITALS OF NEIGHBORING STATES -----
  let capital-patch self
  update-state
  set neighbors-list []
  ask state with [ any? neighbors4 with [ capital != [ capital ] of capital-patch ] ]
  [
    ask neighbors4 with [ capital != [ capital ] of myself ]
    [
      let F-C capital
      ask capital-patch [ set neighbors-list lput F-C neighbors-list ]
    ]
  ]
  set neighbors-list remove-duplicates neighbors-list
  set neighbors-set turtle-set neighbors-list
end 

to update-state
  set state patch-set ( patches with [ capital = [ capital ] of myself ] )
end 

to spawn-new-state
  let q self
   sprout-capitols 1
  [
    set size 0.5  ;; easier to see
    let black? true
    set pcolor (sentence (random 256) (random 256) random (256))
    if max pcolor <= 80 [ set black? false ]
    if-else black? [ set color black ] [ set color white ]
    set capital self
  ]
  set cost x * ( cost-lb + random-float ( cost-ub - cost-lb ) )
  set state patch-set self
  set target nobody
  set target-local nobody
  ask patches with [ member? q combat-patches ]
  [
    set target nobody
    set target-local nobody
    ask combat-paths [ die ]
    set combat-patches no-patches
  ]
  set local-aggressors no-patches
  update-neighbors
end 

to check-exclave
  ask patches with [ capitols-here != no-turtles ]
  [
    let capital-patch self
    set enclosed-state ( patch-set self neighbors4 with [ capital = [ capital ] of myself ] )
    set excl? true
    while [ excl? ]
    [
      ask enclosed-state
      [
        let nbrs neighbors4 with [ capital = [ capital ] of myself and not member? self [ enclosed-state ] of capital-patch ]
        ask capital-patch [ set enclosed-state ( patch-set enclosed-state nbrs ) ]
      ]
      if (patch-set [ neighbors4 with [ capital = [ capital ] of myself and not member? self [ enclosed-state ] of capital-patch ] ] of enclosed-state) =  no-patches
      [
        set excl? false
        ask state with [ not member? self [ enclosed-state ] of capital-patch ]
        [
          if paths-here != no-turtles
          [
            let y sum [ f-resource ] of paths-here
            ask capital-patch
            [
              set resource-n resource-n + y
            ]
          ]
          ask patches with [ member? myself combat-patches ]
          [
            set resource-n resource-n + sum [ f-resource ] of combat-paths with [ path-capital = [ capital ] of myself ]
            ask cmbt-link [ set cas-count-list lput casualty-count cas-count-list ]
            ask combat-paths [ die ]
            set combat-patches no-patches
            set target nobody
          ]
        ]
        let z resource-n / count state
        ask state with [ not member? self [ enclosed-state ] of capital-patch ]
        [
          spawn-new-state
          set resource-n z
          set resource-total resource-n
          ask capital-patch [ set resource-n resource-n * ( ( count state - 1 ) / count state ) ]
        ]
        update-state
      ]
    ]
  ]
end 

;------------------- Bargaining Procedures ------------------------------

to EPM-decision-phase-Barg
  ask patches with [ capitols-here != no-turtles and predator ] [ epm-pick-target-barg ]
  if count capitols = 1 [ stop ]
  ask patches with [ capitols-here != no-turtles ] [ epm-allocate-resources ]
end 

to EPM-Pick-Target-Barg
  let capital-patch self
  if capitols-here != no-turtles and target = nobody and ( not any? neighbors-set with [ target = [ capital ] of capital-patch ] )
  [
    let PPL []
    ask state [ set PPL lput ( max-one-of ( neighbors4 with [ capital != [ capital ] of capital-patch ] ) [ x ] ) PPL ]
    set PPL remove-duplicates PPL
    set PPL patch-set PPL
    set target-local min-one-of PPL [ ( [ resource-total ] of ( [patch-here] of capital ) ) / x ]
    set target [ capital ] of target-local
  ]
  ;------- SELECT COMBAT PATH IN NEW ATTACK -----
  if target != nobody and combat-patches = no-patches and any? state with [ any? neighbors4 with [ capital = [ target ] of capital-patch ] ]
  [
    let cpr [ patch-here ] of target
    let p ( resource-total / ( resource-total + [ resource-total ] of cpr ) )
    ask one-of state with [ any? neighbors4 with [ self = [ target-local ] of capital-patch ] ]
    [
      let local-attacker self
      ask capital-patch [ set combat-patches ( patch-set local-attacker target-local ) ]
    ]
    let g combat-patches
    if-else cost / ( sum [ x ] of combat-patches with [ capital != [ capital ] of capital-patch ] ) > ( 1 - p ) and capitols-on target-local = 0 ; believes defender will capitulate
    ; \/ attacker beleives that defender will capitulate
    [
      ask cpr
      [
        if-else cost / ( sum [ x ] of g with [ capital != [ capital ] of capital-patch ] ) < ( 1 - p ) ; defender will not capitulate
        [
          ask capital-patch [ epm-create-combat-path ]
        ]
        [
          ask capital-patch [ conquer-province ]
        ]
      ]
    ]
    ; \/ attacker believes that defender will resist
    [
      if-else cost / ( sum [ x ] of combat-patches with [ capital != [ capital ] of capital-patch ] ) < p ; war is worth it
      ; \/ attacker still believes the risk of war is worth the threat
      [
        let t target-local
        ask cpr
        [
          if-else cost / ( sum [ x ] of g with [ capital != [ capital ] of capital-patch ] ) < ( 1 - p ) or t = self ; go to war
          [
            ask capital-patch [ epm-create-combat-path ]
          ]
          [
            ask capital-patch [ conquer-province ]
          ]
        ]
      ]
      ; \/ attacker sees war as not worth it and doesn't attack
      [
        surrender-no-capitulation
      ]
    ]
  ]
end 

; setup: if any? patches with [ capitols-here != no-turtles and [ who ] of capitols-here != (sentence [ who ] of capital) ] [ show [ self ] of patches with [ capitols-here != no-turtles and [ who ] of capitols-here != (sentence [ who ] of capital) ]  ]

There is only one version of this model, created almost 5 years ago by Jan Bakaj.

Attached files

File Type Description Last updated
Emergent Polarity Model with Bargaining.png preview Preview for 'Emergent Polarity Model with Bargaining' almost 5 years ago, by Jan Bakaj Download

This model does not have any ancestors.

This model does not have any descendants.