diversity-example.nlogo 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. ;; Diversity example of R-extension
  2. ;;
  3. ;;
  4. ;; by Jan C. Thiele
  5. ;; University of Goettingen, Germany
  6. ;; Department Ecoinformatics, Biometrics and Forest Growth
  7. ;; Buesgenweg 4
  8. ;; 37077 Goettingen
  9. ;; Germany
  10. ;;
  11. ;; Contact: jthiele@gwdg.de
  12. ;;
  13. ;; Copyright: 2011, J.C. Thiele
  14. extensions [r]
  15. ;; one patch equals a plot of 1 ha
  16. ;; at each plot the number of trees will be counted
  17. ;; trees are individuals/turtles
  18. breed [beeches beech]
  19. breed [birches birch]
  20. breed [hornbeams hornbeam]
  21. breed [oaks oak]
  22. breed [maples maple]
  23. breed [ashs ash]
  24. breed [poplars poplar]
  25. patches-own
  26. [
  27. beechcount
  28. birchcount
  29. hornbeamcount
  30. oakcount
  31. maplecount
  32. ashcount
  33. poplarcount
  34. speccount
  35. shannon
  36. simpson
  37. pielou
  38. ]
  39. to setup
  40. clear-all
  41. ;; reset the R-workspace
  42. r:clear
  43. ;; create the forest
  44. set-default-shape turtles "tree"
  45. create-beeches random 100
  46. [
  47. set xcor random-xcor
  48. set ycor random-ycor
  49. ]
  50. create-birches random 100
  51. [
  52. set xcor random-xcor
  53. set ycor random-ycor
  54. ]
  55. create-hornbeams random 100
  56. [
  57. set xcor random-xcor
  58. set ycor random-ycor
  59. ]
  60. create-oaks random 100
  61. [
  62. set xcor random-xcor
  63. set ycor random-ycor
  64. ]
  65. create-maples random 100
  66. [
  67. set xcor random-xcor
  68. set ycor random-ycor
  69. ]
  70. create-ashs random 100
  71. [
  72. set xcor random-xcor
  73. set ycor random-ycor
  74. ]
  75. create-poplars random 100
  76. [
  77. set xcor random-xcor
  78. set ycor random-ycor
  79. ]
  80. let insp one-of patches with [count beeches-here > 0]
  81. inspect insp
  82. end
  83. to calc-diversity
  84. ;; calculate the number of individuals of each species for each patch
  85. ask patches
  86. [
  87. set beechcount count beeches-here
  88. set birchcount count birches-here
  89. set hornbeamcount count hornbeams-here
  90. set oakcount count oaks-here
  91. set maplecount count maples-here
  92. set ashcount count ashs-here
  93. set poplarcount count poplars-here
  94. ]
  95. ;; assign a new data.frame with patch-variables (the patches will be asigned in lines from upper left to lower right)
  96. (r:putagentdf "dfpatches" patches "pxcor" "pycor" "beechcount" "birchcount" "hornbeamcount" "oakcount" "maplecount" "ashcount" "poplarcount")
  97. ;; create a data.frame without the coordinates
  98. r:eval "df <- dfpatches[,3:7]"
  99. ;; load R-library vegan (must be installed!)
  100. r:eval "library(vegan)"
  101. ;; calculate diversity
  102. r:eval "spec <- specnumber(df)"
  103. r:eval "shan <- diversity(df, 'shannon')"
  104. ;; Pielou's evenness (J), if spec = 0, value of pielou will be NaN:
  105. r:eval "pielou <- shan/log(spec)"
  106. r:eval "simp <- diversity(df, 'simpson')"
  107. ;; the following section is a little bit complicated
  108. ;; it's done to save the calculated diversity values in the corresponding patch-variables
  109. ;; to get just the diversity values as a list, call: print r:get "shan"
  110. ;; create lists with the coordinates
  111. let xlist r:get "dfpatches$pxcor"
  112. let ylist r:get "dfpatches$pycor"
  113. let xylist (map [ [?1 ?2] -> list ?1 ?2 ] xlist ylist)
  114. ;; save the diversity value into the corresponding patch-variables
  115. let counter 1
  116. foreach xylist
  117. [ [?1] ->
  118. ask patch (item 0 ?1) (item 1 ?1)
  119. [
  120. set speccount (r:get (word "spec[" counter "]"))
  121. set shannon (r:get (word "shan[" counter "]"))
  122. set simpson (r:get (word "simp[" counter "]"))
  123. set pielou (r:get (word "pielou[" counter "]"))
  124. ]
  125. set counter counter + 1
  126. ]
  127. end
  128. @#$#@#$#@
  129. GRAPHICS-WINDOW
  130. 371
  131. 10
  132. 709
  133. 349
  134. -1
  135. -1
  136. 30.0
  137. 1
  138. 10
  139. 1
  140. 1
  141. 1
  142. 0
  143. 0
  144. 0
  145. 1
  146. 0
  147. 10
  148. 0
  149. 10
  150. 0
  151. 0
  152. 1
  153. ticks
  154. 30.0
  155. BUTTON
  156. 25
  157. 60
  158. 88
  159. 93
  160. NIL
  161. setup
  162. NIL
  163. 1
  164. T
  165. OBSERVER
  166. NIL
  167. NIL
  168. NIL
  169. NIL
  170. 1
  171. BUTTON
  172. 102
  173. 60
  174. 206
  175. 93
  176. NIL
  177. calc-diversity
  178. NIL
  179. 1
  180. T
  181. OBSERVER
  182. NIL
  183. NIL
  184. NIL
  185. NIL
  186. 1
  187. TEXTBOX
  188. 26
  189. 19
  190. 360
  191. 53
  192. R-Package \"vegan\" has to be installed
  193. 17
  194. 15.0
  195. 1
  196. @#$#@#$#@
  197. ## WHAT IS IT?
  198. This section could give a general understanding of what the model is trying to show or explain.
  199. ## HOW IT WORKS
  200. This section could explain what rules the agents use to create the overall behavior of the model.
  201. ## HOW TO USE IT
  202. This section could explain how to use the model, including a description of each of the items in the interface tab.
  203. ## THINGS TO NOTICE
  204. This section could give some ideas of things for the user to notice while running the model.
  205. ## THINGS TO TRY
  206. This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.
  207. ## EXTENDING THE MODEL
  208. This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc.
  209. ## NETLOGO FEATURES
  210. This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features.
  211. ## RELATED MODELS
  212. This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.
  213. ## CREDITS AND REFERENCES
  214. This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references.
  215. @#$#@#$#@
  216. default
  217. true
  218. 0
  219. Polygon -7500403 true true 150 5 40 250 150 205 260 250
  220. airplane
  221. true
  222. 0
  223. Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
  224. arrow
  225. true
  226. 0
  227. Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
  228. box
  229. false
  230. 0
  231. Polygon -7500403 true true 150 285 285 225 285 75 150 135
  232. Polygon -7500403 true true 150 135 15 75 150 15 285 75
  233. Polygon -7500403 true true 15 75 15 225 150 285 150 135
  234. Line -16777216 false 150 285 150 135
  235. Line -16777216 false 150 135 15 75
  236. Line -16777216 false 150 135 285 75
  237. bug
  238. true
  239. 0
  240. Circle -7500403 true true 96 182 108
  241. Circle -7500403 true true 110 127 80
  242. Circle -7500403 true true 110 75 80
  243. Line -7500403 true 150 100 80 30
  244. Line -7500403 true 150 100 220 30
  245. butterfly
  246. true
  247. 0
  248. Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
  249. Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
  250. Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
  251. Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
  252. Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
  253. Circle -16777216 true false 135 90 30
  254. Line -16777216 false 150 105 195 60
  255. Line -16777216 false 150 105 105 60
  256. car
  257. false
  258. 0
  259. Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
  260. Circle -16777216 true false 180 180 90
  261. Circle -16777216 true false 30 180 90
  262. Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
  263. Circle -7500403 true true 47 195 58
  264. Circle -7500403 true true 195 195 58
  265. circle
  266. false
  267. 0
  268. Circle -7500403 true true 0 0 300
  269. circle 2
  270. false
  271. 0
  272. Circle -7500403 true true 0 0 300
  273. Circle -16777216 true false 30 30 240
  274. cow
  275. false
  276. 0
  277. Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
  278. Polygon -7500403 true true 73 210 86 251 62 249 48 208
  279. Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
  280. cylinder
  281. false
  282. 0
  283. Circle -7500403 true true 0 0 300
  284. dot
  285. false
  286. 0
  287. Circle -7500403 true true 90 90 120
  288. face happy
  289. false
  290. 0
  291. Circle -7500403 true true 8 8 285
  292. Circle -16777216 true false 60 75 60
  293. Circle -16777216 true false 180 75 60
  294. Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240
  295. face neutral
  296. false
  297. 0
  298. Circle -7500403 true true 8 7 285
  299. Circle -16777216 true false 60 75 60
  300. Circle -16777216 true false 180 75 60
  301. Rectangle -16777216 true false 60 195 240 225
  302. face sad
  303. false
  304. 0
  305. Circle -7500403 true true 8 8 285
  306. Circle -16777216 true false 60 75 60
  307. Circle -16777216 true false 180 75 60
  308. Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183
  309. fish
  310. false
  311. 0
  312. Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
  313. Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
  314. Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
  315. Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
  316. Circle -16777216 true false 215 106 30
  317. flag
  318. false
  319. 0
  320. Rectangle -7500403 true true 60 15 75 300
  321. Polygon -7500403 true true 90 150 270 90 90 30
  322. Line -7500403 true 75 135 90 135
  323. Line -7500403 true 75 45 90 45
  324. flower
  325. false
  326. 0
  327. Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
  328. Circle -7500403 true true 85 132 38
  329. Circle -7500403 true true 130 147 38
  330. Circle -7500403 true true 192 85 38
  331. Circle -7500403 true true 85 40 38
  332. Circle -7500403 true true 177 40 38
  333. Circle -7500403 true true 177 132 38
  334. Circle -7500403 true true 70 85 38
  335. Circle -7500403 true true 130 25 38
  336. Circle -7500403 true true 96 51 108
  337. Circle -16777216 true false 113 68 74
  338. Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
  339. Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
  340. house
  341. false
  342. 0
  343. Rectangle -7500403 true true 45 120 255 285
  344. Rectangle -16777216 true false 120 210 180 285
  345. Polygon -7500403 true true 15 120 150 15 285 120
  346. Line -16777216 false 30 120 270 120
  347. leaf
  348. false
  349. 0
  350. Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
  351. Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
  352. line
  353. true
  354. 0
  355. Line -7500403 true 150 0 150 300
  356. line half
  357. true
  358. 0
  359. Line -7500403 true 150 0 150 150
  360. pentagon
  361. false
  362. 0
  363. Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
  364. person
  365. false
  366. 0
  367. Circle -7500403 true true 110 5 80
  368. Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
  369. Rectangle -7500403 true true 127 79 172 94
  370. Polygon -7500403 true true 195 90 240 150 225 180 165 105
  371. Polygon -7500403 true true 105 90 60 150 75 180 135 105
  372. plant
  373. false
  374. 0
  375. Rectangle -7500403 true true 135 90 165 300
  376. Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
  377. Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
  378. Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
  379. Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
  380. Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
  381. Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
  382. Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
  383. square
  384. false
  385. 0
  386. Rectangle -7500403 true true 30 30 270 270
  387. square 2
  388. false
  389. 0
  390. Rectangle -7500403 true true 30 30 270 270
  391. Rectangle -16777216 true false 60 60 240 240
  392. star
  393. false
  394. 0
  395. Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
  396. target
  397. false
  398. 0
  399. Circle -7500403 true true 0 0 300
  400. Circle -16777216 true false 30 30 240
  401. Circle -7500403 true true 60 60 180
  402. Circle -16777216 true false 90 90 120
  403. Circle -7500403 true true 120 120 60
  404. tree
  405. false
  406. 0
  407. Circle -7500403 true true 118 3 94
  408. Rectangle -6459832 true false 120 195 180 300
  409. Circle -7500403 true true 65 21 108
  410. Circle -7500403 true true 116 41 127
  411. Circle -7500403 true true 45 90 120
  412. Circle -7500403 true true 104 74 152
  413. triangle
  414. false
  415. 0
  416. Polygon -7500403 true true 150 30 15 255 285 255
  417. triangle 2
  418. false
  419. 0
  420. Polygon -7500403 true true 150 30 15 255 285 255
  421. Polygon -16777216 true false 151 99 225 223 75 224
  422. truck
  423. false
  424. 0
  425. Rectangle -7500403 true true 4 45 195 187
  426. Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
  427. Rectangle -1 true false 195 60 195 105
  428. Polygon -16777216 true false 238 112 252 141 219 141 218 112
  429. Circle -16777216 true false 234 174 42
  430. Rectangle -7500403 true true 181 185 214 194
  431. Circle -16777216 true false 144 174 42
  432. Circle -16777216 true false 24 174 42
  433. Circle -7500403 false true 24 174 42
  434. Circle -7500403 false true 144 174 42
  435. Circle -7500403 false true 234 174 42
  436. turtle
  437. true
  438. 0
  439. Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
  440. Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
  441. Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
  442. Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
  443. Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
  444. Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
  445. wheel
  446. false
  447. 0
  448. Circle -7500403 true true 3 3 294
  449. Circle -16777216 true false 30 30 240
  450. Line -7500403 true 150 285 150 15
  451. Line -7500403 true 15 150 285 150
  452. Circle -7500403 true true 120 120 60
  453. Line -7500403 true 216 40 79 269
  454. Line -7500403 true 40 84 269 221
  455. Line -7500403 true 40 216 269 79
  456. Line -7500403 true 84 40 221 269
  457. x
  458. false
  459. 0
  460. Polygon -7500403 true true 270 75 225 30 30 225 75 270
  461. Polygon -7500403 true true 30 75 75 30 270 225 225 270
  462. @#$#@#$#@
  463. NetLogo 6.0-BETA2
  464. @#$#@#$#@
  465. @#$#@#$#@
  466. @#$#@#$#@
  467. @#$#@#$#@
  468. @#$#@#$#@
  469. default
  470. 0.0
  471. -0.2 0 0.0 1.0
  472. 0.0 1 1.0 0.0
  473. 0.2 0 0.0 1.0
  474. link direction
  475. true
  476. 0
  477. Line -7500403 true 150 150 90 180
  478. Line -7500403 true 150 150 210 180
  479. @#$#@#$#@
  480. 0
  481. @#$#@#$#@