linear_regression-example.nlogo 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. ;; Linear Regression 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. patches-own
  16. [ food-items ]
  17. turtles-own
  18. [
  19. memory ;; list of items-found of the last 20 days
  20. ]
  21. to setup
  22. clear-all
  23. ;; create initial food sources on patches
  24. create-food-sources
  25. ;; create 10 turtles with random initial memory of collected food items
  26. crt 10
  27. [
  28. set xcor random-xcor
  29. set ycor random-ycor
  30. set heading random 360
  31. set memory []
  32. repeat 20
  33. [
  34. set memory lput random 5 memory
  35. ]
  36. ]
  37. ;; setup an R vector for the x variable
  38. r:eval "x <- seq(1,20)"
  39. end
  40. to go
  41. ask turtles
  42. [
  43. ;; send the memory to R
  44. r:put "y" memory
  45. ;; fit a linear model
  46. r:eval "fm <- lm(y ~ x)"
  47. ;; get the results back from R
  48. let b r:get "fm$coefficient[1]"
  49. let x r:get "fm$coefficient[2]"
  50. ;; make a forecast for the next day
  51. let forecast b + x * 11
  52. if forecast < 0
  53. [ set forecast 0 ]
  54. show forecast
  55. let items-found 0
  56. ; move steps, depending on forecast, i.e. motivation and nutritional condition
  57. repeat (forecast)
  58. [
  59. move-to patch-at-heading-and-distance random 90 1
  60. if ([food-items] of patch-here > 0)
  61. [
  62. ;; collect food items of patch
  63. ask patch-here
  64. [
  65. set items-found items-found + food-items
  66. set food-items 0
  67. ]
  68. ]
  69. ]
  70. ;; add the current value to the memory
  71. set memory lput items-found memory
  72. ;; delete the oldest memory value
  73. set memory remove-item 0 memory
  74. ;; if turtle is 20 days without food -> die!
  75. if (sum memory <= 0)
  76. [ die ]
  77. ]
  78. create-food-sources
  79. end
  80. to create-food-sources
  81. ask n-of 30 patches
  82. [
  83. set food-items random 15
  84. ]
  85. ask patches
  86. [
  87. ifelse (food-items > 0)
  88. [ set pcolor yellow ]
  89. [ set pcolor black ]
  90. ]
  91. end
  92. @#$#@#$#@
  93. GRAPHICS-WINDOW
  94. 205
  95. 10
  96. 543
  97. 349
  98. -1
  99. -1
  100. 30.0
  101. 1
  102. 10
  103. 1
  104. 1
  105. 1
  106. 0
  107. 1
  108. 1
  109. 1
  110. 0
  111. 10
  112. 0
  113. 10
  114. 0
  115. 0
  116. 1
  117. ticks
  118. 30.0
  119. BUTTON
  120. 10
  121. 26
  122. 73
  123. 59
  124. NIL
  125. setup
  126. NIL
  127. 1
  128. T
  129. OBSERVER
  130. NIL
  131. NIL
  132. NIL
  133. NIL
  134. 1
  135. BUTTON
  136. 11
  137. 67
  138. 74
  139. 100
  140. NIL
  141. go
  142. T
  143. 1
  144. T
  145. OBSERVER
  146. NIL
  147. NIL
  148. NIL
  149. NIL
  150. 1
  151. MONITOR
  152. 15
  153. 118
  154. 98
  155. 163
  156. NIL
  157. count turtles
  158. 17
  159. 1
  160. 11
  161. @#$#@#$#@
  162. ## WHAT IS IT?
  163. This section could give a general understanding of what the model is trying to show or explain.
  164. ## HOW IT WORKS
  165. This section could explain what rules the agents use to create the overall behavior of the model.
  166. ## HOW TO USE IT
  167. This section could explain how to use the model, including a description of each of the items in the interface tab.
  168. ## THINGS TO NOTICE
  169. This section could give some ideas of things for the user to notice while running the model.
  170. ## THINGS TO TRY
  171. This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.
  172. ## EXTENDING THE MODEL
  173. 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.
  174. ## NETLOGO FEATURES
  175. 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.
  176. ## RELATED MODELS
  177. This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.
  178. ## CREDITS AND REFERENCES
  179. 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.
  180. @#$#@#$#@
  181. default
  182. true
  183. 0
  184. Polygon -7500403 true true 150 5 40 250 150 205 260 250
  185. airplane
  186. true
  187. 0
  188. 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
  189. arrow
  190. true
  191. 0
  192. Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
  193. box
  194. false
  195. 0
  196. Polygon -7500403 true true 150 285 285 225 285 75 150 135
  197. Polygon -7500403 true true 150 135 15 75 150 15 285 75
  198. Polygon -7500403 true true 15 75 15 225 150 285 150 135
  199. Line -16777216 false 150 285 150 135
  200. Line -16777216 false 150 135 15 75
  201. Line -16777216 false 150 135 285 75
  202. bug
  203. true
  204. 0
  205. Circle -7500403 true true 96 182 108
  206. Circle -7500403 true true 110 127 80
  207. Circle -7500403 true true 110 75 80
  208. Line -7500403 true 150 100 80 30
  209. Line -7500403 true 150 100 220 30
  210. butterfly
  211. true
  212. 0
  213. Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
  214. Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
  215. Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
  216. Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
  217. Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
  218. Circle -16777216 true false 135 90 30
  219. Line -16777216 false 150 105 195 60
  220. Line -16777216 false 150 105 105 60
  221. car
  222. false
  223. 0
  224. 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
  225. Circle -16777216 true false 180 180 90
  226. Circle -16777216 true false 30 180 90
  227. Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
  228. Circle -7500403 true true 47 195 58
  229. Circle -7500403 true true 195 195 58
  230. circle
  231. false
  232. 0
  233. Circle -7500403 true true 0 0 300
  234. circle 2
  235. false
  236. 0
  237. Circle -7500403 true true 0 0 300
  238. Circle -16777216 true false 30 30 240
  239. cow
  240. false
  241. 0
  242. 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
  243. Polygon -7500403 true true 73 210 86 251 62 249 48 208
  244. Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
  245. cylinder
  246. false
  247. 0
  248. Circle -7500403 true true 0 0 300
  249. dot
  250. false
  251. 0
  252. Circle -7500403 true true 90 90 120
  253. face happy
  254. false
  255. 0
  256. Circle -7500403 true true 8 8 285
  257. Circle -16777216 true false 60 75 60
  258. Circle -16777216 true false 180 75 60
  259. 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
  260. face neutral
  261. false
  262. 0
  263. Circle -7500403 true true 8 7 285
  264. Circle -16777216 true false 60 75 60
  265. Circle -16777216 true false 180 75 60
  266. Rectangle -16777216 true false 60 195 240 225
  267. face sad
  268. false
  269. 0
  270. Circle -7500403 true true 8 8 285
  271. Circle -16777216 true false 60 75 60
  272. Circle -16777216 true false 180 75 60
  273. 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
  274. fish
  275. false
  276. 0
  277. Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
  278. Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
  279. Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
  280. 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
  281. Circle -16777216 true false 215 106 30
  282. flag
  283. false
  284. 0
  285. Rectangle -7500403 true true 60 15 75 300
  286. Polygon -7500403 true true 90 150 270 90 90 30
  287. Line -7500403 true 75 135 90 135
  288. Line -7500403 true 75 45 90 45
  289. flower
  290. false
  291. 0
  292. Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
  293. Circle -7500403 true true 85 132 38
  294. Circle -7500403 true true 130 147 38
  295. Circle -7500403 true true 192 85 38
  296. Circle -7500403 true true 85 40 38
  297. Circle -7500403 true true 177 40 38
  298. Circle -7500403 true true 177 132 38
  299. Circle -7500403 true true 70 85 38
  300. Circle -7500403 true true 130 25 38
  301. Circle -7500403 true true 96 51 108
  302. Circle -16777216 true false 113 68 74
  303. Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
  304. Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
  305. house
  306. false
  307. 0
  308. Rectangle -7500403 true true 45 120 255 285
  309. Rectangle -16777216 true false 120 210 180 285
  310. Polygon -7500403 true true 15 120 150 15 285 120
  311. Line -16777216 false 30 120 270 120
  312. leaf
  313. false
  314. 0
  315. 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
  316. Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
  317. line
  318. true
  319. 0
  320. Line -7500403 true 150 0 150 300
  321. line half
  322. true
  323. 0
  324. Line -7500403 true 150 0 150 150
  325. pentagon
  326. false
  327. 0
  328. Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
  329. person
  330. false
  331. 0
  332. Circle -7500403 true true 110 5 80
  333. 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
  334. Rectangle -7500403 true true 127 79 172 94
  335. Polygon -7500403 true true 195 90 240 150 225 180 165 105
  336. Polygon -7500403 true true 105 90 60 150 75 180 135 105
  337. plant
  338. false
  339. 0
  340. Rectangle -7500403 true true 135 90 165 300
  341. Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
  342. Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
  343. Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
  344. Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
  345. Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
  346. Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
  347. Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
  348. square
  349. false
  350. 0
  351. Rectangle -7500403 true true 30 30 270 270
  352. square 2
  353. false
  354. 0
  355. Rectangle -7500403 true true 30 30 270 270
  356. Rectangle -16777216 true false 60 60 240 240
  357. star
  358. false
  359. 0
  360. Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
  361. target
  362. false
  363. 0
  364. Circle -7500403 true true 0 0 300
  365. Circle -16777216 true false 30 30 240
  366. Circle -7500403 true true 60 60 180
  367. Circle -16777216 true false 90 90 120
  368. Circle -7500403 true true 120 120 60
  369. tree
  370. false
  371. 0
  372. Circle -7500403 true true 118 3 94
  373. Rectangle -6459832 true false 120 195 180 300
  374. Circle -7500403 true true 65 21 108
  375. Circle -7500403 true true 116 41 127
  376. Circle -7500403 true true 45 90 120
  377. Circle -7500403 true true 104 74 152
  378. triangle
  379. false
  380. 0
  381. Polygon -7500403 true true 150 30 15 255 285 255
  382. triangle 2
  383. false
  384. 0
  385. Polygon -7500403 true true 150 30 15 255 285 255
  386. Polygon -16777216 true false 151 99 225 223 75 224
  387. truck
  388. false
  389. 0
  390. Rectangle -7500403 true true 4 45 195 187
  391. Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
  392. Rectangle -1 true false 195 60 195 105
  393. Polygon -16777216 true false 238 112 252 141 219 141 218 112
  394. Circle -16777216 true false 234 174 42
  395. Rectangle -7500403 true true 181 185 214 194
  396. Circle -16777216 true false 144 174 42
  397. Circle -16777216 true false 24 174 42
  398. Circle -7500403 false true 24 174 42
  399. Circle -7500403 false true 144 174 42
  400. Circle -7500403 false true 234 174 42
  401. turtle
  402. true
  403. 0
  404. Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
  405. Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
  406. Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
  407. Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
  408. Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
  409. Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
  410. wheel
  411. false
  412. 0
  413. Circle -7500403 true true 3 3 294
  414. Circle -16777216 true false 30 30 240
  415. Line -7500403 true 150 285 150 15
  416. Line -7500403 true 15 150 285 150
  417. Circle -7500403 true true 120 120 60
  418. Line -7500403 true 216 40 79 269
  419. Line -7500403 true 40 84 269 221
  420. Line -7500403 true 40 216 269 79
  421. Line -7500403 true 84 40 221 269
  422. x
  423. false
  424. 0
  425. Polygon -7500403 true true 270 75 225 30 30 225 75 270
  426. Polygon -7500403 true true 30 75 75 30 270 225 225 270
  427. @#$#@#$#@
  428. NetLogo 6.0-BETA2
  429. @#$#@#$#@
  430. @#$#@#$#@
  431. @#$#@#$#@
  432. @#$#@#$#@
  433. @#$#@#$#@
  434. default
  435. 0.0
  436. -0.2 0 0.0 1.0
  437. 0.0 1 1.0 0.0
  438. 0.2 0 0.0 1.0
  439. link direction
  440. true
  441. 0
  442. Line -7500403 true 150 150 90 180
  443. Line -7500403 true 150 150 210 180
  444. @#$#@#$#@
  445. 0
  446. @#$#@#$#@