Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Orientated Programming in Perl Simulated Models of Termites.

Similar presentations


Presentation on theme: "Object Orientated Programming in Perl Simulated Models of Termites."— Presentation transcript:

1 Object Orientated Programming in Perl Simulated Models of Termites

2 A Version of the Termite Model This program explores the behavior of termites in a Java simulation. This program explores the behavior of termites in a Java simulation. http://ccl.northwestern.edu/netlogo/models/Ter mites http://ccl.northwestern.edu/netlogo/models/Ter mites http://ccl.northwestern.edu/netlogo/models/Ter mites http://ccl.northwestern.edu/netlogo/models/Ter mites To do this well we should take a look at a different kind of programming, Object Orientated programming. To do this well we should take a look at a different kind of programming, Object Orientated programming.

3 What is an Object? An object is a single entity that holds both the data for a part of a program and the code that works on that data. An object is a single entity that holds both the data for a part of a program and the code that works on that data. This idea came from looking at how things are organized in the world. For example, a gene has a set of properties that define it and there is a set of actions that can be done to it. This idea came from looking at how things are organized in the world. For example, a gene has a set of properties that define it and there is a set of actions that can be done to it. In Perl object orientation is expressed in the form of classes that hold information about variables and methods. In Perl object orientation is expressed in the form of classes that hold information about variables and methods.

4 An Object GenePropertiesName Chromosome Location MethodsTranscribeBlock Gene

5 Properties and Methods Properties Properties are the actual data values that tell you what exactly the object is. Properties are the actual data values that tell you what exactly the object is. For our example each termite needs to have information on where it is on the grid and whether or not it is holding a chip. For our example each termite needs to have information on where it is on the grid and whether or not it is holding a chip. Methods Methods are the things that you can do to your object. Methods are the things that you can do to your object. For this example our termites need to be able to walk around the grid and both pick up and drop woodchips. For this example our termites need to be able to walk around the grid and both pick up and drop woodchips.

6 Objects in Perl An object in perl is a reference to a special kind of hash, also called a class, and is marked by the class name. An object in perl is a reference to a special kind of hash, also called a class, and is marked by the class name. Calling methods is different in OO programming also. This is done by first identifying the object to be acted upon and then specifying which method in that object you want to use. Calling methods is different in OO programming also. This is done by first identifying the object to be acted upon and then specifying which method in that object you want to use. In perl this is done with arrow notation (->). So to call the method step for our termite you would type: $termite->step(args). In perl this is done with arrow notation (->). So to call the method step for our termite you would type: $termite->step(args).

7 Constructors To make a new object of the termite class you need to use a constructor. A constructor is a method inside the termite class called new. To make a new object of the termite class you need to use a constructor. A constructor is a method inside the termite class called new. This method takes in all of the arguments passed to the constructor and returns a scalar reference to a hash and puts the information from the arguments into the proper places in the hash. This method takes in all of the arguments passed to the constructor and returns a scalar reference to a hash and puts the information from the arguments into the proper places in the hash.

8 Gene Example Let’s write a short program that makes a new ‘gene’ and then asks for information about that gene. Let’s write a short program that makes a new ‘gene’ and then asks for information about that gene. We are going to need to write a class and the program that uses it. We are going to need to write a class and the program that uses it.

9 The Termite Class The termite wanders around randomly until it bumps into a wood chip. The termite wanders around randomly until it bumps into a wood chip. If the termite is carrying a chip, it drops the chip and continues to wander. If the termite is carrying a chip, it drops the chip and continues to wander. If the termite is not carrying a chip, it picks up the one it bumped into and continues to wander. If the termite is not carrying a chip, it picks up the one it bumped into and continues to wander.

10 The Grid Perl doesn’t particularly like two-dimensional arrays, but we need an area for our termites to run around and play. Perl doesn’t particularly like two-dimensional arrays, but we need an area for our termites to run around and play. To solve this problem let’s just make a one- dimensional array that we know how to work with. To solve this problem let’s just make a one- dimensional array that we know how to work with.

11 The Step Method We need to add in a feature that will randomly pick a direction for the termite to step in. We need to add in a feature that will randomly pick a direction for the termite to step in. The termite checks its location to see if there is a chip there. Let’s add this in. The termite checks its location to see if there is a chip there. Let’s add this in.

12 Object Orientation and Termites Part 2

13 The Termite Class What do you remember about our simulation from last week? What do you remember about our simulation from last week? We need to finish up the logic for how the termite walks around the grid. We want the termite to immediately turn 180 degrees and take a step after each time it drops a chip. We also want to see what happens if we let the termite have the option of moving in all four cardinal directions as compared to just east and west. We need to finish up the logic for how the termite walks around the grid. We want the termite to immediately turn 180 degrees and take a step after each time it drops a chip. We also want to see what happens if we let the termite have the option of moving in all four cardinal directions as compared to just east and west.

14 Test it! What would happen if after dropping a woodchip the termite it would randomly jump to an empty place on the grid? What would happen if after dropping a woodchip the termite it would randomly jump to an empty place on the grid? How long do we want our termites to run around the grid? Let’s try it out! How long do we want our termites to run around the grid? Let’s try it out! We can take the grid file that we saved at the end of the program and view it in Matlab to see where the termites moved our chips. We can take the grid file that we saved at the end of the program and view it in Matlab to see where the termites moved our chips.


Download ppt "Object Orientated Programming in Perl Simulated Models of Termites."

Similar presentations


Ads by Google