globals [#patch temp i j x y turtle-color-list patch-color-list weights weights-neutral weights-tot weights-cum species-list species-count empty-patches-x empty-patches-y dist-community abundances niche n-width init-complete] turtles-own [species] patches-own [env-factor carrying-capacity weights-list] extensions [matrix] ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;initialise environment and individuals to setup clear-all resize-world (-((world-size - 1) / 2)) ((world-size - 1) / 2) (-((world-size - 1) / 2)) ((world-size - 1) / 2) set-patch-size world-display-size / (world-size - 1) set-default-shape turtles "circle" set turtle-color-list (sentence (n-values 13 [? * 10 + 15]) (n-values 13 [? * 9 + 15]) (n-values 13 [? * 11 + 15]) (n-values 13 [? * 8 + 15]) (n-values 13 [? * 12 + 15]) (n-values 13 [? * 7 + 15]) (n-values 13 [? * 13 + 15]) (n-values 13 [? * 6 + 15])) set patch-color-list (sentence (n-values 10 [?]) 9.9) set init-complete 0 set i 1 set niche (list) set n-width (list) repeat num-species [ assign-niche set i i + 1 ] ;;load environment file-open "landscape_5.txt" foreach sort patches[ ask ?[ set env-factor file-read ] ] file-close ;;initialise patches ask patches[ set carrying-capacity 1 ifelse display-environment [ set pcolor item (round (env-factor / 10)) patch-color-list ][set pcolor 9.9] set weights-list (list) set i 0 while [i < num-species][ set weights-list lput exp((- ((env-factor - item i niche) ^ 2)) / (2 * item i n-width ^ 2)) weights-list set i i + 1 ] ] ;;create list of empty patches coordinates set empty-patches-x (list) set empty-patches-y (list) ask patches[ if count turtles-here < carrying-capacity [ set empty-patches-x lput pxcor empty-patches-x set empty-patches-y lput pycor empty-patches-y ] ] ;;create as many turtles as patches and assign each one to a random patch set i 1 while [i < (num-species + 1)] [ set temp round (world-width ^ 2) / num-species create-turtles temp [ set species i set color item (i - 1) turtle-color-list move-to patch (first empty-patches-x) (first empty-patches-y) set empty-patches-x but-first empty-patches-x set empty-patches-y but-first empty-patches-y ] set i i + 1 ] outputs set dist-community max list round dispersal-ability min-neighborhood reset-ticks tick end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;assign a niche to each species to assign-niche set niche lput ((i - 0.5) * 100 / num-species) niche set n-width lput niche-width n-width end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;execute an iteration to go random-die preserve composite-process tick end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;ecological drift to random-die ask turtles[ if random 100 < death-proba [die] ] end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;repopulate with one individual for each extinct species to preserve set i 0 set species-list (list) foreach sort turtles [ if not member? ([species] of ?) species-list [ set species-list lput [species] of ? species-list ] ] set empty-patches-x (list) set empty-patches-y (list) ask patches[ ;;lists of empty patches coordinates if count turtles-here < carrying-capacity [ set empty-patches-x lput pxcor empty-patches-x set empty-patches-y lput pycor empty-patches-y ] ] set species-list sort species-list set i 1 repeat num-species [ if not member? i species-list [ create-turtles 1[ set species i set color item i turtle-color-list move-to patch (first empty-patches-x) (first empty-patches-y) set empty-patches-x but-first empty-patches-x set empty-patches-y but-first empty-patches-y ] ] set i i + 1 ] end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;combine dispersal and niche selection processes, with the possibility to execute only one pure process to composite-process ask patches [ set weights n-values num-species [0] if count turtles-here < carrying-capacity [ ifelse neutral-importance > 0 [ set #patch patch-at 0 0 ;;dispersal dispersal-process ;; adding the contribution from the metacommunity metacommunity-process ;; adding the contribution from the niche (can be null) niche-process ] [ ;;niche process only set weights-tot weights-list ] populate-process ] ] outputs end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;assign a weight to each species based on dispersal to dispersal-process ask turtles in-radius dist-community[ set weights replace-item (species - 1) weights ((item (species - 1) weights) + (exp(- ((distance #patch) ^ 2) / (- (dispersal-ability ^ 2) / ln 0.01))) - (item (species - 1) weights) * (exp(- ((distance #patch) ^ 2) / (- (dispersal-ability ^ 2) / ln 0.01)))) ] end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;assign a weight to each species based on relative abundance to metacommunity-process let m_local matrix:from-row-list (list weights) let m_ab matrix:from-row-list (list abundances) let m_reg matrix:times-scalar (matrix:times-scalar m_ab influence-of-meta-community) (1 / sum abundances) let m_neutral matrix:plus (matrix:plus m_local m_reg) (matrix:times-scalar (matrix:times-element-wise m_local m_reg) (- 1)) set weights-neutral item 0 matrix:to-row-list m_neutral end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;assign a weight to each species based on their niche to niche-process let m_neutral matrix:from-row-list (list weights-neutral) let m_tot m_neutral ifelse niche-importance > 0[ let m_niche matrix:from-row-list (list weights-list) set m_tot matrix:times-element-wise m_neutral m_niche ] [ set m_tot m_neutral ] set m_tot matrix:times-scalar m_tot (1 / sum (item 0 matrix:to-row-list m_tot)) set weights-tot item 0 matrix:to-row-list m_tot end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;fill empty patches with species according to their weights to populate-process if (neutral-importance > 0 and not (sum weights = 0)) or (neutral-importance = 0)[ ;;-- to delete if "only populate if there are individuals in the vicinity" not wanted ;; cumulative sum of the probabilities set weights-cum butfirst reduce [lput (?2 / sum weights-tot + last ?1) ?1] fput [0] weights-tot ;;choose a species and create the individual set temp (random 10000) / 10000 set i (length filter [? < temp] weights-cum) + 1 sprout 1 [ set species i set color item (i - 1) turtle-color-list ] ] end ;;;;;;;;;;;;; ;;;;;;;;;;;;; ;;compute the species abundance to outputs set species-count (list) foreach sort turtles [ set species-count lput [species] of ? species-count ] set abundances (list) set i 1 repeat num-species [ set abundances lput length (filter [? = i] species-count) abundances set i i + 1 ] end