homerange-example.nlogo 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. ;; Homerange 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. breed [animals animal]
  16. breed [homeranges homerange]
  17. animals-own
  18. [ Name X Y ]
  19. homeranges-own
  20. [ Name ]
  21. to setup
  22. clear-all
  23. r:clear
  24. ;; create three different animals (rabbit, cat, fox)
  25. create-animals 3
  26. [
  27. if (who = 0)
  28. [
  29. set Name "rabbit"
  30. set color 14
  31. ]
  32. if (who = 1)
  33. [
  34. set Name "cat"
  35. set color 44
  36. ]
  37. if (who = 2)
  38. [
  39. set Name "fox"
  40. set color 94
  41. ]
  42. set xcor random-pxcor
  43. set ycor random-pycor
  44. set X (list xcor)
  45. set Y (list ycor)
  46. pen-down
  47. ]
  48. go
  49. end
  50. to go
  51. repeat 100
  52. [
  53. ;; move the animals randomly
  54. ask animals
  55. [
  56. let nextpatch patch-at-heading-and-distance random 360 random 3
  57. if (nextpatch != nobody)
  58. [ move-to nextpatch ]
  59. set X lput xcor X
  60. set Y lput ycor Y
  61. ]
  62. ]
  63. end
  64. to calc-homerange
  65. r:eval "library(adehabitat)"
  66. ;; create an empty data.frame"
  67. r:eval "turtles <- data.frame()"
  68. ;; merge the Name, X- and Y-lists of all animals to one big data.frame
  69. ask animals
  70. [
  71. (r:putdataframe "turtle" "X" X "Y" Y)
  72. r:eval (word "turtle <- data.frame(turtle, Name = '" Name "')")
  73. r:eval "turtles <- rbind(turtles, turtle)"
  74. ]
  75. ;; split the data.frame into coordinates and factor variable
  76. r:eval "xy <- turtles[,c('X','Y')]"
  77. r:eval "id <- turtles$Name"
  78. ;; calculate homerange
  79. r:eval "homerange <- mcp(xy, id)"
  80. ifelse (plot-homerange)
  81. [ plot-into-file ]
  82. [
  83. mark-homeranges
  84. plot-area
  85. ]
  86. end
  87. to plot-into-file
  88. ;; plot the homerange polygons (into pdf-file)
  89. r:eval (word "pdf('" homerange-plot ".pdf')" )
  90. r:eval (word "plot(homerange, xlim=c(" min-pxcor "," max-pxcor "), ylim=c(" min-pycor "," max-pycor "))")
  91. r:eval "dev.off()"
  92. ;; plot the homerange area depending on the homerange level (into pdf-file)
  93. r:eval (word "pdf('" homerange-level-plot ".pdf')" )
  94. r:eval "mcp.area(xy, id, unout='m2')"
  95. r:eval "dev.off()"
  96. end
  97. to mark-homeranges
  98. clear-drawing
  99. ask animals
  100. [
  101. pen-up
  102. ;; get the points of the homerange polygon for the current animal
  103. r:eval (word "temp <- subset(homerange, ID=='"Name"')")
  104. let tempX r:get "temp$X"
  105. let tempY r:get "temp$Y"
  106. let tempXY (map [ [?1 ?2] -> list ?1 ?2 ] tempX tempY)
  107. ;; create a turtle, which draws the homerange boundary
  108. hatch-homeranges 1
  109. [
  110. hide-turtle
  111. set Name [Name] of myself
  112. set color [color] of myself
  113. ]
  114. ;; draw the homerange boundary
  115. foreach tempXY
  116. [ [?1] ->
  117. ask homeranges with [Name = [Name] of myself]
  118. [
  119. move-to patch (item 0 ?1) (item 1 ?1)
  120. pen-down
  121. ]
  122. ]
  123. ;; connect the last point of the homerange with the first one, to close the polygon
  124. ask homeranges with [Name = [Name] of myself]
  125. [
  126. let lastpoint first tempXY
  127. move-to patch (item 0 lastpoint) (item 1 lastpoint)
  128. ]
  129. ]
  130. end
  131. to plot-area
  132. clear-all-plots
  133. let precstart 20
  134. let precincre 5
  135. ;; calculate the size of the homerange depending on homerange level
  136. r:eval (word "area <- mcp.area(xy, id, unout='m2', percent = seq(" precstart ",100, by = " precincre "), plotit=FALSE)")
  137. ;; plot the homerange area of every animal, using temporary plot pen
  138. ask animals
  139. [
  140. let arealist r:get (word "area$" Name)
  141. set-current-plot "home range area"
  142. create-temporary-plot-pen Name
  143. set-plot-pen-color color
  144. let prec precstart
  145. foreach arealist
  146. [ [?1] ->
  147. plotxy prec ?1
  148. set prec prec + precincre
  149. ]
  150. ]
  151. end
  152. @#$#@#$#@
  153. GRAPHICS-WINDOW
  154. 346
  155. 10
  156. 859
  157. 524
  158. -1
  159. -1
  160. 5.0
  161. 1
  162. 10
  163. 1
  164. 1
  165. 1
  166. 0
  167. 0
  168. 0
  169. 1
  170. 0
  171. 100
  172. 0
  173. 100
  174. 0
  175. 0
  176. 1
  177. ticks
  178. 30.0
  179. BUTTON
  180. 10
  181. 68
  182. 73
  183. 101
  184. NIL
  185. setup
  186. NIL
  187. 1
  188. T
  189. OBSERVER
  190. NIL
  191. NIL
  192. NIL
  193. NIL
  194. 1
  195. BUTTON
  196. 9
  197. 113
  198. 128
  199. 146
  200. NIL
  201. calc-homerange
  202. NIL
  203. 1
  204. T
  205. OBSERVER
  206. NIL
  207. NIL
  208. NIL
  209. NIL
  210. 1
  211. TEXTBOX
  212. 10
  213. 16
  214. 339
  215. 58
  216. R-packages \"adehabitat\" and \"gpclib\" have to be installed!
  217. 17
  218. 15.0
  219. 1
  220. SWITCH
  221. 5
  222. 176
  223. 148
  224. 209
  225. plot-homerange
  226. plot-homerange
  227. 1
  228. 1
  229. -1000
  230. INPUTBOX
  231. 5
  232. 220
  233. 200
  234. 280
  235. homerange-plot
  236. homerange-plot-filename
  237. 1
  238. 0
  239. String
  240. INPUTBOX
  241. 5
  242. 290
  243. 200
  244. 350
  245. homerange-level-plot
  246. homerange-level-plot-filename
  247. 1
  248. 0
  249. String
  250. PLOT
  251. 7
  252. 359
  253. 343
  254. 545
  255. home range area
  256. home-range level
  257. home-range size (m2)
  258. 0.0
  259. 1.0
  260. 0.0
  261. 1.0
  262. true
  263. false
  264. "" ""
  265. PENS
  266. "default" 1.0 0 -16777216 true "" ""
  267. BUTTON
  268. 210
  269. 220
  270. 340
  271. 280
  272. Choose path...
  273. set homerange-plot user-new-file
  274. NIL
  275. 1
  276. T
  277. OBSERVER
  278. NIL
  279. NIL
  280. NIL
  281. NIL
  282. 1
  283. BUTTON
  284. 210
  285. 290
  286. 340
  287. 350
  288. Choose path...
  289. set homerange-level-plot user-new-file\n
  290. NIL
  291. 1
  292. T
  293. OBSERVER
  294. NIL
  295. NIL
  296. NIL
  297. NIL
  298. 1
  299. @#$#@#$#@
  300. ## WHAT IS IT?
  301. This section could give a general understanding of what the model is trying to show or explain.
  302. ## HOW IT WORKS
  303. This section could explain what rules the agents use to create the overall behavior of the model.
  304. ## HOW TO USE IT
  305. This section could explain how to use the model, including a description of each of the items in the interface tab.
  306. ## THINGS TO NOTICE
  307. This section could give some ideas of things for the user to notice while running the model.
  308. ## THINGS TO TRY
  309. This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.
  310. ## EXTENDING THE MODEL
  311. 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.
  312. ## NETLOGO FEATURES
  313. 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.
  314. ## RELATED MODELS
  315. This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.
  316. ## CREDITS AND REFERENCES
  317. 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.
  318. @#$#@#$#@
  319. default
  320. true
  321. 0
  322. Polygon -7500403 true true 150 5 40 250 150 205 260 250
  323. airplane
  324. true
  325. 0
  326. 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
  327. arrow
  328. true
  329. 0
  330. Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
  331. box
  332. false
  333. 0
  334. Polygon -7500403 true true 150 285 285 225 285 75 150 135
  335. Polygon -7500403 true true 150 135 15 75 150 15 285 75
  336. Polygon -7500403 true true 15 75 15 225 150 285 150 135
  337. Line -16777216 false 150 285 150 135
  338. Line -16777216 false 150 135 15 75
  339. Line -16777216 false 150 135 285 75
  340. bug
  341. true
  342. 0
  343. Circle -7500403 true true 96 182 108
  344. Circle -7500403 true true 110 127 80
  345. Circle -7500403 true true 110 75 80
  346. Line -7500403 true 150 100 80 30
  347. Line -7500403 true 150 100 220 30
  348. butterfly
  349. true
  350. 0
  351. Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
  352. Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
  353. Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
  354. Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
  355. Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
  356. Circle -16777216 true false 135 90 30
  357. Line -16777216 false 150 105 195 60
  358. Line -16777216 false 150 105 105 60
  359. car
  360. false
  361. 0
  362. 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
  363. Circle -16777216 true false 180 180 90
  364. Circle -16777216 true false 30 180 90
  365. Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
  366. Circle -7500403 true true 47 195 58
  367. Circle -7500403 true true 195 195 58
  368. circle
  369. false
  370. 0
  371. Circle -7500403 true true 0 0 300
  372. circle 2
  373. false
  374. 0
  375. Circle -7500403 true true 0 0 300
  376. Circle -16777216 true false 30 30 240
  377. cow
  378. false
  379. 0
  380. 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
  381. Polygon -7500403 true true 73 210 86 251 62 249 48 208
  382. Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
  383. cylinder
  384. false
  385. 0
  386. Circle -7500403 true true 0 0 300
  387. dot
  388. false
  389. 0
  390. Circle -7500403 true true 90 90 120
  391. face happy
  392. false
  393. 0
  394. Circle -7500403 true true 8 8 285
  395. Circle -16777216 true false 60 75 60
  396. Circle -16777216 true false 180 75 60
  397. 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
  398. face neutral
  399. false
  400. 0
  401. Circle -7500403 true true 8 7 285
  402. Circle -16777216 true false 60 75 60
  403. Circle -16777216 true false 180 75 60
  404. Rectangle -16777216 true false 60 195 240 225
  405. face sad
  406. false
  407. 0
  408. Circle -7500403 true true 8 8 285
  409. Circle -16777216 true false 60 75 60
  410. Circle -16777216 true false 180 75 60
  411. 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
  412. fish
  413. false
  414. 0
  415. Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
  416. Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
  417. Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
  418. 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
  419. Circle -16777216 true false 215 106 30
  420. flag
  421. false
  422. 0
  423. Rectangle -7500403 true true 60 15 75 300
  424. Polygon -7500403 true true 90 150 270 90 90 30
  425. Line -7500403 true 75 135 90 135
  426. Line -7500403 true 75 45 90 45
  427. flower
  428. false
  429. 0
  430. Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
  431. Circle -7500403 true true 85 132 38
  432. Circle -7500403 true true 130 147 38
  433. Circle -7500403 true true 192 85 38
  434. Circle -7500403 true true 85 40 38
  435. Circle -7500403 true true 177 40 38
  436. Circle -7500403 true true 177 132 38
  437. Circle -7500403 true true 70 85 38
  438. Circle -7500403 true true 130 25 38
  439. Circle -7500403 true true 96 51 108
  440. Circle -16777216 true false 113 68 74
  441. Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
  442. Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
  443. house
  444. false
  445. 0
  446. Rectangle -7500403 true true 45 120 255 285
  447. Rectangle -16777216 true false 120 210 180 285
  448. Polygon -7500403 true true 15 120 150 15 285 120
  449. Line -16777216 false 30 120 270 120
  450. leaf
  451. false
  452. 0
  453. 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
  454. Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
  455. line
  456. true
  457. 0
  458. Line -7500403 true 150 0 150 300
  459. line half
  460. true
  461. 0
  462. Line -7500403 true 150 0 150 150
  463. pentagon
  464. false
  465. 0
  466. Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
  467. person
  468. false
  469. 0
  470. Circle -7500403 true true 110 5 80
  471. 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
  472. Rectangle -7500403 true true 127 79 172 94
  473. Polygon -7500403 true true 195 90 240 150 225 180 165 105
  474. Polygon -7500403 true true 105 90 60 150 75 180 135 105
  475. plant
  476. false
  477. 0
  478. Rectangle -7500403 true true 135 90 165 300
  479. Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
  480. Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
  481. Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
  482. Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
  483. Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
  484. Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
  485. Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
  486. square
  487. false
  488. 0
  489. Rectangle -7500403 true true 30 30 270 270
  490. square 2
  491. false
  492. 0
  493. Rectangle -7500403 true true 30 30 270 270
  494. Rectangle -16777216 true false 60 60 240 240
  495. star
  496. false
  497. 0
  498. Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
  499. target
  500. false
  501. 0
  502. Circle -7500403 true true 0 0 300
  503. Circle -16777216 true false 30 30 240
  504. Circle -7500403 true true 60 60 180
  505. Circle -16777216 true false 90 90 120
  506. Circle -7500403 true true 120 120 60
  507. tree
  508. false
  509. 0
  510. Circle -7500403 true true 118 3 94
  511. Rectangle -6459832 true false 120 195 180 300
  512. Circle -7500403 true true 65 21 108
  513. Circle -7500403 true true 116 41 127
  514. Circle -7500403 true true 45 90 120
  515. Circle -7500403 true true 104 74 152
  516. triangle
  517. false
  518. 0
  519. Polygon -7500403 true true 150 30 15 255 285 255
  520. triangle 2
  521. false
  522. 0
  523. Polygon -7500403 true true 150 30 15 255 285 255
  524. Polygon -16777216 true false 151 99 225 223 75 224
  525. truck
  526. false
  527. 0
  528. Rectangle -7500403 true true 4 45 195 187
  529. Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
  530. Rectangle -1 true false 195 60 195 105
  531. Polygon -16777216 true false 238 112 252 141 219 141 218 112
  532. Circle -16777216 true false 234 174 42
  533. Rectangle -7500403 true true 181 185 214 194
  534. Circle -16777216 true false 144 174 42
  535. Circle -16777216 true false 24 174 42
  536. Circle -7500403 false true 24 174 42
  537. Circle -7500403 false true 144 174 42
  538. Circle -7500403 false true 234 174 42
  539. turtle
  540. true
  541. 0
  542. Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
  543. Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
  544. Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
  545. Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
  546. Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
  547. Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
  548. wheel
  549. false
  550. 0
  551. Circle -7500403 true true 3 3 294
  552. Circle -16777216 true false 30 30 240
  553. Line -7500403 true 150 285 150 15
  554. Line -7500403 true 15 150 285 150
  555. Circle -7500403 true true 120 120 60
  556. Line -7500403 true 216 40 79 269
  557. Line -7500403 true 40 84 269 221
  558. Line -7500403 true 40 216 269 79
  559. Line -7500403 true 84 40 221 269
  560. x
  561. false
  562. 0
  563. Polygon -7500403 true true 270 75 225 30 30 225 75 270
  564. Polygon -7500403 true true 30 75 75 30 270 225 225 270
  565. @#$#@#$#@
  566. NetLogo 6.0-BETA2
  567. @#$#@#$#@
  568. @#$#@#$#@
  569. @#$#@#$#@
  570. @#$#@#$#@
  571. @#$#@#$#@
  572. default
  573. 0.0
  574. -0.2 0 0.0 1.0
  575. 0.0 1 1.0 0.0
  576. 0.2 0 0.0 1.0
  577. link direction
  578. true
  579. 0
  580. Line -7500403 true 150 150 90 180
  581. Line -7500403 true 150 150 210 180
  582. @#$#@#$#@
  583. 1
  584. @#$#@#$#@