Courses/CS 461/Winter 2006/Bill Kunyiha/Homework1
From CSWiki
NetLogo is a good applications that can be used in modeling statistics . For the first homework, I moduled the Ants library where Ants follow the leader when looking for food. At first the leader Ant wiggles around until it is close enough to the food and can smell, then it heads straight for the food. The leader ant moves towards the food along a random path; after a small delay, the second ant in the line follows the leader by heading directly towards where the leader is located. Each subsequent ant follows the ant ahead of it in the same manner. Even though the leader may take a very circuitous path towards the food, the ant trail, surprisingly, adopts a smooth shape. While it is not yet clear if this model is a biologically accurate model of ant behavior, it is an interesting mathematical exploration of the emergent behavior of a series of agents following each other serially
Code
breeds [
leader
followers
leader2
]
globals [
nest-x nest-y ;; location of center of nest
food-x food-y ;; location of center of food
leader-heading ;; heading of the leader ant
food-x2 food-y2 ;; location of other food
]
to setup
ca
set-default-shape turtles "bug"
set nest-x 10 - screen-edge-x ;; set up nest and food locations
set nest-y 0
set food-x screen-edge-x - 20
set food-y 0
set food-x2 screen-edge-x - 10
set food-y2 40
;; draw the nest in brown by stamping a circular
;; brown turtle
ask patch nest-x nest-y [
sprout 1 [
set color brown
set shape "circle"
set size 10
stamp
die
]
]
;; draw the nest in orange by stamping a circular
;; orange turtle
ask patch food-x food-y [
sprout 1 [
set color orange
set shape "circle"
set size 10
stamp
die
]
]
;; Here We draw the other food source
ask patch food-x2 food-y2 [
sprout 1 [
set color red
set shape "circle"
set size 8
stamp
die
]
]
create-custom-leader 1
[ set color red ;; leader ant is red
set size 2
wiggle 50 ] ;; ...and starts out with a random heading
create-custom-followers (num-ants - 1)
[ set size 2
set color yellow ] ;; middle ants are yellow
ask turtles
[ setxy nest-x nest-y ;; start the ants out at the nest
set heading 90 ]
ask turtle (num-ants - 1)
[ set color blue ;; last ant is blue
set pen-size 2
pd ] ;; ...and leaves a trail
ask leader
[ set pen-size 2
pd ]
set leader-heading heading-of turtle 0 ;; the leader also leaves a trail
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ask turtles
; [ setxy food-x2 food-y2 ;; start the ants out at the food2
; set heading 90 ]
;ask turtle (num-ants - 1)
; [ set color blue ;; last ant is blue
; set pen-size 2
; pd ] ;; ...and leaves a trail
;ask leader2
; [ set pen-size 2
; pd ]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;set leader-heading heading-of turtle 0
end
to go
if not any? turtles with [xcor < food-x2]
[ stop ]
ask leader ;; the leader ant wiggles and moves
[ wiggle leader-wiggle-angle
keep-in-bounds
if (xcor > (food-x2 - 5 )) ;; leader heads straight for food, if it is close
and (distancexy-nowrap food-x2 food-y2 > 0)
[ set heading towardsxy-nowrap food-x2 food-y2 ]
if xcor < food-x2 ;; do nothing if you're at or past the food
[ fd 0.5 ] ]
ask followers
[ if distance turtle (who - 1) > 0
[ set heading towards turtle (who - 1) ] ;; follower ants follow the ant ahead of them
if time-to-start? and (xcor < food-x2) ;; followers wait a bit before leaving nest
[ fd 0.5 ] ]
set leader-heading heading-of turtle 0
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go2
if not any? turtles with [xcor < food-x2]
[ stop ]
ask leader ;; the leader ant wiggles and moves
[ wiggle leader-wiggle-angle
keep-in-bounds
if (xcor < (food-x2 - 5 )) ;; leader heads straight for food, if it is close
and (distancexy-nowrap food-x2 food-y2 > 0)
[ set heading towardsxy-nowrap food-x2 food-y2 ]
if xcor > food-x2 ;; do nothing if you're at or past the food
[ fd 0.5 ] ]
ask followers
[ if distance turtle (who - 1) < 0
[ set heading towards turtle (who - 1) ] ;; follower ants follow the ant ahead of them
if time-to-start? and (xcor > food-x2) ;; followers wait a bit before leaving nest
[ fd 0.5 ] ]
set leader-heading heading-of turtle 2
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; turtle procedure; wiggle a random amount, averaging zero turn
to wiggle [angle]
rt random-float angle
lt random-float angle
end
;; turtle procedure; keep the ants from wrapping the screen
to keep-in-bounds
ifelse heading > 180
[ rt 180 ]
[ if ycor > (screen-edge-x - 5)
[ rt 100 ]
if ycor < (5 - screen-edge-x)
[ lt 100 ] ]
end
;; turtle reporter; if true, then the ant is authorized to move out of the nest
to-report time-to-start?
report (xcor-of (turtle (who - 1))) > (nest-x + start-delay + random start-delay )
end

