![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Erik's Rabbit SimThe purpose of this project is to learn about simulations and how real life events can be modeled in Squeak.Being able to simulate real life events on computers is one of the greatest things that computers can be used for today. Being able to simulate things on the computer allows us to run experiments that might not otherwise be feasible, like watching how certain diseases spread throughout a city of people, or what happens to molecules of water as they pass through a cell's wall, even the formation of solar systems, a process that takes naturally takes billions of years. In addition, these experiemtns can be run over and over again varying how the individual components interacts. Most simulations comprise of two basic compenents, objects and rules. Objects are usually the physical things that you think about, people, roads, planets, rabbits, etc, while rules are properties defining how the objects interact in the virtual world. So let's take the example of modeling a rabbit population. What are the important considerations behind this simulation?
So our model contains three things, rabbits, something the rabbits eat (grass), and something that eats the rabbits (foxes). Some obvious relations between them is that the grass grows, rabbits eat the grass and the foxes eat the rabbits. So one idea we might want to focus our attention around is to vary the initial quantities of each object and see if we can achieve a stable population. Obviously if the rabbits eat all the grass, the rabbits will eventually die because there will no longer be any grass to make new grass. Likewise if all the rabbits die, then the foxes will die off as a consqeuence. So what are the common behaviours found in each of these objects? Each of these objects share some basic characteristics, for example they all reproduce, move, eat, and die in one way or another. Before going any further, it is important to examine some of the issues behind running computer simulations. It is always important that a simulation be accurate, otherwise the simulation serves little useful purpose, however trying to acheive high levels of accuracy can also render your simulation useless. How? Usually simulations are done in time steps, where everything does a behaviour for a certain amount of time (like moving) before the situation is reexamined and time advances. Things in the past affect how things turn out in the future. For example when caculating the position of a rocket the larger the timestep used, the less acurate your projection of where it's going to end up, because lots of little things could change how the rocket is angled or at what speed it's traveling at. Being more accurate usually means using a smaller time increment between steps, which then means more time speant in computation. In addition, interaction between objects can also cause large amounts of extra computation. For example, consider a field of grass with rabbits, the rabbits are going to hop around and eat the grass, the only problem is, how do we know which piece of grass is being eaten? We could have each piece of grass watch to see if a rabbit is eating it, but most of the time this is unncessary. Likewise, having telling all the grass only when a rabbit tells the system that it's eating grass can start to become inefficient with large quantities of grass, so sometimes it becomes necessary to find alternative ways to represent things in the overall model. Enough with the background, let's create something. Step 1) Let's do the basic stuff we're used to by now. Create a playfield for the simulation to take places within, a rabbit, a piece of grass, and a fox. When creating these objects make sure to use more than one color, we'll need to use that property later.For the grass, make it a box about the size of your rabbit and fox. ![]() Step 2) The first thing that needs to happen in a simulation is to setup the scenario. Let's do that now. Because of some optimizations on how the grass might work (previously mentioned above), the setup scripts will be slightly different for the grass from the other objects, so we'll do that one first since the general setup script requires the grass placement scripts to work. ![]()
Step 3) Now to complete the general object placement/setup scripts we'll use some of the ideas used in the grass placement scripts, but they'll work slightly different since we want to keep track of these objects after we place them. Overall we'll create some variables saying how many of each item type we want, some for how many are currently in existance, and then work these into our item placement script. The item placement script will place the given object at a random location on the map and adjust the initial and current variable accordingly. At the same time we'll also add a script to clear the playfield. ![]()
Step 4) Create another graphic a little larger than you grass object and the color of the background of your playfield. Since it's difficult to sample the color of the playfield while in paint mode, get an approimation and then color the background to match the covering tile. Whenever a rabbit eats a piece of grass it comes across we will stamp out the grass currently in existance with this new graphic. ![]() Step 5) Now that we have a way of setting up the situation, let's define how things interact with each other and themselves. First lets start with the grass, it has one of the easiest behaviours, all it needs to do is make new copies of itself from time to time. Almost half of this is already done for us from the setup scripts. For placing new pieces of grass all we need to do is run the placegrass script from before with the maximum number of grass we could possibly create. ![]()
Step 6) Now let's go one step up the food chain and do the rabbit. A rabbit has three major functions, it eats, moves, and reproduces. Before we begin let's try to figure out what sort of variables the rabbit might need. Each rabbit should probably keep track of it's own age, the last time it ate and reproduced, and it's speed. Let's begin by creating the eat script. After that we'll do the movement and reproduction scripts. ![]()
Extensions : Add in code for the fox, it should work fairly similarly to the rabbit. Update) Some adjustment of the numbers might be necessary to get a good balance. For example, from the time the screenshots were made the maximum number of grass tiles was increased to 120. Usually you'd have real numbers to work with from other studies on the situation, but we don't so it's a matter of fiddling around until we find something that works. Check out the finished file here: ervrabbitsim.pr Link to this Page
|