Advertisement
Guest User

Untitled

a guest
May 11th, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. patches-own [
  2.   chemical             ;; amount of chemical on this patch
  3.   food                 ;; amount of food on this patch (0, 1, or 2)
  4.   nest?                ;; true on nest patches, false elsewhere
  5.   nest-scent           ;; number that is higher closer to the nest
  6.   food-source-number   ;; number (1, 2, or 3) to identify the food sources
  7. ]
  8.  
  9. ;;;;;;;;;;;;;;;;;;;;;;;;
  10. ;;; Setup procedures ;;;
  11. ;;;;;;;;;;;;;;;;;;;;;;;;
  12.  
  13. to setup
  14.   clear-all
  15.   set-default-shape turtles "bug"
  16.   crt population
  17.   [ set size 2         ;; easier to see
  18.     set color red  ]   ;; red = not carrying food
  19.   setup-patches
  20.   reset-ticks
  21. end
  22.  
  23. to setup-patches
  24.   ask patches
  25.   [ setup-nest
  26.     setup-food
  27.     recolor-patch ]
  28. end
  29.  
  30. to setup-nest  ;; patch procedure
  31.   ;; set nest? variable to true inside the nest, false elsewhere
  32.   set nest? (distancexy 0 0) < 5
  33.   ;; spread a nest-scent over the whole world -- stronger near the nest
  34.   set nest-scent 200 - distancexy 0 0
  35. end
  36.  
  37. to setup-food  ;; patch procedure
  38.   ;; setup food source one on the right
  39.   if (distancexy (0.6 * max-pxcor) 0) < 5
  40.   [ set food-source-number 1 ]
  41.   ;; setup food source two on the lower-left
  42.   if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5
  43.   [ set food-source-number 2 ]
  44.   ;; setup food source three on the upper-left
  45.   if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5
  46.   [ set food-source-number 3 ]
  47.   ;; set "food" at sources to either 1 or 2, randomly
  48.   if food-source-number > 0
  49.   [ set food one-of [1 2] ]
  50. end
  51.  
  52. to recolor-patch  ;; patch procedure
  53.   ;; give color to nest and food sources
  54.   ifelse nest?
  55.   [ set pcolor violet ]
  56.   [ ifelse food > 0
  57.     [ if food-source-number = 1 [ set pcolor cyan ]
  58.       if food-source-number = 2 [ set pcolor sky  ]
  59.       if food-source-number = 3 [ set pcolor blue ] ]
  60.     ;; scale color to show chemical concentration
  61.     [ set pcolor scale-color green chemical 0.1 5 ] ]
  62. end
  63.  
  64. ;;;;;;;;;;;;;;;;;;;;;
  65. ;;; Go procedures ;;;
  66. ;;;;;;;;;;;;;;;;;;;;;
  67.  
  68. to go  ;; forever button
  69.   ask turtles
  70.   [ if who >= ticks [ stop ] ;; delay initial departure
  71.     ifelse color = red
  72.     [ look-for-food  ]       ;; not carrying food? look for it
  73.     [ return-to-nest ]       ;; carrying food? take it back to nest
  74.     wiggle
  75.     fd 1 ]
  76.   diffuse chemical (diffusion-rate / 100)
  77.   ask patches
  78.   [ set chemical chemical * (100 - evaporation-rate) / 100  ;; slowly evaporate chemical
  79.     recolor-patch ]
  80.   tick
  81. end
  82.  
  83. to return-to-nest  ;; turtle procedure
  84.   ifelse nest?
  85.   [ ;; drop food and head out again
  86.     set color red
  87.     rt 180 ]
  88.   [ set chemical chemical + 60  ;; drop some chemical
  89.     uphill-nest-scent ]         ;; head toward the greatest value of nest-scent
  90. end
  91.  
  92. to look-for-food  ;; turtle procedure
  93.   if food > 0
  94.   [ set color orange + 1     ;; pick up food
  95.     set food food - 1        ;; and reduce the food source
  96.     rt 180                   ;; and turn around
  97.     stop ]
  98.   ;; go in the direction where the chemical smell is strongest
  99.   if (chemical >= 0.05) and (chemical < 2)
  100.   [ uphill-chemical ]
  101. end
  102.  
  103. ;; sniff left and right, and go where the strongest smell is
  104. to uphill-chemical  ;; turtle procedure
  105.   let scent-ahead chemical-scent-at-angle   0
  106.   let scent-right chemical-scent-at-angle  45
  107.   let scent-left  chemical-scent-at-angle -45
  108.   if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  109.   [ ifelse scent-right > scent-left
  110.     [ rt 45 ]
  111.     [ lt 45 ] ]
  112. end
  113.  
  114. ;; sniff left and right, and go where the strongest smell is
  115. to uphill-nest-scent  ;; turtle procedure
  116.   let scent-ahead nest-scent-at-angle   0
  117.   let scent-right nest-scent-at-angle  45
  118.   let scent-left  nest-scent-at-angle -45
  119.   if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  120.   [ ifelse scent-right > scent-left
  121.     [ rt 45 ]
  122.     [ lt 45 ] ]
  123. end
  124.  
  125. to wiggle  ;; turtle procedure
  126.   rt random 40
  127.   lt random 40
  128.   if not can-move? 1 [ rt 180 ]
  129. end
  130.  
  131. to-report nest-scent-at-angle [angle]
  132.   let p patch-right-and-ahead angle 1
  133.   if p = nobody [ report 0 ]
  134.   report [nest-scent] of p
  135. end
  136.  
  137. to-report chemical-scent-at-angle [angle]
  138.   let p patch-right-and-ahead angle 1
  139.   if p = nobody [ report 0 ]
  140.   report [chemical] of p
  141. end
  142.  
  143.  
  144. ; Copyright 1997 Uri Wilensky.
  145. ; See Info tab for full copyright and license.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement