Resilience assessment of Thessaloniki

Resilience assessment of Thessaloniki preview image

1 collaborator

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.2.0 • Viewed 1 time • Downloaded 0 times • Run 0 times
Download the 'Resilience assessment of Thessaloniki' 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

breed [roadnodes roadnode]             ;; entities of the road network
breed [metronodes metronode]           ;; entities of the metro network
breed [criticalnodes criticalnode]     ;; entities of the critical infrastructure network

undirected-link-breed [rrr rr]         ;; edges of road network (connection road network nodes)
undirected-link-breed [recroad recr]   ;; new edges introduced in the road subsystem after the recovery phase
undirected-link-breed [mmm mm]         ;; edges of metro network (connection metro network nodes)
undirected-link-breed [recmetro recm]  ;; new edges introduced in the metro subsystem after the recovery phase
undirected-link-breed [rmrm rm]        ;; links between the subsystems of roads and metro (connection/dependence between road and metro nodes)
undirected-link-breed [recrmrm recrm]  ;; new trans-system edges introduced in the system after the recovery phase
undirected-link-breed [chroad chr]     ;; temporary edges to check the potentional entropy of the road subsystem
undirected-link-breed [chmetro chm]    ;; temporary edges to check the potentional entropy of the metro subsystem

roadnodes-own     [roadcapacity  ;; capacity measure of the nodes of the road network
                    road_factor  ;; average length of out-links of each node of the road network, used as a factor of performance
             road_utility_factor ;; average population's needs from the neighboring patches of each road node
                     my_r_links  ;; number of my-links of each road network's node
                  missing_linksR ;; difference between original links and the links after the disturbance of the road subsystem
                    ch_R_entropy ;; entropy of each node of the road subsystem based on the curretnt links / original links
                       R_entropy ;; marginal entropy of each node of the road subsystem during the check for the recovery status
                               ]

metronodes-own   [metrocapacity  ;; capacity measure of the nodes of the metro network
                    metro_factor ;; average length of out-links of each node of the metro network, used as a factor of performance
            metro_utility_factor ;; average population's needs from the neighboring patches of each metro node
                     my_m_links  ;; number of my-links of each metro network's node
                  missing_linksM ;; difference between original links and the links after the disturbance of the metro subsystem
                    ch_M_entropy ;; entropy of each node of the metro subsystem based on the curretnt links / original links
                       M_entropy ;; marginal entropy of each node of the metro subsystem during the check for the recovery status
                               ]

criticalnodes-own    [cicapacity ;; capacity measure of the nodes of the critical infrastructure network
                          is-on? ;; boolean variable to identify if a criticalnode is on or not
                        ci_range ;; distance of the closest criticalnode
                       ci_factor ;; number of served agents of each node of the critical infrastructure network, used as a factor of performance / 10
                   ci_roadfactor ;; number of served roadnodes of each node of the critical infrastructure network
                  ci_metrofactor ;; number of served metronodes of each node of the critical infrastructure network
               ci_utility_factor ;; average population's needs from the neighboring patches of each critical node
                    my_CI_factor ;; number of my coverage factor of each critical infrastructure network's node
                 missing_linksCI ;; difference between original links and the links after the disturbance of the critical infrastructure subsystem
                   ch_CI_entropy ;; entropy of each node of the critical infrastructure subsystem based on the curretnt links / original links
                      CI_entropy ;; marginal entropy of each node of the critical infrastructure subsystem during the check for the recovery status
                               ]

turtles-own       [nodeneighbors ;; list of initial number of connections for each node of the entire system
                       nodelinks ;; list of number of connections for each node of the entire system
                 missing_linksRM ;; difference between original links and the links after the disturbance of the road-metro trans-system links
                        is-dist? ;; boolean variable to identify if an agent is missing an intra-system link or not
                     is-dist-RM? ;; boolean variable to identify if an agent is missing a trans-system link or not
                      is-served? ;; boolean variable to identify if a road or metro agent is missing the connection to the CI nodes or not
                   is-served-RM? ;; boolean variable to identify if a road-metro trans-system link is missing the connection to the CI nodes or not
             original_neighborsR ;; number of road node's neighbors at the beginning of the run
             original_neighborsM ;; number of metro node's neighbors at the beginning of the run
           original_neighbors_RM ;; number of trans-system road-metro neighbors at the beginning of the run
            original_neighborsCI ;; number of critical infrastrucure node's neighbors at the beginning of the run
                   ch_RM_entropy ;; entropy of each RM-node of the metro subsystem based on the curretnt RM-links / original RM-links
                         priopop ;; popuation for each node calculated at the beginning of the run for the prioritization of recovery
                              ]

patches-own         [population  ;; attribute related with the population's density
                  new_population ;; population-based utility measure affected by the system's performance loss
                new_populationRM ;; population-based utility measure affected by the road-metro subsystem's performance loss
                               ]


globals        [roadcapacitylist ;; list of the capacity measure of the road subsystem
               metrocapacitylist ;; list of the capacity measure of the metro subsystem
                  CIcapacitylist ;; list of the capacity measure of the CI subsystem
                original_rm_road ;; number of road nodes connected with metro nodes at the beginning of the run
               original_rm_metro ;; number of metro nodes connected with road nodes at the beginning of the run
           original_rm_roadnodes ;; set of road nodes interconnected with metro nodes via rmrm links at the begining of the run
                    rm_roadnodes ;; set of road nodes with rmrm or recovered rmrm links throughout the run
          original_rm_metronodes ;; set of metro nodes interconnected with road nodes via rmrm links at the begining of the run
                   rm_metronodes ;; set of metro nodes with rmrm or recovered rmrm links throughout the run
                          roadrm ;; capacity measure of interconnected road nodes
                         metrorm ;; capacity measure of interconnected metro nodes
                      rmcapacity ;; capacity measure of interconnected road and metro nodes
                   totalcapacity ;; capacity measure of the nodes of the entire system
               totalcapacitylist ;; list of the capacity measure of the entire system
              totalcapacitylevel ;; capacity percentage
              original_neighbors ;; number of each node's neighbors at the beginning of the run


                  this_entropy_R ;; marginal entropy of the road subsystem during the check for the recovery status
                  this_entropy_M ;; marginal entropy of the metro subsystem during the check for the recovery status
               ch_entropy_list_R ;; the list of the road subsystem's entropy values during the check for the recovery status
               ch_entropy_list_M ;; the list of the metro subsystem's entropy values during the check for the recovery status
                     ch_roadnode ;; to identify the disturbed road nodes
                    ch_metronode ;; to identify the disturbed metro nodes
                 ch_metronode_RM ;; to identify the disturbed road-metro links using the nodes of the metro subsystem
                  ch_roadnode_RM ;; to identify the disturbed road-metro links using the nodes of the road subsystem
                    rec_roadnode ;; the road node identified to create the link and recover the road subsystem
                   rec_roadnode2 ;;
               rec_roadnode_casc ;; the second road node identified to create the additional link and restore the road subsystem from the cascading event
                   rec_metronode ;; the metro node identified to create the link and recover the metro subsystem
                  rec_metronode2 ;;
              rec_metronode_casc ;; the second metro node identified to create the additional link and restore the metro subsystem from the cascading event


  ;; Global variables for performance-based resilience calculation
              recovery_tick_list ;; auxiliary list of ticks during the recovery in order to set the inital recovery_tick
                   recovery_tick ;; ticks after recovery is completed
                     initialperf ;; total capacity percentage before disturbance
                        distperf ;; min total capacity performance during the disturbance
                       finalperf ;; total capacity percentage after recovery
                       totalloss ;; total loss in the performance of the system (observed performance - initial performance)
                   totallosslist ;; list of differences (observed performance - initial performance) of the entire system
                      resilience ;; resilience measure defined by Bruneau et al. 2003 based on resilience triangle
                    z_resilience ;; resilience measure defined by Zobel 2011 R=1-(XT/2T*) based on resilience triangle
                  h-r_resilience ;; resilience measure defined by Henry & Ramirez-Marquez 2012


 ;; Global variables for entropy calculation
                       roadlinks ;; maximum possible road nodes' connections
                       sort-road ;; list of road nodes by turtle ID
                      metrolinks ;; maximum possible metro nodes' connections
                      sort-metro ;; list of metro nodes by turtle ID
                    served_nodes ;; maximum possible agents powered by a single CI node at the beginning of the run
                served_roadnodes ;; maximum possible roadnodes powered by a single CI node at the beginning of the run
               served_metronodes ;; maximum possible metronodes powered by a single CI node at the beginning of the run
                   sort-critical ;; list of CI nodes by turtle ID
                             p4r ;; probability of road nodes' connections to be the same as their initial connections
                             p4m ;; probability of metro nodes' connections to be the same as their initial connections
                             p4c ;; probability of CI nodes' connections to be the same as their initial connections
                             h4r ;; plogp for the road subsystem
                             h4m ;; plogp for the metro subsystem
                             h4c ;; plogp for the CI subsystem
                         entropy ;; measure of the Shannon entropy of the subsystems' nodes' connections over the total connections (EntropyExplorerTwoColors)
                     entropylist ;; list of entropy measures as recorded during the run

                            p41r ;; probability of road nodes' connections not to be the same as their initial connections (S. Lloyd's tutorial)
                            p41m ;; probability of metro nodes' connections not to be the same as their initial connections (S. Lloyd's tutorial)
                            p41c ;; probability of CI nodes' connections not to be the same as their initial connections
                            h41r ;; plogp for the road subsystem
                            h41m ;; plogp for the metro subsystem
                            h41c ;; plogp for the CI subsystem
                            p4rm ;; joint probability for the connnections in road and metro subsystems
                            p4rc ;; joint probability for the connnections in road and CI subsystems
                            p4mc ;; joint probability for the connnections in metro and CI subsystems
                           p4rmc ;; joint probability for the connnections in road, metro and CI subsystems
                            h4rm ;; plogp for the joint probability
                            h4rc ;; plogp for the joint probability
                            h4mc ;; plogp for the joint probability
                           h4rmc ;; plogp for the joint probability
                              sr ;; marginal entropy of the road subsystem
                              sm ;; marginal entropy of the metro subsystem
                              sc ;; marginal entropy of the CI subsystem
                             srm ;; entropy of joint propability (active only in case of dependent disturbances)
                             src ;; entropy of joint propability (active only in case of dependent disturbances)
                             smc ;; entropy of joint propability (active only in case of dependent disturbances)
                            srmc ;; entropy of joint propability (active only in case of dependent disturbances)
                      entropyr:m ;; mutual entropy of the entire system
                      entropyr:c ;; mutual entropy of the road - CI system
                      entropym:c ;; mutual entropy of the metro - CI system
                    entropyr:m:c ;; mutual entropy of the road - metro - CI system
                  entropyr:mlist ;; list of entropyr:m measures as recorded during the run
                  entropyr:clist ;; list of entropyr:c measures as recorded during the run
                  entropym:clist ;; list of entropyr:c measures as recorded during the run
                entropyr:m:clist ;; list of entropyr:m:c measures as recorded during the run
                     entropy_dep ;; mutual entropy of the etnire system (entropy 4) considering dependent disturbances of joint probabilities
                entropy_dep_list ;; list of entropy_dep measures as recorded during the run

                    original_h4r ;; marginal entropy of the road subsystem at the beginning of the run
                    original_h4m ;; marginal entropy of the metro subsystem at the beginning of the run
            original_entropy_dep ;; entropy of the entire system at the beginning of the run

                      initialent ;; total information entropy before disturbance
                        totalent ;; total loss in the information entropy of the system (observed entropy - initial entropy)
                    totalentlist ;; list of differences (observed utility - initial utility) of the entire system
                   entropymetric ;; resilience measure defined by Bruneau et al. 2003 based on resilience triangle adapted for information entropy

  ;; Global variables for population needs' calculation
                        popneeds ;; utility amount earned from the operation of each node of the two subsystems
                       popneedsR ;; utility amount earned from the operation of each node of the road subsystem
                       popneedsM ;; utility amount earned from the operation of each node of the metro subsystem
                      popneedsRM ;; utility amount earned from the operation of each node of the road-metro subsystem
                      popneedsCI ;; utility amount earned from the operation of each node of the CI subsystem
                    popneedslist ;; list of popneeds values

                     initialutil ;; total utility amount before disturbance
                       totalutil ;; total loss in the utility of the system (observed utility - initial utility)
                   totalutillist ;; list of differences (observed utility - initial utility) of the entire system
                         utility ;; resilience measure defined by Bruneau et al. 2003 based on resilience triangle adapted for utility
                               ]

;;;; VARIABLES REPRESENTED IN THE INTERFACE TAB
                  ;; show_gradient: switch to control the appearance of the gradient map of population density attribute
                  ;;    dist_tick : ticks when the disturbance starts
                  ;; recovery_lag : number of ticks between the original disturbance and the respectiive recovery
              ;; disturbance_type : chooser for the type of disturbance: Simple, Cascading, Complex
         ;; initial_response_type : chooser for the type of initial response based on the level of smartness: 0, 1, 2, 3
                 ;; recovery_type : chooser for the type of the recovery process, based on the level of smartnes: 0,1,2,3,4
         ;; utility_prioitization : chooser for the utility prioritization type, based on the relevant process (level of smartnes): no, inspection, recovery, both
               ;; road_dist_links : user input for the number of the disturbed links in the road network

;;;;;;;;;;;;;;;;;;;;;;;;;;;; MAIN PROCEDURES ;;;;;;;;;;;;;;;;;;;;;;;;;;;; MAIN PROCEDURES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup
  clear-all
  import-pcolors "test map.png"
  setup-patches
  setup-road-net
  setup-metro-net
  setup-road-metro-dep
  setup-CI-net
  setup-road-CI-dep
  setup-metro-CI-dep
  setup-road-metro-CI-dep
  setup-metrics
  reset-ticks
end 

to go

   if ticks > 100
   [stop]
  countroadcapacity
  countmetrocapacity
  countCIcapacity
  counttotalcapacity

;;;;;;;;;;; DISTURBANCE ;;;;;;;;;;;;;;
  if disturbance_type = "simple"
    [simple_disturb]
  if disturbance_type = "cascading"
    [cascading_disturb]
  if disturbance_type = "complex"
    [complex_disturb]

;;;;;;;;;;; INITIAL REPSONSE ;;;;;;;;;;;;;;
  if initial_response_type = "response 0"
   [if utility_prioritization = "no" or utility_prioritization ="recovery"
      [response_0]]
  if initial_response_type = "response 0"
   [if utility_prioritization = "inspection" or utility_prioritization ="both"
      [response_0_prioritized]]

  if initial_response_type = "response 1"
   [if utility_prioritization = "no" or utility_prioritization ="recovery"
      [response_1]]
  if initial_response_type = "response 1"
   [if utility_prioritization = "inspection" or utility_prioritization ="both"
      [response_1_prioritized]]

  if initial_response_type = "response 2"
   [if utility_prioritization = "no" or utility_prioritization ="recovery"
      [response_2]]
  if initial_response_type = "response 2"
   [if utility_prioritization = "inspection" or utility_prioritization ="both"
      [response_2_prioritized]]

  if initial_response_type = "response 3"
   [if utility_prioritization = "no" or utility_prioritization ="recovery"
      [response_3]]
  if initial_response_type = "response 3"
   [if utility_prioritization = "inspection" or utility_prioritization ="both"
      [response_3_prioritized]]

;;;;;;;;;;; RECOVERY ;;;;;;;;;;;;;;
  if recovery_type = "recovery 0"
   [if utility_prioritization = "no" or utility_prioritization ="inspection"
      [recovery_0]]
  if recovery_type = "recovery 0"
   [if utility_prioritization = "recovery" or utility_prioritization ="both"
      [recovery_0_prioritized]]

  if recovery_type = "recovery 1"
   [if utility_prioritization = "no" or utility_prioritization ="inspection"
      [recovery_1]]
  if recovery_type = "recovery 1"
   [if utility_prioritization = "recovery" or utility_prioritization ="both"
      [recovery_1_prioritized]]

  if recovery_type = "recovery 2"
   [if utility_prioritization = "no" or utility_prioritization ="inspection"
      [recovery_2]]
  if recovery_type = "recovery 2"
   [if utility_prioritization = "recovery" or utility_prioritization ="both"
      [recovery_2_prioritized]]

  if recovery_type = "recovery 3"
    [recovery_3]
  if recovery_type = "recovery 4"
    [recovery_4]


  set-recovery-tick
  calcresilience
  calcentropy
  calcpopulationneeds

  tick
end 

;;;;;;;;; SETUP PROCEDURES ;;;;;;;;; SETUP PROCEDURES ;;;;;;;;; SETUP PROCEDURES ;;;;;;;;; SETUP PROCEDURES

to setup-patches

;;; 1) blue region 1;;;;
  ask patches with [pcolor >= 111.7 and pcolor <= 132.7 or pcolor = 83.3 or pcolor = 2.9 or pcolor = 2.8]
      [set population 0.2653 + random-float 0.11 + random-float -0.11]

;;; 2) light blue region 4 ;;;;
  ask patches with [pcolor >= 56.0 and pcolor <= 78.2]
      [set population 0.9088 + random-float 0.11 + random-float -0.11]

;;; 3) brown region 4-5 only in NetLogo
  ask patches with [pcolor >= 34.3 and pcolor <= 36.6]
      [set population mean list (0.9088)(1.3378) + random-float 0.11 + random-float -0.11]

;;; 5) light green region 6 ;;;;
  ask patches with [pcolor >= 44.2 and pcolor <= 44.7  ]
      [set population 1.3378 + random-float 0.11 + random-float -0.11]

;;; 6) green- region 6-7 only in NetLogo
  ask patches with [pcolor >= 46.9 and pcolor <= 47.0]
      [set population mean list (1.3378)(1.5523) + random-float 0.11 + random-float -0.11]

;;; 7)  region 7 ;;;;
  ask patches with [pcolor = 44.0 or pcolor = 44.6 or pcolor >= 45.5 and pcolor < 46.9 ]
      [set population 1.5523 + random-float 0.11 + random-float -0.11]

;;; 8) orange region 8 ;;;;
  ask patches with [pcolor >= 23.0 and pcolor <= 28.3]
      [set population 1.7668 + random-float 0.11 + random-float -0.11]

;;; 9) red region 9 ;;;;
  ask patches with [pcolor >= 13.5 and pcolor <= 18.5]
      [set population 1.9813 + random-float 0.11 + random-float -0.11]

;;; 10) dark red region 10 ;;;;
  ask patches with [pcolor >= 12.6 and pcolor <= 13.4]
      [set population 2.1958 + random-float 0.11 + random-float -0.11]

;;; grey region - sea ;;;;
  ask patches with [pcolor = 7.1]
      [set population 0]

  if show_gradient
    [ask patches with [ population > 0]
      [set pcolor scale-color green population 2.5 0 ]]
end 

to setup-road-net
    ;;setting up of road network nodes
  create-roadnodes 60

  ask roadnodes
      [set shape "circle"
       set color red
       set size 1
       set is-dist? false
       set is-dist-RM? false]

;;;;;  setting precise positions ;;;;
  ask roadnode 0
     [setxy -35 38
      create-rr-with roadnode 4]
   ask roadnode 1
     [setxy -25 37
      create-rr-with roadnode 3 ]
  ask roadnode 2
     [setxy -37 33
      create-rr-with roadnode 6]
  ask roadnode 3
     [setxy -21 34
      create-rr-with roadnode 4
      create-rr-with roadnode 5]
  ask roadnode 4
     [setxy -24 31
      create-rr-with roadnode 6]
  ask roadnode 5
     [setxy -12 28
      create-rr-with roadnode 9
      create-rr-with roadnode 7]
  ask roadnode 6
     [setxy -12 24
      create-rr-with roadnode 12]
  ask roadnode 7
     [setxy -8 26
      create-rr-with roadnode 6
      create-rr-with roadnode 9
      create-rr-with roadnode 10
      create-rr-with roadnode 11
      create-rr-with roadnode 14]
  ask roadnode 8
     [setxy -8 34
      create-rr-with roadnode 9]
  ask roadnode 9
     [setxy -8 30
      create-rr-with roadnode 10]
  ask roadnode 10
     [setxy -1 26
      create-rr-with roadnode 13]
  ask roadnode 11
     [setxy -7 21
      create-rr-with roadnode 12
      create-rr-with roadnode 19]
  ask roadnode 12
     [setxy -8 19
      create-rr-with roadnode 22]
  ask roadnode 13
     [setxy 4 22
      create-rr-with roadnode 14
      create-rr-with roadnode 16
      create-rr-with roadnode 17]
  ask roadnode 14
     [setxy 2 18
      create-rr-with roadnode 18]
  ask roadnode 15
     [setxy 16 28
      create-rr-with roadnode 16]
  ask roadnode 16
     [setxy 9 28]
  ask roadnode 17
     [setxy 11 18
      create-rr-with roadnode 25]
  ask roadnode 18
     [setxy 6 15
      create-rr-with roadnode 19]
  ask roadnode 19
     [setxy 4 12
      create-rr-with roadnode 21
      create-rr-with roadnode 22]
  ask roadnode 20
     [setxy 9 14
      create-rr-with roadnode 17
      create-rr-with roadnode 18
      create-rr-with roadnode 21
      create-rr-with roadnode 24]
  ask roadnode 21
     [setxy 8 9
      create-rr-with roadnode 22
      create-rr-with roadnode 23
      create-rr-with roadnode 29]
  ask roadnode 22
     [setxy 2 10
      create-rr-with roadnode 23]
  ask roadnode 23
     [setxy 8 6
      create-rr-with roadnode 38
      create-rr-with roadnode 39]
  ask roadnode 24
     [setxy 14 8
      create-rr-with roadnode 23
      create-rr-with roadnode 25
      create-rr-with roadnode 28]
  ask roadnode 25
     [setxy 18 9
      create-rr-with roadnode 27]
  ask roadnode 26
     [setxy 24 7
      create-rr-with roadnode 27]
  ask roadnode 27
     [setxy 21 4
      create-rr-with roadnode 30]
  ask roadnode 28
     [setxy 17 3
      create-rr-with roadnode 30
      create-rr-with roadnode 31]
  ask roadnode 29
     [setxy 11 3
      create-rr-with roadnode 31
      create-rr-with roadnode 32]
  ask roadnode 30
     [setxy 20 1
      create-rr-with roadnode 31
      create-rr-with roadnode 34]
  ask roadnode 31
     [setxy 17 0
      create-rr-with roadnode 32
      create-rr-with roadnode 35]
  ask roadnode 32
     [setxy 13 -3
      create-rr-with roadnode 35
      create-rr-with roadnode 36
      create-rr-with roadnode 37]
  ask roadnode 33
     [setxy 32 1]
  ask roadnode 34
     [setxy 26 -4
      create-rr-with roadnode 33
      create-rr-with roadnode 35
      create-rr-with roadnode 49]
  ask roadnode 35
     [setxy 17 -5
      create-rr-with roadnode 36
      create-rr-with roadnode 44]
  ask roadnode 36
     [setxy 17 -9
      create-rr-with roadnode 37
      create-rr-with roadnode 43]
  ask roadnode 37
     [setxy 12 -10
      create-rr-with roadnode 38
      create-rr-with roadnode 42]
  ask roadnode 38
     [setxy 9 -10
      create-rr-with roadnode 39
      create-rr-with roadnode 41]
  ask roadnode 39
     [setxy 7 -10
     create-rr-with roadnode 40]
  ask roadnode 40
     [setxy 6 -15
      create-rr-with roadnode 41
      create-rr-with roadnode 45]
  ask roadnode 41
     [setxy 8 -15
      create-rr-with roadnode 42
      create-rr-with roadnode 45]
  ask roadnode 42
     [setxy 12 -14
      create-rr-with roadnode 43
      create-rr-with roadnode 46]
  ask roadnode 43
     [setxy 17 -14
      create-rr-with roadnode 44
      create-rr-with roadnode 47]
  ask roadnode 44
     [setxy 20 -13
      create-rr-with roadnode 48]
  ask roadnode 45
     [setxy 7 -24
      create-rr-with roadnode 54
      create-rr-with roadnode 55
      create-rr-with roadnode 58]
  ask roadnode 46
     [setxy 13 -22
      create-rr-with roadnode 47
      create-rr-with roadnode 58]
  ask roadnode 47
     [setxy 18 -20
      create-rr-with roadnode 48
      create-rr-with roadnode 52]
  ask roadnode 48
     [setxy 22 -19
      create-rr-with roadnode 51]
  ask roadnode 49
     [setxy 39 -10
      create-rr-with roadnode 50]
  ask roadnode 50
     [setxy 32 -21
      create-rr-with roadnode 51]
  ask roadnode 51
     [setxy 23 -21
      create-rr-with roadnode 59]
  ask roadnode 52
     [setxy 18 -26
      create-rr-with roadnode 53
      create-rr-with roadnode 56
      create-rr-with roadnode 59]
  ask roadnode 53
     [setxy 12 -32
      create-rr-with roadnode 55
      create-rr-with roadnode 58]
  ask roadnode 54
     [setxy 6 -32]
  ask roadnode 55
     [setxy 9 -35]
  ask roadnode 56
     [setxy 20 -33
      create-rr-with roadnode 57]
  ask roadnode 57
     [setxy 24 -38]
  ask roadnode 58
     [setxy 9 -24]
  ask roadnode 59
     [setxy 24 -26]


;;;;;  setting up the network ;;;;

ask rrr
     [set color red
      set thickness 0.3]
end 

to setup-metro-net
    ;;setting up of metro network nodes
  create-metronodes 13

  ask metronodes
      [set shape "circle"
       set color blue
       set size 1
       set is-dist? false
       set is-dist-RM? false]

;;;;;  setting precise positions ;;;;
  ask metronode 60
    [setxy -14 29]
  ask metronode 61
    [setxy -9 26]
  ask metronode 62
    [setxy -3 22]
  ask metronode 63
    [setxy 1 19]
  ask metronode 64
    [setxy 8 15]
  ask metronode 65
    [setxy 14 9]
  ask metronode 66
      [setxy 17 2]
  ask metronode 67
      [setxy 14 -2]
  ask metronode 68
      [setxy 12 -8]
  ask metronode 69
      [setxy 12 -15]
  ask metronode 70
      [setxy 13 -21]
  ask metronode 71
      [setxy 16 -27]
  ask metronode 72
      [setxy 23 -30]

  ;;;;;  setting up the network ;;;;

  foreach sort metronodes
          [met -> ask met
            [if who != 72
              [create-mm-with metronode ([who] of met + 1)]] ]

ask mmm
     [set color blue
      set thickness 0.3]
end 

to setup-road-metro-dep
  ;;setting up of road-metro network dependency links
  ask metronode 60
     [create-rm-with roadnode 5]
  ask metronode 61
     [create-rm-with roadnode 7]
  ask metronode 63
     [create-rm-with roadnode 14]
  ask metronode 64
     [create-rm-with roadnode 20]
  ask metronode 65
     [create-rm-with roadnode 24]
  ask metronode 66
     [create-rm-with roadnode 28]
  ask metronode 67
     [create-rm-with roadnode 32]
  ask metronode 68
     [create-rm-with roadnode 37]
  ask metronode 69
     [create-rm-with roadnode 42]
  ask metronode 70
     [create-rm-with roadnode 46]
  ask metronode 71
     [create-rm-with roadnode 52]

ask rmrm
     [set color violet
      set thickness 0.3]
end 

to setup-CI-net
;;setting up of power network nodes
  create-criticalnodes 6

  ask criticalnodes
      [set shape "target"
       set size 2
       set is-on? true]

;;;;;  setting precise positions ;;;;
  ask criticalnode 73
    [setxy 13 13
     set color white
     set ci_range (distance criticalnode 74)]
  ask criticalnode 74
    [setxy 16 -2
     set color white
     set ci_range (distance criticalnode 75)]
  ask criticalnode 75
    [setxy 9 21
     set color 27
     set ci_range (distance criticalnode 77)]
  ask criticalnode 76
    [setxy -16 32
     set color 27
     set ci_range (distance criticalnode 77)]
  ask criticalnode 77
    [setxy 11 -24
     set color 27
     set ci_range (distance criticalnode 75) / 2]
  ask criticalnode 78
    [setxy -20 22
     set color 27
     set ci_range (distance criticalnode 76)]
end 

to setup-road-CI-dep

ask roadnodes
        [let ci-ref-node min-one-of criticalnodes with [is-on? = true] [distance myself]
         if distance ci-ref-node <= [ci_range] of ci-ref-node
              [ask roadnodes
                  [set is-served? true]]]
end 

to setup-metro-CI-dep

ask metronodes
      [let ci-ref-node min-one-of criticalnodes with [is-on? = true] [distance myself]
         if distance ci-ref-node <= [ci_range] of ci-ref-node
              [ask metronodes
                  [set is-served? true]]]
end 

to setup-road-metro-CI-dep

  ask metronodes with [any? my-rmrm = true]
      [let ci-ref-node min-one-of criticalnodes with [is-on? = true] [distance myself]
         if distance ci-ref-node <= [ci_range] of ci-ref-node
              [ask metronodes with [any? my-rmrm = true]
                  [set is-served-RM? true]]]
end 

to setup-metrics
  set recovery_tick_list []
  set roadcapacitylist []
  set metrocapacitylist []
  set CIcapacitylist []
  set totalcapacitylist []
  set totallosslist []

  set entropylist []
  set entropyr:mlist []
  set entropyr:clist []
  set entropym:clist []
  set entropyr:m:clist []
  set entropy_dep_list []
  set totalentlist []

  set popneedslist []
  set totalutillist []
end 

to set-recovery-tick
if ticks > dist_tick + recovery_lag
  [if not any? turtles with [color = black] and last totalcapacitylist / item (length totalcapacitylist - 2) totalcapacitylist > 1
    [set recovery_tick_list lput ticks recovery_tick_list
     set recovery_tick first recovery_tick_list ]]
end 

;;;;;;;;; PROCEDURES ;;;;;;;;; PROCEDURES;;;;;;;;; PROCEDURES ;;;;;;;;; PROCEDURES ;;;;;;;;;

to countroadcapacity
  ask roadnodes with [count rr-neighbors > 0]
      [if ticks < 2
           [set road_factor (mean [link-length] of my-out-rrr) / 10]
       set road_utility_factor mean [population] of neighbors
       set roadcapacity (count my-rrr + count my-recroad) * road_factor * road_utility_factor
       set roadcapacitylist sum [roadcapacity] of roadnodes]
end 

to countmetrocapacity
    ask metronodes with [count mm-neighbors > 0]
      [if ticks < 2
           [set metro_factor (mean [link-length] of my-out-mmm) / 10]
       set metro_utility_factor mean [population] of neighbors in-radius 3
       set metrocapacity (count my-out-mmm + count my-out-recmetro) * metro_factor * metro_utility_factor
       set metrocapacitylist sum [metrocapacity] of metronodes]
end 

to countCIcapacity

  ask criticalnodes with [is-on? = true]
     [set ci_factor (count turtles with [is-dist? = false] in-radius ci_range ) / 10
      set ci_utility_factor mean [population] of neighbors in-radius ci_range
      set cicapacity ((count patches with [population > 0] in-radius ci_range)/ 100) * ci_factor * ci_utility_factor
      set CIcapacitylist sum [cicapacity] of criticalnodes]

  ask criticalnodes with [is-on? = true]
      [set ci_roadfactor (count roadnodes with [is-dist? = false] in-radius ci_range)
       set ci_metrofactor (count metronodes with [is-dist? = false] in-radius ci_range)]
end 

to counttotalcapacity
  if ticks < 2
      [set original_rm_road count roadnodes with [any? my-rmrm]
       set original_rm_roadnodes roadnodes with [any? my-rmrm]]
  set rm_roadnodes roadnodes with [any? my-rmrm or any? my-recrmrm]
  set roadrm sum [roadcapacity] of rm_roadnodes / original_rm_road

   if ticks < 2
      [set original_rm_metro count metronodes with [any? my-rmrm]
       set original_rm_metronodes metronodes with [any? my-rmrm]]
   set rm_metronodes metronodes with [any? my-rmrm or any? my-recrmrm]
   set metrorm sum [metrocapacity] of rm_metronodes / original_rm_metro


 set rmcapacity (roadrm + metrorm )
  set totalcapacity ( roadcapacitylist + metrocapacitylist + rmcapacity + CIcapacitylist )
  if totalcapacity > 0
   [set totalcapacitylist lput totalcapacity totalcapacitylist
      set totalcapacitylevel ( totalcapacity / max totalcapacitylist * 100)]
end 

;;;;;; DISTURBANCE PROCEDURES ;;;;;; DISTURBANCE PROCEDURES ;;;;;;

to simple_disturb ;user-input condition of disturbance

  if road = true
     [if ticks = dist_tick
       [let disturbed_R n-of road_dist_links roadnodes
        ask disturbed_R
         [ask one-of my-rrr with [both-ends != other disturbed_R]
            [die]]]]

  if metro = true
     [if ticks = dist_tick
       [let disturbed_M n-of metro_dist_links metronodes
        ask disturbed_M
         [ask one-of my-mmm with [both-ends != other disturbed_M]
            [die]]]]
end 

to cascading_disturb ;user-input condition of disturbance

   if road = true
      [if ticks = dist_tick
        [ask n-of road_dist_links roadnodes with [count my-rrr > 2]
          [ask n-of 2 my-rrr
              [die]]]]

   if metro = true
      [if ticks = dist_tick
        [ask n-of metro_dist_links metronodes with [count my-mmm >= 2]
          [ask my-mmm
              [die]]]]
end 

to complex_disturb ;user-input condition of disturbance

  if road = true and metro = true
      [if ticks = dist_tick
        [ask n-of road_dist_links rmrm
               [die]]]
end 

 ;;;;;;; INITIAL RESPONSE PROCEDURES ;;;;;;;

to response_0 ; sequential repetitive checks and then recovery

 ask roadnodes
   [foreach sort roadnodes [r -> ask r
                             [if ticks < 2
                                  [set original_neighborsR count rr-neighbors]
                              let i position r sort roadnodes
                              if ticks = dist_tick + recovery_lag + i and original_neighborsR > count rr-neighbors
                                  [set is-dist? true]
                              if ticks > count roadnodes
                                  [ask roadnodes with [is-dist? = true]
                                    [set color black]]]]]

  ask original_rm_roadnodes
   [foreach sort original_rm_roadnodes [r+ -> ask r+
                             [let ri position r+ sort roadnodes
                              if ticks = dist_tick + recovery_lag + ri and count original_rm_roadnodes > count rm-neighbors
                                 [ask original_rm_roadnodes with [not any? rm-neighbors]
                                    [set is-dist-RM? true
                                     set color black]] ] ]]

 ask metronodes
   [foreach sort metronodes [m -> ask m
                             [if ticks < 2
                                  [set original_neighborsM count mm-neighbors]
                              let n position m sort turtles
                              if ticks = dist_tick + recovery_lag + n and original_neighborsM >= count mm-neighbors + 1
                                  [set is-dist? true]
                              if ticks > count roadnodes + count metronodes
                                  [ask metronodes with [is-dist? = true]
                                    [set color black]] ]]]

  ask original_rm_metronodes
   [foreach sort original_rm_metronodes [m+ -> ask m+
                             [let mi position m+ sort metronodes
                              if ticks = dist_tick + recovery_lag + mi and count original_rm_metronodes > count rm-neighbors
                                 [ask original_rm_metronodes with [not any? rm-neighbors]
                                    [set is-dist-RM? true
                                     set color black]] ] ]]
end 

to response_1 ; sequential repetitive checks and recovery combined

 ask roadnodes
   [foreach sort roadnodes [r -> ask r
                             [if ticks < 2
                                  [set original_neighborsR count rr-neighbors]
                              let i position r sort roadnodes
                              if ticks = dist_tick + recovery_lag + i and original_neighborsR > count rr-neighbors
                                  [set is-dist? true
                                set color black]
                              if ticks = dist_tick + recovery_lag + i and count original_rm_roadnodes > count rm_roadnodes
                                  [ask original_rm_roadnodes with [not any? rm-neighbors]
                                     [set is-dist-RM? true
                                      set color black]]]]]

 ask metronodes
   [foreach sort metronodes [m -> ask m
                             [if ticks < 2
                                  [set original_neighborsM count mm-neighbors]
                              let n position m sort turtles
                              if ticks = dist_tick + recovery_lag + n and original_neighborsM >= count mm-neighbors + 1
                                  [set is-dist? true
                                   set color black]
                              if ticks = dist_tick + recovery_lag + n and count original_rm_metronodes > count rm_metronodes
                                  [ask original_rm_metronodes with [not any? rm-neighbors]
                                    [set is-dist-RM? true
                                     set color black]]]]]
end 

to response_2 ; parallel repetitive checks and recovery combined

 ask roadnodes
   [foreach sort roadnodes [r -> ask r
                             [if ticks < 2
                                  [set original_neighborsR count rr-neighbors]
                              let i position r sort roadnodes
                              if ticks = dist_tick + i and original_neighborsR > count rr-neighbors
                                  [set is-dist? true
                                   set color black]
                              if ticks = dist_tick + recovery_lag + i and count original_rm_roadnodes > count rm_roadnodes
                                  [ask original_rm_roadnodes with [not any? rm-neighbors]
                                     [set is-dist-RM? true
                                      set color black]]]]]

 ask metronodes
   [foreach sort metronodes [m -> ask m
                             [if ticks < 2
                                  [set original_neighborsM count mm-neighbors]
                              let n position m sort metronodes
                              if ticks = dist_tick + n and original_neighborsM > count mm-neighbors
                                  [set is-dist? true
                                   set color black]
                              if ticks = dist_tick + recovery_lag + n and count original_rm_metronodes > count rm_metronodes
                                  [ask original_rm_metronodes with [not any? rm-neighbors]
                                     [set is-dist-RM? true
                                      set color black]]]]]
end 

to response_3 ; constant monitoring of agents and immediate pesponse - the smartest
 if  ticks < 1
   [ask roadnodes
      [set original_neighborsR count rr-neighbors]]

 if  ticks < 1
    [ask metronodes
     [set original_neighborsM count mm-neighbors]]

;; Check for road links failure ;;;
if initial_response_type = "response 3"
 [ask roadnodes
   [if ticks = dist_tick + 1 and original_neighborsR > count rr-neighbors
      [set is-dist? true
       set color black]]
   if ticks = dist_tick + 1 and count original_rm_roadnodes > count rm_roadnodes
    [ask original_rm_roadnodes with [not any? rm-neighbors]
      [set is-dist-RM? true
       set color black]]

;;Check for metro links failure ;;;
  ask metronodes
   [if ticks = dist_tick + 1 and original_neighborsM > count mm-neighbors
      [set is-dist? true
      set color black]]
    if ticks = dist_tick + 1 and count original_rm_metronodes > count rm_metronodes
     [ask original_rm_metronodes with [not any? rm-neighbors]
      [set is-dist-RM? true
        set color black]] ]
end 

;;;;;;; RECOVERY PROCEDURES ;;;;;;; RECOVERY PROCEDURES ;;;;;;;

to recovery_0 ; restore link at the node with lost links at the end of all the inspections
;ROAD simple and cascading
  if ticks > dist_tick + recovery_lag and count roadnodes with [color = black] > 1 and disturbance_type = "simple"
  [ask roadnodes with [is-dist? = true]
    [create-recr-with min-one-of other roadnodes with [is-dist? = true] [distance myself] ]] ;one-of other roadnodes  and min-one-of other roadnodes [distance myself] ]]


  if ticks > dist_tick + recovery_lag and count roadnodes with [color = black] > 2 and disturbance_type = "cascading"
    [ask roadnodes with [is-dist? = true]
      [set ch_roadnode one-of roadnodes with [original_neighborsR - count rr-neighbors >= 2]
     ask ch_roadnode
        [set rec_roadnode min-n-of 2 other roadnodes with [is-dist? = true] [distance myself] ;original_neighborsR - count rr-neighbors < 2 and
         create-recroad-with rec_roadnode]]]
  ask recroad
      [set color yellow]


;METRO simple and cascading
  if ticks > dist_tick + recovery_lag and count metronodes with [color = black] > 1
   [ask metronodes with [is-dist? = true]
      [create-recm-with min-one-of other metronodes with [is-dist? = true] [distance myself]]]

  if ticks > dist_tick + recovery_lag and count metronodes with [color = black] > 2 and disturbance_type = "cascading"
    [ask metronodes with [is-dist? = true]
      [set ch_metronode one-of metronodes with [original_neighborsM - count mm-neighbors >= 2]
       ask ch_metronode
        [set rec_metronode min-n-of 2 other metronodes with [is-dist? = true] [distance ch_metronode]
         create-recmetro-with rec_metronode]]]
   ask recmetro
      [set color yellow]

;ROAD - METRO
  if ticks > dist_tick + recovery_lag + 1 and count roadnodes with [is-dist-RM? = true] > 1 and count metronodes with [is-dist-RM? = true] > 1 and count rm_roadnodes < original_rm_road
   [ask one-of original_rm_roadnodes with [is-dist-RM? = true]
      [create-recrm-with min-one-of original_rm_metronodes in-radius 5 with [is-dist-RM? = true] [distance myself] ]
    ask recrmrm
        [set color yellow]]

;;;color identification for recovery;;;;
   ask roadnodes with [is-dist? = true]
    [if count my-rrr + count my-recroad >= original_neighborsR
      [set is-dist? false
       set color red]]

 ask metronodes with [is-dist? = true]
    [if count my-mmm + count my-recmetro >= original_neighborsM
      [set is-dist? false
       set color blue]]

 ask original_rm_roadnodes with [is-dist-RM? = true]
   [if count rm_roadnodes = original_rm_road
      [set is-dist-RM? false
       set color red]]
 ask original_rm_metronodes with [is-dist-RM? = true]
    [if count rm_metronodes = original_rm_metro
      [set is-dist-RM? false
       set color blue]]
end 

to recovery_1 ; restore links at the node with lost links based on the trigger of entropy increase

    if  ticks = 2 ;dist_tick
  [set original_h4r (-1 * sum h4r)
   set original_h4m (-1 * sum h4m)]

;ROAD simple and cascading
  if ticks > dist_tick + recovery_lag and (-1 * sum h4r) > original_h4r and count roadnodes with [color = black] >= 2
  [ask roadnodes with [is-dist? = true]
    [create-recr-with min-one-of other roadnodes with [is-dist? = true] [distance myself]]]

  if ticks > dist_tick + recovery_lag and (-1 * sum h4r) != original_h4r and count roadnodes with [color = black] > 2 and disturbance_type = "cascading" ;count roadnodes with [is-dist? = true] > 2
    [ask roadnodes with [is-dist? = true]
      [set ch_roadnode one-of roadnodes with [original_neighborsR - count rr-neighbors >= 2]
     ask ch_roadnode
       [set rec_roadnode min-n-of 2 other roadnodes with [original_neighborsR - count rr-neighbors < 2] [distance myself]
        create-recroad-with rec_roadnode]]]

  if ticks > dist_tick + recovery_lag and count roadnodes with [color = black] = 1 and disturbance_type = "cascading" and any? recroad
   [ask roadnodes with [is-dist? = true]
    [create-recr-with min-one-of other roadnodes with [any? recr-neighbors] [distance myself]]]

  ask recroad
     [set color yellow]

;METRO simple and cascading
  if ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m and count metronodes with [color = black] >= 2
  [ask metronodes with [is-dist? = true]
    [create-recm-with min-one-of other metronodes with [is-dist? = true] [distance myself]]]

  if ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m and count metronodes with [color = black] > 2 and disturbance_type = "cascading"
    [ask metronodes
      [set ch_metronode metronodes with [original_neighborsM - count mm-neighbors >= 2]
     ask ch_metronode
       [set rec_metronode min-n-of 2 other metronodes with [original_neighborsM - count mm-neighbors - count recm-neighbors < 2] [distance myself]
        create-recmetro-with rec_metronode in-radius 10]]]
  ask recmetro
     [set color yellow]

;ROAD - METRO
  if ticks > dist_tick + recovery_lag and count rm_roadnodes < original_rm_road and any? roadnodes with [is-dist-RM? = true] and any? metronodes with [is-dist-RM? = true]
   [ask original_rm_roadnodes with [count my-rmrm = 0]
      [create-recrm-with min-one-of original_rm_metronodes with [is-dist-RM? = true] [distance myself]
       set is-dist-RM? false
    ask recrmrm
        [set color yellow]]]

;;;color identification for recovery;;;;
 ask roadnodes with [is-dist? = true]
    [if count my-rrr + count my-recroad >= original_neighborsR
      [set is-dist? false
       set color red]]

 ask metronodes with [is-dist? = true]
    [if count my-mmm + count my-recmetro >= original_neighborsM
      [set is-dist? false
       set color blue]]

 ask original_rm_roadnodes with [is-dist-RM? = true]
   [if count rm_roadnodes = original_rm_road
      [set is-dist-RM? false
       set color red]]
 ask original_rm_metronodes with [is-dist-RM? = true]
    [if count rm_metronodes = original_rm_metro
      [set is-dist-RM? false
       set color blue]]
end 

to recovery_2 ; restore links based on the list of missing links per node - memory of agents
;;;;; check missing links ;;;;;;
;ROAD simple and cascading
  ask roadnodes
     [set missing_linksR (original_neighborsR - count my-rrr)]

  ifelse ticks > dist_tick + recovery_lag and disturbance_type = "cascading" and count roadnodes with [color = black] > 2
     [ask roadnodes
         [set ch_roadnode roadnodes with [missing_linksR = max [missing_linksR] of roadnodes]
          set rec_roadnode roadnodes with [missing_linksR = (max [missing_linksR] of roadnodes - 1)] ]]
     [ask roadnodes
         [set ch_roadnode roadnodes with [missing_linksR = max [missing_linksR] of roadnodes] ]]

;METRO simple and cascading
  ask metronodes
         [set missing_linksM (original_neighborsM - count my-mmm)]

  ifelse ticks > dist_tick + recovery_lag and disturbance_type = "cascading" and count metronodes with [color = black] > 2
     [ask metronodes
          [set ch_metronode metronodes with [missing_linksM = max [missing_linksM] of metronodes]
           set rec_metronode metronodes with [missing_linksM = max [missing_linksM] of metronodes - 1]]]
     [ask metronodes
          [set ch_metronode metronodes with [missing_linksM = max [missing_linksM] of metronodes] ]]

;ROAD - METRO
if recovery_type = "recovery 2"
 [ask original_rm_metronodes
     [set missing_linksRM (count original_rm_metronodes - count my-rmrm)]
  if any? metronodes with [is-dist-RM? = true ]
    [ask metronodes
      [set ch_metronode_RM metronodes with [missing_linksRM = max [missing_linksRM] of metronodes]]] ]

 ;;;;;;; recover ;;;;;;;;;;;;;
     if  ticks = 2
  [set original_h4r (-1 * sum h4r)
   set original_h4m(-1 * sum h4m)
   set original_entropy_dep entropy_dep]

   if ticks > dist_tick + recovery_lag and (-1 * sum h4r) != original_h4r
      [ask ch_roadnode
        [if disturbance_type = "simple" and count roadnodes with [color = black] >= 2 and count ch_roadnode > 1
            [create-recr-with min-one-of other ch_roadnode [distance myself]]
         if disturbance_type = "cascading" and count roadnodes with [color = black] > 2
            [create-recroad-with min-n-of 2 rec_roadnode [distance myself] ]
         if disturbance_type = "cascading" and count roadnodes with [color = black] = 2
            [create-recr-with min-one-of other roadnodes with [missing_linksR > 0]  [distance myself]]
         if disturbance_type = "cascading" and any? roadnodes with [color = black] and count roadnodes with [color = black] < 2 and any? recroad
            [create-recr-with min-one-of other roadnodes with [any? recr-neighbors] [distance myself]] ]]

;;;redundant recroad links' removal ;;;
 if ticks > dist_tick + recovery_lag and any? roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
   [ask roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
      [let redundant recr-with max-one-of recr-neighbors [distance myself]
       ask redundant
          [die]]]

       ask recroad
         [set color yellow]

 if ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m
      [ask ch_metronode
          [if disturbance_type = "simple" and count metronodes with [color = black] >= 2 and count ch_metronode > 1
            [create-recm-with min-one-of other ch_metronode [distance myself]
             create-recm-with min-one-of other metronodes with [missing_linksM > 0] [distance myself]]
           if disturbance_type = "cascading" and count metronodes with [color = black] > 2
            [create-recmetro-with min-n-of 2 rec_metronode [distance myself] ]]]

;;;redundant recmetro links' removal ;;;
 if ticks > dist_tick + recovery_lag and any? metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
   [ask metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
      [let redundant recm-with max-one-of recm-neighbors [distance myself]
       ask redundant
          [die]]]

       ask recmetro
        [set color yellow]

  if ticks > dist_tick + recovery_lag + 1 and  entropy_dep > original_entropy_dep and any? roadnodes with [is-dist-RM? = true] and any? metronodes with [is-dist-RM? = true]
     [ask ch_metronode_RM
        [create-recrm-with min-one-of original_rm_roadnodes with [is-dist-RM? = true] [distance myself] ]

      ask recrmrm
        [set color yellow]]


  ;;;color identification for recovery;;;;
 ask roadnodes with [is-dist? = true]
    [if count my-rrr + count my-recroad >= original_neighborsR
      [set is-dist? false
       set color red]]

 ask metronodes with [is-dist? = true]
    [if count my-mmm + count my-recmetro >= original_neighborsM
      [set is-dist? false
       set color blue]]

 ask original_rm_roadnodes with [is-dist-RM? = true]
   [if count rm_roadnodes >= original_rm_road
      [set is-dist-RM? false
       set color red]]
 ask original_rm_metronodes with [is-dist-RM? = true]
    [if count rm_metronodes >= original_rm_metro
      [set is-dist-RM? false
       set color blue]]
end 

to check_rec_max_entropy ; check of the potential entropy of system with the use of temporary (dummy) links

;;;; check entropy of all the possible recoveries/linkages with each one of the other ROAD nodes -> identify recovery that yields max entropy in the ROAD SUBSYSTEM
if ticks > dist_tick + recovery_lag and any? roadnodes with [is-dist? = true] and disturbance_type != "complex"
   [ask roadnodes
        [ifelse count my-rrr > 0
          [set ch_R_entropy (-1 * (count my-rrr / original_neighborsR) * log (count my-rrr / original_neighborsR) 2)]
          [set ch_R_entropy -1]
           set ch_roadnode max-n-of road_dist_links roadnodes [ch_R_entropy]]

    ask roadnodes
      [set ch_entropy_list_R []
          foreach sort roadnodes
            [r -> ifelse r != ch_roadnode
              [ask r
                [create-chr-with min-one-of other ch_roadnode [distance myself]
                 let ch_p4r (count my-rrr + count my-chroad) / original_neighborsR
                 let ch_h4r ch_p4r * log ch_p4r 2
                 set R_entropy -1 * ch_h4r
                 set this_entropy_R -1 * ch_h4r ]]

             [set this_entropy_R -1] ;; I can set this_entropy_ of ch_node to 0 for the min Ent approach OR to -1 for the max Ent approach

            set ch_entropy_list_R lput this_entropy_R ch_entropy_list_R
            ask chroad [die]]]

      ask ch_roadnode
           [set rec_roadnode min-one-of other roadnodes with [R_entropy = max ch_entropy_list_R] [distance myself]]]

;;;; check entropy of all the possible recoveries/linkages with each one of the other METRO nodes -> identify recovery that yields max entropy in the METRO SUBSYSTEM
 if ticks > dist_tick + recovery_lag and any? metronodes with [is-dist? = true] and disturbance_type != "complex"
   [ask metronodes
      [ifelse count my-mmm > 0
          [set ch_M_entropy (-1 * (count my-mmm / original_neighborsM) * log (count my-mmm / original_neighborsM) 2)]
          [set ch_M_entropy 1]
           set ch_metronode max-n-of metro_dist_links metronodes [ch_M_entropy]]

    ask metronodes
      [set ch_entropy_list_M []
         foreach sort metronodes
          [m -> ifelse m != ch_metronode
            [ask m
                [create-chm-with min-one-of other ch_metronode [distance myself]
                 let ch_p4m (count my-mmm + count my-chmetro) / original_neighborsM
                 let ch_h4m (ch_p4m * log ch_p4m 2)
                 set M_entropy -1 * ch_h4m
                 set this_entropy_M -1 * ch_h4m ]]

            [set this_entropy_M -1]

           set ch_entropy_list_M lput this_entropy_M ch_entropy_list_M
            ask chmetro [die]]]

       ask ch_metronode
          [set rec_metronode min-one-of other metronodes with [M_entropy = max ch_entropy_list_M] [distance myself] ]]

;;; check entropy of all the possible recoveries/linkages with each one of the ROAD-METRO trans-system links
if ticks > dist_tick + recovery_lag and count roadnodes with [is-dist-RM? = true and color = black] > 1 and count metronodes with [is-dist-RM? = true and color = black] > 1
  [ask turtles with [any? rm-neighbors]
    [ask original_rm_metronodes with [is-dist-RM? = false]
          [set ch_RM_entropy (-1 * (count my-rmrm / original_rm_metro) * log (count my-rmrm / original_rm_metro) 2)]
     ask original_rm_metronodes with [is-dist-RM? = true]
          [set ch_RM_entropy 1]
     ask original_rm_roadnodes with [is-dist-RM? = false]
          [set ch_RM_entropy (-1 * (count my-rmrm / original_rm_road) * log (count my-rmrm / original_rm_road) 2)]
     ask original_rm_roadnodes with [is-dist-RM? = true]
          [set ch_RM_entropy 1]

     set ch_metronode_RM metronodes with [ch_RM_entropy = max [ch_RM_entropy] of original_rm_metronodes]
     set ch_roadnode_RM roadnodes with [ch_RM_entropy = max [ch_RM_entropy] of original_rm_roadnodes] ]]
end 

to recovery_3 ;  links based on the check of max entropy

if  ticks = 2
  [set original_h4r (-1 * sum h4r)
   set original_h4m (-1 * sum h4m)
   set original_entropy_dep entropy_dep]

if  ticks < 1
  [ask roadnodes
      [set priopop mean [population] of neighbors4]
   ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]

if any? turtles with [is-dist? = true] or any? turtles with [is-dist-RM? = true] and recovery_type = "recovery 3"
  [ifelse utility_prioritization != "recovery" and utility_prioritization !="both"
      [check_rec_max_entropy]
      [check_rec_max_entropy_prioritized]]

;;;ROAD
if ticks > dist_tick + recovery_lag and (-1 * sum h4r) != original_h4r and disturbance_type != "complex"
  [if count roadnodes with [color = black] > 2 and any? ch_roadnode
    [ifelse utility_prioritization != "recovery" and utility_prioritization !="both"
       [ask ch_roadnode
        [;create-recr-with min-one-of other roadnodes with [color = black] [distance myself] ;rec_roadnode ;[distance myself] ;no need because entropy method is topologically agnostic
          create-recr-with min-one-of other roadnodes with [is-dist? = true and who != [who] of myself] [distance myself]]]
        [foreach sort-on [priopop] ch_roadnode
           [ch_3R -> ask ch_3R
          [create-recr-with min-one-of other roadnodes with [is-dist? = true and who != [who] of myself] [distance myself]]]]]]

   if count roadnodes with [color = black] = 2
    [ask one-of roadnodes with [color = black]
       [create-recr-with min-one-of other roadnodes with [color = black] [distance myself] ]]

  if any? roadnodes with [count rr-neighbors + count recr-neighbors < original_neighborsR]
    [ask one-of roadnodes with [count rr-neighbors + count recr-neighbors < original_neighborsR]
       [create-recr-with min-one-of other roadnodes with [count rr-neighbors < original_neighborsR] [distance myself] ]]

   if count roadnodes with [color = black] = 1 and any? recroad and disturbance_type = "simple"
    [ask one-of roadnodes with [color = black]
      [create-recr-with min-one-of other roadnodes with [any? recr-neighbors] [distance myself] ]]


 ask roadnodes with [is-dist? = true]
    [if count my-rrr + count my-recroad >= original_neighborsR
      [set is-dist? false
       set color red]]

 ;;;redundant recroad links' removal ;;;
  if ticks > dist_tick + recovery_lag and any? roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
   [ask roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
      [let redundant recr-with max-one-of recr-neighbors [distance myself]
       ask redundant
         [die]]]

    ask recroad
      [set color yellow]


;;; METRO
 if ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m and disturbance_type != "complex"
   [if count metronodes with [color = black] > 2 and any? ch_metronode
     [ifelse utility_prioritization != "recovery" and utility_prioritization !="both"
        [ask ch_metronode
         [create-recm-with min-one-of other metronodes with [is-dist? = true and who != [who] of myself] [distance myself]]]
        [foreach sort-on [priopop] ch_metronode
           [ch_3M -> ask ch_3M
            [create-recm-with min-one-of other metronodes with [is-dist? = true and who != [who] of myself] [distance myself]]]]]]

   if count metronodes with [color = black] = 2
        [ask one-of metronodes with [is-dist? = true]
            [create-recm-with min-one-of other metronodes with [is-dist? = true] [distance myself]]]

   if any? metronodes with [count mm-neighbors + count recm-neighbors < original_neighborsM]
    [ask one-of metronodes with [count mm-neighbors + count recm-neighbors < original_neighborsM]
       [create-recm-with min-one-of other metronodes with [count mm-neighbors < original_neighborsM] [distance myself] ]]

  ask metronodes with [is-dist? = true]
    [if count my-mmm + count my-recmetro >= original_neighborsM
      [set is-dist? false
       set color blue]]

  ;;;redundant recmero links' removal ;;;
 if ticks > dist_tick + recovery_lag and any? metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
   [ask metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
      [let redundant recm-with max-one-of recm-neighbors [distance myself]
       ask redundant
         [die]]]

  ask recmetro
      [set color yellow]


;;;ROAD - METRO
if ticks > dist_tick + recovery_lag and any? roadnodes with [is-dist-RM? = true and color = black] and any? metronodes with [is-dist-RM? = true and color = black]
 [ifelse recovery_type != "recovery" and recovery_type != "both"
    [ask ch_metronode_RM
      [create-recrm-with min-one-of ch_roadnode_RM [distance myself] ]]
    [foreach sort-on [priopop] ch_metronode_RM
      [rec_3RM -> ask rec_3RM
        [create-recrm-with min-one-of ch_roadnode_RM [distance myself] ]]]]

ask recrmrm
  [set color yellow]

 ask original_rm_roadnodes with [is-dist-RM? = true]
   [if count rm_roadnodes >= original_rm_road
      [set is-dist-RM? false
       set color red]]
 ask original_rm_metronodes with [is-dist-RM? = true]
    [if count rm_metronodes >= original_rm_metro
      [set is-dist-RM? false
       set color blue]]
end 

to check_rec_min_entropy ; check of the potential entropy of system with the use of temporary (dummy) links

;;;; check entropy of all the possible recoveries/linkages with each one of the other ROAD nodes -> identify recovery that yields max entropy in the ROAD SUBSYSTEM
if ticks > dist_tick
     [ask roadnodes
        [ifelse count my-rrr > 0
          [set ch_R_entropy (-1 * (count my-rrr / original_neighborsR) * log (count my-rrr / original_neighborsR) 2)]
          [set ch_R_entropy 0]
         set ch_roadnode max-n-of road_dist_links roadnodes [ch_R_entropy]]]

;;;; check entropy of all the possible recoveries/linkages with each one of the other METRO nodes -> identify recovery that yields max entropy in the METRO SUBSYSTEM
 if ticks > dist_tick
   [ask metronodes
      [ifelse count my-mmm > 0
          [set ch_M_entropy (-1 * (count my-mmm / original_neighborsM) * log (count my-mmm / original_neighborsM) 2)]
          [set ch_M_entropy 0]
        set ch_metronode max-n-of metro_dist_links metronodes [ch_M_entropy]]]


;;; check entropy of all the possible recoveries/linkages with each one of the ROAD-METRO trans-system links
if ticks > dist_tick + recovery_lag and count roadnodes with [is-dist-RM? = true and color = black] > 1 and count metronodes with [is-dist-RM? = true and color = black] > 1
  [ask turtles with [any? rm-neighbors]
    [ask original_rm_metronodes with [is-dist-RM? = false]
          [set ch_RM_entropy (-1 * (count my-rmrm + count my-recrmrm / original_rm_metro) * log (count my-rmrm + count my-recrmrm / original_rm_metro) 2)]
     ask original_rm_metronodes with [is-dist-RM? = true]
          [set ch_RM_entropy 1]
     ask original_rm_roadnodes with [is-dist-RM? = false]
          [set ch_RM_entropy (-1 * (count my-rmrm + count my-recrmrm / original_rm_metro) * log (count my-rmrm + count my-recrmrm / original_rm_metro) 2)]
     ask original_rm_roadnodes with [is-dist-RM? = true]
          [set ch_RM_entropy 1]

     set ch_metronode_RM metronodes with [ch_RM_entropy = max [ch_RM_entropy] of original_rm_metronodes ]
     set ch_roadnode_RM roadnodes with [ch_RM_entropy = min [ch_RM_entropy] of original_rm_roadnodes  ] ]]
end 

to recovery_4 ;  links based on the check of min entropy

if  ticks = 2
  [set original_h4r (-1 * sum h4r)
   set original_h4m (-1 * sum h4m)
   set original_entropy_dep entropy_dep]

if  ticks < 1
  [ask roadnodes
      [set priopop mean [population] of neighbors4]
   ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]

if recovery_type = "recovery 4" and any? turtles with [is-dist? = true] or any? turtles with [is-dist-RM? = true]
  [ifelse utility_prioritization != "recovery" and utility_prioritization !="both"
      [check_rec_min_entropy]
      [check_rec_min_entropy_prioritized]]

;;;ROAD
 if ticks > dist_tick + recovery_lag and (-1 * sum h4r) != original_h4r and disturbance_type != "complex"
  [if count roadnodes with [color = black] > 2 and any? ch_roadnode
    [ifelse utility_prioritization != "recovery" and utility_prioritization !="both"
        [ask ch_roadnode
            [create-recr-with min-one-of other roadnodes [R_entropy]]] ;with [is-dist? = true and who != [who] of myself]
        [foreach sort-on [priopop] ch_roadnode
           [ch_3R -> ask ch_3R
             [create-recr-with min-one-of other roadnodes with [is-dist? = true and who != [who] of myself] [R_entropy]]]]]

   if count roadnodes with [color = black] = 2
    [ask one-of roadnodes with [color = black]
       [create-recr-with min-one-of other roadnodes with [color = black] [R_entropy]]]

   if count roadnodes with [color = black] = 1 and any? recroad and disturbance_type = "simple"
      [ask one-of roadnodes with [color = black]
          [create-recr-with min-one-of other roadnodes with [any? recr-neighbors]  [R_entropy]]] ]

 ask roadnodes with [is-dist? = true]
    [if count my-rrr + count my-recroad >= original_neighborsR
      [set is-dist? false
       set color red]]


    ask recroad
      [set color yellow]

;;; METRO
 if ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m and disturbance_type != "complex"
   [if count metronodes with [is-dist? = true] > 2 and any? ch_metronode
     [ifelse utility_prioritization != "recovery" and utility_prioritization !="both"
        [ask ch_metronode
          [create-recm-with min-one-of other metronodes [M_entropy]]]
         [foreach sort-on [priopop] ch_metronode
           [ch_3M -> ask ch_3M
             [create-recm-with min-one-of other metronodes with [is-dist? = true and who != [who] of myself] [M_entropy]]]]]

   if count metronodes with [color = black] = 2
        [ask one-of metronodes with [color = black]
            [create-recm-with min-one-of other metronodes with [color = black] [M_entropy]]] ]


  ask metronodes with [is-dist? = true]
    [if count my-mmm + count my-recmetro >= original_neighborsM
      [set is-dist? false
       set color blue]]

  ask recmetro
      [set color yellow]

;;;ROAD - METRO
if ticks > dist_tick + recovery_lag and any? roadnodes with [is-dist-RM? = true and color = black] and any? metronodes with [is-dist-RM? = true and color = black]
 [ifelse recovery_type != "recovery" and recovery_type != "both"
    [ask ch_metronode_RM
      [create-recrm-with min-one-of ch_roadnode_RM [distance myself] ]]
    [foreach sort-on [priopop] ch_metronode_RM
      [rec_3RM -> ask rec_3RM
        [create-recrm-with min-one-of ch_roadnode_RM [distance myself] ]]]]

  ask recrmrm
  [set color yellow]

 ask original_rm_roadnodes with [is-dist-RM? = true]
   [if count rm_roadnodes = original_rm_road
      [set is-dist-RM? false
       set color red]]
 ask original_rm_metronodes with [is-dist-RM? = true]
    [if count rm_metronodes = original_rm_metro
      [set is-dist-RM? false
       set color blue]]
end 

;;;;;; UTILITY PRIORITIZATION IN INITIAL RESPONSE ;;;;;; UTILITY PRIORITIZATION IN INITIAL RESPONSE ;;;;;;

to response_0_prioritized ; sequential repetitive checks and then recovery - agents inspected in priopop descending order

if  ticks < 1
    [ask roadnodes
      [set priopop mean [population] of neighbors4]
     ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]

   ask roadnodes
     [foreach sort-on [(- priopop)] roadnodes [r -> ask r
                             [if ticks < 2
                                  [set original_neighborsR count rr-neighbors]
                              let i position r sort-on [(- priopop)] roadnodes
                              if ticks = dist_tick + recovery_lag + i and original_neighborsR > count rr-neighbors
                                  [set is-dist? true]
                              if ticks > count roadnodes
                                  [ask roadnodes with [is-dist? = true]
                                    [set color black]]
                              if ticks = dist_tick + recovery_lag + i and count original_rm_roadnodes > count roadnodes with [any? rm-neighbors]
                                 [ask original_rm_roadnodes with [count rm-neighbors = 0]
                                    [set is-dist-RM? true]
                                  ask original_rm_metronodes with [count rm-neighbors = 0]
                                    [set is-dist-RM? true] ]
                              if ticks > count roadnodes + count metronodes + 1
                                  [ask original_rm_roadnodes with [is-dist-RM? = true]
                                    [set color black]
                                  ask original_rm_metronodes with [is-dist-RM? = true]
                                    [set color black] ]]] ]

 ask metronodes
   [foreach sort-on [(- priopop)] metronodes [m -> ask m
                             [if ticks < 2
                                  [set original_neighborsM count mm-neighbors]
                              let n position m sort-on [(- priopop)] turtles
                              if ticks = dist_tick + recovery_lag + n and original_neighborsM >= count mm-neighbors + 1
                                  [set is-dist? true]
                              if ticks > count roadnodes + count metronodes
                                  [ask metronodes with [is-dist? = true]
                                     [set color black]]
                              if ticks = dist_tick + recovery_lag + n and count original_rm_metronodes > count metronodes with [any? rm-neighbors]
                                  [set is-dist-RM? true
                              if ticks > count roadnodes + count metronodes + 1
                                  [ask original_rm_metronodes with [is-dist-RM? = true]
                                    [set color black] ]]]]]
end 

to response_1_prioritized  ; sequential repetitive checks and recovery combined - agents inspected in priopop descending order

  if  ticks < 1
    [ask roadnodes
      [set priopop mean [population] of neighbors4]
     ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]

 ask roadnodes
   [foreach sort-on [(- priopop)] roadnodes [r -> ask r
                             [if ticks < 2
                                  [set original_neighborsR count rr-neighbors]
                              let i position r sort-on [(- priopop)] roadnodes
                              if ticks = dist_tick + recovery_lag + i and original_neighborsR > count rr-neighbors
                                  [set is-dist? true
                                set color black]
                              if ticks = dist_tick + recovery_lag + i and count original_rm_roadnodes > count rm_roadnodes
                                  [ask original_rm_roadnodes with [not any? my-rmrm]
                                     [set is-dist-RM? true
                                      set color black]]]]]

 ask metronodes
   [foreach sort-on [(- priopop)] metronodes [m -> ask m
                             [if ticks < 2
                                  [set original_neighborsM count mm-neighbors]
                              let n position m sort-on [(- priopop)] turtles
                              if ticks = dist_tick + recovery_lag + n and original_neighborsM >= count mm-neighbors + 1
                                  [set is-dist? true
                                   set color black]
                              if ticks = dist_tick + recovery_lag + n and count original_rm_metronodes > count rm_metronodes
                                  [ask original_rm_metronodes with [not any? my-rmrm]
                                    [set is-dist-RM? true
                                     set color black]]]]]
end 

to response_2_prioritized  ; parallel repetitive checks and recovery combined - agents inspected in priopop descending order

  if  ticks < 1
    [ask roadnodes
      [set priopop mean [population] of neighbors4]
     ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]

   ask roadnodes
   [foreach sort-on [(- priopop)] roadnodes [r -> ask r
                             [if ticks < 2
                                  [set original_neighborsR count rr-neighbors]
                              let i position r sort-on [(- priopop)] roadnodes
                              if ticks = dist_tick + i and original_neighborsR > count rr-neighbors
                                  [set is-dist? true
                                   set color black]
                              if ticks = dist_tick + recovery_lag + i and count original_rm_roadnodes > count rm_roadnodes
                                  [ask original_rm_roadnodes with [not any? my-rmrm]
                                     [set is-dist-RM? true
                                      set color black]]]]]

 ask metronodes
   [foreach sort-on [(- priopop)] metronodes [m -> ask m
                             [if ticks < 2
                                  [set original_neighborsM count mm-neighbors]
                              let n position m sort-on [(- priopop)] metronodes
                              if ticks = dist_tick + n and original_neighborsM > count mm-neighbors
                                  [set is-dist? true
                                   set color black]
                              if ticks = dist_tick + recovery_lag + n and count original_rm_metronodes > count rm_metronodes
                                  [ask original_rm_metronodes with [not any? my-rmrm]
                                     [set is-dist-RM? true
                                      set color black]]]]]
end 

to response_3_prioritized ; constant monitoring of agents and immediate pesponse - the smartest - agents inspected in priopop descending order

 if  ticks < 1
    [ask roadnodes
      [set priopop mean [population] of neighbors4]
     ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]

   if  ticks < 1
   [ask roadnodes
        [set original_neighborsR count rr-neighbors]]

 if  ticks < 1
    [ask metronodes
     [set original_neighborsM count mm-neighbors]]

;; Check for road links failure ;;;
if initial_response_type = "response 3"
[ask roadnodes
  [foreach sort-on [(- priopop)] roadnodes [r -> ask r
                          [if ticks = dist_tick + 1 and original_neighborsR > count rr-neighbors
                                   [set is-dist? true
                                    set color black]]]]

   foreach sort-on [(- priopop)] original_rm_roadnodes [rmn -> ask rmn
                          [if ticks = dist_tick + 1 and count original_rm_roadnodes > count rm_roadnodes
                               [ask original_rm_roadnodes with [not any? my-rmrm]
                                   [set is-dist-RM? true
                                    set color black]]]]

;;Check for metro links failure ;;;
ask metronodes
  [foreach sort-on [(- priopop)] metronodes [m -> ask m
                          [if ticks = dist_tick + 1 and original_neighborsM > count mm-neighbors
                                  [set is-dist? true
                                   set color black]]]]

   foreach sort-on [(- priopop)] original_rm_metronodes [mrn -> ask mrn
                          [if ticks = dist_tick + 1 and count original_rm_metronodes > count rm_metronodes
                              [ask original_rm_metronodes with [not any? my-rmrm]
                                  [set is-dist-RM? true
                                   set color black]]]] ]
end 

;;;;;; UTILITY PRIORITIZATION IN RECOVERY ;;;;;; UTILITY PRIORITIZATION IN RECOVERY ;;;;;;

to recovery_0_prioritized ; restore links as in recovey_0 but with utility prioritization

  if  ticks < 1
    [ask roadnodes
      [set priopop mean [population] of neighbors4]
     ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]


;ROAD simple and cascading
 if ticks > dist_tick + recovery_lag and count roadnodes with [color = black] > 1
  [ask roadnodes with [is-dist? = true and priopop = max [priopop] of roadnodes with [is-dist? = true]]
    [create-recr-with min-one-of other roadnodes with [is-dist? = true] [distance myself]]]

 if ticks > dist_tick + recovery_lag and count roadnodes with [color = black] > 2 and disturbance_type = "cascading"
    [ask roadnodes with [is-dist? = true]
        [set ch_roadnode roadnodes with [original_neighborsR - count rr-neighbors >= 2]
     ask ch_roadnode
        [set rec_roadnode max-n-of 2 other roadnodes with [is-dist? = true] [priopop] ; first sort-on [priopop] roadnodes with [original_neighborsR - count rr-neighbors > 2] ;min-one-of other roadnodes with [is-dist? = true] [distance myself] ;original_neighborsR - count rr-neighbors < 2 and
         create-recroad-with rec_roadnode ]]]
  ask recroad
      [set color yellow]
;;;redundant recroad links' removal ;;;
 if ticks > dist_tick + recovery_lag and any? roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
   [ask roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
      [let redundant recr-with max-one-of recr-neighbors [distance myself]
       ask redundant
          [die]]]


;METRO simple and cascading
  if ticks > dist_tick + recovery_lag and count metronodes with [color = black] > 1
   [ask metronodes with [is-dist? = true and priopop = max [priopop] of metronodes with [is-dist? = true]]
      [create-recm-with min-one-of other metronodes with [is-dist? = true] [distance myself]]]

  if ticks > dist_tick + recovery_lag and count metronodes with [color = black] > 2 and disturbance_type = "cascading"
    [ask metronodes with [is-dist? = true]
      [set ch_metronode one-of metronodes with [original_neighborsM - count mm-neighbors >= 2]
     ask ch_metronode
      [set rec_metronode max-n-of 2 other metronodes with [is-dist? = true] [priopop]
       create-recmetro-with rec_metronode]]]
   ask recmetro
      [set color yellow]
;;;redundant recmetro links' removal ;;;
 if ticks > dist_tick + recovery_lag and any? metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
   [ask metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
      [let redundant recm-with max-one-of recm-neighbors [distance myself]
       ask redundant
          [die]]]


;ROAD - METRO
  if ticks > dist_tick + recovery_lag + 1 and count roadnodes with [is-dist-RM? = true] > 1 and count metronodes with [is-dist-RM? = true] > 1 and count rm_roadnodes < original_rm_road
    [foreach sort-on [priopop] original_rm_roadnodes with [is-dist-RM? = true]
      [rm_R -> ask rm_R
        [create-recrm-with min-one-of other original_rm_metronodes with [count my-rmrm = 0] [distance myself]]]
    ask recrmrm
        [set color yellow]]


;;;color identification for recovery;;;;
   ask roadnodes with [is-dist? = true]
    [if count my-rrr + count my-recroad >= original_neighborsR
      [set is-dist? false
       set color red]]

 ask metronodes with [is-dist? = true]
    [if count my-mmm + count my-recmetro >= original_neighborsM
      [set is-dist? false
       set color blue]]

 ask original_rm_roadnodes with [is-dist-RM? = true]
   [if count rm_roadnodes = original_rm_road
      [set is-dist-RM? false
       set color red]]
 ask original_rm_metronodes with [is-dist-RM? = true]
    [if count rm_metronodes = original_rm_metro
      [set is-dist-RM? false
       set color blue]]
end 

to recovery_1_prioritized ; restore links as in recovey_1 but with utility prioritization

 if  ticks = 2 ;dist_tick
  [set original_h4r (-1 * sum h4r)
   set original_h4m (-1 * sum h4m)]

 if  ticks < 1
  [ask roadnodes
      [set priopop mean [population] of neighbors4]
   ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]

;ROAD simple and cascading
  if ticks > dist_tick + recovery_lag and (-1 * sum h4r) > original_h4r and count roadnodes with [color = black] >= 2 and disturbance_type != "cascading"
    [ask max-one-of roadnodes with [is-dist? = true] [priopop]
     [create-recr-with one-of other roadnodes in-radius 10 with [is-dist? = true] ]]

  if ticks > dist_tick + recovery_lag and (-1 * sum h4r) != original_h4r and count roadnodes with [color = black] > 2 and disturbance_type = "cascading"
    [ask roadnodes
      [set ch_roadnode max-one-of roadnodes with [original_neighborsR - count rr-neighbors >= 2] [priopop]
     ask ch_roadnode
        [set rec_roadnode min-n-of 2 other roadnodes with [is-dist? = true] [distance myself] ;[priopop]
         create-recroad-with rec_roadnode]]]

  if ticks > dist_tick + recovery_lag and count roadnodes with [color = black] = 1 and disturbance_type = "cascading" and any? recroad
   [ask roadnodes with [is-dist? = true]
    [create-recr-with max-one-of other roadnodes with [any? recr-neighbors] [priopop]]]

   ask recroad
     [set color yellow]

;;;redundant recroad links' removal ;;;
; if ticks > dist_tick + recovery_lag and any? roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
;   [ask roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
;      [let redundant recr-with max-one-of recr-neighbors [distance myself]
;       ask redundant
;          [die]]]

;METRO simple and cascading
  if ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m and count metronodes with [color = black] >= 2
  [ask max-one-of metronodes with [is-dist? = true] [priopop]
    [create-recm-with min-one-of other metronodes with [is-dist? = true] [distance myself]]]

  if ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m and count metronodes with [color = black] > 2 and disturbance_type = "cascading"
    [ask metronodes
      [set ch_metronode max-one-of metronodes with [original_neighborsM - count mm-neighbors >= 2] [priopop]
     ask ch_metronode
        [set rec_metronode min-n-of 2 other metronodes with [is-dist? = true] [distance myself];[priopop]
         create-recmetro-with rec_metronode]]]

  if ticks > dist_tick + recovery_lag and count metronodes with [color = black] = 1 and disturbance_type = "cascading" and any? recmetro
   [ask metronodes with [is-dist? = true]
    [create-recm-with max-one-of other metronodes with [any? recm-neighbors] [priopop]]]

   ask recmetro
      [set color yellow]

;;;redundant recmetro links' removal ;;;
 if ticks > dist_tick + recovery_lag and any? metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
   [ask metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
      [let redundant recm-with max-one-of recm-neighbors [distance myself]
       ask redundant
          [die]]]


;ROAD - METRO
  if ticks > dist_tick + recovery_lag and count rm_roadnodes < original_rm_road
   [ask original_rm_roadnodes with [is-dist-RM? = true and priopop = max [priopop] of roadnodes with [is-dist-RM? = true]]
      [create-recrmrm-with other original_rm_metronodes with [count my-rmrm = 0]
        set is-dist-RM? false]]
    ask recrmrm
        [set color yellow]

;;;color identification for recovery;;;;
 ask roadnodes with [is-dist? = true]
    [if count my-rrr + count my-recroad = original_neighborsR
      [set is-dist? false
       set color red]]

 ask metronodes with [is-dist? = true]
    [if count my-mmm + count my-recmetro = original_neighborsM
      [set is-dist? false
       set color blue]]

 ask original_rm_roadnodes with [is-dist-RM? = true]
   [if count rm_roadnodes = original_rm_road
      [set is-dist-RM? false
       set color red]]
 ask original_rm_metronodes with [is-dist-RM? = true]
    [if count rm_metronodes = original_rm_metro
      [set is-dist-RM? false
       set color blue]]
end 

to recovery_2_prioritized ; restore links as in recovey_2 but with utility prioritization

if  ticks < 1
  [ask roadnodes
      [set priopop mean [population] of neighbors4]
   ask metronodes
      [set priopop mean [population] of neighbors in-radius 3]]

;;;;; check missing links ;;;;;;
;ROAD simple and cascading
  ask roadnodes
     [set missing_linksR (original_neighborsR - count my-rrr)]


ifelse ticks > dist_tick + recovery_lag and disturbance_type = "cascading" and count roadnodes with [color = black] > 2
     [ask roadnodes
         [set ch_roadnode roadnodes with [missing_linksR = max [missing_linksR] of roadnodes]
          set rec_roadnode roadnodes with [missing_linksR = (max [missing_linksR] of roadnodes - 1)]]]
     [ask roadnodes
         [set ch_roadnode roadnodes with [missing_linksR = max [missing_linksR] of roadnodes]]] ;

;METRO simple and cascading
  ask metronodes
         [set missing_linksM (original_neighborsM - count my-mmm)]

ifelse ticks > dist_tick + recovery_lag and disturbance_type = "cascading" and count metronodes with [color = black] > 2
     [ask metronodes
          [set ch_metronode metronodes with [missing_linksM = max [missing_linksM] of metronodes]
           set rec_metronode metronodes with [missing_linksM = max [missing_linksM] of metronodes - 1 ]]]
     [ask metronodes
          [set ch_metronode metronodes with [missing_linksM = max [missing_linksM] of metronodes ]]]

;ROAD - METRO
 ask original_rm_metronodes
     [set missing_linksRM (count original_rm_metronodes - count my-rmrm)]
 if any? metronodes with [is-dist-RM? = true and priopop = max [priopop] of metronodes with [is-dist-RM? = true]]
     [ask metronodes
        [set ch_metronode_RM metronodes with [missing_linksRM = max [missing_linksRM] of metronodes]]]

 ;;;;;;; recover ;;;;;;;;;;;;;
     if  ticks = 2
  [set original_h4r (-1 * sum h4r)
   set original_h4m(-1 * sum h4m)
   set original_entropy_dep entropy_dep]

  if ticks > dist_tick + recovery_lag and (-1 * sum h4r) != original_h4r and count roadnodes with [color = black] >= 1
      [ask ch_roadnode with [priopop = max [priopop] of roadnodes with [color = black]]
        [if disturbance_type = "simple" and count roadnodes with [color = black] >= 1 and count ch_roadnode > 1
          [create-recr-with min-one-of other ch_roadnode [distance myself] ]]]

  if ticks > dist_tick + recovery_lag and (-1 * sum h4r) != original_h4r and count roadnodes with [color = black] >= 3 and disturbance_type = "cascading" and count rec_roadnode > 1
   [foreach sort-on [priopop] ch_roadnode
     [ch_2R -> ask ch_2R
        [create-recroad-with min-n-of 2 rec_roadnode [distance myself]]]]

  if ticks > dist_tick + recovery_lag and (-1 * sum h4r) != original_h4r and count roadnodes with [color = black] = 2
     [ask ch_roadnode with [color = black]
        [create-recr-with min-one-of other roadnodes with [missing_linksR > 0 and color = black] [distance myself]]]

;;;redundant recroad links' removal ;;;
 if ticks > dist_tick + recovery_lag and any? roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
   [ask roadnodes with [count rr-neighbors + count recr-neighbors > original_neighborsR]
      [let redundant recr-with max-one-of recr-neighbors [distance myself]
       ask redundant
          [die]]]

  ask recroad
         [set color yellow]


 if ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m and count metronodes with [color = black] >= 2
     [ask ch_metronode with [priopop = max [priopop] of metronodes with [color = black]]
          [if disturbance_type = "simple" and count metronodes with [color = black] >= 2 and count ch_metronode >= 1
              [create-recm-with min-one-of other ch_metronode [distance myself]]]]

 if  ticks > dist_tick + recovery_lag and (-1 * sum h4m) > original_h4m and disturbance_type = "cascading" and count metronodes with [color = black] > 2 ;and count rec_metronode > 1
   [foreach sort-on [priopop] ch_metronode
     [ch_2M -> ask ch_2M
        [create-recmetro-with min-n-of 2 rec_metronode [distance myself]]]]

 if ticks > dist_tick + recovery_lag and (-1 * sum h4m) != original_h4m and count metronodes with [color = black] = 2
     [ask ch_metronode with [color = black]
        [create-recm-with min-one-of other metronodes with [missing_linksM > 0 and color = black] [distance myself]]]

 ;;;redundant recmetro links' removal ;;;
 if ticks > dist_tick + recovery_lag and any? metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
   [ask metronodes with [count mm-neighbors + count recm-neighbors > original_neighborsM]
      [let redundant recm-with max-one-of recm-neighbors [distance myself]
       ask redundant
          [die]]]

   ask recmetro
      [set color yellow]

  if ticks > dist_tick + recovery_lag + 1 and any? roadnodes with [is-dist-RM? = true] and any? metronodes with [is-dist-RM? = true]
      [foreach sort-on [priopop] ch_metronode_RM
        [ch_RM -> ask ch_RM
          [create-recrm-with min-one-of original_rm_roadnodes with [is-dist-RM? = true][distance myself]]]]
      ask recrmrm
        [set color yellow]


  ;;;color identification for recovery;;;;
 ask roadnodes with [is-dist? = true]
    [if count my-rrr + count my-recroad = original_neighborsR
      [set is-dist? false
       set color red]]

 ask metronodes with [is-dist? = true]
    [if count my-mmm + count my-recmetro = original_neighborsM
      [set is-dist? false
       set color blue]]

 ask original_rm_roadnodes with [is-dist-RM? = true]
   [if count rm_roadnodes = original_rm_road
      [set is-dist-RM? false
       set color red]]
 ask original_rm_metronodes with [is-dist-RM? = true]
    [if count rm_metronodes = original_rm_metro
      [set is-dist-RM? false
       set color blue]]
end 

to check_rec_max_entropy_prioritized ; check of the potential entropy of system but with utility prioritization

 ;;;; check entropy of all the possible recoveries/linkages with each one of the other ROAD nodes -> identify recovery that yields max entropy in the ROAD SUBSYSTEM
if ticks > dist_tick + recovery_lag and any? roadnodes with [is-dist? = true] and disturbance_type != "complex"
   [ask roadnodes
        [ifelse count my-rrr > 0
          [set ch_R_entropy (-1 * (count my-rrr / original_neighborsR) * log (count my-rrr / original_neighborsR) 2)]
          [set ch_R_entropy -1]
         set ch_roadnode max-n-of road_dist_links roadnodes with [priopop > (mean[priopop] of roadnodes / 8)] [ch_R_entropy] ]]


;;;; check entropy of all the possible recoveries/linkages with each one of the other METRO nodes -> identify recovery that yields max entropy in the METRO SUBSYSTEM
 if ticks > dist_tick + recovery_lag and any? metronodes with [is-dist? = true] and disturbance_type != "complex"
   [ask metronodes
      [ifelse count my-mmm > 0
          [set ch_M_entropy (-1 * (count my-mmm / original_neighborsM) * log (count my-mmm / original_neighborsM) 2)]
          [set ch_M_entropy 1]
       set ch_metronode max-n-of metro_dist_links metronodes with [priopop > (mean[priopop] of metronodes / 8)] [ch_M_entropy]]]  ;with [ch_M_entropy = max [ch_M_entropy] of metronodes and priopop = max [priopop] of metronodes with [is-dist? = true]]]


;;; check entropy of all the possible recoveries/linkages with each one of the ROAD-METRO trans-system links
 if ticks > dist_tick + recovery_lag and any? roadnodes with [is-dist-RM? = true] and any? metronodes with [is-dist-RM? = true]
    [ask turtles with [any? rm-neighbors]
      [ask original_rm_metronodes with [is-dist-RM? = false]
          [set ch_RM_entropy (-1 * (count my-rmrm / original_rm_metro) * log (count my-rmrm / original_rm_metro) 2)]
      ask original_rm_metronodes with [is-dist-RM? = true]
          [set ch_RM_entropy 1]
      ask original_rm_roadnodes with [is-dist-RM? = false]
        [set ch_RM_entropy (-1 * (count my-rmrm / original_rm_road) * log (count my-rmrm / original_rm_road) 2)]
      ask original_rm_roadnodes with [is-dist-RM? = true]
        [set ch_RM_entropy 1]

      set ch_metronode_RM metronodes with [ch_RM_entropy = max [ch_RM_entropy] of original_rm_metronodes]
      set ch_roadnode_RM roadnodes with [ch_RM_entropy = max [ch_RM_entropy] of original_rm_roadnodes]] ]
end 

to check_rec_min_entropy_prioritized ; check of the potential entropy of system but with utility prioritization

 ;;;; check entropy of all the possible recoveries/linkages with each one of the other ROAD nodes -> identify recovery that yields max entropy in the ROAD SUBSYSTEM
if ticks > dist_tick ;+ recovery_lag ;and any? roadnodes with [is-dist? = true]
   [ask roadnodes
        [ifelse count my-rrr > 0
          [set ch_R_entropy (-1 * (count my-rrr / original_neighborsR) * log (count my-rrr / original_neighborsR) 2)]
          [set ch_R_entropy -1]
         set ch_roadnode max-n-of road_dist_links roadnodes with [priopop > mean [priopop] of roadnodes] [ch_R_entropy]]]


;;;; check entropy of all the possible recoveries/linkages with each one of the other METRO nodes -> identify recovery that yields max entropy in the METRO SUBSYSTEM
 if ticks > dist_tick
   [ask metronodes
      [ifelse count my-mmm > 0
          [set ch_M_entropy (-1 * (count my-mmm / original_neighborsM) * log (count my-mmm / original_neighborsM) 2)]
          [set ch_M_entropy 1]
        set ch_metronode max-n-of metro_dist_links metronodes with [priopop > mean [priopop] of metronodes] [ch_M_entropy]]]


;;; check entropy of all the possible recoveries/linkages with each one of the ROAD-METRO trans-system links
if ticks > dist_tick + recovery_lag and count roadnodes with [is-dist-RM? = true and color = black] > 1 and count metronodes with [is-dist-RM? = true and color = black] > 1
  [ask turtles with [any? rm-neighbors]
    [ask original_rm_metronodes with [is-dist-RM? = false]
          [set ch_RM_entropy (-1 * (count my-rmrm + count my-recrmrm / original_rm_metro) * log (count my-rmrm + count my-recrmrm / original_rm_metro) 2)]
     ask original_rm_metronodes with [is-dist-RM? = true]
          [set ch_RM_entropy 1]
     ask original_rm_roadnodes with [is-dist-RM? = false]
          [set ch_RM_entropy (-1 * (count my-rmrm + count my-recrmrm / original_rm_metro) * log (count my-rmrm + count my-recrmrm / original_rm_metro) 2)]
     ask original_rm_roadnodes with [is-dist-RM? = true]
          [set ch_RM_entropy 1]

     set ch_metronode_RM metronodes with [ch_RM_entropy = max [ch_RM_entropy] of original_rm_metronodes ]
     set ch_roadnode_RM roadnodes with [ch_RM_entropy = min [ch_RM_entropy] of original_rm_roadnodes  ] ]]
end 

;;;;;; RESILIENCE ASSESSMENT PROCEDURES ;;;;;;; RESILIENCE ASSESSMENT PROCEDURES ;;;;;;;

to calcresilience
  if ticks < dist_tick
      [set initialperf mean (sublist totalcapacitylist 0 (length totalcapacitylist)) ]

  set distperf (min totalcapacitylist) / max totalcapacitylist

  if ticks > recovery_tick
  [set finalperf item (length totalcapacitylist - 1) totalcapacitylist]

  foreach totalcapacitylist [ i -> set totalloss (i - initialperf )]
   set totallosslist lput totalloss totallosslist

  set resilience (1 - (abs (sum totallosslist) / (initialperf * 100)))
  ;;; static deterministic resilience metric by Bruneau et al. 2003

  if length totalcapacitylist != recovery_tick
  [set z_resilience 1 - (((((initialperf / max totalcapacitylist) - distperf)) * (recovery_lag + 2))/ (2 * (length totalcapacitylist - recovery_tick )) )]
  ;;; deterministic resilience metric by Zobel 2011

  if initialperf != finalperf and initialperf != distperf
     [set h-r_resilience (finalperf - distperf) / (initialperf - distperf)]
  ;;; deterministic time-based resilience metric by Henry & Ramirez-Marquez 2012
end 

to calcentropy
    ;; calculate the probability of  nodes' connections as aggregated set (sum -> if the two subsystems are statistically independent (Bais & Farmer 2007))
  ask roadnodes
    [if ticks < 2
       [set roadlinks max [count my-links] of roadnodes]
     set sort-road sort roadnodes
     foreach sort-road [?1 -> ask ?1
         [set my_R_links count my-links]]]

 if roadlinks > 0
  [set p4r map [?1 -> ?1 / roadlinks] [my_R_links] of roadnodes]  ;; based on code from BRADFORD expectations
   set h4r map [?2 -> ?2 * log ?2 2] filter [n -> n > 0] p4r ;; BRADFORD expectations

 if roadlinks > 0
  [set p41r map [?1 -> 1 - ?1 / roadlinks] [my_R_links] of roadnodes ];; based on S. Lloyd's tutorial
   set h41r map [?2 -> ?2 * log ?2 2] filter [n -> n > 0] p41r
    set sr (-1 * sum h4r) + (-1 * sum h41r)

  ;; calculate the probability of  nodes' connections as aggregated set (sum -> if the two subsystems are statistically independent (Bais & Farmer 2007))
  ask metronodes
      [if ticks < 2
          [set metrolinks max [count my-links] of metronodes]
       set sort-metro sort metronodes
      foreach sort-metro [?1 -> ask ?1
        [set my_M_links count my-links]]]

  set p4m map [?1 -> ?1 / metrolinks ] [my_M_links] of metronodes ;; based on code from BRADFORD expectations
  set h4m map [?2 -> ?2 * log ?2 2] filter [n -> n > 0] p4m ;; BRADFORD expectations

  set p41m map [?1 -> 1 - ?1 / metrolinks] [my_M_links] of metronodes ;; based on S. Lloyd's tutorial
  set h41m map [?2 -> ?2 * log ?2 2] filter [n -> n > 0] p41m
  set sm (-1 * sum h4m) + (-1 * sum h41m)

  ;; calculate the probability of  powernodes' connections as aggregated set (sum -> if the two subsystems are statistically independent (Bais & Farmer 2007))
  ask criticalnodes
      [if ticks < 2
        [set served_nodes max [ci_factor * 10] of criticalnodes
         set served_roadnodes max [ci_roadfactor] of criticalnodes
         set served_metronodes max [ci_metrofactor] of criticalnodes]
       set my_CI_factor ci_factor * 10]

  set p4c map [?1 -> ?1 / served_nodes] [my_CI_factor] of criticalnodes ;; based on code from BRADFORD expectations
  set h4c map [?2 -> ?2 * log ?2 2] filter [n -> n > 0] p4c ;; BRADFORD expectations

  set p41c map [?1 -> 1 - ?1 / served_nodes] [my_CI_factor] of criticalnodes ;; based on S. Lloyd's tutorial
  set h41c map [?2 -> ?2 * log ?2 2] filter [n -> n > 0] p41c
  set sc (-1 * sum h4c) + (-1 * sum h41c)

  ;; calculate the entropy based on the summation of the entropies of the two subsystems, i.e.  and  *if the two subsystems are statistically independent (Bais & Farmer 2007)
       ;set h4y (p4y * log p4y 2)
       ;set h4c (p4c * log p4c 2)
  ;; add the h's of each subsystem's nodes and multiply by -1 to get Shannon entropy and then add the two entropies
  set entropy (-1 * sum h4r) + (-1 * sum h4m) + (-1 * sum h4c)
  set entropylist lput entropy entropylist

  ;; method to calculate joint probability for dependent disruptions
  ;; the unidirectional links cannot reflect the disturbance's propagation + directed network!
  ask turtles
      ;[foreach sort turtles [?1 -> ask ?1
        [if ticks < 1
    [set nodeneighbors count link-neighbors]
    set nodelinks nodeneighbors - count link-neighbors ]

;; calculation of srm
  let index range length [nodeneighbors] of turtles
  ifelse any? turtles with [nodelinks >= 2]
     [set p4rm (count turtles with [nodelinks >= 2]) / (length [nodeneighbors] of turtles)
      set h4rm p4rm * log p4rm 2
      set srm -1 * h4rm ]
    [set p4rm 0
     set h4rm 0
     set srm -1 * h4rm ]

  set entropyr:m sr + sm - srm
  set entropyr:mlist lput entropyr:m entropyr:mlist

  set entropy_dep (-1 * sum h4r) + (-1 * sum h4m) - srm
  set entropy_dep_list lput entropy_dep entropy_dep_list

;; calculation of srp
  ifelse any? roadnodes with [nodelinks >= 1 and is-served? = false]
    [set p4rc (count roadnodes with [nodelinks >= 1 and is-served? = false]) / served_roadnodes
     set h4rc p4rc * log p4rc 2
     set src -1 * h4rc]
    [set p4rc 0
     set h4rc 0
     set src -1 * h4rc]
  set entropyr:c sr + sc - src
  set entropyr:clist lput entropyr:c entropyr:clist

;; calculation of smp
  ifelse any? metronodes with [nodelinks >= 1 and is-served? = false]
    [set p4mc (count metronodes with [nodelinks >= 1 and is-served? = false]) / served_metronodes
     set h4mc p4mc * log p4mc 2
     set smc -1 * h4mc]
    [set p4mc 0
     set h4mc 0
     set smc -1 * h4mc]
  set entropym:c sm + sc - smc
  set entropym:clist lput entropym:c entropym:clist

;; calculation of srmp
  ifelse any? roadnodes with [nodelinks >= 1 and is-served? = false] and any? metronodes with [nodelinks >= 1 and is-served? = false]
    [set p4rmc (count turtles with [nodelinks >= 1 and is-served? = false]) / served_nodes
     set h4rmc p4rmc * log p4rmc 2
     set srmc -1 * h4rmc]
    [set p4rmc 0
     set h4rmc 0
     set srmc -1 * h4rmc]

  set entropyr:m:c sr + sm + sc - srmc
  set entropyr:m:clist lput entropyr:m:c entropyr:m:clist

  set entropy_dep (-1 * sum h4r) + (-1 * sum h4m) + (-1 * sum h4c) - srm - src - smc - srmc
  set entropy_dep_list lput entropy_dep entropy_dep_list

;; calcualte Bruneau Resilience Metric for system's information entropy ;;
  if ticks < dist_tick
     [set initialent mean (sublist entropy_dep_list 0 (length entropy_dep_list)) ]

  foreach entropy_dep_list [ i -> set totalent (i - initialent )]
   set totalentlist lput totalent totalentlist

  set entropymetric (1 - (abs (sum totalentlist) / (initialent * 100)))
  ;;; static deterministic resilience metric by Bruneau et al. 2003
end 

to calcpopulationneeds

 if  ticks = 1
   [ask turtles
      [set original_neighbors_RM count rm-neighbors]]

ask roadnodes
  [ifelse original_neighborsR > count rr-neighbors + count recr-neighbors
    [set new_population 0]
    [set new_population mean [population] of patches in-radius 4]

    set popneedsR mean [new_population] of roadnodes]

ask metronodes
  [ifelse original_neighborsM > count mm-neighbors + count recm-neighbors
    [set new_population 0]
    [set new_population mean [population] of patches in-radius 6]

    set popneedsM mean [new_population] of metronodes]

ask criticalnodes with [is-on? = true]
  [set new_population mean ([population] of patches with [population > 0] in-radius ci_range) + mean [new_population] of roadnodes in-radius ci_range + mean [new_population] of metronodes in-radius ci_range

    set popneedsCI mean [new_population] of criticalnodes]

ask turtles
  [ifelse original_neighbors_RM > count rm-neighbors + count recrm-neighbors
    [set new_populationRM 0]
    [set new_populationRM mean [population] of patches in-radius 2]

   set popneedsRM mean [new_populationRM] of turtles]


set popneeds popneedsR + popneedsM + popneedsCI + popneedsRM

ifelse popneeds != 0
  [set popneedslist lput popneeds popneedslist]
  [set popneedslist 0 ]

;; calcualte Bruneau Resilience Metric for system's utility ;;
if ticks < dist_tick
      [set initialutil mean (sublist popneedslist 0 (length popneedslist)) ]

  foreach popneedslist [ i -> set totalutil (i - initialutil)]
   set totalutillist lput totalutil totalutillist

  set utility (1  - (abs (sum totalutillist) / (initialutil * 100)))
  ;;; static deterministic resilience metric by Bruneau et al. 2003
end 

There is only one version of this model, created 4 days ago by Anastasia Tzioutziou.

Attached files

File Type Description Last updated
Resilience assessment of Thessaloniki.png preview Preview for 'Resilience assessment of Thessaloniki' 4 days ago, by Anastasia Tzioutziou Download

This model does not have any ancestors.

This model does not have any descendants.