Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. breed [ lights light ]
  2. breed [ moths moth ]
  3.  
  4. globals
  5. [
  6. scale-factor ;; to control the form of the light field
  7. ]
  8.  
  9. lights-own
  10. [
  11. intensity
  12. ]
  13.  
  14. moths-own
  15. [
  16. ;; +1 means the moths turn to the right to try to evade a bright light
  17. ;; (and thus circle the light source clockwise). -1 means the moths
  18. ;; turn to the left (and circle the light counter-clockwise)
  19. ;; The direction tendency is assigned to each moth when it is created and does not
  20. ;; change during the moth's lifetime.
  21. direction
  22. ]
  23.  
  24. patches-own
  25. [
  26. light-level ;; represents the light energy from all light sources
  27. ]
  28.  
  29. to setup
  30. clear-all
  31. set-default-shape lights "circle 2"
  32. set-default-shape moths "butterfly"
  33. set scale-factor 50
  34. if number-lights > 0
  35. [
  36. make-lights number-lights
  37. ask patches [ generate-field ]
  38. ]
  39. make-moths number-moths
  40. reset-ticks
  41. end
  42.  
  43. to go
  44. ask moths [ move-thru-field ]
  45. tick
  46. end
  47.  
  48. ;;;;;;;;;;;;;;;;;;;;;;
  49. ;; Setup Procedures ;;
  50. ;;;;;;;;;;;;;;;;;;;;;;
  51.  
  52. to make-lights [ number ]
  53. create-lights number [
  54. set color white
  55. jump 10 + random-float (max-pxcor - 30)
  56. set intensity random luminance + 20
  57. set size sqrt intensity
  58. ]
  59. end
  60.  
  61. to make-moths [ number ]
  62. create-moths number [
  63. ifelse (random 2 = 0)
  64. [ set direction 1 ]
  65. [ set direction -1 ]
  66. set color white
  67. jump random-float max-pxcor
  68. set size 5
  69. ]
  70. end
  71.  
  72. to generate-field ;; patch procedure
  73. set light-level 0
  74. ;; every patch needs to check in with every light
  75. ask lights
  76. [ set-field myself ]
  77. set pcolor scale-color blue (sqrt light-level) 0.1 ( sqrt ( 20 * max [intensity] of lights ) )
  78. end
  79.  
  80. ;; do the calculations for the light on one patch due to one light
  81. ;; which is proportional to the distance from the light squared.
  82. to set-field [p] ;; turtle procedure; input p is a patch
  83. let rsquared (distance p) ^ 2
  84. let amount intensity * scale-factor
  85. ifelse rsquared = 0
  86. [ set amount amount * 1000 ]
  87. [ set amount amount / rsquared ]
  88. ask p [ set light-level light-level + amount ]
  89. end
  90.  
  91. ;;;;;;;;;;;;;;;;;;;;;;;;
  92. ;; Runtime Procedures ;;
  93. ;;;;;;;;;;;;;;;;;;;;;;;;
  94.  
  95. to move-thru-field ;; turtle procedure
  96. ifelse (light-level <= ( 1 / (10 * sensitivity) ))
  97. [
  98. ;; if there is no detectable light move randomly
  99. rt flutter-amount 45
  100. ]
  101. [
  102. ifelse (random 25 = 0)
  103. ;; add some additional randomness to the moth's movement, this allows some small
  104. ;; probability that the moth might "escape" from the light.
  105. [
  106. rt flutter-amount 60
  107. ]
  108. [
  109. ;; turn toward the brightest light
  110. maximize
  111. ;; if the light ahead is not above the sensitivity threshold head towards the light
  112. ;; otherwise move randomly
  113. ifelse ( [light-level] of patch-ahead 1 / light-level > ( 1 + 1 / (10 * sensitivity) ) )
  114. [
  115. lt ( direction * turn-angle )
  116. ]
  117. [
  118. rt flutter-amount 60
  119. ]
  120. ]
  121. ]
  122. if not can-move? 1
  123. [ maximize ]
  124. fd 1
  125. end
  126.  
  127. to maximize ;; turtle procedure
  128. face max-one-of patches in-radius 1 [light-level]
  129. end
  130.  
  131. to-report flutter-amount [limit]
  132. ;; This routine takes a number as an input and returns a random value between
  133. ;; (+1 * input value) and (-1 * input value).
  134. ;; It is used to add a random flutter to the moth's movements
  135. report random-float (2 * limit) - limit
  136. end
  137.  
  138.  
  139. ; Copyright 2005 Uri Wilensky.
  140. ; See Info tab for full copyright and license.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement