Monte Carlo example: Estimating the area of four different regions using NetLogo turtles. Each turtle located to a random x (its xcor or xcoordinate) and a random y (its ycor or ycoordinate).
CS 1025 Computational Modeling and Simulation Fall 2011
Computational Modeling and Simulation - December 4th, 2011
powered by NetLogo
view/download model file: December4th.nlogo
This model is used to estimate the area between two circles. That area is reserved for COWS. It also is used to estimate the area of each circle and the sum of the area of the four corners that are outside of the circles.
A turtle hatches either a PLANT, STAR, COW or CAR, depending on where it landed. Each turtle has a random location between -44 and +44 for the x position, and between -16 and 16 for the y position on the GRID of patches. After a turtle hatches a specific breed, the turtle dies.
The MONITORs are used to display and count of each of the four breeds, as well as the count of the turtles and the proportion of each breed. A monitor is also used to display the COUNT of the PATCHES, which is the one thing that never changes.
Click on the Draw Circles and Grid button to set up the checkerboard of white and yellow squares (patches). Each patch (square) represents one unit of area.
Click on the MonteCarlo button to create 100 turtles. Each time you click this button it will add 100 more turtles to the simulation.
Using only your knowledge of math and basic geometry: Calculate the actual area of the circle. Calculate the actual area of the region between the two circles. Calculate the distance between the center of the two circles.
There are two known areas to start with: The radius of the circles and the width and height of the entire rectangular grid give you the area of the circles and the area of the entire turtle patch world.
Try adding 3 more breeds so each one of the four corners has a different breed of turtle. Then add 3 more monitors, or rather 6 more monitors to display the counts and the proportion for that breed.
Try changing it so that it offers the option of just getting an estimate of the area between the two circles: cowsOnly JPG
What happens if you download the .nlogo file and then comment out or remove one or more of the die statements? Does the output look different? If you only comment uut one die statement, how does the lower left monitor (count turtles) change its behavior? Is it still going from 0 to 100 to 200 to 300 to 400 and so on?
if inLeftCircle [ hatch-stars 1 [set shape "star" set color blue] die ] if inRightCircle [ hatch-cars 1 [set shape "car" set color red] die ] could be changed to: if inLeftCircle [ hatch-stars 1 [set shape "star" set color blue] ;; die ] if inRightCircle [ hatch-cars 1 [set shape "car" set color red] die ] Why do the STARS look very funny after this change???
JPG screen snapshot of Two Circles and Three Areas and the .NLOGO model file MonteCarloCircles.nlogo that you can right mouse button download and play with.
In-class exercise: MonteCarloNetLogoCirclesNov14th.pdf and the shorter answer sheet form that was turned in: MonteCarloNetLogoCirclesAnswerSheet.pdf
Another modification that could be attempted would involve locating turtles randomly anywhere inside the narrower rectangle that goes from -28 to 28 for its x or horizontal range.
set inLeftCircle distancexy -28 0 < 16.5 set inRightCircle distancexy 28 0 < 16.5 The key idea is random-float. random-float 56 - 28 would generate a random floating point number between 0 and 56 and then subtract 28 from it. The result: A random number between -28 and +28. ask turtles [ ifelse cowsOnly ;; cowsOnly is a SWITCH [ set randomX random-float 56 - 28 set randomY random-float 33 - 16.5 setxy randomX randomY ] [ setxy random-xcor random-ycor ] ]
globals [ sideLength inLeftCircle inRightCircle ] ;; Created on Sunday, December 4th, 2011 - CS 1025 Computational Modeling and Simulation ;; Mark Jacobson ;; Monte Carlo area estimation breed [ cars car ] breed [ stars star ] breed [ cows cow ] breed [ plants plant ] to checkerboard ask patches [ ifelse remainder (pxcor + pycor) 2 = 0 [ set pcolor white ] [ set pcolor yellow ] if pxcor = 0 and pycor = 0 [ set pcolor green ] if pxcor = -28 and pycor = 0 [ set pcolor green ] if pxcor = 28 and pycor = 0 [ set pcolor green ] ] end to MonteCarlo cro 100 ask turtles [ ;;set breedDecided false setxy random-xcor random-ycor ] ask turtles [ set inLeftCircle distancexy -28 0 < 16.5 set inRightCircle distancexy 28 0 < 16.5 if inLeftCircle [ hatch-stars 1 [set shape "star" set color blue] die ] if inRightCircle [ hatch-cars 1 [set shape "car" set color red] die ] if (xcor > -28) and (xcor < 28) [ hatch-cows 1 [ set shape "cow" set color black ] die ] hatch-plants 1 [ set shape "plant" set color green ] die ] ask turtles [ set size 1.5 ] END to circle ca checkerboard set sideLength 33 * 3.14159 / 180 cro 1 ask turtles [ setxy -28 0 pd fd 16.5 rt 90 set pen-size 2 fd sideLength / 2 repeat 179 [ rt 2 fd sideLength ] rt 2 fd sideLength / 2 pu setxy 28 0 set heading 0 pd set pen-size 1 fd 16.5 rt 90 set pen-size 2 fd sideLength / 2 repeat 179 [ rt 2 fd sideLength ] rt 2 fd sideLength / 2 die ] end