1 / 91

Chapter 9 - Simulations

Chapter 9 - Simulations. Bruce Chittenden. Supercomputers www.top500.org. Foxes and Rabbits. Exercise 9.1. Rabbits thin out where the Foxes are in the Field. Exercise 9.2. When the Rabbit population decreases to below a certain threshold, the Foxes go extinct.

callia
Download Presentation

Chapter 9 - Simulations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 - Simulations Bruce Chittenden

  2. Supercomputers www.top500.org

  3. Foxes and Rabbits

  4. Exercise 9.1 Rabbits thin out where the Foxes are in the Field

  5. Exercise 9.2 When the Rabbit population decreases to below a certain threshold, the Foxes go extinct As the Rabbit population increases the Fox population increases

  6. Exercise 9.3 (First Run)

  7. Exercise 9.3 (Second Run)

  8. Exercise 9.3 (Third Run)

  9. Exercise 9.3 (Fourth Run)

  10. Exercise 9.3 (Fifth Run)

  11. Exercise 9.3 (Sixth Run)

  12. Exercise 9.3 (Seventh Run)

  13. Exercise 9.3 (Eighth Run)

  14. Exercise 9.3 (Ninth Run)

  15. Exercise 9.3 (Tenth Run)

  16. Exercise 9.3

  17. Exercise 9.4 Height and Width of the Field public class Field extends World { // Constants representing configuration information for the simulation. // The default width for the grid. private static final int WIDTH = 40; // The default depth of the grid. private static final int HEIGHT = 30; // The probability that a fox will be created in any given grid position (in percent). private static final int FOX_CREATION_PROBABILITY = 2; // The probability that a rabbit will be created in any given grid position (in percent). private static final int RABBIT_CREATION_PROBABILITY = 8; // The current step of the simulation. private int step = 0;

  18. Exercise 9.4 Changed the Field to be 80 by 60 or 400% of the Original Size public class Field extends World { // Constants representing configuration information for the simulation. // The default width for the grid. private static final int WIDTH = 80; // The default depth of the grid. private static final int HEIGHT = 60; // The probability that a fox will be created in any given grid position (in percent). private static final int FOX_CREATION_PROBABILITY = 2; // The probability that a rabbit will be created in any given grid position (in percent). private static final int RABBIT_CREATION_PROBABILITY = 8; // The current step of the simulation. private int step = 0;

  19. Exercise 9.4 Ran a Long Time

  20. Exercise 9.4 Changed the Field to be 20 by 15 or 25% of the Original Size public class Field extends World { // Constants representing configuration information for the simulation. // The default width for the grid. private static final int WIDTH = 20; // The default depth of the grid. private static final int HEIGHT = 15; // The probability that a fox will be created in any given grid position (in percent). private static final int FOX_CREATION_PROBABILITY = 2; // The probability that a rabbit will be created in any given grid position (in percent). private static final int RABBIT_CREATION_PROBABILITY = 8; // The current step of the simulation. private int step = 0;

  21. Exercise 9.4 With 25% of the Space the Species Went Extinct Very Quickly

  22. Exercise 9.5 Increase the Size of the Area from 30 x 40 or 1200 sq units to 35 x 45 or 1575 sq units public class Field extends World { // Constants representing configuration information for the simulation. // The default width for the grid. private static final int WIDTH = 45; // The default depth of the grid. private static final int HEIGHT = 35; // The probability that a fox will be created in any given grid position (in percent). private static final int FOX_CREATION_PROBABILITY = 2; // The probability that a rabbit will be created in any given grid position (in percent). private static final int RABBIT_CREATION_PROBABILITY = 8; // The current step of the simulation. private int step = 0;

  23. Exercise 9.5 Establish a Baseline

  24. Exercise 9.5 Double the change of Breeding from 12 to 25 public class Rabbit extends Animal { // Characteristics shared by all rabbits (static fields). // The age at which a rabbit can start to breed. private static final int BREEDING_AGE = 5; // The age to which a rabbit can live. private static final int MAX_AGE = 50; // The likelihood of a rabbit breeding (in percent). private static final double BREEDING_PROBABILITY = 25; // The maximum number of births. private static final int MAX_LITTER_SIZE = 5;

  25. Exercise 9.5 Did not seem to make a lot of difference

  26. Exercise 9.6 public class Fox extends Animal { // Characteristics shared by all foxes (static fields). // The age at which a fox can start to breed. private static final int BREEDING_AGE = 10; // The age to which a fox can live. private static final int MAX_AGE = 150; // The likelihood of a fox breeding (in percent). private static final int BREEDING_PROBABILITY = 8; // The maximum number of births. private static final int MAX_LITTER_SIZE = 3; // The food value of a single rabbit. In effect, this is the // number of steps a fox can go before it has to eat again. private static final int RABBIT_FOOD_VALUE = 6; Double Rabbit Food Value for 6 to 12

  27. Exercise 9.6 Fox Population Seems to Do Very Well

  28. Exercise 9.7 public class Fox extends Animal { // Characteristics shared by all foxes (static fields). // The age at which a fox can start to breed. private static final int BREEDING_AGE = 20; // The age to which a fox can live. private static final int MAX_AGE = 100; // The likelihood of a fox breeding (in percent). private static final int BREEDING_PROBABILITY = 5; // The maximum number of births. private static final int MAX_LITTER_SIZE = 3; // The food value of a single rabbit. In effect, this is the // number of steps a fox can go before it has to eat again. private static final int RABBIT_FOOD_VALUE = 12; Changed MAX_AGE from 150 to 100 Changed BREEDING_PROBABLY from 8 to 5 Changed BREEDING_AGE from 10 to 20

  29. Exercise 9.7

  30. 9.2 Ants

  31. Exercise 9.8 Nothing Happens

  32. Exercise 9.9 import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /* * An ant that collects food. * * @author Michael Kolling * @version 0.1 */ public class Ant extends Creature { /** * Create an ant with a given home hill. The initial speed is zero (not moving). */ public Ant(AntHill home) { setHomeHill(home); } /* * Do what an ant's gotta do. */ public void act() { } } Nothing in the act Method

  33. Exercise 9.10

  34. Exercise 9.11 /* * Act: If there are still ants left inside, see whether one should come out. */ public void act() { if(ants < maxAnts) { if(Greenfoot.getRandomNumber(100) < 10) { getWorld().addObject(new Ant(this), getX(), getY()); ants++; } } } Ants do not move and 10% of the time when the act Method is called another Ant is created at exactly the location of the Ant Hill up to the Maximum number of ants

  35. Exercise 9.12 public class Ant extends Creature { /* * Create an ant with a given home hill. The initial speed is zero (not moving). */ public Ant(AntHill home) { setHomeHill(home); } /* * Do what an ant's gotta do. */ public void act() { randomWalk(); } } Add the randomWalk Method to the Ants act Method

  36. Exercise 9.12

  37. 9.3 Collecting Food Crumbs with Even Distribution Crumbs with Gaussian Distribution

  38. Exercise 9.13 New subclass Food

  39. Exercise 9.13 import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /* * Write a description of class Food here. * * @author (your name) * @version (a version number or a date) */ public class Food extends Actor { /* * Act - do whatever the Food wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }

  40. Exercise 9.14 public class Food extends Actor { private int crumbs = 100; // number of bits of food in this pile /* * Act - do whatever the Food wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } } Number of Crumbs initialized to 100

  41. Exercise 9.15 /* * Update the image */ private void updateImage() { GreenfootImage image = new GreenfootImage(SIZE, SIZE); for (int i = 0; i < crumbs; i++) { int x = randomCoord(); int y = randomCoord(); image.setColorAt(x, y, color1); image.setColorAt(x + 1, y, color2); image.setColorAt(x, y + 1, color2); image.setColorAt(x + 1, y + 1, color3); } setImage(image); }

  42. Exercise 9.15 /* * Returns a random number relative to the size of the food pile. */ private int randomCoord() { int val = Greenfoot.getRandomNumber(SIZE); if (val < 0) return 0; if (val > SIZE - 2) return SIZE - 2; else return val; } Greenfoot.getRandomNumber()

  43. Exercise 9.15 Even Distribution of the Random Number Produces a Square Food Pile

  44. Exercise 9.16

  45. Exercise 9.16

  46. Exercise 9.17 /* * Returns a random number relative to the size of the food pile. */ private int randomCoord() { int val = HALFSIZE + (int) (new Random().nextGaussian() * (HALFSIZE / 2)); if (val < 0) return 0; if (val > SIZE - 2) return SIZE - 2; else return val; } Random().nextGaussian()

  47. Exercise 9.17 Normal Distribution (Gaussian Distribution) of the Random Number Produces a Better Looking Food Pile

  48. Random Distributions If we need random behavior in our program it is sometime important to think about what type of distribution we need. Uniform Distribution Greenfoot.getRandomNumber(int range) Normal Distribution new Random().nextGaussian()

  49. Exercise 9.18 /* * Remove some food from this pile of food. */ public void takeSome() { crumbs = crumbs - 3; if (crumbs <= 0) { getWorld().removeObject(this); } else { updateImage(); } }

  50. Exercise 9.18 Interactively Remove Food from the Food Pile Until it is All Gone

More Related